generated from bitfocus/companion-module-template-js
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfields.js
More file actions
183 lines (168 loc) · 5.11 KB
/
fields.js
File metadata and controls
183 lines (168 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Fields for PTZOptics SuperJoy actions and feedbacks.
*/
export const FIELDS = {
Camera: {
type: 'textinput',
label: 'Camera ID',
id: 'id',
default: 1,
useVariables: true,
},
Preset: {
type: 'textinput',
label: 'Preset (1 - 255',
id: 'preset',
default: 0,
useVariables: true,
},
Speed: {
type: 'textinput',
label: 'Preset Speed (1 - 24)',
id: 'speed',
default: 24,
useVariables: true,
},
Group: {
type: 'textinput',
label: 'Group (1 - 5)',
id: 'group',
default: 1,
useVariables: true,
},
HDMIControl: {
type: 'dropdown',
id: 'hdmicontrol',
label: 'Choose Control Behavior',
default: 'toggle',
choices: [
{ id: 'toggle', label: 'Toggle' },
{ id: 'off', label: 'Off' },
{ id: 'on', label: 'On' },
],
},
HDMIState: {
type: 'dropdown',
id: 'hdmistate',
label: 'HDMI State',
default: 'on',
choices: [
{ id: 'off', label: 'Off' },
{ id: 'on', label: 'On' },
],
},
CustomButton: {
type: 'textinput',
id: 'buttonid',
label: 'Custom Button to Trigger (1 - 5)',
default: 1,
useVariables: true,
},
}
/**
* SuperJoyValueError is thrown when an error occurs due to an incorrect value
* in a field. This can be a literal value or a variable that resolves to an incorrect value.
* @class
* @extends Error
*/
export class SuperJoyValueError extends Error {
constructor(message) {
super(message)
this.name = 'SuperJoyValueError'
}
}
/**
* Class for PTZOptics SuperJoy action and feedback fields.
* @class
* @param {PTZSuperJoyInstance} superJoyInstance
*/
export class PTZSuperJoyFields {
/**
* @property {PTZSuperJoyInstance} superJoyInstance - The instance of the SuperJoy controller.
*/
superJoyInstance = null
constructor(superJoyInstance) {
this.superJoyInstance = superJoyInstance
}
/**
* Takes a string value generated by potentially parsing variables in a field and validate
* that it is an integer in the given range and returns the string
* @raises {SuperJoyValueError} if the value is invalid
* @param {string} locationTxt locationTxt Description of the location of the field (action or feedback)
* @param {string} valueTxt Incoming text value to be validated
* @param {number} min Minimum valid value
* @param {number} max Maximum valid value
* @param {string} fieldName Name of the field being validated for good error messages
* @returns number
*/
validateNumberInRange(locationTxt, valueTxt, min, max, fieldName) {
let num = Number(valueTxt)
if (!Number.isInteger(num)) {
throw new SuperJoyValueError(`${locationTxt}: ${fieldName} must be an integer, got ${valueTxt}`, {
caller: this.superJoyInstance,
})
}
if (num < min || num > max) {
throw new SuperJoyValueError(
`${locationTxt}: ${fieldName} must be a number between ${min} and ${max}, got ${num}`,
{
caller: this.superJoyInstance,
},
)
}
return num.toString()
}
/**
* Validate that a custom button ID is valid (1-5)
* @param {string} locationTxt Description of the location of the button field (action or feedback)
* @param {string} valueTxt Incoming button ID text value
* @returns {string} Validated button ID as a string
*/
validateCustomButton(locationTxt, valueTxt) {
return this.validateNumberInRange(locationTxt, valueTxt, 1, 5, FIELDS.CustomButton.label)
}
/**
* Valdate that a preset number is valid (0-255)
* @param {string} locationTxt Description of the location of the preset field (action or feedback)
* @param {string} valueTxt Incoming preset text value
* @returns {string} Validated preset as a string
*/
validatePreset(locationTxt, valueTxt) {
return this.validateNumberInRange(locationTxt, valueTxt, 0, 255, FIELDS.Preset.label)
}
/**
* Valdate that a preset speed number is valid (1-24)
* @param {string} locationTxt Description of the location of the preset speed field (action or feedback)
* @param {string} valueTxt Incoming preset speed text value
* @returns {string} Validated preset speed as a string
*/
validatePresetSpeed(locationTxt, valueTxt) {
return this.validateNumberInRange(locationTxt, valueTxt, 1, 24, FIELDS.Speed.label)
}
/**
* Validate that a group number is valid (1-5)
* @param {string} locationTxt Description of the location of the group field (action or feedback)
* @param {string} valueTxt
* @returns
*/
validateGroup(locationTxt, valueTxt) {
return this.validateNumberInRange(locationTxt, valueTxt, 1, 5, FIELDS.Group.label)
}
/**
* Validate that both a group and camera ID are valid, taking into account that group 5
* allows camera IDs up to 255 and the rest are just 1-6.
* @param {string} locationTxt Description of the location of the group/camera fields (action or feedback)
* @param {string} groupTxt Incoming group text value
* @param {string} camTxt Incoming camera text value
* @returns {group: string, camid: string}
*/
validateGroupAndCamera(locationTxt, groupTxt, camTxt) {
let group = this.validateGroup(locationTxt, groupTxt)
let camMax = 6
if (group == 5) {
camMax = 255
}
let camid = this.validateNumberInRange(locationTxt, camTxt, 1, camMax, FIELDS.Camera.label)
return { group: group, camid: camid }
}
}