-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
348 lines (308 loc) · 9.53 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
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
const { InstanceBase, Regex, runEntrypoint, TCPHelper, InstanceStatus } = require('@companion-module/base')
const { numberOfPresentationSlots, numberOfMediaPlayerSlots } = require('./constants')
var actions = require('./actions')
var feedbacks = require('./feedbacks')
var states = require('./states')
var presets = require('./presets')
class APSInstance extends InstanceBase {
constructor(internal) {
super(internal)
}
async configUpdated(config) {
this.config = config
this.captureStates = states.generateCaptureStates()
this.displayStates = states.generateDisplayStates()
this.slotStates = states.generateSlotStates()
this.mediaPlayerState = {
slots: states.generateMediaSlotStates(),
playing: false,
paused: false,
loop_on: false,
fade_on: false,
}
this.captureTimeoutObj = null
this.statesTimeoutObj = null
this.receiver = new MessageBuffer('$')
this.initTCP()
this.actions() // export actions
this.feedbacks()
this.variables()
this.presets()
}
async init(config) {
this.configUpdated(config)
}
initTCP() {
var self = this
if (self.socket !== undefined) {
self.socket.destroy()
delete self.socket
}
if (self.config.host && self.config.port) {
self.socket = new TCPHelper(self.config.host, self.config.port)
self.socket.on('status_change', (status, message) => {
//self.log('debug', `Status ${status}, message: ${message}`)
//self.updateStatus(status)
})
self.socket.on('error', (_err) => {
self.updateStatus(InstanceStatus.UnknownError)
})
self.socket.on('connect', () => {
self.updateStatus(InstanceStatus.Ok)
if (self.statesTimeoutObj !== null) {
clearTimeout(self.statesTimeoutObj)
}
self.statesTimeoutObj = setTimeout(() => {
try {
self.socket.send('states$')
self.statesTimeoutObj = null
} catch (err) {
this.log('debug', err)
}
}, 1000)
})
self.socket.on('data', (data) => {
self.receiver.push(data)
let messages = self.receiver.getMessages()
if (messages == null) return
for (let i = 0; i < messages.length; i++) {
let message = messages[i]
try {
let jsonData = JSON.parse(message)
if (jsonData.action === 'states') {
states.updateStates(self.displayStates, jsonData.data)
self.checkFeedbacks('loaded', 'displayed')
} else if (jsonData.action === 'display') {
states.updateDisplayStates(self.displayStates, jsonData.data)
self.checkFeedbacks('displayed')
} else if (jsonData.action === 'capture') {
states.uploadLoadStates(self.displayStates, jsonData.index)
states.updateCaptureStates(self.captureStates, jsonData.index)
self.checkFeedbacks('captured')
if (self.captureTimeoutObj !== null) {
clearTimeout(self.captureTimeoutObj)
}
self.captureTimeoutObj = setTimeout(() => {
states.updateCaptureStates(self.captureStates, 999)
self.checkFeedbacks('captured', 'loaded')
self.captureTimeoutObj = null
}, 1500)
} else if (jsonData.action === 'delete') {
states.updateUnloadStates(self.displayStates, jsonData.index)
self.checkFeedbacks('loaded')
} else if (jsonData.action === 'files') {
let update_obj = {
Presentation_previous: jsonData.data.prev,
Presentation_current: jsonData.data.curr,
Presentation_next: jsonData.data.next,
}
// For not raising exception while using old verions of APS
if (jsonData.data.slide_number) {
update_obj['slide_number'] = jsonData.data.slide_number
update_obj['slides_count'] = jsonData.data.slides_count
update_obj['Slides_builds_count'] = jsonData.data.builds_count
}
self.setVariableValues(update_obj)
} else if (jsonData.action === 'slots') {
self.setSlotVariables(jsonData.data)
states.updateSlotStates(self.slotStates, jsonData.data)
self.checkFeedbacks('slot_exist', 'slot_displayed')
} else if (jsonData.action === 'MediaPlayer') {
self.setMediaPlayerVariables(jsonData.data)
states.updateMediaPlayerState(self.mediaPlayerState, jsonData.data)
self.checkFeedbacks(
'Media_playing',
'Media_loaded',
'Media_playback_state_playing',
'Media_playback_state_paused',
'Media_player_loop_on',
'Media_player_fade_on',
)
}
} catch (e) {
console.error(e)
}
}
})
}
}
getConfigFields() {
return [
{
type: 'static-text',
id: 'info',
width: 12,
label: 'Information',
value: 'This will establish a TCP connection to interact with the APS app',
},
{
type: 'textinput',
id: 'host',
label: 'Target IP (For local: 127.0.0.1)',
default: '127.0.0.1',
width: 6,
regex: Regex.IP,
},
{
type: 'textinput',
id: 'port',
label: 'Target Port (Default: 31600)',
default: '31600',
width: 6,
regex: Regex.PORT,
},
{
type: 'static-text',
id: 'info-defaultport',
width: 12,
label:
'Check that the port in APS matches the target port shown here. To change the default port in APS, go to “Settings” in the app interface. Note that for earlier versions of APS, (2.2 and below) the default port is 4778. We recommend using port 31600 for connection. If this port is not available, try something else in the same range.',
},
]
}
actions() {
let ats = actions.getActions(this)
this.setActionDefinitions(ats)
}
feedbacks() {
var self = this
var fdbs = feedbacks.getFeedbacks(self)
self.setFeedbackDefinitions(fdbs)
}
variables() {
var self = this
var variables = [
{ name: 'Presentation: Previous in folder', variableId: 'Presentation_previous' },
{ name: 'Presentation: Current', variableId: 'Presentation_current' },
{ name: 'Presentation: Next in folder', variableId: 'Presentation_next' },
{ name: 'Slide: Current', variableId: 'slide_number' },
{ name: 'Slide: Total number', variableId: 'slides_count' },
{ name: 'Slide: Builds count', variableId: 'Slides_builds_count' },
{ name: 'Media player: Playing media', variableId: 'Media_playing' },
{ name: 'Media player: Loaded media', variableId: 'Media_loaded' },
{ name: 'Media player: Playing media filename', variableId: 'Media_playing_filename' },
{ name: 'Media player: Loaded media filename', variableId: 'Media_loaded_filename' },
{ name: 'Media player: Playback state', variableId: 'Media_playback_state' },
{ name: 'Media player: Time left', variableId: 'Media_time_left' },
{ name: 'Media player: Time elapsed', variableId: 'Media_time_elapsed' },
{ name: 'Media player: Time duration', variableId: 'Media_time_duration' },
]
for (let i = 1; i <= numberOfPresentationSlots; i++) {
variables.push({
name: `Presentation Slot ${i}`,
variableId: `presentation_slot${i}`,
})
}
for (let i = 1; i <= numberOfPresentationSlots; i++) {
variables.push({
name: `Media ${i}`,
variableId: `media_slot${i}`,
})
}
self.setVariableDefinitions(variables)
const values = {
Presentation_previous: '',
Presentation_current: '',
Presentation_next: '',
slide_number: '',
slides_count: '',
Slides_builds_count: '',
Media_playing: '',
Media_loaded: '',
Media_playing_filename: '',
Media_loaded_filename: '',
Media_playback_state: '',
Media_time_left: '',
Media_time_elapsed: '',
Media_time_duration: '',
}
try {
for (let i = numberOfPresentationSlots; i > 0; i--) {
values[`presentation_slot${i}`] = '-'
}
} catch (err) {
self.log('debug', err)
}
try {
for (let i = numberOfMediaPlayerSlots; i > 0; i--) {
values[`media_slot${i}`] = '-'
}
} catch (err) {
self.log('debug', err)
}
self.setVariableValues(values)
}
setSlotVariables(data) {
var self = this
const values = {}
try {
for (let i = numberOfPresentationSlots; i > 0; i--) {
values[`presentation_slot${i}`] = data.filenames[i - 1]
}
} catch (err) {
self.log('debug', err)
}
self.setVariableValues(values)
}
setMediaPlayerVariables(data) {
var self = this
const values = {
Media_playing: data.Media_playing,
Media_loaded: data.Media_loaded,
Media_playing_filename: data.Media_playing_filename,
Media_loaded_filename: data.Media_loaded_filename,
Media_playback_state: data.Media_playback_state,
Media_time_left: data.Media_time_left,
Media_time_elapsed: data.Media_time_elapsed,
Media_time_duration: data.Media_duration,
}
try {
for (let i = numberOfPresentationSlots; i > 0; i--) {
values[`media_slot${i}`] = data.filenames[i - 1]
}
} catch (err) {
self.log('debug', err)
}
self.setVariableValues(values)
}
presets() {
var self = this
try {
self.setPresetDefinitions(presets.getPresets(self))
} catch (err) {
self.log('debug', err)
}
}
async destroy() {
var self = this
if (self.socket !== undefined) {
self.socket.destroy()
}
self.log('debug', `destroy ${self.id}`)
}
}
class MessageBuffer {
constructor(delimiter) {
this.delimiter = delimiter
this.buffer = ''
}
isFinished() {
return this.buffer.length === 0 || this.buffer.indexOf(this.delimiter) === -1
}
push(data) {
this.buffer += data
}
getMessages() {
const messages = []
while (!this.isFinished()) {
const delimiterIndex = this.buffer.indexOf(this.delimiter)
if (delimiterIndex !== -1) {
const message = this.buffer.slice(0, delimiterIndex)
this.buffer = this.buffer.replace(message + this.delimiter, '')
messages.push(message)
}
}
return messages.length > 0 ? messages : null
}
}
runEntrypoint(APSInstance, [])