generated from bitfocus/companion-module-template-js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
260 lines (227 loc) · 6.02 KB
/
main.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
import { InstanceBase, Regex, runEntrypoint, InstanceStatus } from '@companion-module/base'
import { getActions } from './actions.js'
import { getPresets } from './presets.js'
import { getVariables } from './variables.js'
import { getFeedbacks } from './feedbacks.js'
import UpgradeScripts from './upgrades.js'
import fetch from 'node-fetch'
import { XMLParser, XMLBuilder } from 'fast-xml-parser'
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration.js'
dayjs.extend(duration)
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '',
allowBooleanAttributes: true,
ignoreDeclaration: true,
})
class ElementalLiveInstance extends InstanceBase {
constructor(internal) {
super(internal)
}
async init(config) {
this.config = config
this.updateStatus(InstanceStatus.Connecting)
this.initConnection()
this.initActions()
this.initPresets()
this.initVariables()
this.initFeedbacks()
}
async destroy() {
this.stopPoll()
this.log('debug', 'destroy')
}
async configUpdated(config) {
this.config = config
}
getConfigFields() {
return [
{
type: 'textinput',
id: 'host',
label: 'Device IP',
width: 8,
regex: Regex.IP,
},
{
type: 'static-text',
id: 'rejectUnauthorizedInfo',
width: 12,
value: `
<hr />
<h5>WARNING</h5>
This module rejects server certificates considered invalid for the following reasons:
<ul>
<li>Certificate is expired</li>
<li>Certificate has the wrong host</li>
<li>Untrusted root certificate</li>
<li>Certificate is self-signed</li>
</ul>
<p>
We DO NOT recommend turning off this option. However, if you NEED to connect to a host
with a self-signed certificate you will need to set <strong>Unauthorized Certificates</strong>
to <strong>Accept</strong>.
</p>
<p><strong>USE AT YOUR OWN RISK!<strong></p>
`,
},
{
type: 'dropdown',
id: 'rejectUnauthorized',
label: 'Unauthorized Certificates',
width: 6,
default: true,
choices: [
{ id: true, label: 'Reject' },
{ id: false, label: 'Accept - Use at your own risk!' },
],
},
]
}
initVariables() {
const variables = getVariables.bind(this)()
this.setVariableDefinitions(variables)
}
initFeedbacks() {
const feedbacks = getFeedbacks.bind(this)()
this.setFeedbackDefinitions(feedbacks)
}
initPresets() {
const presets = getPresets.bind(this)()
this.setPresetDefinitions(presets)
}
initActions() {
const actions = getActions.bind(this)()
this.setActionDefinitions(actions)
}
sendGetRequest(request) {
if (!this.config.rejectUnauthorized) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
} else {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'
}
let url = `https://${this.config.host}/${request}`
return fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
})
.then((res) => {
if (res.status == 200) {
this.updateStatus(InstanceStatus.Ok)
return res.text()
} else if (res.status == 401) {
this.updateStatus('bad_config', 'Authentication Error')
}
})
.then((data) => {
let object = JSON.parse(data)
if ('live_event' in object) {
this.processStatus(object)
} else {
this.processData(object)
}
})
.catch((error) => {
let errorText = String(error)
if (errorText.match('ETIMEDOUT') || errorText.match('ENOTFOUND') || errorText.match('ECONNREFUSED')) {
this.updateStatus('connection_failure')
}
this.log('debug', errorText)
})
}
sendPostRequest(request, body) {
if (!this.config.rejectUnauthorized) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
} else {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'
}
let output
if (body) {
let builder = new XMLBuilder({})
output = builder.build(body)
}
let url = `https://${this.config.host}/${request}`
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'text/xml',
Accept: 'application/xml',
},
body: output,
})
.then((res) => {
if (res.status == 200) {
this.updateStatus('ok')
return res.text()
} else if (res.status == 401) {
this.updateStatus('bad_config', 'Authentication Error')
}
})
.then((data) => {
if (data) {
let object = JSON.parse(data)
this.processData(object)
} else {
this.log('debug', 'No response received')
}
})
.catch((error) => {
let errorText = String(error)
if (errorText.match('ETIMEDOUT') || errorText.match('ENOTFOUND') || errorText.match('ECONNREFUSED')) {
this.updateStatus('connection_failure')
}
this.log('debug', errorText)
})
}
initConnection() {
this.live_events = {}
this.system = {}
this.sendGetRequest('live_events')
this.startPoll()
}
startPoll() {
this.stopPoll()
this.poll = setInterval(() => {
this.pollDevice()
}, 500)
}
stopPoll() {
if (this.poll) {
clearInterval(this.poll)
delete this.poll
}
}
pollDevice() {
this.sendGetRequest('live_events')
this.checkFeedbacks()
}
processData(data) {
data.forEach((eventData) => {
let liveEventData = eventData.live_event
let oldEventCount = Object.keys(this.live_events).length
let newEventCount = Object.keys(data).length
let changed = oldEventCount != newEventCount || oldEventCount === 0 ? true : false
this.live_events[liveEventData.id] = { ...liveEventData }
if (changed) {
this.initVariables()
this.initPresets()
}
this.setVariableValues({
[`event_${liveEventData.id}_name`]: liveEventData.name,
})
this.sendGetRequest(`live_events/${liveEventData.id}/status`)
})
}
processStatus(data) {
this.live_events[data.live_event.id] = { ...this.live_events[data.live_event.id], ...data.live_event }
this.setVariableValues({
[`event_${data.live_event.id}_status`]: data.live_event.status,
[`event_${data.live_event.id}_average_fps`]: data.live_event.average_fps,
})
}
}
runEntrypoint(ElementalLiveInstance, UpgradeScripts)