forked from bitpool/edge-modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-modbus-client.js
744 lines (656 loc) · 24.9 KB
/
bp-modbus-client.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/**
Copyright (c) 2016,2017,2018,2019,2020,2021,2022 Klaus Landsdorf (http://node-red.plus/)
Copyright 2016 - Jason D. Harper, Argonne National Laboratory
Copyright 2015,2016 - Mika Karaila, Valmet Automation Inc.
All rights reserved.
node-red-contrib-modbus
@author <a href="mailto:[email protected]">Klaus Landsdorf</a> (Bianco Royal)
*/
/**
* Modbus connection node.
* @module NodeRedModbusClient
*
* @param RED
*/
module.exports = function (RED) {
'use strict'
// SOURCE-MAP-REQUIRED
const mbBasics = require('./modbus-basics')
const coreModbusClient = require('./core/modbus-client-core')
const coreModbusQueue = require('./core/modbus-queue-core')
const internalDebugLog = require('debug')('contribModbus:config:client')
const _ = require('underscore')
function ModbusClientNode (config) {
RED.nodes.createNode(this, config)
// create an empty modbus client
const ModbusRTU = require('@open-p4nr/modbus-serial')
const unlimitedListeners = 0
const minCommandDelayMilliseconds = 1
const defaultUnitId = 1
const defaultTcpUnitId = 0
const serialConnectionDelayTimeMS = 500
const timeoutTimeMS = 1000
const reconnectTimeMS = 2000
const logHintText = ' Get More About It By Logging'
const serialAsciiResponseStartDelimiter = '0x3A'
this.clienttype = config.clienttype
if (config.parallelUnitIdsAllowed === undefined) {
this.bufferCommands = true
} else {
this.bufferCommands = config.bufferCommands
}
this.bpChkShowDebugWarnings = config.bpChkShowDebugWarnings
this.queueLogEnabled = config.queueLogEnabled
this.stateLogEnabled = config.stateLogEnabled
this.failureLogEnabled = config.failureLogEnabled
this.tcpHost = config.tcpHost
this.tcpPort = parseInt(config.tcpPort) || 502
this.tcpType = config.tcpType
this.serialPort = config.serialPort
this.serialBaudrate = config.serialBaudrate
this.serialDatabits = config.serialDatabits
this.serialStopbits = config.serialStopbits
this.serialParity = config.serialParity
this.serialType = config.serialType
this.serialConnectionDelay = parseInt(config.serialConnectionDelay) || serialConnectionDelayTimeMS
this.serialAsciiResponseStartDelimiter = config.serialAsciiResponseStartDelimiter || serialAsciiResponseStartDelimiter
this.unit_id = parseInt(config.unit_id)
this.commandDelay = parseInt(config.commandDelay) || minCommandDelayMilliseconds
this.clientTimeout = parseInt(config.clientTimeout) || timeoutTimeMS
this.reconnectTimeout = parseInt(config.reconnectTimeout) || reconnectTimeMS
this.reconnectOnTimeout = config.reconnectOnTimeout
if (config.parallelUnitIdsAllowed === undefined) {
this.parallelUnitIdsAllowed = true
} else {
this.parallelUnitIdsAllowed = config.parallelUnitIdsAllowed
}
this.showErrors = config.showErrors
this.showWarnings = config.showWarnings
this.showLogs = config.showLogs
const node = this
node.isFirstInitOfConnection = true
node.closingModbus = false
node.client = null
node.bufferCommandList = new Map()
node.sendingAllowed = new Map()
node.unitSendingAllowed = []
node.messageAllowedStates = coreModbusClient.messageAllowedStates
node.serverInfo = ''
node.stateMachine = null
node.stateService = null
node.stateMachine = coreModbusClient.createStateMachineService()
node.actualServiceState = node.stateMachine.initialState
node.actualServiceStateBefore = node.actualServiceState
node.stateService = coreModbusClient.startStateService(node.stateMachine)
node.reconnectTimeoutId = 0
node.serialSendingAllowed = false
node.internalDebugLog = internalDebugLog
coreModbusQueue.queueSerialLockCommand(node)
node.setDefaultUnitId = function () {
if (this.clienttype === 'tcp') {
node.unit_id = defaultTcpUnitId
} else {
node.unit_id = defaultUnitId
}
}
node.setUnitIdFromPayload = function (msg) {
const unitId = coreModbusClient.getActualUnitId(node, msg)
if (!coreModbusClient.checkUnitId(unitId, node.clienttype)) {
node.setDefaultUnitId()
}
node.client.setID(unitId)
msg.unitId = unitId
}
if (Number.isNaN(node.unit_id) || !coreModbusClient.checkUnitId(node.unit_id, node.clienttype)) {
node.setDefaultUnitId()
}
node.updateServerinfo = function () {
if (node.clienttype === 'tcp') {
node.serverInfo = ' TCP@' + node.tcpHost + ':' + node.tcpPort
} else {
node.serverInfo = ' Serial@' + node.serialPort + ':' + node.serialBaudrate + 'bit/s'
}
node.serverInfo += ' default Unit-Id: ' + node.unit_id
}
function verboseWarn (logMessage) {
if (node.bpChkShowDebugWarnings) {
node.updateServerinfo()
node.warn('Client -> ' + logMessage + ' ' + node.serverInfo)
}
}
node.errorProtocolMsg = function (err, msg) {
if (node.showErrors) {
mbBasics.logMsgError(node, err, msg)
}
}
function verboseLog (logMessage) {
if (node.bpChkShowDebugWarnings) {
coreModbusClient.internalDebug('Client -> ' + logMessage + ' ' + node.serverInfo)
}
}
function stateLog (logMessage) {
// if (node.stateLogEnabled) {
// verboseLog(logMessage)
// }
}
node.queueLog = function (logMessage) {
if (node.bufferCommands && node.queueLogEnabled) {
verboseLog(logMessage)
}
}
node.stateService.subscribe(state => {
node.actualServiceStateBefore = node.actualServiceState
node.actualServiceState = state
stateLog(state.value)
if (!state.value || node.actualServiceState.value === undefined) {
// verboseWarn('fsm ignore invalid state')
return
}
if (node.actualServiceStateBefore.value === node.actualServiceState.value) {
// verboseWarn('fsm ignore equal state ' + node.actualServiceState.value + ' after ' + node.actualServiceStateBefore.value)
return
}
if (state.matches('init')) {
verboseWarn('fsm init state after ' + node.actualServiceStateBefore.value)
node.updateServerinfo()
coreModbusQueue.initQueue(node)
node.reconnectTimeoutId = 0
try {
if (node.isFirstInitOfConnection) {
node.isFirstInitOfConnection = false
verboseWarn('first fsm init in ' + serialConnectionDelayTimeMS + ' ms')
setTimeout(node.connectClient, serialConnectionDelayTimeMS)
} else {
verboseWarn('fsm init in ' + node.reconnectTimeout + ' ms')
setTimeout(node.connectClient, node.reconnectTimeout)
}
} catch (err) {
node.error(err, { payload: 'client connection error ' + logHintText })
}
node.emit('mbinit')
}
if (state.matches('connected')) {
verboseWarn('fsm connected after state ' + node.actualServiceStateBefore.value + logHintText)
coreModbusQueue.queueSerialUnlockCommand(node)
node.emit('mbconnected')
}
if (state.matches('activated')) {
node.emit('mbactive')
if (node.bufferCommands && !coreModbusQueue.checkQueuesAreEmpty(node)) {
node.stateService.send('QUEUE')
}
}
if (state.matches('queueing')) {
if (node.clienttype === 'tcp') {
node.stateService.send('SEND')
} else {
if (node.serialSendingAllowed) {
coreModbusQueue.queueSerialLockCommand(node)
node.stateService.send('SEND')
}
}
}
if (state.matches('sending')) {
setTimeout(() => {
coreModbusQueue.dequeueCommand(node)
}, node.commandDelay)
node.emit('mbqueue')
}
if (state.matches('opened')) {
coreModbusQueue.queueSerialUnlockCommand(node)
node.emit('mbopen')
}
if (state.matches('switch')) {
node.emit('mbswitch')
node.stateService.send('CLOSE')
}
if (state.matches('closed')) {
node.emit('mbclosed')
node.stateService.send('RECONNECT')
}
if (state.matches('stopped')) {
verboseWarn('stopped state without reconnecting')
node.emit('mbclosed')
}
if (state.matches('failed')) {
verboseWarn('fsm failed state after ' + node.actualServiceStateBefore.value + logHintText)
node.emit('mberror', 'Modbus Failure On State ' + node.actualServiceStateBefore.value + logHintText)
node.stateService.send('BREAK')
}
if (state.matches('broken')) {
verboseWarn('fsm broken state after ' + node.actualServiceStateBefore.value + logHintText)
node.emit('mbbroken', 'Modbus Broken On State ' + node.actualServiceStateBefore.value + logHintText)
if (node.reconnectOnTimeout) {
node.stateService.send('RECONNECT')
} else {
node.stateService.send('ACTIVATE')
}
}
if (state.matches('reconnecting')) {
verboseWarn('fsm reconnect state after ' + node.actualServiceStateBefore.value + logHintText)
coreModbusQueue.queueSerialLockCommand(node)
node.emit('mbreconnecting')
if (node.reconnectTimeout <= 0) {
node.reconnectTimeout = reconnectTimeMS
}
setTimeout(() => {
node.reconnectTimeoutId = 0
node.stateService.send('INIT')
}, node.reconnectTimeout)
}
})
node.connectClient = function () {
try {
if (node.client) {
try {
node.client.close(function () {
verboseLog('connection closed')
})
verboseLog('connection close sent')
} catch (err) {
verboseLog(err.message)
}
}
node.client = null
node.client = new ModbusRTU()
node.client.on('error', (err) => {
node.modbusErrorHandling(err)
mbBasics.setNodeStatusTo('error', node)
})
if (!node.clientTimeout) {
node.clientTimeout = timeoutTimeMS
}
if (!node.reconnectTimeout) {
node.reconnectTimeout = reconnectTimeMS
}
if (node.clienttype === 'tcp') {
if (!coreModbusClient.checkUnitId(node.unit_id, node.clienttype)) {
node.error(new Error('wrong unit-id (0..255)'), { payload: node.unit_id })
node.stateService.send('FAILURE')
return false
}
try {
switch (node.tcpType) {
case 'C701':
verboseLog('C701 port UDP bridge')
node.client.connectC701(node.tcpHost, {
port: node.tcpPort,
autoOpen: true
}).then(node.setTCPConnectionOptions)
.then(node.setTCPConnected)
.catch((err) => {
node.modbusTcpErrorHandling(err)
return false
})
break
case 'TELNET':
verboseLog('Telnet port')
node.client.connectTelnet(node.tcpHost, {
port: node.tcpPort,
autoOpen: true
}).then(node.setTCPConnectionOptions)
.catch((err) => {
node.modbusTcpErrorHandling(err)
return false
})
break
case 'TCP-RTU-BUFFERED':
verboseLog('TCP RTU buffered port')
node.client.connectTcpRTUBuffered(node.tcpHost, {
port: node.tcpPort,
autoOpen: true
}).then(node.setTCPConnectionOptions)
.catch((err) => {
node.modbusTcpErrorHandling(err)
return false
})
break
default:
verboseLog('TCP port')
node.client.connectTCP(node.tcpHost, {
port: node.tcpPort,
autoOpen: true
}).then(node.setTCPConnectionOptions)
.catch((err) => {
node.modbusTcpErrorHandling(err)
return false
})
}
} catch (e) {
node.modbusTcpErrorHandling(e)
return false
}
} else {
if (!coreModbusClient.checkUnitId(node.unit_id, node.clienttype)) {
node.error(new Error('wrong unit-id serial (0..247)'), { payload: node.unit_id })
node.stateService.send('FAILURE')
return false
}
if (!node.serialConnectionDelay) {
node.serialConnectionDelay = serialConnectionDelayTimeMS
}
if (!node.serialPort) {
node.error(new Error('wrong serial port'), { payload: node.serialPort })
node.stateService.send('FAILURE')
return false
}
const serialPortOptions = {
baudRate: parseInt(node.serialBaudrate),
dataBits: parseInt(node.serialDatabits),
stopBits: parseInt(node.serialStopbits),
parity: node.serialParity,
autoOpen: false
}
try {
switch (node.serialType) {
case 'ASCII':
verboseLog('ASCII port serial')
// Make sure is parsed when string, otherwise just assign.
if (node.serialAsciiResponseStartDelimiter && typeof node.serialAsciiResponseStartDelimiter === 'string') {
serialPortOptions.startOfSlaveFrameChar = parseInt(node.serialAsciiResponseStartDelimiter, 16)
} else {
serialPortOptions.startOfSlaveFrameChar = node.serialAsciiResponseStartDelimiter
}
verboseLog('Using response delimiter: 0x' + serialPortOptions.startOfSlaveFrameChar.toString(16))
node.client.connectAsciiSerial(node.serialPort, serialPortOptions).then(node.setSerialConnectionOptions)
.catch((err) => {
node.modbusSerialErrorHandling(err)
return false
})
break
case 'RTU':
verboseLog('RTU port serial')
node.client.connectRTU(node.serialPort, serialPortOptions).then(node.setSerialConnectionOptions)
.catch((err) => {
node.modbusSerialErrorHandling(err)
return false
})
break
default:
verboseLog('RTU buffered port serial')
node.client.connectRTUBuffered(node.serialPort, serialPortOptions).then(node.setSerialConnectionOptions)
.catch((err) => {
node.modbusSerialErrorHandling(err)
return false
})
break
}
} catch (e) {
node.modbusSerialErrorHandling(e)
return false
}
}
} catch (err) {
node.modbusErrorHandling(err)
return false
}
return true
}
node.setTCPConnectionOptions = function () {
node.client.setID(node.unit_id)
node.client.setTimeout(node.clientTimeout)
node.stateService.send('CONNECT')
}
node.setTCPConnected = function () {
coreModbusClient.modbusSerialDebug('modbus tcp connected on ' + node.tcpHost)
}
node.setSerialConnectionOptions = function () {
node.stateService.send('OPENSERIAL')
setTimeout(node.openSerialClient, parseInt(node.serialConnectionDelay))
}
node.modbusErrorHandling = function (err) {
coreModbusQueue.queueSerialUnlockCommand(node)
if (err.message) {
coreModbusClient.modbusSerialDebug('modbusErrorHandling:' + err.message)
} else {
coreModbusClient.modbusSerialDebug('modbusErrorHandling:' + JSON.stringify(err))
}
if (err.errno && coreModbusClient.networkErrors.includes(err.errno)) {
node.stateService.send('FAILURE')
}
}
node.modbusTcpErrorHandling = function (err) {
coreModbusQueue.queueSerialUnlockCommand(node)
if (node.showErrors) {
node.error(err)
}
if (node.failureLogEnabled) {
if (err.message) {
coreModbusClient.modbusSerialDebug('modbusTcpErrorHandling:' + err.message)
} else {
coreModbusClient.modbusSerialDebug('modbusTcpErrorHandling:' + JSON.stringify(err))
}
}
if ((err.errno && coreModbusClient.networkErrors.includes(err.errno)) ||
(err.code && coreModbusClient.networkErrors.includes(err.code))) {
node.stateService.send('BREAK')
}
}
node.modbusSerialErrorHandling = function (err) {
coreModbusQueue.queueSerialUnlockCommand(node)
if (node.showErrors) {
node.error(err)
}
if (node.failureLogEnabled) {
if (err.message) {
coreModbusClient.modbusSerialDebug('modbusSerialErrorHandling:' + err.message)
} else {
coreModbusClient.modbusSerialDebug('modbusSerialErrorHandling:' + JSON.stringify(err))
}
}
node.stateService.send('BREAK')
}
node.openSerialClient = function () {
// some delay for windows
if (node.actualServiceState.value === 'opened') {
verboseLog('time to open Unit ' + node.unit_id)
coreModbusClient.modbusSerialDebug('modbus connection opened')
node.client.setID(node.unit_id)
node.client.setTimeout(parseInt(node.clientTimeout))
node.client._port.on('close', node.onModbusClose)
node.stateService.send('CONNECT')
} else {
verboseLog('wrong state on connect serial ' + node.actualServiceState.value)
coreModbusClient.modbusSerialDebug('modbus connection not opened state is %s', node.actualServiceState.value)
node.stateService.send('BREAK')
}
}
node.onModbusClose = function () {
coreModbusQueue.queueSerialUnlockCommand(node)
verboseWarn('Modbus closed port')
coreModbusClient.modbusSerialDebug('modbus closed port')
node.stateService.send('CLOSE')
}
node.on('customModbusMessage', function (msg, cb, cberr) {
// const state = node.actualServiceState
coreModbusClient.customModbusMessage(node, msg, cb, cberr)
})
node.on('readModbus', function (msg, cb, cberr) {
const state = node.actualServiceState
if (node.isInactive()) {
cberr(new Error('Client Not Ready To Read At State ' + state.value), msg)
} else {
if (node.bufferCommands) {
coreModbusQueue.pushToQueueByUnitId(node, coreModbusClient.readModbus, msg, cb, cberr).then(function () {
node.queueLog(JSON.stringify({
info: 'queued read msg',
message: msg.payload,
state: state.value,
queueLength: node.bufferCommandList.get(msg.queueUnitId).length
}))
}).catch(function (err) {
cberr(err, msg)
}).finally(function () {
node.stateService.send('QUEUE')
})
} else {
coreModbusClient.readModbus(node, msg, cb, cberr)
}
}
})
node.on('writeModbus', function (msg, cb, cberr) {
const state = node.actualServiceState
if (node.isInactive()) {
cberr(new Error('Client Not Ready To Write At State ' + state.value), msg)
} else {
if (node.bufferCommands) {
coreModbusQueue.pushToQueueByUnitId(node, coreModbusClient.writeModbus, msg, cb, cberr).then(function () {
node.queueLog(JSON.stringify({
info: 'queued write msg',
message: msg.payload,
state: state.value,
queueLength: node.bufferCommandList.get(msg.queueUnitId).length
}))
}).catch(function (err) {
cberr(err, msg)
}).finally(function () {
node.stateService.send('QUEUE')
})
} else {
coreModbusClient.writeModbus(node, msg, cb, cberr)
}
}
})
node.activateSending = function (msg) {
node.sendingAllowed.set(msg.queueUnitId, true)
coreModbusQueue.queueSerialUnlockCommand(node)
return new Promise(
function (resolve, reject) {
try {
if (node.bufferCommands) {
node.queueLog(JSON.stringify({
info: 'queue response activate sending',
queueLength: node.bufferCommandList.length,
sendingAllowed: node.sendingAllowed.get(msg.queueUnitId),
serialSendingAllowed: node.serialSendingAllowed,
queueUnitId: msg.queueUnitId
}))
if (coreModbusQueue.checkQueuesAreEmpty(node)) {
node.stateService.send('EMPTY')
}
}
resolve()
} catch (err) {
reject(err)
}
})
}
verboseLog('initialized')
node.setMaxListeners(unlimitedListeners)
node.on('reconnect', function () {
node.stateService.send('CLOSE')
})
node.on('dynamicReconnect', function (msg, cb, cberr) {
if (mbBasics.invalidPayloadIn(msg)) {
throw new Error('Message Or Payload Not Valid')
}
coreModbusClient.internalDebug('Dynamic Reconnect Parameters ' + JSON.stringify(msg.payload))
if (coreModbusClient.setNewNodeSettings(node, msg)) {
cb(msg)
} else {
cberr(new Error('Message Or Payload Not Valid'), msg)
}
coreModbusClient.internalDebug('Dynamic Reconnect Starts on actual state ' + node.actualServiceState.value)
node.stateService.send('SWITCH')
})
node.on('close', function (done) {
const nodeIdentifierName = node.name || node.id
node.closingModbus = true
verboseLog('stop fsm on close ' + nodeIdentifierName)
node.stateService.send('STOP')
verboseLog('close node ' + nodeIdentifierName)
node.internalDebugLog('close node ' + nodeIdentifierName)
node.removeAllListeners()
if (node.client) {
if (node.client.isOpen) {
node.client.close(function (err) {
if (err) {
verboseLog('Connection closed with error ' + nodeIdentifierName)
} else {
verboseLog('Connection closed well ' + nodeIdentifierName)
}
done()
})
} else {
verboseLog('connection was closed ' + nodeIdentifierName)
done()
}
} else {
verboseLog('Connection closed simple ' + nodeIdentifierName)
done()
}
})
// handle using as config node
node.registeredNodeList = {}
node.registerForModbus = function (clientUserNodeId) {
node.registeredNodeList[clientUserNodeId] = clientUserNodeId
if (Object.keys(node.registeredNodeList).length === 1) {
node.closingModbus = false
node.stateService.send('NEW')
node.stateService.send('INIT')
}
node.emit('mbregister', clientUserNodeId)
}
node.setStoppedState = function (clientUserNodeId, done) {
node.stateService.send('STOP')
node.emit('mbderegister', clientUserNodeId)
done()
}
node.closeConnectionWithoutRegisteredNodes = function (clientUserNodeId, done) {
if (Object.keys(node.registeredNodeList).length === 0) {
node.closingModbus = true
if (node.client && node.actualServiceState.value !== 'stopped') {
if (node.client.isOpen) {
node.client.close(function () {
node.setStoppedState(clientUserNodeId, done)
})
} else {
node.setStoppedState(clientUserNodeId, done)
}
} else {
node.setStoppedState(clientUserNodeId, done)
}
} else {
node.setStoppedState(clientUserNodeId, done)
}
}
node.deregisterForModbus = function (clientUserNodeId, done) {
try {
delete node.registeredNodeList[clientUserNodeId]
if (node.closingModbus) {
done()
node.emit('mbderegister', clientUserNodeId)
} else {
node.closeConnectionWithoutRegisteredNodes(clientUserNodeId, done)
}
} catch (err) {
verboseWarn(err.message + ' on de-register node ' + clientUserNodeId)
node.error(err)
done()
}
}
node.isInactive = function () {
return _.isUndefined(node.actualServiceState) || node.messageAllowedStates.indexOf(node.actualServiceState.value) === -1
}
node.isActive = function () {
return !node.isInactive()
}
node.isReadyToSend = function (node) {
if (node.actualServiceState.matches('queueing')) {
return true
}
verboseWarn('Client not ready to send')
return false
}
}
RED.nodes.registerType('bp-modbus-client', ModbusClientNode)
RED.httpAdmin.get('/modbus/serial/ports', RED.auth.needsPermission('serial.read'), function (req, res) {
const SerialPort = require('serialport')
SerialPort.SerialPort.list().then(ports => {
res.json(ports)
}).catch(err => {
res.json([err.message])
coreModbusClient.internalDebug(err.message)
})
})
}