-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
260 lines (254 loc) · 7.26 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
const modbus = require('modbus-stream');
function getData(address, quantity, options) {
return new Promise((resolve, reject) => {
modbus.tcp.connect(options.port, options.ip, {
debug: null,
}, (err, connection) => {
if (err) {
reject(err);
return;
}
connection.once('error', (connErr) => {
reject(connErr);
});
connection.readHoldingRegisters({
address,
quantity,
}, (readErr, res) => {
if (readErr) {
reject(err);
return;
}
resolve(res.response.data);
});
});
});
}
module.exports = function createPlugin(app) {
const plugin = {};
plugin.id = 'signalk-teltonika-rutx11';
plugin.name = 'Teltonika Modem Modbus';
plugin.description = 'Plugin that retrieves status from a Teltonika RUT modem via Modbus';
let timeout = null;
plugin.start = function start(options) {
app.setPluginStatus('Initializing');
plugin.setMeta();
plugin.fetchStatus(options);
};
plugin.setMeta = function setMeta() {
app.handleMessage(plugin.id, {
context: `vessels.${app.selfId}`,
updates: [
{
meta: [
{ path: 'networking.modem.temperature', value: { units: 'K' } },
],
},
],
});
};
plugin.fetchStatus = function fetchStatus(options) {
function sendValues(values) {
app.handleMessage(plugin.id, {
context: `vessels.${app.selfId}`,
updates: [
{
source: {
label: plugin.id,
},
timestamp: (new Date().toISOString()),
values,
},
],
});
}
getData(1, 38, options)
.then((data) => {
if (!data) {
return Promise.resolve();
}
const modemUptime = Buffer.concat(data.slice(0, 2)).readUInt32BE();
const values = [];
values.push({
path: 'networking.modem.uptime',
value: modemUptime,
});
const signalStrength = Buffer.concat(data.slice(2, 5)).readInt32BE();
values.push({
path: 'networking.lte.rssi',
value: signalStrength,
});
const signalBars = Math.min(Math.floor((signalStrength + 100) / 8), 5);
values.push({
path: 'networking.lte.bars',
value: signalBars,
});
const radioQuality = Math.min((signalStrength + 100) / 8, 5) / 5;
values.push({
path: 'networking.lte.radioQuality',
value: radioQuality,
});
const modemTemperature = Buffer.concat(data.slice(4, 7)).readInt32BE() / 10 + 273.15;
values.push({
path: 'networking.modem.temperature',
value: modemTemperature,
});
const operator = Buffer.concat(data.slice(22)).toString().replace(/\0.*$/g, '');
values.push({
path: 'networking.lte.registerNetworkDisplay',
value: operator,
});
app.setPluginStatus(`Connected to ${operator}, signal strength ${signalStrength}dBm`);
sendValues(values);
return getData(119, 16, options);
})
.then((data) => {
if (!data) {
return Promise.resolve();
}
const connectionType = Buffer.concat(data.slice(0, 15)).toString().replace(/\0.*$/g, '');
const values = [];
values.push({
path: 'networking.lte.connectionText',
value: connectionType,
});
sendValues(values);
return getData(87, 16, options);
})
.then((data) => {
if (!data) {
return Promise.resolve();
}
const activeSim = Buffer.concat(data.slice(0, 15)).toString();
switch (activeSim.slice(0, 4)) {
case 'sim2': {
return getData(300, 4, options);
}
default: {
if (options.RUT240) {
return getData(135, 4, options);
}
return getData(185, 4, options);
}
}
})
.then((data) => {
if (!data) {
return Promise.resolve();
}
const rx = Buffer.concat([data[0], data[1]]).readUInt32BE();
const tx = Buffer.concat([data[2], data[3]]).readUInt32BE();
const values = [];
values.push(
{
path: 'networking.lte.usage.tx',
value: tx,
},
{
path: 'networking.lte.usage.rx',
value: rx,
},
);
sendValues(values);
if (options.RUT240) {
return Promise.resolve();
}
if (!options.enable_gps) {
return Promise.resolve();
}
return getData(143, 4, options);
})
.then((data) => {
if (!data) {
return Promise.resolve();
}
const modemLat = options.GPSBE
? Buffer.concat(data.slice(0, 2)).readFloatBE()
: Buffer.concat(data.slice(0, 2)).readFloatLE();
const modemLon = options.GPSBE
? Buffer.concat(data.slice(2, 4)).readFloatBE()
: Buffer.concat(data.slice(2, 4)).readFloatLE();
const values = [];
values.push({
path: 'navigation.position',
value: {
latitude: modemLat,
longitude: modemLon,
},
});
sendValues(values);
return getData(179, 4, options);
})
.then((data) => {
if (!data) {
return Promise.resolve();
}
const modemSpeed = Buffer.concat(data.slice(0, 2)).readInt32BE();
const values = [];
values.push({
path: 'navigation.speedOverGround',
value: modemSpeed,
});
const modemSats = Buffer.concat(data.slice(2, 4)).readUInt32BE();
values.push({
path: 'navigation.gnss.satellites',
value: modemSats,
});
sendValues(values);
return Promise.resolve();
})
.catch((err) => {
if (!err) {
app.setPluginError('Unknown error');
return;
}
app.setPluginError(err.message);
});
timeout = setTimeout(() => {
plugin.fetchStatus(options);
}, options.interval * 1000);
};
plugin.stop = function stop() {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};
plugin.schema = {
type: 'object',
description: 'For Teltonika RUT240, 360, 950, 955, X9, X11, X14 modems',
properties: {
RUT240: {
type: 'boolean',
title: 'Select only in case using RUT240 with older firmware than 7.x',
default: false,
},
enable_gps: {
type: 'boolean',
title: 'Get GPS position from the RUT',
default: true,
},
GPSBE: {
type: 'boolean',
title: 'Select only in case GPS data is big endian (usualy identified by unexpected values for longitude and latitude).',
default: false,
},
ip: {
type: 'string',
default: '192.168.1.1',
title: 'Modem IP address',
},
port: {
type: 'integer',
default: 502,
title: 'Modem Modbus port (note: Modbus must be enabled on the router)',
},
interval: {
type: 'integer',
default: 60,
title: 'How often to fetch the status (in seconds)',
},
},
};
return plugin;
};