-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbp-modbus-reader.js
222 lines (186 loc) · 7.83 KB
/
bp-modbus-reader.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
/*
MIT License Copyright 2021, 2022 - Bitpool Pty Ltd
*/
module.exports = function (RED) {
'use strict'
const mbBasics = require('./modbus-basics')
const mbCore = require('./core/modbus-core')
const mbIOCore = require('./core/modbus-io-core')
const internalDebugLog = require('debug')('contribModbus:flex:getter')
function bpModbusReader(config) {
const node = this;
RED.nodes.createNode(node, config);
this.name = config.name
this.unitid = config.unitid
this.showStatusActivities = config.showStatusActivities
this.showErrors = config.showErrors
this.showWarnings = config.showWarnings
this.connection = null
this.useIOFile = config.useIOFile
this.ioFile = RED.nodes.getNode(config.ioFile)
this.useIOForPayload = config.useIOForPayload
this.logIOActivities = config.logIOActivities
this.emptyMsgOnFail = config.emptyMsgOnFail
this.keepMsgProperties = config.keepMsgProperties
this.internalDebugLog = internalDebugLog
this.verboseLogging = RED.settings.verbose
this.delayOnStart = config.delayOnStart
this.startDelayTime = parseInt(config.startDelayTime) || 10
node.bufferMessageList = new Map()
node.INPUT_TIMEOUT_MILLISECONDS = 1000
node.delayOccured = false
node.inputDelayTimer = null
mbBasics.setNodeStatusTo('waiting', node)
const modbusClient = RED.nodes.getNode(config.server)
if (!modbusClient) {
return
}
modbusClient.registerForModbus(node)
mbBasics.initModbusClientEvents(node, modbusClient)
// Workaround for issues with udev and permissions
const { exec } = require("child_process");
exec("doas sh /usr/local/bin/allowSerial.sh");
node.onModbusReadDone = function (resp, msg) {
if (node.showStatusActivities) {
mbBasics.setNodeStatusTo('reading done', node)
}
node.send(mbIOCore.buildMessageWithIO(node, resp.data, resp, msg))
node.emit('modbusFlexGetterNodeDone')
}
node.errorProtocolMsg = function (err, msg) {
mbBasics.logMsgError(node, err, msg)
mbBasics.sendEmptyMsgOnFail(node, err, msg)
}
//Retry on error logic
//existing
node.onModbusReadError = function (err, msg) {
node.internalDebugLog(err.message)
const origMsg = mbCore.getOriginalMessage(node.bufferMessageList, msg)
node.errorProtocolMsg(err, origMsg)
mbBasics.sendEmptyMsgOnFail(node, err, msg)
mbBasics.setModbusError(node, modbusClient, err, origMsg)
node.emit('modbusFlexGetterNodeError')
}
node.prepareMsg = function (msg) {
if (typeof msg.payload === 'string') {
msg.payload = JSON.parse(msg.payload)
}
msg.payload.fc = parseInt(msg.payload.fc) || 3
msg.payload.unitid = parseInt(msg.payload.unitid)
msg.payload.address = parseInt(msg.payload.address) || 0
msg.payload.quantity = parseInt(msg.payload.quantity) || 1
return msg
}
node.isValidModbusMsg = function (msg) {
let isValid = true
if (!(Number.isInteger(msg.payload.fc) &&
msg.payload.fc >= 1 &&
msg.payload.fc <= 4)) {
node.error('FC Not Valid', msg)
isValid &= false
}
if (isValid &&
!(Number.isInteger(msg.payload.address) &&
msg.payload.address >= 0 &&
msg.payload.address <= 65535)) {
node.error('Address Not Valid', msg)
isValid &= false
}
if (isValid &&
!(Number.isInteger(msg.payload.quantity) &&
msg.payload.quantity >= 1 &&
msg.payload.quantity <= 65535)) {
node.error('Quantity Not Valid', msg)
isValid &= false
}
return isValid
}
node.buildNewMessageObject = function (node, msg) {
const messageId = mbCore.getObjectId()
return {
topic: msg.topic || node.id,
messageId,
payload: {
value: msg.payload.value || msg.value,
unitid: msg.payload.unitid,
fc: msg.payload.fc,
address: msg.payload.address,
quantity: msg.payload.quantity,
emptyMsgOnFail: node.emptyMsgOnFail,
keepMsgProperties: node.keepMsgProperties,
messageId
}
}
}
function verboseWarn (logMessage) {
if (RED.settings.verbose && node.showWarnings) {
node.warn('Flex-Getter -> ' + logMessage)
}
}
node.isReadyForInput = function () {
return (modbusClient.client && modbusClient.isActive() && node.delayOccured)
}
node.isNotReadyForInput = function () {
return !node.isReadyForInput()
}
node.resetInputDelayTimer = function () {
if (node.inputDelayTimer) {
verboseWarn('reset input delay timer node ' + node.id)
clearTimeout(node.inputDelayTimer)
}
node.inputDelayTimer = null
node.delayOccured = false
}
node.initializeInputDelayTimer = function () {
node.resetInputDelayTimer()
if (node.delayOnStart) {
verboseWarn('initialize input delay timer node ' + node.id)
node.inputDelayTimer = setTimeout(() => {
node.delayOccured = true
}, node.INPUT_TIMEOUT_MILLISECONDS * node.startDelayTime)
} else {
node.delayOccured = true
}
}
node.initializeInputDelayTimer()
node.on('input', function (msg) {
if (mbBasics.invalidPayloadIn(msg)) {
verboseWarn('Invalid message on input')
return
}
if (node.isNotReadyForInput()) {
verboseWarn('Inject while node is not ready for input.')
return
}
if (modbusClient.isInactive()) {
verboseWarn('You sent an input to inactive client. Please use initial delay on start or send data more slowly.')
return
}
const origMsgInput = Object.assign({}, msg) // keep it origin
try {
const inputMsg = node.prepareMsg(origMsgInput)
if (node.isValidModbusMsg(inputMsg)) {
const newMsg = node.buildNewMessageObject(node, inputMsg)
node.bufferMessageList.set(newMsg.messageId, mbBasics.buildNewMessage(node.keepMsgProperties, inputMsg, newMsg))
modbusClient.emit('readModbus', newMsg, node.onModbusReadDone, node.onModbusReadError)
}
} catch (err) {
node.errorProtocolMsg(err, origMsgInput)
mbBasics.sendEmptyMsgOnFail(node, err, origMsgInput)
}
if (node.showStatusActivities) {
mbBasics.setNodeStatusTo(modbusClient.actualServiceState, node)
}
})
node.on('close', function (done) {
node.resetInputDelayTimer()
mbBasics.setNodeStatusTo('closed', node)
node.bufferMessageList.clear()
modbusClient.deregisterForModbus(node.id, done)
})
if (!node.showStatusActivities) {
mbBasics.setNodeDefaultStatus(node)
}
}
RED.nodes.registerType("bp-reader", bpModbusReader);
};