|
1 | | -module.exports = function (self) { |
2 | | - self.setActionDefinitions({ |
3 | | - sample_action: { |
4 | | - name: 'My First Action', |
5 | | - options: [ |
6 | | - { |
7 | | - id: 'num', |
8 | | - type: 'number', |
9 | | - label: 'Test', |
10 | | - default: 5, |
11 | | - min: 0, |
12 | | - max: 100, |
| 1 | +import { FIELDS } from './fields.js' |
| 2 | +import { SuperJoyCommandError } from './error.js' |
| 3 | + |
| 4 | +const ACTION_NAMES = { |
| 5 | + hdmiout: 'HDMI Output Control', |
| 6 | + custom: 'Trigger Custom Button', |
| 7 | + camselect: 'Select Group and Camera', |
| 8 | + directpresets: 'Select Group, Camera, and Preset', |
| 9 | + presets: 'Select Preset on Current Camera', |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Class representing PTZ SuperJoy actions. |
| 14 | + * @class |
| 15 | + * |
| 16 | + * This is implemented as a class so that the SuperJoy instance can be stored |
| 17 | + * as a member variable for use in callbacks and error messages. |
| 18 | + */ |
| 19 | +export class PTZSuperJoyActions { |
| 20 | + /** |
| 21 | + * @property {PTZSuperJoyInstance} superJoyInstance - The instance of the SuperJoy controller. |
| 22 | + */ |
| 23 | + superJoyInstance = null |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructor for PTZSuperJoyActions. |
| 27 | + * @param {PTZSuperJoyInstance} superJoyInstance - The instance of the SuperJoy controller. |
| 28 | + */ |
| 29 | + constructor(superJoyInstance) { |
| 30 | + this.superJoyInstance = superJoyInstance |
| 31 | + this.initActions() |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Callback function for handling action responses. |
| 36 | + * @param {*} json - The json representation of the http response |
| 37 | + * @param {*} data - callback data given by the caller with the current url added |
| 38 | + */ |
| 39 | + actionCallback = (json, data) => { |
| 40 | + if (json.result !== '0') { |
| 41 | + throw new SuperJoyCommandError(`Result is ${json.result}, expect zero`, { |
| 42 | + url: data.url, |
| 43 | + superJoyInstance: this.superJoyInstance, |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Initialize action definitions for the SuperJoy controller. |
| 50 | + */ |
| 51 | + initActions() { |
| 52 | + let actions = { |
| 53 | + hdmiout: { |
| 54 | + name: ACTION_NAMES['hdmiout'], |
| 55 | + options: [FIELDS.HDMIControl], |
| 56 | + callback: async (action) => { |
| 57 | + let argMap = new Map([['action', action.options.hdmicontrol]]) |
| 58 | + this.superJoyInstance.sendCommand('hdmiout', argMap, { |
| 59 | + function: this.actionCallback, |
| 60 | + data: null, |
| 61 | + }) |
| 62 | + }, |
| 63 | + }, |
| 64 | + custom: { |
| 65 | + name: ACTION_NAMES['custom'], |
| 66 | + options: [FIELDS.CustomButton], |
| 67 | + callback: async (action, context) => { |
| 68 | + let fields = this.superJoyInstance.getFields() |
| 69 | + let buttonTxt = await context.parseVariablesInString(action.options.buttonid) |
| 70 | + let buttonId = fields.validateCustomButton(ACTION_NAMES['custom'], buttonTxt) |
| 71 | + let argMap = new Map([ |
| 72 | + ['action', 'trigger'], |
| 73 | + ['buttonid', buttonId], |
| 74 | + ]) |
| 75 | + this.superJoyInstance.sendCommand('custom', argMap, { |
| 76 | + function: this.actionCallback, |
| 77 | + data: null, |
| 78 | + }) |
| 79 | + }, |
| 80 | + }, |
| 81 | + camselect: { |
| 82 | + name: ACTION_NAMES['camselect'], |
| 83 | + options: [FIELDS.Group, FIELDS.Camera], |
| 84 | + callback: async (action, context) => { |
| 85 | + let fields = this.superJoyInstance.getFields() |
| 86 | + let groupTxt = await context.parseVariablesInString(action.options.group) |
| 87 | + let camidTxt = await context.parseVariablesInString(action.options.id) |
| 88 | + let groupAndCam = fields.validateGroupAndCamera(ACTION_NAMES['camselect'], groupTxt, camidTxt) |
| 89 | + let argMap = new Map([ |
| 90 | + ['group', groupAndCam.group], |
| 91 | + ['camid', groupAndCam.camid], |
| 92 | + ]) |
| 93 | + this.superJoyInstance.sendCommand('camselect', argMap, { |
| 94 | + function: this.actionCallback, |
| 95 | + data: null, |
| 96 | + }) |
| 97 | + }, |
| 98 | + }, |
| 99 | + directpresets: { |
| 100 | + name: ACTION_NAMES['directpresets'], |
| 101 | + options: [FIELDS.Group, FIELDS.Camera, FIELDS.Preset, FIELDS.Speed], |
| 102 | + callback: async (action, context) => { |
| 103 | + let fields = this.superJoyInstance.getFields() |
| 104 | + let groupTxt = await context.parseVariablesInString(action.options.group) |
| 105 | + let camidTxt = await context.parseVariablesInString(action.options.id) |
| 106 | + let groupAndCam = fields.validateGroupAndCamera(ACTION_NAMES['directpresets'], groupTxt, camidTxt) |
| 107 | + let presetTxt = await context.parseVariablesInString(action.options.preset) |
| 108 | + let presetSpeedTxt = await context.parseVariablesInString(action.options.speed) |
| 109 | + let preset = fields.validatePreset(ACTION_NAMES['directpresets'], presetTxt) |
| 110 | + let presetSpeed = fields.validatePresetSpeed(ACTION_NAMES['directpresets'], presetSpeedTxt) |
| 111 | + let argMap = new Map([ |
| 112 | + ['action', 'recall'], |
| 113 | + ['group', groupAndCam.group], |
| 114 | + ['camid', groupAndCam.camid], |
| 115 | + ['preset', preset], |
| 116 | + ['presetspeed', presetSpeed], |
| 117 | + ]) |
| 118 | + this.superJoyInstance.sendCommand('directpresets', argMap, { |
| 119 | + function: this.actionCallback, |
| 120 | + data: null, |
| 121 | + }) |
| 122 | + }, |
| 123 | + }, |
| 124 | + presets: { |
| 125 | + name: ACTION_NAMES['presets'], |
| 126 | + options: [FIELDS.Preset, FIELDS.Speed], |
| 127 | + callback: async (action, context) => { |
| 128 | + let fields = this.superJoyInstance.getFields() |
| 129 | + let presetTxt = await context.parseVariablesInString(action.options.preset) |
| 130 | + let presetSpeedTxt = await context.parseVariablesInString(action.options.speed) |
| 131 | + let preset = fields.validatePreset(ACTION_NAMES['presets'], presetTxt) |
| 132 | + let presetSpeed = fields.validatePresetSpeed(ACTION_NAMES['presets'], presetSpeedTxt) |
| 133 | + let argMap = new Map([ |
| 134 | + ['action', 'recall'], |
| 135 | + ['preset', preset], |
| 136 | + ['presetspeed', presetSpeed], |
| 137 | + ]) |
| 138 | + this.superJoyInstance.sendCommand('presets', argMap, { |
| 139 | + function: this.actionCallback, |
| 140 | + data: null, |
| 141 | + }) |
13 | 142 | }, |
14 | | - ], |
15 | | - callback: async (event) => { |
16 | | - console.log('Hello world!', event.options.num) |
17 | 143 | }, |
18 | | - }, |
19 | | - }) |
| 144 | + } |
| 145 | + this.superJoyInstance.setActionDefinitions(actions) |
| 146 | + } |
20 | 147 | } |
0 commit comments