timeline-state-resolver
Version:
Have timeline, control stuff
173 lines • 8.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuantelDevice = void 0;
const timeline_state_resolver_types_1 = require("timeline-state-resolver-types");
const device_1 = require("../../service/device");
const debug_1 = require("debug");
const types_1 = require("./types");
const tv_automation_quantel_gateway_client_1 = require("tv-automation-quantel-gateway-client");
const connection_1 = require("./connection");
const state_1 = require("./state");
const diff_1 = require("./diff");
const debug = (0, debug_1.default)('timeline-state-resolver:quantel');
class QuantelDevice extends device_1.Device {
constructor() {
super(...arguments);
this._disconnectedSince = undefined;
this.actions = {
[timeline_state_resolver_types_1.QuantelActions.ClearStates]: async () => {
this.context.resetResolver();
return {
result: timeline_state_resolver_types_1.ActionExecutionResultCode.Ok,
};
},
[timeline_state_resolver_types_1.QuantelActions.RestartGateway]: async () => {
if (this._quantel) {
try {
await this._quantel.kill();
return { result: timeline_state_resolver_types_1.ActionExecutionResultCode.Ok };
}
catch (e) {
this.context.logger.error('Error killing quantel gateway', new Error(e)); // note - not 100% sure this is correct?
}
}
return { result: timeline_state_resolver_types_1.ActionExecutionResultCode.Error };
},
};
}
async init(options) {
this.options = options;
this._quantel = new tv_automation_quantel_gateway_client_1.QuantelGateway();
this._quantel.on('error', (e) => this.context.logger.error('Quantel.QuantelGateway', e));
this._quantelManager = new connection_1.QuantelManager(this._quantel, () => this.context.getCurrentTime(), {
allowCloneClips: options.allowCloneClips,
});
this._quantelManager.on('info', (x) => this.context.logger.info(`Quantel: ${typeof x === 'string' ? x : JSON.stringify(x)}`));
this._quantelManager.on('warning', (x) => this.context.logger.warning(`Quantel: ${typeof x === 'string' ? x : JSON.stringify(x)}`));
this._quantelManager.on('error', (e) => this.context.logger.error('Quantel: ', e));
this._quantelManager.on('debug', (...args) => this.context.logger.debug(...args));
const ISAUrlMaster = options.ISAUrlMaster || options['ISAUrl']; // tmp: ISAUrl for backwards compatibility, to be removed later
if (!options.gatewayUrl)
throw new Error('Quantel bad connection option: gatewayUrl');
if (!ISAUrlMaster)
throw new Error('Quantel bad connection option: ISAUrlMaster');
if (!options.serverId)
throw new Error('Quantel bad connection option: serverId');
const isaURLs = [];
if (ISAUrlMaster)
isaURLs.push(ISAUrlMaster);
if (options.ISAUrlBackup)
isaURLs.push(options.ISAUrlBackup);
this._quantel
.init(options.gatewayUrl, isaURLs, options.zoneId, options.serverId)
.then(() => {
this._quantel.monitorServerStatus((connected) => {
if (!this._disconnectedSince && connected === false) {
this._disconnectedSince = Date.now();
if (options.suppressDisconnectTime) {
// trigger another update after debounce
setTimeout(() => {
if (!this._quantel.connected) {
this.context.connectionChanged(this.getStatus());
}
}, options.suppressDisconnectTime);
}
}
else if (connected === true) {
if (!this._disconnectedSince) {
// this must be our first time connecting, so let's resend any commands we missed
this.context
.resetToState({ time: 0, port: {} })
.catch((e) => this.context.logger.warning('Failed to reset to state after first connection, device may be in unknown state (reason: ' +
e +
')'));
}
this._disconnectedSince = undefined;
}
this.context.connectionChanged(this.getStatus());
});
})
.catch((e) => this.context.logger.error('Error initialising quantel', e));
return Promise.resolve(true);
}
async terminate() {
this._quantel.dispose();
}
convertTimelineStateToDeviceState(timelineState, mappings) {
return (0, state_1.convertTimelineStateToQuantelState)(timelineState, mappings);
}
diffStates(oldState, newState, mappings, currentTime) {
this._quantel.setMonitoredPorts((0, state_1.getMappedPorts)(mappings));
return (0, diff_1.diffStates)(oldState, newState, currentTime);
}
async sendCommand({ command, context, timelineObjId }) {
const cwc = {
context: context,
command: command,
timelineObjId: timelineObjId,
};
this.context.logger.debug(cwc);
debug(command);
try {
const cmdType = command.type;
if (command.type === types_1.QuantelCommandType.SETUPPORT) {
await this._quantelManager.setupPort(command);
}
else if (command.type === types_1.QuantelCommandType.RELEASEPORT) {
await this._quantelManager.releasePort(command);
}
else if (command.type === types_1.QuantelCommandType.LOADCLIPFRAGMENTS) {
await this._quantelManager.tryLoadClipFragments(command);
}
else if (command.type === types_1.QuantelCommandType.PLAYCLIP) {
await this._quantelManager.playClip(command);
}
else if (command.type === types_1.QuantelCommandType.PAUSECLIP) {
await this._quantelManager.pauseClip(command);
}
else if (command.type === types_1.QuantelCommandType.CLEARCLIP) {
await this._quantelManager.clearClip(command);
}
else if (command.type === types_1.QuantelCommandType.CANCELWAITING) {
this._quantelManager.clearAllWaitWithPort(command.portId);
}
else {
throw new Error(`Unsupported command type "${cmdType}"`);
}
}
catch (e) {
const error = e;
let errorString = error && error.message ? error.message : error.toString();
if (error?.stack) {
errorString += error.stack;
}
this.context.commandError(new Error(errorString), cwc);
}
}
get connected() {
return this._quantel.connected;
}
getStatus() {
let statusCode = timeline_state_resolver_types_1.StatusCode.GOOD;
const messages = [];
const suppressServerDownWarning = Date.now() < (this._disconnectedSince ?? 0) + (this.options?.suppressDisconnectTime ?? 0);
if (!this._quantel.connected && !suppressServerDownWarning) {
statusCode = timeline_state_resolver_types_1.StatusCode.BAD;
messages.push('Not connected');
}
if (this._quantel.statusMessage && !suppressServerDownWarning) {
statusCode = timeline_state_resolver_types_1.StatusCode.BAD;
messages.push(this._quantel.statusMessage);
}
if (!this._quantel.initialized) {
statusCode = timeline_state_resolver_types_1.StatusCode.BAD;
messages.push(`Quantel device connection not initialized (restart required)`);
}
return {
statusCode: statusCode,
messages: messages,
};
}
}
exports.QuantelDevice = QuantelDevice;
//# sourceMappingURL=index.js.map