forked from bitfocus/companion-module-generic-tcp-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
608 lines (543 loc) · 14.4 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/**
* Class tsp TCP Server for Companion
* Copyright 2020 Information Systems Technology
*/
/* eslint-disable no-useless-escape */
import { combineRgb, Regex, TCPHelper } from '@companion-module/base'
import * as net from 'net'
import { runEntrypoint, InstanceBase, InstanceStatus } from '@companion-module/base'
import { SerialPort } from 'serialport'
import * as CHOICES from './choices.js'
const UpgradeScripts = []
/**
* Returns the passed string expanded to 2-digit hex for each character
* @param {string} data: string to hexify
* @param {string} delim: string to insert between characters
* @since 1.0.0
*/
const toHex = (data, delim = '') => {
return [...data]
.map((hex) => {
return ('0' + Number(hex.charCodeAt(0)).toString(16)).slice(-2)
})
.join(delim)
}
/**
* Companion instance class tsp
* creates a small TCP server for a selected serial port
*
* @extends InstanceBase
* @version 2.1.0
* @since 1.0.0
* @author John A Knight, Jr <[email protected]>
*/
class TSPInstance extends InstanceBase {
/**
* Create a new instance of class ip-serial
* @param {Object} internal - Internal Companion reference
* @version 2.1.0
* @since 1.0.0
*/
constructor(internal) {
super(internal)
// Wait a few seconds so we don't spam log with 'no ports/unconfigured'
// as those processes take a few moments to settle
this.LOG_DELAY = 10000
// module defaults
this.foundPorts = []
this.tSockets = []
this.sPortPath = 'none'
this.isOpen = false
this.IPPort = 32100
this.devMode = process.env.DEVELOPER
}
/**
* Clear all ports and timers
* @since 1.0.1
*/
clearAll() {
if (this.portScan) {
clearInterval(this.portScan)
delete this.portScan
}
if (this.tSockets) {
this.tSockets.forEach((sock) => {
sock.end()
sock.removeAllListeners()
})
delete this.tSockets
}
if (this.tServer) {
if (this.tServer.connections > 0) {
this.tServer.close()
}
this.tServer.removeAllListeners()
delete this.tServer
}
if (this.sPort) {
this.sPort.removeAllListeners()
if (this.sPort.isOpen) {
this.sPort.close()
}
delete this.sPort
}
}
/**
* Cleanup module before being disabled or closed
* @since 1.0.0
*/
async destroy() {
this.clearAll()
clearInterval(this.SERIAL_INTERVAL)
this.updateStatus(InstanceStatus.Disconnected, 'Disabled')
this.log('debug', 'Destroyed')
}
/**
* Initialize the module.
* Called once when the system is ready for the module to start.
*
* @param {Object} config - module configuration details
* @version
* @since 1.0.0
*/
async init(config) {
this.applyConfig(config)
}
/**
* Apply user configuration parameters and start the server.
*
* @param {Object} config - saved user configuration items
* @since 1.0.0
*/
applyConfig(config) {
this.config = config
this.clearAll()
this.isListening = false
this.IPPort = config.iport || 32100
this.sPortPath = config.sport || 'none'
this.tSockets = []
this.isOpen = false
this.startedAt = Date.now()
this.portScan = setInterval(() => this.scanForPorts(), 5000)
this.scanForPorts()
this.init_actions()
this.init_variables()
this.updateVariables()
}
/**
* Called when 'Apply changes' is pressed on the module 'config' tab
* @param {Object} config - updated user configuration items
* @since 2.0.0
*/
async configUpdated(config) {
this.config = config
this.applyConfig(config)
}
/**
* Initialize the serial port and attach for read/write
* @since 1.0.0
*/
init_serial() {
if (this.sPortPath == '' || this.sPortPath === 'none') {
// not configured yet
return
}
let portOptions = {
path: this.sPortPath,
autoOpen: false,
baudRate: parseInt(this.config.baud),
dataBits: parseInt(this.config.bits),
stopBits: parseInt(this.config.stop),
parity: this.config.parity,
}
this.sPort = new SerialPort(portOptions)
this.sPort.on('error', this.doUpdateStatus.bind(this))
this.sPort.on('open', this.init_tcp.bind(this))
this.sPort.on('close', (err) => {
this.doUpdateStatus(err)
if (err.disconnected) {
// close all connections
this.tSockets.forEach((sock) => sock.end())
this.tServer.close()
this.isListening = false
}
})
this.sPort.on('data', (data) => {
// make sure client is connected
if (this.tSockets.length > 0) {
// forward data to the TCP connection (data is a buffer)
this.log('debug', 'COM> ' + toHex(data.toString('latin1') + ' '))
this.tSockets.forEach((sock) => sock.write(data))
}
clearInterval(this.SERIAL_INTERVAL)
})
this.sPort.open()
this.doUpdateStatus()
}
/**
* Update the dynamic variable(s)
* since 1.0.0
*/
updateVariables() {
let addr = 'Not connected'
if (this.tSockets.length > 0) {
addr = this.tSockets.map((s) => s.remoteAddress + ':' + s.remotePort).join('\n')
}
this.setVariableValues({ ip_addr: addr })
}
/**
* Initialize the TCP server (after the serial port is ready)
* @since 1.0.0
*/
init_tcp() {
let tServer = (this.tServer = new net.Server())
tServer.maxConnections = 4
tServer.on('error', (err) => {
this.doUpdateStatus(err)
})
tServer.on('connection', (socket) => {
let cid = socket.remoteAddress + ':' + socket.remotePort
this.tSockets.push(socket)
this.updateVariables()
socket.setKeepAlive(true,60000)
socket.on('err', this.doUpdateStatus.bind(this))
socket.on('close', () => {
this.tSockets.splice(this.tSockets.indexOf(socket), 1)
this.isListening = this.tSockets.length > 0
this.updateVariables()
})
socket.on('data', (data) => {
// forward data to the serial port
this.log('debug', 'TCP: ' + toHex(data.toString('latin1') + ' '))
this.sPort.write(data)
if (this.config.response == true) {
this.SERIAL_INTERVAL = setTimeout(this.sendError.bind(this), this.config.maxresponse)
}
})
})
tServer.listen(this.IPPort)
this.isListening = true
this.doUpdateStatus()
}
/**
* Send an error to all TCP sockets if no response was receieved on the Serial Port
* @since 1.0.7
*/
sendError() {
this.updateStatus(InstanceStatus.Error)
this.log(
'error',
'Error: No response received via Serial connection in the max allotted time of ' + this.config.maxresponse + 'ms'
)
let msg = this.config.errormessage
try {
this.tSockets.forEach((sock) => sock.write(msg))
} catch (error) {
this.log('debug', 'Unable to send error message to sockets: ' + error.toString())
}
clearInterval(this.SERIAL_INTERVAL)
}
/**
* Update companion status and log
* @param {Object} err - optional error message from sPort or tPort
*/
doUpdateStatus(err) {
let s
let l
let m
if (this.isListening) {
l = 'info'
s = InstanceStatus.Ok
m = `Listening on TCP port ${this.IPPort}`
} else if (err) {
l = 'error'
s = InstanceStatus.Error
m = `Error: ${err.message}`
} else if (!this.foundPorts || this.startedAt + this.LOG_DELAY > Date.now()) {
// haven't scanned yet so the rest of the statuses don't apply
s = null
} else if (this.foundPorts.length == 0) {
l = 'error'
s = InstanceStatus.ConnectionFailure
m = 'No serial ports detected'
} else if (this.sPort && this.sPortPath !== 'none') {
l = 'info'
s = InstanceStatus.Connecting
m = `Connecting to ${this.sPortPath}`
} else {
l = 'error'
s = InstanceStatus.BadConfig
m = 'No serial port configured'
}
if (s != null && l + m + s != this.lastStatus) {
this.updateStatus(s, m)
this.log(l, m)
this.lastStatus = l + m + s
}
}
/**
* Periodically scan the system for attached serial ports
* This is the callback attached to portScan interval timer
* @since 1.0.0
*/
scanForPorts() {
let setSerial = false
setSerial = this.foundPorts.length > 0 && !this.sPort
setSerial = setSerial || (this.sPort && !this.sPort.isOpen)
if (!this.sPort || !this.sPort.isOpen) {
this.doUpdateStatus()
this.findPorts()
}
if (setSerial) {
this.init_serial()
}
}
/**
* The actual port scanner function
* @since 1.0.0
*/
findPorts() {
if (this.scanning) {
return
}
this.scanning = true
this.foundPorts = []
SerialPort.list().then(
(ports) => {
ports.forEach((p) => {
if (this.devMode) {
let nb = ''
for (const [k, v] of Object.entries(p)) {
nb += (nb == '' ? '' : ', ') + `${k}: ${v}`
}
this.log('debug', nb)
}
if (p.locationId || p.vendorId || p.pnpId) {
this.foundPorts.push({
path: p.path ? p.path : p.comName,
manufacturer: p.manufacturer ? p.manufacturer : 'Internal',
})
}
})
if (this.foundPorts.length > 0) {
this.foundPorts.unshift({ path: 'none', manufacturer: 'Not configured' })
}
this.doUpdateStatus()
this.scanning = false
},
(err) => {
this.log('debug', 'SerialPort.list: ' + err)
this.scanning = false
}
)
}
/**
* Initialize Actions
* @since 1.0.7
*/
init_actions() {
let actionsArr = {
previousSPort: {
name: 'Select Previous Serial Port in List',
options: [],
callback: async (action, context) => {
try {
let index = this.foundPorts.findIndex((port) => port.path == this.config.sport)
index--
if (index > 0) {
this.log('info', 'Selecting previous Serial port in list: ' + this.foundPorts[index].path)
if (this.sPort) {
//close the serial port if it is already opened
this.log('info', 'First closing already open port: ' + this.config.sport)
this.sPort.removeAllListeners()
if (this.sPort.isOpen) {
this.sPort.close()
}
delete this.sPort
}
this.config.sport = this.foundPorts[index].path
this.applyConfig(this.config)
} else {
this.log('info', 'Cannot select previous Serial port in list: Already on the first port in the list.')
}
} catch (error) {
this.log('debug', 'Error Selecting previous Serial Port in List: ' + error.toString())
}
},
},
nextSPort: {
name: 'Select Next Serial Port in List',
options: [],
callback: async (action, context) => {
try {
let index = this.foundPorts.findIndex((port) => port.path == this.config.sport)
index++
if (index < this.foundPorts.length) {
this.log('info', 'Selecting next Serial port in list: ' + this.foundPorts[index].path)
if (this.sPort) {
//close the serial port if it is already opened
this.log('info', 'First closing already open port: ' + this.config.sport)
this.sPort.removeAllListeners()
if (this.sPort.isOpen) {
this.sPort.close()
}
delete this.sPort
}
this.config.sport = this.foundPorts[index].path
this.applyConfig(this.config)
} else {
this.log('info', 'Cannot select next Serial port in list: Already on the last port in the list.')
}
} catch (error) {
this.log('debug', 'Error Selecting next Serial Port in List: ' + error.toString())
}
},
},
}
this.setActionDefinitions(actionsArr)
}
/**
* Define the dynamic variables for Companion
* @since 1.0.0
*/
init_variables() {
this.setVariableDefinitions([
{
name: 'Remote IP address',
variableId: 'ip_addr',
},
])
}
/**
* Define the items that are user configurable.
* Return them to companion.
* @since 2.0.0
*/
getConfigFields() {
let ports = []
const fields = [
{
type: 'static-text',
id: 'info',
width: 12,
label: 'Information',
value: 'This is a helper module to provide TCP access to a serial port',
},
{
type: 'textinput',
id: 'iport',
label: 'Listen Port',
tooltip: 'Enter the IP Port to listen for TCP connections on this computer',
width: 8,
default: this.IPPort,
regex: Regex.PORT,
},
]
if (this.foundPorts.length == 0) {
fields.push({
type: 'static-text',
id: 'info1',
width: 12,
label: 'Try again',
value:
"No ports detected yet, which may take a few seconds.<br>Select the 'Connections' tab and wait for log entry 'No serial port configured' Then choose 'Edit' to return here. ",
})
} else {
if (this.foundPorts && this.foundPorts.length) {
this.foundPorts.forEach((port) => {
ports.push({ id: port.path, label: `${port.manufacturer} (${port.path})` })
})
let portObj = ports.find((port) => port.id === this.config.sport)
if (!portObj) {
if (this.config.selectfirstfound) {
this.log('info', 'Previously selected port (' + this.config.sport + ') not found.')
if (this.ports.length > 1) {
this.log('info', 'Selecting first found port: ' + ports[1].id)
this.config.sport = ports[1].id
}
}
}
} else {
ports = [{ id: 'none', label: 'No serial ports detected' }]
}
fields.push(
{
type: 'dropdown',
id: 'sport',
label: 'Serial port',
width: 12,
default: ports[0].id,
choices: ports,
},
{
type: 'dropdown',
id: 'baud',
label: 'Baud Rate',
width: 6,
default: CHOICES.BAUD_RATES.slice(0,1).id,
choices: CHOICES.BAUD_RATES,
},
{
type: 'dropdown',
id: 'bits',
label: 'Data Bits',
width: 6,
default: CHOICES.BITS.slice(0,1).id,
choices: CHOICES.BITS,
},
{
type: 'dropdown',
id: 'parity',
label: 'Parity',
width: 6,
default: CHOICES.PARITY.slice(0,1).id,
choices: CHOICES.PARITY,
},
{
type: 'dropdown',
id: 'stop',
label: 'Stop Bits',
width: 6,
default: CHOICES.STOP.slice(0,1).id,
choices: CHOICES.STOP,
}
)
//Select First Port if Previous Port is Not Found
fields.push({
type: 'checkbox',
id: 'selectfirstfound',
label: 'Select First Found Port if Previously Configured Port is Not Found',
default: false,
width: 12,
})
//Response Expected fields
fields.push(
{
type: 'checkbox',
id: 'response',
label: 'Response Expected',
default: false,
width: 3,
},
{
type: 'textinput',
id: 'maxresponse',
label: 'Max Response Time Allowed (in ms)',
default: 1000,
width: 3,
isVisible: (configValues) => configValues.response === true,
},
{
type: 'textinput',
id: 'errormessage',
label: 'Message to emit to TCP Clients if no response received',
default: 'ERR:NORESPONSE',
width: 3,
isVisible: (configValues) => configValues.response === true,
}
)
}
return fields
}
}
runEntrypoint(TSPInstance, UpgradeScripts)