forked from Apollon77/ioBroker.smartmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartmeter.js
540 lines (500 loc) · 23.9 KB
/
smartmeter.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
/* jshint -W097 */
// jshint strict:true
/*jslint node: true */
/*jslint esversion: 6 */
'use strict';
/**
*
* Smartmeter adapter
*
* Adapter reading smartmeter data and pushing the values into ioBroker
*
*/
const fs = require('fs');
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const SmartmeterObis = require('smartmeter-obis');
let smTransport;
let serialport;
const smValues = {};
let stopInProgress = false;
let connected = null;
let adapter;
let Sentry;
let SentryIntegrations;
function initSentry(callback) {
if (!adapter.ioPack.common || !adapter.ioPack.common.plugins || !adapter.ioPack.common.plugins.sentry) {
return callback && callback();
}
const sentryConfig = adapter.ioPack.common.plugins.sentry;
if (!sentryConfig.dsn) {
adapter.log.warn('Invalid Sentry definition, no dsn provided. Disable error reporting');
return callback && callback();
}
// Require needed tooling
Sentry = require('@sentry/node');
SentryIntegrations = require('@sentry/integrations');
// By installing source map support, we get the original source
// locations in error messages
require('source-map-support').install();
let sentryPathWhitelist = [];
if (sentryConfig.pathWhitelist && Array.isArray(sentryConfig.pathWhitelist)) {
sentryPathWhitelist = sentryConfig.pathWhitelist;
}
if (adapter.pack.name && !sentryPathWhitelist.includes(adapter.pack.name)) {
sentryPathWhitelist.push(adapter.pack.name);
}
let sentryErrorBlacklist = [];
if (sentryConfig.errorBlacklist && Array.isArray(sentryConfig.errorBlacklist)) {
sentryErrorBlacklist = sentryConfig.errorBlacklist;
}
if (!sentryErrorBlacklist.includes('SyntaxError')) {
sentryErrorBlacklist.push('SyntaxError');
}
Sentry.init({
release: adapter.pack.name + '@' + adapter.pack.version,
dsn: sentryConfig.dsn,
integrations: [
new SentryIntegrations.Dedupe()
]
});
Sentry.configureScope(scope => {
scope.setTag('version', adapter.common.installedVersion || adapter.common.version);
if (adapter.common.installedFrom) {
scope.setTag('installedFrom', adapter.common.installedFrom);
}
else {
scope.setTag('installedFrom', adapter.common.installedVersion || adapter.common.version);
}
scope.addEventProcessor(function(event, hint) {
// Try to filter out some events
if (event.exception && event.exception.values && event.exception.values[0]) {
const eventData = event.exception.values[0];
// if error type is one from blacklist we ignore this error
if (eventData.type && sentryErrorBlacklist.includes(eventData.type)) {
return null;
}
if (eventData.stacktrace && eventData.stacktrace.frames && Array.isArray(eventData.stacktrace.frames) && eventData.stacktrace.frames.length) {
// if last exception frame is from an nodejs internal method we ignore this error
if (eventData.stacktrace.frames[eventData.stacktrace.frames.length - 1].filename && (eventData.stacktrace.frames[eventData.stacktrace.frames.length - 1].filename.startsWith('internal/') || eventData.stacktrace.frames[eventData.stacktrace.frames.length - 1].filename.startsWith('Module.'))) {
return null;
}
// Check if any entry is whitelisted from pathWhitelist
const whitelisted = eventData.stacktrace.frames.find(frame => {
if (frame.function && frame.function.startsWith('Module.')) {
return false;
}
if (frame.filename && frame.filename.startsWith('internal/')) {
return false;
}
if (frame.filename && !sentryPathWhitelist.find(path => path && path.length && frame.filename.includes(path))) {
return false;
}
return true;
});
if (!whitelisted) {
return null;
}
}
}
return event;
});
adapter.getForeignObject('system.config', (err, obj) => {
if (obj && obj.common && obj.common.diag) {
adapter.getForeignObject('system.meta.uuid', (err, obj) => {
// create uuid
if (!err && obj) {
Sentry.configureScope(scope => {
scope.setUser({
id: obj.native.uuid
});
});
}
callback && callback();
});
}
else {
callback && callback();
}
});
});
}
function stopIt(logMessage) {
setConnected(false);
adapter.log.error(logMessage);
adapter.extendForeignObject('system.adapter.' + adapter.namespace, {
common: {
enabled: false
}
});
adapter.stop();
}
function setConnected(isConnected) {
if (connected !== isConnected) {
connected = isConnected;
adapter && adapter.setState('info.connection', connected, true, (err) => {
// analyse if the state could be set (because of permissions)
if (err && adapter && adapter.log) adapter.log.error('Can not update connected state: ' + err);
else if (adapter && adapter.log) adapter.log.debug('connected set to ' + connected);
});
}
}
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: 'smartmeter'
});
adapter = new utils.Adapter(options);
adapter.on('ready', () => {
setConnected(false);
try {
serialport = require('serialport');
} catch (err) {
stopIt('Cannot load serialport module. Please use "npm rebuild". Stop adapter.');
return;
}
if (adapter.supportsFeature && adapter.supportsFeature('PLUGINS')) {
main();
}
else {
initSentry(main);
}
});
adapter.on('message', msg => {
processMessage(msg);
});
/*
adapter.on('stateChange', (id, state) => {
adapter.log.debug('stateChange ' + id + ' ' + JSON.stringify(state));
});
*/
adapter.on('unload', callback => {
stopInProgress = true;
setConnected(false);
if (smTransport) {
smTransport.stop(callback);
} else {
callback();
}
});
return adapter;
}
process.on('SIGINT', () => {
setConnected(false);
if (smTransport) smTransport.stop();
});
process.on('uncaughtException', err => {
setConnected(false);
if (adapter && adapter.log) {
adapter.log.warn('Exception: ' + err);
}
if (smTransport) smTransport.stop();
});
function main() {
const smOptions = {};
if (adapter.common.loglevel === 'debug') {
smOptions.debug = 2;
smOptions.logger = adapter.log.debug;
}
else if (adapter.common.loglevel === 'info') {
smOptions.debug = 1;
smOptions.logger = adapter.log.info;
}
else {
smOptions.debug = 0;
smOptions.logger = adapter.log.warn;
}
if (!adapter.config.protocol) {
adapter.log.error('Smartmeter Protocol is undefined, check your configuration!');
return;
}
smOptions.protocol = adapter.config.protocol;
if (!adapter.config.transport) {
adapter.log.error('Smartmeter Transfer is undefined, check your configuration!');
return;
}
smOptions.transport = adapter.config.transport;
smOptions.requestInterval = adapter.config.requestInterval = adapter.config.requestInterval || 300;
if (adapter.config.anotherQueryDelay) smOptions.anotherQueryDelay = adapter.config.anotherQueryDelay;
if (adapter.config.transport.indexOf('Serial') === 0) { // we have a Serial connection
if (!adapter.config.transportSerialPort) {
adapter.log.error('Serial port device is undefined, check your configuration!');
return;
}
smOptions.transportSerialPort = adapter.config.transportSerialPort;
if (adapter.config.transportSerialBaudrate !== null && adapter.config.transportSerialBaudrate !== undefined) {
adapter.config.transportSerialBaudrate = parseInt(adapter.config.transportSerialBaudrate, 10);
if (adapter.config.transportSerialBaudrate < 300) {
adapter.log.error('Serial port baudrate invalid, check your configuration!');
return;
}
smOptions.transportSerialBaudrate = adapter.config.transportSerialBaudrate;
}
if (adapter.config.transportSerialDataBits !== null && adapter.config.transportSerialDataBits !== undefined && adapter.config.transportSerialDataBits !== "") {
adapter.config.transportSerialDataBits = parseInt(adapter.config.transportSerialDataBits, 10);
if ((adapter.config.transportSerialDataBits < 5) || (adapter.config.transportSerialDataBits > 8)) {
adapter.log.error('Serial port data bits ' + adapter.config.transportSerialDataBits + ' invalid, check your configuration!');
return;
}
smOptions.transportSerialDataBits = adapter.config.transportSerialDataBits;
}
if (adapter.config.transportSerialStopBits !== null && adapter.config.transportSerialStopBits !== undefined && adapter.config.transportSerialStopBits !== "") {
adapter.config.transportSerialStopBits = parseInt(adapter.config.transportSerialStopBits, 10);
if ((adapter.config.transportSerialStopBits !== 1) && (adapter.config.transportSerialStopBits !== 2)) {
adapter.log.error('Serial port stopbits ' + adapter.config.transportSerialStopBits + ' invalid, check your configuration!');
return;
}
smOptions.transportSerialStopBits = adapter.config.transportSerialStopBits;
}
if (adapter.config.transportSerialParity !== null && adapter.config.transportSerialParity !== undefined && adapter.config.transportSerialParity !== "") {
if ((adapter.config.transportSerialParity !== "none") && (adapter.config.transportSerialParity !== "even") &&
(adapter.config.transportSerialParity !== "mark") && (adapter.config.transportSerialParity !== "odd") &&
(adapter.config.transportSerialParity !== "space")) {
adapter.log.error('Serial port parity ' + adapter.config.transportSerialParity + ' invalid, check your configuration!');
return;
}
smOptions.transportSerialParity = adapter.config.transportSerialParity;
}
if (adapter.config.transportSerialMessageTimeout !== null && adapter.config.transportSerialMessageTimeout !== undefined) {
adapter.config.transportSerialMessageTimeout = parseInt(adapter.config.transportSerialMessageTimeout, 10)*1000;
if (adapter.config.transportSerialMessageTimeout < 1000) {
adapter.log.error('HTTP Request timeout ' + adapter.config.transportSerialMessageTimeout + ' invalid, check your configuration!');
return;
}
smOptions.transportSerialMessageTimeout = adapter.config.transportSerialMessageTimeout;
}
//adapter.config.transportSerialMaxBufferSize
}
else if (adapter.config.transport === 'HttpRequestTransport') { // we have a Serial connection
if (!adapter.config.transportHttpRequestUrl) {
adapter.log.error('HTTP Request URL is undefined, check your configuration!');
return;
}
smOptions.transportHttpRequestUrl = adapter.config.transportHttpRequestUrl;
if (adapter.config.transportHttpRequestTimeout !== null && adapter.config.transportHttpRequestTimeout !== undefined) {
adapter.config.transportHttpRequestTimeout = parseInt(adapter.config.transportHttpRequestTimeout, 10);
if (adapter.config.transportHttpRequestTimeout < 500) {
adapter.log.error('HTTP Request timeout ' + adapter.config.transportHttpRequestTimeout + ' invalid, check your configuration!');
return;
}
smOptions.transportHttpRequestTimeout = adapter.config.transportHttpRequestTimeout;
}
}
else if (adapter.config.transport === 'LocalFileTransport') { // we have a LocalFile connection
if (!adapter.config.transportLocalFilePath) {
adapter.log.error('HTTP Request URL is undefined, check your configuration!');
return;
}
smOptions.transportLocalFilePath = adapter.config.transportLocalFilePath;
}
else if (adapter.config.transport === 'TCPTransport') { // we have a TCP connection
if (!adapter.config.transportTcpHost) {
adapter.log.error('TCP Host is undefined, check your configuration!');
return;
}
if (!adapter.config.transportTcpPort) {
adapter.log.error('TCP Port is undefined, check your configuration!');
return;
}
smOptions.transportTcpHost = adapter.config.transportTcpHost;
smOptions.transportTcpPort = adapter.config.transportTcpPort;
}
if (adapter.config.protocol === 'D0Protocol') { // we have a Serial connection
if (adapter.config.protocolD0WakeupCharacters !== null && adapter.config.protocolD0WakeupCharacters !== undefined) {
adapter.config.protocolD0WakeupCharacters = parseInt(adapter.config.protocolD0WakeupCharacters, 10);
if (adapter.config.protocolD0WakeupCharacters < 0) {
adapter.log.error('D0 Number of Wakeup NULL characters ' + adapter.config.protocolD0WakeupCharacters + ' invalid, check your configuration!');
return;
}
smOptions.protocolD0WakeupCharacters = adapter.config.protocolD0WakeupCharacters;
}
if (adapter.config.protocolD0DeviceAddress) smOptions.protocolD0DeviceAddress = adapter.config.protocolD0DeviceAddress;
if (adapter.config.protocolD0SignOnMessage) smOptions.protocolD0SignOnMessage = adapter.config.protocolD0SignOnMessage;
if (adapter.config.protocolD0SignOnMessage) smOptions.protocolD0SignOnMessage = adapter.config.protocolD0SignOnMessage;
if (adapter.config.protocolD0BaudrateChangeoverOverwrite !== null && adapter.config.protocolD0BaudrateChangeoverOverwrite !== undefined && adapter.config.protocolD0BaudrateChangeoverOverwrite !== "") {
adapter.config.protocolD0BaudrateChangeoverOverwrite = parseInt(adapter.config.protocolD0BaudrateChangeoverOverwrite, 10);
if (adapter.config.protocolD0BaudrateChangeoverOverwrite < 300) {
adapter.log.error('D0 baudrate changeover overwrite ' + adapter.config.protocolD0BaudrateChangeoverOverwrite + ' invalid, check your configuration!');
return;
}
smOptions.protocolD0BaudrateChangeoverOverwrite = adapter.config.protocolD0BaudrateChangeoverOverwrite;
}
}
if (adapter.config.protocol === 'SmlProtocol') { // we have a Serial connection
smOptions.protocolSmlIgnoreInvalidCRC = adapter.config.protocolSmlIgnoreInvalidCRC = adapter.config.protocolSmlIgnoreInvalidCRC === 'true' || adapter.config.protocolSmlIgnoreInvalidCRC === true;
if (adapter.config.protocolSmlInputEncoding) {
smOptions.protocolSmlInputEncoding = adapter.config.protocolSmlInputEncoding;
}
}
if (adapter.config.obisFallbackMedium !== null && adapter.config.obisFallbackMedium !== undefined) {
adapter.config.obisFallbackMedium = parseInt(adapter.config.obisFallbackMedium, 10);
if (adapter.config.obisFallbackMedium < 0 || adapter.config.obisFallbackMedium > 18 ) {
adapter.log.error('OBIS Fallback medium code ' + adapter.config.obisFallbackMedium + ' invalid, check your configuration!');
return;
}
smOptions.obisFallbackMedium = adapter.config.obisFallbackMedium;
}
adapter.log.debug('SmartmeterObis options: ' + JSON.stringify(smOptions));
smTransport = SmartmeterObis.init(smOptions, storeObisData);
smTransport.process();
}
async function storeObisData(err, obisResult) {
if (stopInProgress) return;
if (err) {
adapter.log.warn(err.message);
adapter.log.debug(err);
setConnected(false);
return;
}
setConnected(true);
let updateCount = 0;
for (const obisId in obisResult) {
if (!obisResult.hasOwnProperty(obisId)) continue;
adapter.log.debug(obisResult[obisId].idToString() + ': ' + SmartmeterObis.ObisNames.resolveObisName(obisResult[obisId], adapter.config.obisNameLanguage).obisName + ' = ' + obisResult[obisId].valueToString());
let i;
let ioChannelId = obisResult[obisId].idToString().replace(/[\]\[*,;'"`<>\\?]/g, '__');
ioChannelId = ioChannelId.replace(/\./g, '_');
if (!smValues[obisId]) {
let ioChannelName = SmartmeterObis.ObisNames.resolveObisName(obisResult[obisId], adapter.config.obisNameLanguage).obisName;
adapter.log.debug('Create Channel ' + ioChannelId + ' with name ' + ioChannelName);
try {
await adapter.setObjectNotExistsAsync(ioChannelId, {
type: 'channel',
common: {
name: ioChannelName
},
native: {}
});
} catch (err) {
adapter.log.error('Error creating Channel: ' + err);
}
if (obisResult[obisId].getRawValue() !== undefined) {
adapter.log.debug('Create State ' + ioChannelId + '.rawvalue');
try {
await adapter.setObjectNotExistsAsync(ioChannelId + '.rawvalue', {
type: 'state',
common: {
name: ioChannelId + '.rawvalue',
type: 'string',
read: true,
role: 'value',
write: false
},
native: {
id: ioChannelId + '.rawvalue'
}
});
} catch (err) {
adapter.log.error('Error creating State: ' + err);
}
}
adapter.log.debug('Create State ' + ioChannelId + '.value');
try {
await adapter.setObjectNotExistsAsync(ioChannelId + '.value', {
type: 'state',
common: {
name: ioChannelId + '.value',
type: (typeof obisResult[obisId].getValue(0).value),
read: true,
unit: obisResult[obisId].getValue(0).unit,
role: 'value',
write: false
},
native: {
id: ioChannelId + '.value'
}
});
} catch (err) {
adapter.log.error('Error creating State: ' + err);
}
if (obisResult[obisId].getValueLength() > 1) {
for (i = 1; i < obisResult[obisId].getValueLength(); i++) {
adapter.log.debug('Create State ' + ioChannelId + '.value' + (i + 1));
try {
await adapter.setObjectNotExistsAsync(ioChannelId + '.value' + (i + 1), {
type: 'state',
common: {
name: ioChannelId + '.value' + (i + 1),
type: (typeof obisResult[obisId].getValue(i).value),
read: true,
unit: obisResult[obisId].getValue(i).unit,
role: 'value',
write: false
},
native: {
id: ioChannelId + '.value' + (i + 1)
}
});
} catch (err) {
adapter.log.error('Error creating State: ' + err);
}
}
}
}
if (!smValues[obisId] || smValues[obisId].valueToString() !== obisResult[obisId].valueToString()) {
if (obisResult[obisId].getRawValue() !== undefined) {
adapter.log.debug('Set State ' + ioChannelId + '.rawvalue = ' + obisResult[obisId].getRawValue());
await adapter.setStateAsync(ioChannelId + '.rawvalue', {ack: true, val: obisResult[obisId].getRawValue()});
}
adapter.log.debug('Set State ' + ioChannelId + '.value = ' + obisResult[obisId].getValue(0).value);
await adapter.setStateAsync(ioChannelId + '.value', {ack: true, val: obisResult[obisId].getValue(0).value});
if (obisResult[obisId].getValueLength() > 1) {
for (i = 1; i < obisResult[obisId].getValueLength(); i++) {
adapter.log.debug('Set State '+ ioChannelId + '.value' + (i + 1) + ' = ' + obisResult[obisId].getValue(i).value);
await adapter.setStateAsync(ioChannelId + '.value' + (i + 1), {ack: true, val: obisResult[obisId].getValue(i).value});
}
}
smValues[obisId] = obisResult[obisId];
updateCount++;
}
else {
adapter.log.debug('Data for '+ ioChannelId + ' unchanged');
}
}
adapter.log.info('Received ' + Object.keys(obisResult).length + ' values, ' + updateCount + ' updated');
}
function processMessage(obj) {
if (!obj) return;
adapter.log.debug('Message received = ' + JSON.stringify(obj));
switch (obj.command) {
case 'listUart':
if (obj.callback) {
if (serialport) {
// read all found serial ports
serialport.list().then(ports => {
adapter.log.info('List of port: ' + JSON.stringify(ports));
if (process.platform !== 'win32') {
ports.forEach(port => {
if (port.pnpId) {
try {
const pathById = '/dev/serial/by-id/' + port.pnpId;
if (fs.existsSync(pathById)) {
port.realPath = port.path;
port.path = pathById;
}
} catch (err) {
adapter.log.debug('pnpId ' + port.pnpId + ' not existing: ' + err);
}
return port;
}
});
}
adapter.sendTo(obj.from, obj.command, ports, obj.callback);
}).catch(err => {
adapter.log.warn('Can not get Serial port list: ' + err);
adapter.sendTo(obj.from, obj.command, [{path: 'Not available'}], obj.callback);
});
} else {
adapter.log.warn('Module serialport is not available');
adapter.sendTo(obj.from, obj.command, [{path: 'Not available'}], 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();
}