-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.js
392 lines (368 loc) · 11.6 KB
/
actions.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
const { InstanceStatus } = require('@companion-module/base')
module.exports = {
/**
* Setup the actions.
*
* @access public
* @since 2.0.0
* @returns {Object} the action definitions
*/
get_actions() {
const actions = {}
actions['channelChangeLayout'] = {
name: 'Change channel layout',
options: [
{
type: 'dropdown',
id: 'channelIdlayoutId',
label: 'Change layout to:',
choices: this.choicesChannelLayout(),
default: this.firstId(this.choicesChannelLayout()),
},
],
callback: (action) => {
let type = 'get',
url,
body,
callback
if (typeof action.options.channelIdlayoutId !== 'string' || !action.options.channelIdlayoutId.includes('-')) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Channel and layout are not known! Please review your button config'
)
this.debug('channelIdlayoutId: ' + action.options.channelIdlayoutId)
return
}
const [channelId, layoutId] = action.options.channelIdlayoutId.split('-')
if (!this.state.channels[channelId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing channel! Please review your button config'
)
this.log('error', 'Action on non existing channel: ' + channelId)
return
}
if (!this.state.channels[channelId].layouts[layoutId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing layout! Please review your button config'
)
this.log('error', 'Action on non existing layout ' + layoutId + ' on channel ' + channelId)
return
}
type = 'put'
url = '/api/channels/' + channelId + '/layouts/active'
body = { id: Number(layoutId) }
callback = (response) => {
if (response && response.status === 'ok') {
this.updateActiveChannelLayout(channelId, layoutId)
}
}
// Send request
this.sendRequest(type, url, body).then(callback)
},
}
actions['channelStreaming'] = {
name: 'Start/stop streaming',
options: [
{
type: 'dropdown',
label: 'Channel publishers',
id: 'channelIdpublisherId',
choices: this.choicesChannelPublishers(),
default: this.firstId(this.choicesChannelPublishers()),
tooltip:
'If a channel has only one "publisher" or "stream" then you just select all. Else you can pick the "publisher" you want to start/stop',
},
{
type: 'dropdown',
id: 'startStopAction',
label: 'Action',
choices: [
{ id: 99, label: '---' },
{ id: 1, label: 'Start' },
{ id: 0, label: 'Stop' },
{ id: 3, label: 'Toggle Start/Stop' },
],
default: 1,
},
],
callback: (action) => {
let type = 'post',
url,
body
if (
typeof action.options.channelIdpublisherId !== 'string' ||
!action.options.channelIdpublisherId.includes('-')
) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Channel or Publisher are not valid! Please review your button config'
)
this.debug('Undefined channelIdpublisherId ... ' + action.options.channelIdpublisherId)
return
}
const [channelId, publisherId] = action.options.channelIdpublisherId.split('-')
if (!this.state.channels[channelId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing channel! Please review your button config.'
)
this.log('error', 'Action on non existing channel: ' + channelId)
return
}
if (publisherId !== 'all' && !this.state.channels[channelId].publishers[publisherId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing publisher! Please review your button config.'
)
this.log('error', 'Action on non existing publisher ' + publisherId + ' on channel ' + channelId)
return
}
if (action.options.startStopAction === 99) return
let startStopAction = action.options.startStopAction === 1 ? 'start' : 'stop'
if (action.options.startStopAction === 3) {
// toggle
let isStreaming
const channel = this.state.channels[channelId]
if (publisherId !== 'all') {
isStreaming = channel.publishers[publisherId].status.state === 'started'
} else {
// if we should toggle all, check if there is at least one not streaming and then turn it on
isStreaming = !Object.keys(channel.publishers)
.map((id) => channel.publishers[id].status.state)
.some((state) => state !== 'started')
}
startStopAction = isStreaming ? 'stop' : 'start'
}
if (publisherId !== 'all') {
url = '/api/channels/' + channelId + '/publishers/' + publisherId + '/control/' + startStopAction
} else {
url = '/api/channels/' + channelId + '/publishers/control/' + startStopAction
}
// Send request
this.sendRequest(type, url, body)
},
}
actions['recorderRecording'] = {
name: 'Control recording',
options: [
{
type: 'dropdown',
label: 'Recorder',
id: 'recorderId',
choices: this.choicesRecorders(),
default: this.firstId(this.choicesRecorders()),
},
{
type: 'dropdown',
id: 'startStopAction',
label: 'Action',
choices: [
{ id: 99, label: '---' },
{ id: 1, label: 'Start' },
{ id: 0, label: 'Stop' },
{ id: 2, label: 'Reset' },
{ id: 3, label: 'Toggle Start/Stop' },
],
default: 1,
},
],
callback: (action) => {
let type = 'post',
url,
body,
callback
const recorderId = action.options.recorderId
if (!this.state.recorders[recorderId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing recorder! Please review your button config.'
)
this.log('warn', 'Action on non existing recorder ' + recorderId)
return
}
let startStopAction = action.options.startStopAction
if (startStopAction === 3) {
// Toggle
if (this.state.recorders[recorderId]?.status?.state === 'started') {
startStopAction = 0
} else {
startStopAction = 1
}
}
if (startStopAction === 0) {
url = `/api/recorders/${recorderId}/control/stop`
} else if (startStopAction === 1) {
url = `/api/recorders/${recorderId}/control/start`
} else if (startStopAction === 2) {
url = `/api/recorders/${recorderId}/control/reset`
} else if (startStopAction === 99) {
return
} else {
this.setStatus(InstanceStatus.UnknownWarning, 'Called an unknown action! Please review your button config.')
this.log('error', 'Called an unknown action: ' + action.options.startStopAction)
return
}
callback = async (response) => {
if (response && response.status === 'ok') {
this.updateRecorderStatus(recorderId)
}
}
// Send request
this.sendRequest(type, url, body).then(callback)
},
}
actions['insertMarker'] = {
name: 'Insert Marker',
options: [
{
type: 'dropdown',
label: 'Channel',
id: 'channel',
choices: this.choicesChannel(),
default: this.firstId(this.choicesChannel()),
},
{
type: 'textinput',
id: 'markertext',
label: 'Marker text',
useVariables: true,
default: '',
tooltip: 'You can use variables in this field like current time',
},
],
callback: async (action) => {
let type = 'post'
let url = `/api/channels/${action.options.channel}/bookmarks`
let body = { text: await this.parseVariablesInString(action.options.markertext) }
// Send request
try {
await this.sendRequest(type, url, body)
this.log('info', 'marker successful sent: ' + body.text)
} catch (error) {
this.log('error', 'marker could not be set')
}
},
}
actions['getLayoutData'] = {
name: 'Get layout data',
options: [
{
type: 'dropdown',
id: 'channelIdlayoutId',
label: 'Layout to get',
choices: this.choicesChannelLayout(),
default: this.firstId(this.choicesChannelLayout()),
},
{
id: 'destination',
type: 'custom-variable',
label: 'Destination Variable',
},
],
callback: async (action) => {
if (typeof action.options.channelIdlayoutId !== 'string' || !action.options.channelIdlayoutId.includes('-')) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Channel and layout are not known! Please review your button config'
)
this.debug('channelIdlayoutId: ' + action.options.channelIdlayoutId)
return
}
const [channelId, layoutId] = action.options.channelIdlayoutId.split('-')
if (!this.state.channels[channelId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing channel! Please review your button config'
)
this.log('error', 'Action on non existing channel: ' + channelId)
return
}
if (!this.state.channels[channelId].layouts[layoutId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing layout! Please review your button config'
)
this.log('error', 'Action on non existing layout ' + layoutId + ' on channel ' + channelId)
return
}
//get the data
const url = '/api/channels/' + channelId + '/layouts/' + layoutId + '/settings'
try {
const layoutData = JSON.stringify(await this.sendRequest('GET', url, {}))
this.log(
'debug',
`Layout Data retrieved for Channel ${this.state.channels[channelId].name}, Layout ${this.state.channels[channelId].layouts[layoutId].name}:\n${layoutData}`
)
this.setCustomVariableValue(action.options.destination, layoutData)
} catch (error) {
this.log('error', 'Layout data could not be retrieved or stored')
}
},
}
actions['setLayoutData'] = {
name: 'Set layout data',
options: [
{
type: 'dropdown',
id: 'channelIdlayoutId',
label: 'Layout to set',
choices: this.choicesChannelLayout(),
default: this.firstId(this.choicesChannelLayout()),
},
{
id: 'source',
type: 'textinput',
label: 'Layout Data',
useVariables: true,
default: '{}',
tooltip:
'this text needs to hold a JSON-string describing a Pearl layout, you can retrieve a valid string with the according get action, variables are allowed in this option',
},
],
callback: async (action) => {
if (typeof action.options.channelIdlayoutId !== 'string' || !action.options.channelIdlayoutId.includes('-')) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Channel and layout are not known! Please review your button config'
)
this.debug('channelIdlayoutId: ' + action.options.channelIdlayoutId)
return
}
const [channelId, layoutId] = action.options.channelIdlayoutId.split('-')
if (!this.state.channels[channelId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing channel! Please review your button config'
)
this.log('error', 'Action on non existing channel: ' + channelId)
return
}
if (!this.state.channels[channelId].layouts[layoutId]) {
this.setStatus(
InstanceStatus.UnknownWarning,
'Action on non existing layout! Please review your button config'
)
this.log('error', 'Action on non existing layout ' + layoutId + ' on channel ' + channelId)
return
}
//set the data
const url = '/api/channels/' + channelId + '/layouts/' + layoutId + '/settings'
let body = {}
try {
body = JSON.parse(await this.parseVariablesInString(action.options.source))
} catch (error) {
this.log('error', 'Option is no valid JSON')
return
}
try {
await this.sendRequest('PUT', url, body)
} catch (error) {
this.log('error', 'Layout data could not be sent')
}
},
}
return actions
},
}