generated from bitfocus/companion-module-template-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
310 lines (281 loc) · 7.64 KB
/
index.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
const bent = require('bent')
const { InstanceStatus, InstanceBase, runEntrypoint } = require('@companion-module/base')
const UpgradeScripts = require('./upgrades')
class SoundrInstance extends InstanceBase {
constructor(internal) {
super(internal)
this.vardefs = [];
}
async init(config) {
const tThis = this
this.config = config
this.updateStatus(InstanceStatus.Connecting)
this.log("info", "Exporting actions")
this.updateActions() // export actions
this.variables()
this.initPresets()
this.isReady = true
this.intervalId = setInterval(function handleInterval() {
tThis.log('info', 'Updating variables')
tThis.updateVariables()
}, this.config.refreshTime)
}
async retrieveSound() {
const tTemp = this
if (this.config.host != undefined) {
const body = await bent(
'GET',
200,
'http://' + this.config.host + ':' + this.config['port'] + '/v1/list',
'json'
)()
const namesAndValues = []
tTemp.updateStatus(InstanceStatus.Ok)
tTemp.log("info", 'Connected to ' + this.config.host)
tTemp.log(body)
for (var i = 0; i < body.length; i++) {
namesAndValues.push({ id: body[i][1], label: body[i][0] })
}
return namesAndValues
} else {
return []
}
}
updateVariables() {
const tThis = this
if (this.isReady) {
this.log('info', 'Updating variables')
this.vardefs.forEach(function handleList(item) {
if (item.variableId != "amount_sounds_currently_playing") {
let idT = item.variableId.split("_")[1]
console.log("debug", "Checking ID " + idT + " remaining time")
bent('GET', 200, 'http://' + tThis.config.host + ':' + tThis.config['port'] + '/v1/remaining?id=' + idT, 'json')().then(
function handleList(body) {
let updates = {}
tThis.updateStatus(InstanceStatus.Ok)
//tThis.setVariableValues({('sound_' + body.id + "_remaining_time") = body.RemaningSec})
updates["sound_" + body.id + "_remaining_time"] = String(body.RemaningSec)
tThis.setVariableValues(updates)
}
)
}
})
bent('GET', 200, 'http://' + this.config.host + ':' + this.config['port'] + '/v1/current', 'json')().then(
function handleList(body) {
tThis.updateStatus(InstanceStatus.Ok)
let updates = {}
updates["amount_sounds_currently_playing"] = String(Object.keys(body).length)
tThis.setVariableValues(updates)
}
)
}
}
variables() {
const tThis = this
const VarDefs = []
if (this.config.soundsToMonitor != undefined && this.config.soundsToMonitor.length > 0) {
var listy = (this.config.soundsToMonitor + ",").split(",")
listy.forEach(function handleList(item) {
VarDefs.push({
name: 'Sound ' + item + ' remaining time',
variableId: 'sound_' + item + '_remaining_time'
})
})
}
VarDefs.push({
name: 'Songs currently playing',
variableId: 'amount_sounds_currently_playing',
})
this.vardefs = VarDefs
this.setVariableDefinitions(VarDefs)
const newVariables = {}
VarDefs.forEach(function handleList(item) {
newVariables[item.variableId] = '0'
})
this.setVariableValues(newVariables)
}
async updateActions() {
const optis = await this.retrieveSound()
this.setActionDefinitions({
stopAll: {
name: 'Stop all sounds',
callback: async (action) => {
this.log('info', 'Stopping all sounds')
bent('GET', 200, 'http://' + this.config.host + ':' + this.config['port'] + '/v1/stopAll', 'json')()
},
options: []
},
bufferAll: {
name: 'Buffer all sounds',
callback: (action) => {
bent('GET', 200, 'http://' + this.config.host + ':' + this.config['port'] + '/v1/bufferAll', 'json')()
},
options: []
},
stop: {
name: 'Stop sound',
options: [
{
type: 'number',
label: 'Sound ID',
id: 'id',
min: 0,
required: true,
tooltip: 'The ID of the sound to stop',
}
],
callback: (action) => {
this.log('info', 'Stopping sound ' + action.options.id)
bent('GET', 200, 'http://' + this.config.host + ':' + this.config['port'] + '/v1/stop?id=' + action.options.id, 'json')()
}
},
play: {
name: 'Play sound',
options: [
{
type: 'dropdown',
label: 'Select a sound',
id: 'soundDropdown',
default: 0,
tooltip: 'Please select a sound to be played',
choices: optis,
//choices: [],
minChoicesForSearch: 0,
},
{
type: 'checkbox',
label: 'Loop',
id: 'soundLoop',
default: false,
tooltip: 'Loop the audio or not',
},
{
type: 'number',
label: 'Vanity Id / Nickname',
id: 'vanityId',
default: -1,
tooltip: 'The vanityId of the sound, -1 to disable it.',
}
],
callback: (action) => {
this.log('info', 'Playing sound ' + action.options.soundDropdown + " on http://" + this.config.host + ':' + this.config['port'] + '/v1/play?file=' + action.options.soundDropdown + '&loop=' + action.options.soundLoop + '&id=' + action.options.vanityId)
bent('GET', 200, 'http://' + this.config.host + ':' + this.config['port'] + '/v1/play?file=' + action.options.soundDropdown + '&loop=' + action.options.soundLoop + '&id=' + action.options.vanityId, 'json')()
}
},
})
}
initPresets() {
var presets = {}
presets ["stop_all"] = {
category: 'Group Controls',
label: '',
type: "button",
name: "Stop all sounds",
style: {
style: 'text',
text: 'Stop all sounds',
size: 'auto',
color: '16777215',
},
steps: [
{
down: [
{
// add an action on down press
actionId: 'stopAll',
options: {
},
},
],
},
],
feedbacks: []
}
presets["buffer_all"] = {
category: 'Group Controls',
type: "button",
name: "Buffer all sounds",
label: '',
style: {
style: 'text',
text: 'Buffer all sounds',
size: 'auto',
color: '16777215',
},
steps: [
{
down: [
{
// add an action on down press
actionId: 'bufferAll',
options: {
},
},
],
},
],
feedbacks: []
}
this.setPresetDefinitions(presets)
}
getConfigFields() {
return [
{
type: 'textinput',
id: 'host',
label: 'Target IP',
width: 8,
regex: this.REGEX_IP,
},
{
type: 'textinput',
id: 'port',
label: 'Target Port',
width: 4,
default: 4449,
regex: this.REGEX_PORT,
},
{
type: 'number',
id: 'refreshTime',
label: 'Update interval',
width: 4,
default: 1000,
},
{
type: 'textinput',
id: 'soundsToMonitor',
label: 'Sounds to monitor (enter a comma separated list)',
width: 12,
}
]
}
async configUpdated(config) {
tThis = this
this.config = config
this.isReady = false
this.log('info', 'Config updated, connecting to ' + this.config.host + ':' + this.config.port)
this.updateStatus(InstanceStatus.Connecting)
if (this.config.host != undefined && this.config.port != undefined) {
this.log('info', 'Connecting to http://' + this.config['host'] + ':' + this.config['port'])
bent('GET', 200, 'http://' + this.config.host + ':' + this.config['port'] + '/v1/list', 'json')().then(
function handleList(body) {
tThis.updateStatus(InstanceStatus.Ok)
tThis.log('Connected to ' + this.config.host)
tThis.log(body)
tThis.isReady = true
}
).catch(function handleFail(reason) {
tThis.updateStatus(InstanceStatus.ConnectionFailure)
tThis.log('error', 'Failed to connect to ' + this.config.host + ' - ' + reason)
})
} else {
this.updateStatus(InstanceStatus.BadConfig)
}
}
async destroy() {
clearInterval(this.intervalId)
this.debug('destroy')
}
}
runEntrypoint(SoundrInstance, UpgradeScripts)