forked from bitfocus/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_skel.js
438 lines (351 loc) · 11.7 KB
/
instance_skel.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* This file is part of the Companion project
* Copyright (c) 2018 Bitfocus AS
* Authors: William Viker <william@bitfocus.io>, Håkon Nessjøen <haakon@bitfocus.io>
*
* This program is free software.
* You should have received a copy of the MIT licence as well as the Bitfocus
* Individual Contributor License Agreement for companion along with
* this program.
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial activities involving the Companion software without
* disclosing the source code of your own applications.
*
*/
var util = require('util')
var debug = require('debug')('lib/instance_skel')
var image = require('./lib/image')
var icons = require('./lib/resources/icons')
function instance(system, id, config) {
var self = this
self.system = system
self.id = id
self.config = config
self.package_info = {}
self._feedbackDefinitions = {}
self._actionDefinitions = {}
// we need this object from instance, and I don't really know how to get it
// out of instance.js without adding an argument to instance() for every
// single module? TODO: håkon: look over this, please.
system.emit('instance_get_package_info', function (obj) {
self.package_info = obj[self.config.instance_type]
})
self._versionscripts = []
for (var key in icons) {
self.defineConst(key, icons[key])
}
self.defineConst(
'REGEX_IP',
'/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'
)
self.defineConst('REGEX_BOOLEAN', '/^(true|false|0|1)$/i')
self.defineConst(
'REGEX_PORT',
'/^([1-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-4])$/'
)
self.defineConst('REGEX_PERCENT', '/^(100|[0-9]|[0-9][0-9])$/')
self.defineConst('REGEX_FLOAT', '/^([0-9]*\\.)?[0-9]+$/')
self.defineConst('REGEX_FLOAT_OR_INT', '/^([0-9]+)(\\.[0-9]+)?$/')
self.defineConst('REGEX_SIGNED_FLOAT', '/^[+-]?([0-9]*\\.)?[0-9]+$/')
self.defineConst('REGEX_NUMBER', '/^\\d+$/')
self.defineConst('REGEX_SOMETHING', '/^.+$/')
self.defineConst('REGEX_SIGNED_NUMBER', '/^[+-]?\\d+$/')
self.defineConst(
'REGEX_TIMECODE',
'/^(0*[0-9]|1[0-9]|2[0-4]):(0*[0-9]|[1-5][0-9]|60):(0*[0-9]|[1-5][0-9]|60):(0*[0-9]|[12][0-9]|30)$/'
)
self.defineConst('CHOICES_YESNO_BOOLEAN', [
{ id: 'true', label: 'Yes' },
{ id: 'false', label: 'No' },
])
// Going to be deprecated sometime
self.defineConst('STATE_UNKNOWN', null)
self.defineConst('STATE_OK', 0)
self.defineConst('STATE_WARNING', 1)
self.defineConst('STATE_ERROR', 2)
// Use these instead
self.defineConst('STATUS_UNKNOWN', null)
self.defineConst('STATUS_OK', 0)
self.defineConst('STATUS_WARNING', 1)
self.defineConst('STATUS_ERROR', 2)
self.currentStatus = self.STATUS_UNKNOWN
self.currentStatusMessage = ''
}
instance.prototype.defineConst = function (name, value) {
Object.defineProperty(this, name, {
value: value,
enumerable: true,
})
}
instance.prototype.Image = image
instance.prototype.rgb = image.rgb
instance.prototype.rgbRev = image.rgbRev
instance.prototype._init = function () {
var self = this
// These two functions needs to be defined after the module has been instanced,
// as they reference the original constructors static data
// Debug with module-name prepeded
self.debug = require('debug')('instance:' + self.package_info.name + ':' + self.id)
// Log to the skeleton (launcher) log window
self.log = function (level, info) {
self.system.emit('log', 'instance(' + self.label + ')', level, info)
}
if (typeof self.init == 'function') {
self.init()
}
}
// Update instance health, levels: null = unknown, 0 = ok, 1 = warning, 2 = error
instance.prototype.status = function (level, message) {
var self = this
self.currentStatus = level
self.currentStatusMessage = message
self.system.emit('instance_status_update', self.id, level, message)
}
instance.prototype.upgradeConfig = function () {
var self = this
var idx = self.config._configIdx
if (idx === undefined) {
idx = -1
}
var debug = require('debug')('instance:' + self.package_info.name + ':' + self.id)
if (idx + 1 < self._versionscripts.length) {
debug('upgradeConfig(' + self.package_info.name + '): ' + (idx + 1) + ' to ' + self._versionscripts.length)
}
for (var i = idx + 1; i < self._versionscripts.length; ++i) {
debug('UpgradeConfig: Upgrading to version ' + (i + 1))
// Fetch instance actions
var actions = []
self.system.emit('actions_for_instance', self.id, function (_actions) {
actions = _actions
})
var release_actions = []
self.system.emit('release_actions_for_instance', self.id, function (_release_actions) {
release_actions = _release_actions
})
var feedbacks = []
self.system.emit('feedbacks_for_instance', self.id, function (_feedbacks) {
feedbacks = _feedbacks
})
var result
try {
result = self._versionscripts[i](self.config, actions, release_actions, feedbacks)
} catch (e) {
debug('Upgradescript in ' + self.package_info.name + ' failed', e)
}
self.config._configIdx = i
for (const action of [...actions, release_actions]) {
action.instance = self.id
action.label = `${self.id}:${action.action}`
}
// If anything was changed, update system and db
if (result) {
self.system.emit('config_save')
self.system.emit('action_save')
self.system.emit('release_action_save')
self.system.emit('feedback_save')
self.system.emit('instance_save')
self.system.emit('db_save')
}
}
if (idx + 1 < self._versionscripts.length) {
// Save the _configIdx change
this.saveConfig()
}
debug('instance save')
self.system.emit('instance_save')
}
instance.prototype.saveConfig = function () {
var self = this
// Save config, but do not automatically call this module's updateConfig again
self.system.emit('instance_config_put', self.id, self.config, true)
}
instance.prototype.addUpgradeScript = function (cb) {
var self = this
self._versionscripts.push(cb)
}
instance.prototype.setActions = function (actions) {
var self = this
if (actions === undefined) {
self._actionDefinitions = {}
} else {
self._actionDefinitions = actions
}
self.system.emit('instance_actions', self.id, actions)
}
instance.prototype.setVariableDefinitions = function (variables) {
var self = this
self.system.emit('variable_instance_definitions_set', self, variables)
}
instance.prototype.setVariable = function (variable, value) {
var self = this
self.system.emit('variable_instance_set', self, variable, value)
}
instance.prototype.getVariable = function (variable, cb) {
var self = this
self.system.emit('variable_get', self.label, variable, cb)
}
instance.prototype.parseVariables = function (string, cb) {
var self = this
self.system.emit('variable_parse', string, cb)
}
instance.prototype.setFeedbackDefinitions = function (feedbacks) {
var self = this
if (feedbacks === undefined) {
self._feedbackDefinitions = {}
} else {
self._feedbackDefinitions = feedbacks
}
self.system.emit('feedback_instance_definitions_set', self, feedbacks)
}
instance.prototype.setPresetDefinitions = function (presets) {
var self = this
// Because RegExp.escape did not become a standard somehow
function escape(str) {
return str.replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, '\\$&')
}
/*
* Clean up variable references: $(instance:variable)
* since the name of the instance is dynamic. We don't want to
* demand that your presets MUST be dynamically generated.
*/
for (var i = 0; i < presets.length; ++i) {
var bank = presets[i].bank
var fixtext = bank.text
if (bank !== undefined && fixtext !== undefined) {
if (fixtext.match(/\$\(/)) {
var matches,
reg = /\$\(([^:)]+):([^)]+)\)/g
while ((matches = reg.exec(fixtext)) !== null) {
if (matches[1] !== undefined) {
if (matches[2] !== undefined) {
reg2 = new RegExp('\\$\\(' + escape(matches[1]) + ':' + escape(matches[2]) + '\\)')
bank.text = bank.text.replace(reg2, '$(' + self.label + ':' + matches[2] + ')')
}
}
}
}
}
}
self.system.emit('preset_instance_definitions_set', self, presets)
}
instance.prototype.checkFeedbacks = function (type) {
var self = this
self.system.emit('feedback_instance_check', self, type)
}
instance.prototype.getAllFeedbacks = function () {
var self = this
var result = undefined
self.system.emit('feedbacks_for_instance', self.id, function (_result) {
result = _result
})
return result
}
instance.prototype.subscribeFeedbacks = function (type) {
var self = this
var feedbacks = self.getAllFeedbacks()
if (feedbacks.length > 0) {
for (var i in feedbacks) {
let feedback = feedbacks[i]
if (type !== undefined && feedback.type != type) {
continue
}
self.subscribeFeedback(feedback)
}
}
}
instance.prototype.unsubscribeFeedbacks = function (type) {
var self = this
var feedbacks = self.getAllFeedbacks()
if (feedbacks.length > 0) {
for (var i in feedbacks) {
let feedback = feedbacks[i]
if (type !== undefined && feedback.type != type) {
continue
}
self.unsubscribeFeedback(feedback)
}
}
}
instance.prototype.subscribeFeedback = function (feedback) {
var self = this
if (feedback.type !== undefined && self._feedbackDefinitions[feedback.type] !== undefined) {
let definition = self._feedbackDefinitions[feedback.type]
// Run the subscribe function if needed
if (definition.subscribe !== undefined && typeof definition.subscribe == 'function') {
definition.subscribe(feedback)
}
}
}
instance.prototype.unsubscribeFeedback = function (feedback) {
var self = this
if (feedback.type !== undefined && self._feedbackDefinitions[feedback.type] !== undefined) {
let definition = self._feedbackDefinitions[feedback.type]
// Run the unsubscribe function if needed
if (definition.unsubscribe !== undefined && typeof definition.unsubscribe == 'function') {
definition.unsubscribe(feedback)
}
}
}
instance.prototype.getAllActions = function () {
var self = this
var result = []
self.system.emit('actions_for_instance', self.id, function (_result) {
result = _result
})
self.system.emit('release_actions_for_instance', self.id, function (_result) {
result.push(..._result)
})
return result
}
instance.prototype.subscribeActions = function (type) {
var self = this
var actions = self.getAllActions()
if (actions.length > 0) {
for (var i in actions) {
let action = actions[i]
if (type !== undefined && action.action != type) {
continue
}
self.subscribeAction(action)
}
}
}
instance.prototype.unsubscribeActions = function (type) {
var self = this
var actions = self.getAllActions()
if (actions.length > 0) {
for (var i in actions) {
let action = actions[i]
if (type !== undefined && action.action != type) {
continue
}
self.unsubscribeAction(action)
}
}
}
instance.prototype.subscribeAction = function (action) {
var self = this
if (action.action !== undefined && self._actionDefinitions[action.action] !== undefined) {
let definition = self._actionDefinitions[action.action]
// Run the subscribe function if needed
if (definition.subscribe !== undefined && typeof definition.subscribe == 'function') {
definition.subscribe(action)
}
}
}
instance.prototype.unsubscribeAction = function (action) {
var self = this
if (action.action !== undefined && self._actionDefinitions[action.action] !== undefined) {
let definition = self._actionDefinitions[action.action]
// Run the unsubscribe function if needed
if (definition.unsubscribe !== undefined && typeof definition.unsubscribe == 'function') {
definition.unsubscribe(action)
}
}
}
instance.extendedBy = function (module) {
util.inherits(module, instance)
}
module.exports = instance