timeline-state-resolver
Version:
Have timeline, control stuff
187 lines • 9.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PanasonicPtzDevice = void 0;
const timeline_state_resolver_types_1 = require("timeline-state-resolver-types");
const device_1 = require("../../service/device");
const state_1 = require("./state");
const diff_1 = require("./diff");
const connection_1 = require("./connection");
const lib_1 = require("../../lib");
const commands_1 = require("./commands");
const FOCUS_MODE_MAP = {
[timeline_state_resolver_types_1.FocusMode.AUTO]: connection_1.PanasonicFocusMode.AUTO,
[timeline_state_resolver_types_1.FocusMode.MANUAL]: connection_1.PanasonicFocusMode.MANUAL,
};
class PanasonicPtzDevice extends device_1.Device {
constructor() {
super(...arguments);
this._device = undefined;
this.actions = {
[timeline_state_resolver_types_1.PanasonicPTZActions.RecallPreset]: async (_id, payload) => {
return this.safelyExecuteActionCommand(() => new commands_1.PresetPlaybackControl(payload.presetNumber));
},
[timeline_state_resolver_types_1.PanasonicPTZActions.StorePreset]: async (_id, payload) => {
return this.safelyExecuteActionCommand(() => new commands_1.PresetRegisterControl(payload.presetNumber));
},
[timeline_state_resolver_types_1.PanasonicPTZActions.ResetPreset]: async (_id, payload) => {
return this.safelyExecuteActionCommand(() => new commands_1.PresetDeleteControl(payload.presetNumber));
},
[timeline_state_resolver_types_1.PanasonicPTZActions.SetPanTiltSpeed]: async (_id, payload) => {
const { panSpeed, tiltSpeed } = this.mapPanTiltSpeedToPanasonic(payload.panSpeed, payload.tiltSpeed);
return this.safelyExecuteActionCommand(() => new commands_1.PanTiltSpeedControl(panSpeed, tiltSpeed));
},
[timeline_state_resolver_types_1.PanasonicPTZActions.GetPanTiltPosition]: async (_id) => {
return this.safelyExecuteActionCommand(() => new commands_1.PanTiltPositionQuery());
},
[timeline_state_resolver_types_1.PanasonicPTZActions.SetZoomSpeed]: async (_id, payload) => {
const speed = this.mapZoomSpeedToPanasonic(payload.zoomSpeed);
return this.safelyExecuteActionCommand(() => new commands_1.ZoomSpeedControl(speed));
},
[timeline_state_resolver_types_1.PanasonicPTZActions.GetZoomPosition]: async (_id) => {
return this.safelyExecuteActionCommand(() => new commands_1.ZoomPositionQuery());
},
[timeline_state_resolver_types_1.PanasonicPTZActions.SetFocusSpeed]: async (_id, payload) => {
const speed = this.mapFocusSpeedToPanasonic(payload.focusSpeed);
return this.safelyExecuteActionCommand(() => new commands_1.FocusSpeedControl(speed));
},
[timeline_state_resolver_types_1.PanasonicPTZActions.SetFocusMode]: async (_id, payload) => {
const mode = FOCUS_MODE_MAP[payload.mode];
return this.safelyExecuteActionCommand(() => new commands_1.AutoFocusOnOffControl(mode));
},
[timeline_state_resolver_types_1.PanasonicPTZActions.TriggerOnePushFocus]: async (_id) => {
return this.safelyExecuteActionCommand(() => new commands_1.OneTouchFocusControl());
},
[timeline_state_resolver_types_1.PanasonicPTZActions.GetFocusPosition]: async (_id) => {
return this.safelyExecuteActionCommand(() => new commands_1.FocusPositionQuery());
},
[timeline_state_resolver_types_1.PanasonicPTZActions.GetFocusMode]: async (_id) => {
return this.safelyExecuteActionCommand(() => new commands_1.AutoFocusOnOffQuery());
},
};
}
async init(options) {
this._device = new connection_1.PanasonicPtzHttpInterface(options.host, options.port, options.https);
this._device.init();
this._device.on('error', (e) => this.context.logger.error('Error in PanasonicPtzHttpInterface', e));
return true;
}
async terminate() {
this._device?.dispose();
}
convertTimelineStateToDeviceState(state, newMappings) {
return (0, state_1.convertStateToPtz)(state, newMappings);
}
diffStates(oldState, newState) {
return (0, diff_1.diffStates)(oldState ?? (0, state_1.getDefaultState)(), newState);
}
async sendCommand(command) {
this.context.logger.debug(command);
const cmd = command.command;
try {
if (this._device) {
let result;
switch (cmd.type) {
case timeline_state_resolver_types_1.TimelineContentTypePanasonicPtz.PRESET:
// recall preset
if (cmd.preset !== undefined) {
result = await this._device.executeCommand(new commands_1.PresetPlaybackControl(cmd.preset));
}
else
throw new Error(`Bad parameter: preset`);
break;
case timeline_state_resolver_types_1.TimelineContentTypePanasonicPtz.SPEED:
// set speed
if (cmd.speed !== undefined) {
result = await this._device.executeCommand(new commands_1.PresetSpeedControl(cmd.speed));
}
else
throw new Error(`Bad parameter: speed`);
break;
case timeline_state_resolver_types_1.TimelineContentTypePanasonicPtz.ZOOM_SPEED:
// set zoom speed
if (cmd.zoomSpeed !== undefined) {
// scale -1 - 0 - +1 range to 01 - 50 - 99 range
result = await this._device.executeCommand(new commands_1.ZoomSpeedControl(cmd.zoomSpeed * 49 + 50));
}
else
throw new Error(`Bad parameter: zoomSpeed`);
break;
case timeline_state_resolver_types_1.TimelineContentTypePanasonicPtz.ZOOM:
// set zoom
if (cmd.zoom !== undefined) {
// scale 0 - +1 range to 555h - FFFh range
result = await this._device.executeCommand(new commands_1.ZoomPositionControl(cmd.zoom * 0xaaa + 0x555));
}
else
throw new Error(`Bad parameter: zoom`);
break;
default:
throw new Error(`PTZ: Unknown type: "${cmd.type}"`);
}
this.context.logger.debug(`Panasonic PTZ result: ${result}`);
}
else
throw new Error(`PTZ device not set up`);
}
catch (e) {
this.context.commandError(e, command);
}
}
get connected() {
return this._device?.connected ?? false;
}
getStatus() {
if (!this._device?.connected) {
return {
statusCode: timeline_state_resolver_types_1.StatusCode.GOOD,
messages: [],
};
}
else {
return {
statusCode: timeline_state_resolver_types_1.StatusCode.BAD,
messages: ['Not connected'],
};
}
}
async safelyExecuteActionCommand(createCommandFun) {
try {
const command = createCommandFun();
await this._device?.executeCommand(command);
}
catch {
return {
result: timeline_state_resolver_types_1.ActionExecutionResultCode.Error,
};
}
return {
result: timeline_state_resolver_types_1.ActionExecutionResultCode.Ok,
};
}
mapPanTiltSpeedToPanasonic(panSpeed, tiltSpeed) {
panSpeed = Math.round(panSpeed * 49 + 50);
tiltSpeed = Math.round(tiltSpeed * 49 + 50);
return {
panSpeed,
tiltSpeed,
};
}
mapZoomSpeedToPanasonic(speed) {
return Math.round(speed * 49 + 50);
}
mapFocusSpeedToPanasonic(speed) {
return Math.round(speed * 49 + 50);
}
async executeAction(actionId, payload) {
const actionFun = this.actions[actionId];
if (actionFun) {
return actionFun(actionId, payload);
}
return {
result: timeline_state_resolver_types_1.ActionExecutionResultCode.Error,
response: (0, lib_1.t)('Device does not implement an action handler for this action ID'),
};
}
}
exports.PanasonicPtzDevice = PanasonicPtzDevice;
//# sourceMappingURL=index.js.map