Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
265 lines (242 loc) · 6.59 KB
/
Copy pathmain.js
File metadata and controls
265 lines (242 loc) · 6.59 KB
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
import { InstanceBase, runEntrypoint, InstanceStatus } from '@companion-module/base'
import WebSocket from 'ws'
import objectPath from 'object-path'
import { upgradeScripts } from './upgrade.js'
class WebsocketInstance extends InstanceBase {
isInitialized = false
subscriptions = new Map()
wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$'
async init(config) {
this.config = config
this.initWebSocket()
this.isInitialized = true
this.updateVariables()
this.initActions()
this.initFeedbacks()
this.subscribeFeedbacks()
}
async destroy() {
this.isInitialized = false
if (this.reconnect_timer) {
clearTimeout(this.reconnect_timer)
this.reconnect_timer = null
}
if (this.ws) {
this.ws.close(1000)
delete this.ws
}
}
async configUpdated(config) {
this.config = config
this.initWebSocket()
}
updateVariables(callerId = null) {
let variables = new Set()
let defaultValues = {}
this.subscriptions.forEach((subscription, subscriptionId) => {
if (!subscription.variableName.match(/^[-a-zA-Z0-9_]+$/)) {
return
}
variables.add(subscription.variableName)
if (callerId === null || callerId === subscriptionId) {
defaultValues[subscription.variableName] = ''
}
})
let variableDefinitions = []
variables.forEach((variable) => {
variableDefinitions.push({
name: variable,
variableId: variable,
})
})
this.setVariableDefinitions(variableDefinitions)
if (this.config.reset_variables) {
this.setVariableValues(defaultValues)
}
}
maybeReconnect() {
if (this.isInitialized && this.config.reconnect) {
if (this.reconnect_timer) {
clearTimeout(this.reconnect_timer)
}
this.reconnect_timer = setTimeout(() => {
this.initWebSocket()
}, 5000)
}
}
initWebSocket() {
if (this.reconnect_timer) {
clearTimeout(this.reconnect_timer)
this.reconnect_timer = null
}
const url = this.config.url
if (!url || url.match(new RegExp(this.wsRegex)) === null) {
this.updateStatus(InstanceStatus.BadConfig, `WS URL is not defined or invalid`)
return
}
this.updateStatus(InstanceStatus.Connecting)
if (this.ws) {
this.ws.close(1000)
delete this.ws
}
this.ws = new WebSocket(url)
this.ws.on('open', () => {
this.updateStatus(InstanceStatus.Ok)
this.log('debug', `Connection opened`)
if (this.config.reset_variables) {
this.updateVariables()
}
})
this.ws.on('close', (code) => {
this.log('debug', `Connection closed with code ${code}`)
this.updateStatus(InstanceStatus.Disconnected, `Connection closed with code ${code}`)
this.maybeReconnect()
})
this.ws.on('message', this.messageReceivedFromWebSocket.bind(this))
this.ws.on('error', (data) => {
this.log('error', `WebSocket error: ${data}`)
})
}
messageReceivedFromWebSocket(data) {
if (this.config.debug_messages) {
this.log('debug', `Message received: ${data}`)
}
let msgValue = null
try {
msgValue = JSON.parse(data)
} catch (e) {
msgValue = data
}
this.subscriptions.forEach((subscription) => {
if (subscription.variableName === '') {
return
}
if (subscription.subpath === '') {
this.setVariableValues({
[subscription.variableName]: typeof msgValue === 'object' ? JSON.stringify(msgValue) : msgValue,
})
} else if (typeof msgValue === 'object' && objectPath.has(msgValue, subscription.subpath)) {
let value = objectPath.get(msgValue, subscription.subpath)
this.setVariableValues({
[subscription.variableName]: typeof value === 'object' ? JSON.stringify(value) : value,
})
}
})
}
getConfigFields() {
return [
{
type: 'static-text',
id: 'info',
width: 12,
label: 'Information',
value:
"<strong>PLEASE READ THIS!</strong> Generic modules is only for use with custom applications. If you use this module to control a device or software on the market that more than you are using, <strong>PLEASE let us know</strong> about this software, so we can make a proper module for it. If we already support this and you use this to trigger a feature our module doesn't support, please let us know. We want companion to be as easy as possible to use for anyone.",
},
{
type: 'textinput',
id: 'url',
label: 'Target URL',
tooltip: 'The URL of the WebSocket server (ws[s]://domain[:port][/path])',
width: 12,
regex: '/' + this.wsRegex + '/',
},
{
type: 'checkbox',
id: 'reconnect',
label: 'Reconnect',
tooltip: 'Reconnect on WebSocket error (after 5 secs)',
width: 6,
default: true,
},
{
type: 'checkbox',
id: 'append_new_line',
label: 'Append new line',
tooltip: 'Append new line (\\r\\n) to commands',
width: 6,
default: true,
},
{
type: 'checkbox',
id: 'debug_messages',
label: 'Debug messages',
tooltip: 'Log incomming and outcomming messages',
width: 6,
},
{
type: 'checkbox',
id: 'reset_variables',
label: 'Reset variables',
tooltip: 'Reset variables on init and on connect',
width: 6,
default: true,
},
]
}
initFeedbacks() {
this.setFeedbackDefinitions({
websocket_variable: {
type: 'advanced',
name: 'Update variable with value from WebSocket message',
description:
'Receive messages from the WebSocket and set the value to a variable. Variables can be used on any button.',
options: [
{
type: 'textinput',
label: 'JSON Path (blank if not json)',
id: 'subpath',
default: '',
},
{
type: 'textinput',
label: 'Variable',
id: 'variable',
regex: '/^[-a-zA-Z0-9_]+$/',
default: '',
},
],
callback: () => {
// Nothing to do, as this feeds a variable
return {}
},
subscribe: (feedback) => {
this.subscriptions.set(feedback.id, {
variableName: feedback.options.variable,
subpath: feedback.options.subpath,
})
if (this.isInitialized) {
this.updateVariables(feedback.id)
}
},
unsubscribe: (feedback) => {
this.subscriptions.delete(feedback.id)
},
},
})
}
initActions() {
this.setActionDefinitions({
send_command: {
name: 'Send generic command',
options: [
{
type: 'textinput',
label: 'data',
id: 'data',
default: '',
useVariables: true,
},
],
callback: async (action, context) => {
const value = await context.parseVariablesInString(action.options.data)
if (this.config.debug_messages) {
this.log('debug', `Message sent: ${value}`)
}
this.ws.send(value + (this.config.append_new_line ? '\r\n' : ''))
},
},
})
}
}
runEntrypoint(WebsocketInstance, upgradeScripts)