-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.js
468 lines (419 loc) · 15.9 KB
/
main.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
/*
# vim: tabstop=4 shiftwidth=4 expandtab
*
* ioBroker wmbus adapter
*
* Copyright (c) 2019 ISFH
* This work is licensed under the terms of the GPL2 license.
* See NOTICE for detailed listing of other contributors
*
* This file contains large portions from the ioBroker mbus adapter
* by Apollon77 which is originally published under the MIT License.
*
* Adapter loading data from an wM-Bus devices
*
*/
'use strict';
const utils = require('@iobroker/adapter-core');
const fs = require('fs');
const WMBusDecoder = require('./lib/wmbus_decoder.js');
const SerialPort = require('serialport');
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: 'wmbus'
});
adapter = new utils.Adapter(options);
adapter.on('ready', main);
adapter.on('message', processMessage);
adapter.on('unload', callback => {
onClose(callback);
});
return adapter;
}
const receiverPath = '/lib/receiver/';
let ReceiverModule;
let receiver = null;
let receiverAvailable = {};
let decoder = null;
let wmBusDevices = {};
let connected = false;
let stateValues = {};
let needsKey = [];
let failedDevices = [];
function setConnected(isConnected) {
if (connected !== isConnected) {
connected = isConnected;
adapter.setState('info.connection', connected, true, err => {
// analyse if the state could be set (because of permissions)
if (err) {
adapter.log.error('Can not update connected state: ' + err);
} else {
adapter.log.debug('connected set to ' + connected);
}
});
}
}
function onClose(callback) {
try {
receiver.port.close();
}
catch (e) { }
finally {
decoder = undefined;
receiver = undefined;
}
callback && callback();
}
function parseID(data) {
function man2ascii(idhex) {
return String.fromCharCode((idhex >> 10) + 64) + String.fromCharCode(((idhex >> 5) & 0x1f) + 64) + String.fromCharCode((idhex & 0x1f) + 64);
}
if (data.length < 8) {
return "ERR-XXXXXXXX";
}
return man2ascii(data.readUInt16LE(2)) + "-" + data.readUInt32LE(4).toString(16).padStart(8,'0');
}
function dataReceived(data) {
setConnected(true);
// id == 'PIK-20104317'
let id = parseID(data.raw_data);
if (data.raw_data.length < 11) {
if (id == "ERR-XXXXXXXX") {
adapter.log.info("Invalid telegram received? " + data.raw_data.toString('hex'));
} else {
adapter.log.debug("Beacon of device: " + id);
}
return;
}
// check blacklist
if ((typeof adapter.config.blacklist !== 'undefined') && adapter.config.blacklist.length) {
let found = adapter.config.blacklist.find(function(item) { if (typeof item.id === 'undefined') return false; else return item.id == this; }, id);
if (typeof found !== 'undefined') { // found
return;
}
}
// look for AES key
let key;
if ((typeof adapter.config.aeskeys !== 'undefined') && adapter.config.aeskeys.length) {
// look for perfect match
let found = adapter.config.aeskeys.find(function(item) { if (typeof item.id === 'undefined') return false; else return item.id == this; }, id);
if (typeof found !== 'undefined') { // found
key = found.key;
} else { // which devices names start with our id
found = adapter.config.aeskeys.filter(function(item) { if (typeof item.id === 'undefined') return false; else return this.startsWith(item.id); }, id);
if (found.length == 1) { // only 1 match - take it
key = found[0].key;
} else if (found.length > 1) { // more than one, find the best
let len = found[0].id.length;
let pos = 0;
for (let i = 1; i < found.length; i++) {
if (found[i].id.length > len) {
len = found[i].id.length;
pos = i;
}
}
key = found[pos].key;
}
}
}
if (typeof key !== 'undefined') {
if (key === "UNKNOWN") {
key = undefined;
} else {
adapter.log.debug("Found AES key: " + key);
}
}
decoder.parse(data.raw_data, data.contains_crc, key, data.frame_type, function(err, result) {
let i = failedDevices.findIndex(function(d) { return d.id == this; }, id);
if (err) {
if (i === -1) {
failedDevices.push({ id: id, count: 1 });
} else {
failedDevices[i].count++;
if (failedDevices[i].count > 10) {
adapter.config.blacklist.push({ id: id });
adapter.log.warn("Device " + id + " was blacklisted until adapter restart!");
return;
}
}
adapter.log.error('Error parsing wMBus device: ' + id);
if (err.code == 9) { // ERR_NO_AESKEY
if (typeof needsKey.find(function (el) { return el == this; }, id) === 'undefined') {
needsKey.push(id);
}
}
adapter.setState('info.rawdata', data.raw_data.toString('hex'), true);
adapter.log.error(err.message);
return;
}
if ((i !== -1) && (failedDevices[i].count)) {
failedDevices[i].count = 0;
}
updateDevice(result.deviceInformation.Manufacturer + '-' + result.deviceInformation.Id, result);
});
}
function updateDevice(deviceId, data) {
adapter.log.debug('Updating device: ' + deviceId);
initializeDeviceObjects(deviceId, data, () => {
updateDeviceStates(wmBusDevices[deviceId], data);
});
}
function initializeDeviceObjects(deviceId, data, callback) {
let neededStates = [];
let units2roles = {
'value.power.consumption': [ 'Wh', 'kWh', 'MWh', 'GWh', 'J', 'kJ', 'MJ', 'GJ' ],
'value.power': [ 'W', 'kW', 'MW', 'J/h', 'GJ/h' ],
'value.temperature': [ '°C', 'K', '°F' ],
'value.volume': [ 'm³', 'feet³' ],
'value.duration': [ 's', 'min', 'h', 'd', 'months', 'years' ],
'value.price': [ '€', '$', 'EUR', 'USD' ],
'value.mass': [ 'kg', 't' ],
'value.flow': [ 'm³/h', 'm³/min', 'm³/s', 'kg/h' ],
'value.pressure': [ 'bar' ],
'value.current': [ 'A' ],
'value.voltage': [ 'V' ]
};
function createStates() {
if (!neededStates.length) {
callback();
return;
}
const state = neededStates.shift();
let name = (typeof state.name !== 'undefined' ? state.name : '');
let role;
if (state.id.includes('TIME_POINT')) {
role = "date";
} else {
role = Object.keys(units2roles).find(function(k) { return units2roles[k].includes(state.unit); }) || 'value';
}
if (adapter.config.forcekWh) {
if (state.unit == "Wh") {
state.unit = "kWh";
} else if (state.unit == "J") {
state.unit = "kWh";
}
}
adapter.setObjectNotExists(deviceNamespace + state.id, {
type: 'state',
common: {
name: (name ? name : state.id),
role: role,
read: true,
write: false,
unit: state.unit
},
native: {
id: state.id,
StorageNumber: state.StorageNumber,
Tariff: state.Tariff,
}
}, (err, obj) => {
if (err) {
adapter.log.error('Error creating State: ' + err);
}
createStates();
});
}
if (typeof wmBusDevices[deviceId] !== 'undefined') {
callback();
return;
}
wmBusDevices[deviceId] = deviceId;
let deviceNamespace = wmBusDevices[deviceId];
adapter.setObjectNotExists(deviceNamespace, {
type: 'device',
common: {name: deviceNamespace},
native: {}
}, (err, obj) => {
if (err) {
adapter.log.error('Error creating State: ' + err);
}
adapter.setObjectNotExists(deviceNamespace + '.info', {
type: 'channel',
common: {name: deviceNamespace + '.info'},
native: {}
}, err => {
if (err) {
adapter.log.error('Error creating State: ' + err);
}
adapter.setObjectNotExists(deviceNamespace + '.data', {
type: 'channel',
common: {name: deviceNamespace + '.data'},
native: {}
}, err => {
if (err) {
adapter.log.error('Error creating State: ' + err);
}
let currentState;
let currentType;
Object.keys(data.deviceInformation).forEach(function (key) {
currentState = {};
currentState.id = '.info.' + key;
currentState.name = key;
neededStates.push(currentState);
});
currentState = {};
currentState.id = '.info.Updated';
currentState.name = 'Updated';
neededStates.push(currentState);
data.dataRecord.forEach(function(item) {
currentState = {};
currentState.id = '.data.' + item.number + '-' + item.storageNo + '-' + item.type;
let name = item.description + ' (';
if (item.tariff) {
name += 'Tariff ' + item.tariff + '; ';
}
name += item.functionFieldText + ')';
currentState.name = name;
currentState.unit = item.unit;
currentState.Tariff = item.tariff;
currentState.StorageNumber = item.storageNo;
neededStates.push(currentState);
});
createStates();
});
});
});
}
function updateDeviceStates(deviceNamespace, data, callback) {
Object.keys(data.deviceInformation).forEach(function (key) {
if ((typeof stateValues[deviceNamespace + '.info.' + key] === 'undefined') || stateValues[deviceNamespace + '.info.' + key] !== data.deviceInformation[key]) {
stateValues[deviceNamespace + '.info.' + key] = data.deviceInformation[key];
adapter.setState(deviceNamespace + '.info.' + key, data.deviceInformation[key], true, err => { if (err) adapter.log.error(err) });
}
});
adapter.setState(deviceNamespace + '.info.Updated', Math.floor(Date.now() / 1000), true, err => { if (err) adapter.log.error(err) });
data.dataRecord.forEach(function(item) {
let stateId = '.data.' + item.number + '-' + item.storageNo + '-' + item.type;
if (adapter.config.alwaysUpdate || (typeof stateValues[deviceNamespace + stateId] === 'undefined') || stateValues[deviceNamespace + stateId] !== item.value) {
stateValues[deviceNamespace + stateId] = item.value;
let val = item.value;
if (adapter.config.forcekWh) {
if (item.unit == "Wh") {
val = val / 1000;
} else if (item.unit == "J") {
val = val / 3600000;
}
}
adapter.log.debug('Value ' + deviceNamespace + stateId + ': ' + val);
adapter.setState(deviceNamespace + stateId, val, true, err => { if (err) adapter.log.error(err) });
}
});
callback && callback();
}
function serialError(err) {
adapter.log.error('Serialport errror: ' + err.message);
setConnected(false);
onClose();
}
function getAllReceivers() {
receiverAvailable = {};
let json = JSON.parse(fs.readFileSync(adapter.adapterDir + receiverPath + 'receiver.json', 'utf8'));
Object.keys(json).forEach(function (item) {
if (fs.existsSync(adapter.adapterDir + receiverPath + item)) {
receiverAvailable[item] = json[item];
}
});
}
function main() {
let objConnection = {
"_id": "info.connection",
"type": "state",
"common": {
"role": "indicator.connected",
"name": "If connected to wM-Bus receiver",
"type": "boolean",
"read": true,
"write": false,
"def": false
},
"native": {}
};
adapter.setObjectNotExists(objConnection._id, objConnection);
let objRaw = {
"_id": "info.rawdata",
"type": "state",
"common": {
"role": "value",
"name": "Telegram raw data if parser failed",
"type": "string",
"read": true,
"write": false,
"def": false
},
"native": {}
};
adapter.setObjectNotExists(objRaw._id, objRaw);
if (typeof adapter.config.aeskeys !== 'undefined') {
adapter.config.aeskeys.forEach(function (item) {
if (item.key === "UNKNOWN") {
needsKey.push(item.id);
}
});
}
getAllReceivers();
setConnected(false);
let port = (typeof adapter.config.serialPort !== 'undefined' ? adapter.config.serialPort : '/dev/ttyWMBUS');
let baud = (typeof adapter.config.serialBaudRate !== 'undefined' ? adapter.config.serialBaudRate : 9600);
let mode = (typeof adapter.config.wmbusMode !== 'undefined' ? adapter.config.wmbusMode : 'T');
try {
if (Object.keys(receiverAvailable).includes(adapter.config.deviceType + '.js')) {
ReceiverModule = require('.' + receiverPath + adapter.config.deviceType + '.js');
receiver = new ReceiverModule(adapter.log.debug);
adapter.log.debug('Created device of type: ' + receiverAvailable[adapter.config.deviceType + '.js'].name);
decoder = new WMBusDecoder({debug: adapter.log.debug, error: adapter.log.error}, adapter.config.drCacheEnabled);
receiver.incomingData = dataReceived;
receiver.init(port, {baudRate: parseInt(baud)}, mode);
receiver.port.on('error', serialError);
} else {
adapter.log.error('No or unknown adapter type selected! ' + adapter.config.deviceType);
}
} catch(e) {
adapter.log.error("Error opening serial port " + port + " with baudrate " + baud);
adapter.log.error(e);
setConnected(false);
return;
//onClose(main);
}
setConnected(true);
}
function processMessage(obj) {
if (!obj) return;
if (obj) {
switch (obj.command) {
case 'listUart':
if (obj.callback) {
if (SerialPort) {
// read all found serial ports
SerialPort.list(function (err, ports) {
adapter.log.info('List of port: ' + JSON.stringify(ports));
adapter.sendTo(obj.from, obj.command, ports, obj.callback);
});
} else {
adapter.log.warn('Module serialport is not available');
adapter.sendTo(obj.from, obj.command, [{comName: 'Not available'}], obj.callback);
}
}
break;
case 'listReceiver':
if (obj.callback) {
adapter.sendTo(obj.from, obj.command, receiverAvailable, obj.callback);
}
break;
case 'needsKey':
if (obj.callback) {
adapter.sendTo(obj.from, obj.command, needsKey, obj.callback);
}
break;
}
}
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}