-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
332 lines (293 loc) · 9.72 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
const PLUGIN_ID = 'signalk-modbus-serial-client';
const PLUGIN_NAME = 'SignalK Modbus RTU/Serial client';
module.exports = function(app) {
var plugin = {};
const Modbus = require("jsmodbus");
var socket = undefined;
var clients = [];
const jexl = require("jexl");
var timers = [];
plugin.id = PLUGIN_ID;
plugin.name = PLUGIN_NAME;
plugin.description = 'Plugin to import data via Modbus RTU/serial';
function getValueDataType(fcCode, configuredDataType) {
if (fcCode == 1 || fcCode == 2) {
return 'uint8'
}
const configuredDataTypeStr = String(configuredDataType)
if (configuredDataTypeStr == 'uint16' || configuredDataTypeStr == 'int16') {
return configuredDataTypeStr
}
app.debug(`Could not detect data type (fcCode: ${fcCode}, configured data type: ${configuredDataTypeStr}) - check your configuration`)
app.setPluginError(`Could not detect data type (fcCode: ${fcCode}, configured data type: ${configuredDataTypeStr}) - check your configuration`)
return null
}
function getValueFromDataBuffer(buffer, dataType) {
switch (String(dataType)) {
case 'uint8':
return buffer.readUInt8(0)
break
case 'uint16':
return buffer.readUInt16BE(0)
break
case 'int16':
return buffer.readInt16BE(0)
break
}
app.debug(`Unknown data type ${dataType} - check your configuration`)
app.setPluginError(`Unknown data type ${dataType} - check your configuration`)
return null
}
function getCalculatedValue(rawValue, jexlExpression) {
// context for jexl, x is the data, other constants can be added here
var context = {
x: rawValue
}
return jexlExpression.evalSync(context);
}
/**
* Send a single update to SignalK.
*/
function handleData(data, mapping, serverID, expression) {
const dataType = getValueDataType(data.response._body._fc, mapping.dataType)
if (dataType == null) {
return
}
const buffer = data.response._body._valuesAsBuffer
const rawValue = getValueFromDataBuffer(buffer, dataType)
const calculatedValue = getCalculatedValue(rawValue, expression)
// denormalized SignalK delta for a single value
var delta = {
values: [{
path: mapping.path,
value: calculatedValue
}],
context: app.getSelfPath('uuid'),
$source: "modbus-serial." + serverID + "." + mapping.register + "." + mapping.operation,
timestamp: new Date().toISOString()
};
var deltas = {
updates: [delta]
};
app.handleMessage(PLUGIN_ID, deltas);
app.setPluginStatus("Receiving data normally")
}
/**
* Logs the error and stops the plugin
*/
function catchError(error) {
app.debug(error);
if (Modbus.errors.isUserRequestError(error)) {
app.setPluginError("Error: " + error.message + ", still trying...")
}
else {
app.setPluginError("Fatal error: " + error.message + ", stopping the plugin")
plugin.stop();
}
}
function getPollModbusPromise(client, operation, register) {
switch (String(operation)) {
case 'fc1':
return client.readCoils(register, 1);
break;
case 'fc2':
return client.readDiscreteInputs(register, 1);
break;
case 'fc3':
return client.readHoldingRegisters(register, 1);
break;
case 'fc4':
return client.readInputRegisters(register, 1);
}
app.debug(`Unknown operation ${operation} for register ${register} - check your configuration`)
app.setPluginError(`Unknown operation ${operation} for register ${register} - check your configuration`)
return null
}
/**
* Ask the server for the contents of a single register.
* calls handleData to send the data to SignalK
*/
function pollModbus(client, mapping, serverID, expression) {
const promise = getPollModbusPromise(client, mapping.operation, mapping.register)
if (promise != null) {
promise.then(data => handleData(data, mapping, serverID, expression))
.catch(catchError);
}
}
/**
* Setup the connection to a server
* and add create all timers to poll the registers
*/
function setupConnection(connection) {
// connect to modbus server.
const Serialport = require('serialport')
socket = new Serialport(connection.connection.devicepath, {
baudRate: connection.connection.baudrate,
Parity: 'none',
stopBits: 1,
dataBits: 8
})
socket.on('open', function () {
app.debug("serial connection " + connection.connection.devicepath + " is open, now creating clients")
connection.servers.forEach(function(server) {
const client = new Modbus.client.RTU(socket, server.serverID, connection.connection.timeout)
server.mappings.forEach(function(mapping) {
timers.push(
setInterval(pollModbus, connection.pollingInterval * 1000,
client, mapping, server.serverID, jexl.compile(mapping.conversion))
)
})
clients.push(client);
});
});
socket.on('error', function (err) {
const message = "an error occured while setting up serial connection " + connection.connection.devicepath + ": " + err;
app.debug(message);
app.setPluginError(message);
});
}
// called when the plugin is started
plugin.start = function(options, restartPlugin) {
app.setPluginStatus("Initializing");
plugin.options = options;
app.debug('Plugin started');
options.connections.forEach(setupConnection);
app.setPluginStatus("Running");
};
// called when the plugin is stopped or encounters an error
plugin.stop = function() {
socket.close();
socket = undefined;
app.debug('Plugin stopped');
timers.forEach(timer => clearInterval(timer));
app.setPluginStatus('Stopped');
};
// The plugin configuration
plugin.schema = {
title: PLUGIN_NAME,
type: 'object',
properties: {
connections: {
type: 'array',
title: 'Servers:',
items: {
type: 'object',
title: 'connection',
properties: {
pollingInterval: {
type: 'number',
title: "Interval (in seconds) to poll device",
default: 20
},
connection: {
type: 'object',
title: "connection information",
properties: {
devicepath: {
type: 'string',
title: "device path",
default: "/dev/ttyUSB0"
},
baudrate: {
type: 'number',
title: "Connection speed (a.k.a. baudrate)",
default: 9600
}
}
},
servers: {
type: 'array',
title: "servers",
items: {
type: 'object',
title: 'Server',
properties: {
serverID: {
type: 'number',
title: "ServerID",
default: 0
},
mappings: {
title: 'map registers to SignalK paths',
type: 'array',
items: {
type: 'object',
title: 'Map register to SignalK path',
properties: {
operation: {
type: 'string',
title: 'operation type',
enum: ['fc1', 'fc2', 'fc3', 'fc4'],
enumNames: [
'read coil (FC1)',
'read discrete input (FC2)',
'read holding register (FC3)',
'read input register (FC4)'
],
default: 'fc3'
},
register: {
type: 'number',
title: 'register',
default: 11
},
dataType: {
type: 'string',
title: 'DataType',
enum: ['uint16', 'int16'],
enumNames: [
'16 bit integer (unsigned, 0..65535)',
'16 bit integer (signed, -32767..32767)'
],
default: 'int16'
},
path: {
type: 'string',
title: "Path to store data",
default: "modbus.test"
},
conversion: {
type: 'string',
title: 'conversion expression',
default: "x"
}
}
}
}
}
}
}
}
}
}
}
};
// set the order for fields and make the arrays unorderable
plugin.uiSchema = {
connections: {
"ui:options": {
orderable: false
},
items: {
"ui:order": ["connection", "pollingInterval", "servers"],
servers: {
"ui:options": {
orderable: false
},
items: {
"ui:order": ["serverID", "mappings"],
mappings: {
"ui:options": {
orderable: false
},
items: {
"ui:order": ["operation", "register", "dataType", "path", "conversion"]
}
}
}
}
}
}
};
return plugin;
};