-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
176 lines (149 loc) · 5.31 KB
/
app.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
const SerialPort = require('serialport');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const bunyan = require('bunyan');
const bformat = require('bunyan-format');
const formatOut = bformat({ outputMode: 'short' });
const log = bunyan.createLogger({ name: 'app', stream: formatOut, level: 'info' });
const Influx = require('influx');
const parser = require('./dsmr-parser');
const config = require('./config');
const utils = require('./utils');
const DSMDatagram = require('./dsmr_datagram');
let packetCnt = 0;
let go = false;
let decryptedData;
let previousHexData;
const startByte = 0xdb;
const headerLength = 14;
let influx = null;
if (config.storeInfluxDB) {
let dbFields = Object.assign({}, {"dsmr_version": Influx.FieldType.STRING }, ...Object.keys(config.influx_database_mapping).map((key) => ({[config.influx_database_mapping[key]]: Influx.FieldType.FLOAT })));
influx = new Influx.InfluxDB({
host: config.influxDBHost,
port: config.influxDBPort,
database: config.influxDBName,
schema: [
{
measurement: config.influxDBMeasurement,
fields: dbFields,
tags: [ 'identifier' ]
}]
});
influx.getDatabaseNames()
.then(names => {
if (!names.includes(config.influxDBMeasurement)) {
return influx.createDatabase(config.influxDBMeasurement);
}
});
}
log.info('Listening to device:', config.device);
const port = new SerialPort(config.device, {
baudRate: 115200,
});
port.on('error', (err) => {
log.error('Serial port: ', err.message);
});
const expectedHeader = [0xdb, 0x08, 0x53, 0x41, 0x47];
function DecryptDatagram(datagram, key, auth_data)
{
iv = datagram.IV();
auth_tag = datagram.GCMTag;
cipher = datagram.CipherText;
const decipher = crypto.createDecipheriv('aes-128-gcm', key, iv);
decipher.setAAD(auth_data);
decipher.setAuthTag(auth_tag);
decryptedData = decipher.update(cipher, 'hex', 'utf8') + decipher.final('utf8');
log.debug(`Decrypted data:`, utils.truncate(decryptedData));
return decryptedData;
}
let key = Buffer.from(config.encryption_key, 'hex');
let auth_data = Buffer.from(config.additional_auth_data, 'hex');
port.on('data', (sdata) => {
packetCnt++;
let idx = -1;
log.debug(`Received data ${packetCnt}:`, `${utils.truncate(sdata.toString('hex'))} / size: ${sdata.length}`);
if (previousHexData != null) {
data = Buffer.concat([previousHexData, sdata]);
} else {
data = sdata;
}
while(data.length > 0) {
let match = false;
idx = idx + 1;
if (data[0] == expectedHeader[0]) {
match = true;
for (let j = 1; j < expectedHeader.length; j++) {
if (data[j] != expectedHeader[j]) {
match = false;
break;
}
}
}
if (data[0] == expectedHeader[0] && !match) {
log.debug(`ignoring data after startbyte found at ${idx}`);
}
if (!match) {
// sequence not as expected - consume byte and go along
data = data.subarray(1);
continue;
}
// we found the start of a datagram - try reading the datagram
let datagram = DSMDatagram.Read(data);
if (datagram == null) {
log.debug('reading datagram failed - waiting for more data...');
// previousHexData = data;
break;
}
log.debug(`Datagram header: ${datagram.HeaderToString()}`);
log.debug(`FrameCounter: ${datagram.FrameCounter.toString('hex')}`)
log.debug(`CipherText: ${utils.truncate(datagram.CipherText.toString('hex'))}`)
log.debug(`GCM Tag: ${datagram.GCMTag.toString('hex')}`)
try {
let decrypted = DecryptDatagram(datagram, key, auth_data);
log.info(`Decrypted data: ${packetCnt} ${decrypted}`);
if (config.storeInfluxDB) {
const telegram = parser.parse(decryptedData, parser.ParseTypes.NumericIDs | parser.ParseTypes.ReadUnits);
let timestamp = telegram.objects.timestamp;
delete telegram.objects.timestamp;
let fields = {};
Object.keys(config.influx_database_mapping).forEach((key) => {
if (Object.keys(telegram.objects).includes(key)) {
fields[config.influx_database_mapping[key]] = telegram.objects[key].value;
}
});
influx.writePoints([{
measurement: config.influxDBMeasurement,
fields: fields,
tags: { identifier: telegram.identifier },
timestamp: timestamp.value //* 1e6
}], {
database: config.influxDBName,
precision: 's',
})
.then(() => log.info("Data saved to DB"))
.catch(error => log.error(error));
}
if (config.storeMongoDB) {
const telegram = new models.Telegram({
hexSize: datagram.MessageLength,
hexData: data.subarray(0, datagram.MessageLength).toString('hex'),
decryptedData: decryptedData,
parsedData: parser.parse(decryptedData),
});
telegram.save()
.then(() => log.info('Telegram saved to database:', telegram._id))
.catch(error => log.error(error));
}
} catch (err) {
log.error(err);
}
// successfully read a datagram - so consume bytes from data
data = data.subarray(datagram.MessageLength);
}
previousHexData = data;
if (previousHexData != null) {
log.trace(`remaining data: ${previousHexData.length} ${previousHexData.toString('hex')}`)
}
});