-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdsmr-parser.js
278 lines (264 loc) · 8.64 KB
/
dsmr-parser.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
const crc = require('crc');
const utils = require('./utils');
const ParseTypes = Object.freeze({"TextBased": 1, "NumericIDs":2, "ReadUnits": 8 });
// Common patterns.
const patterns = {
TELEGRAM : /^\/...5(.*?)\r\n\r\n((?:.|[\r\n])*?)!([a-f0-9]{4})\r\n$/mi,
CHECKSUM : /^((?:.|[\r\n])+?!)([a-f0-9]{4})\r\n$/mi,
};
const object_patterns = {
'dsmr version' : {
pattern : /^(1-3:0\.2\.8)\(([0-9]+)\)$/m,
transform : (v) => String((v / 10).toFixed(1))
},
timestamp : {
pattern : /^(0-0:1\.0\.0)\(([0-9]{12}[WS])\)$/m,
transform : utils.parseTimestamp
},
'equipment identifier' : {
pattern : /^(0-0:96\.1\.1)\(([0-9a-f]+)\)$/mi,
transform : utils.parseHex
},
'gas equipment identifier' : {
pattern : /^(0-[0-9]+:96\.1\.0)\(([0-9a-f]+)\)$/mi,
transform : utils.parseHex
},
'text message short' : {
pattern : /^(0-0:96\.13\.1)\(([0-9a-f]+)\)$/mi,
transform : utils.parseHex
},
'text message long' : {
pattern : /^(0-0:96\.13\.0)\(([0-9a-f]+)\)$/mi,
transform : utils.parseHex
},
'electricity delivered tariff 1' : {
pattern : /^(1-0:1\.8\.1)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'electricity delivered tariff 2' : {
pattern : /^(1-0:1\.8\.2)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'electricity received tariff 1' : {
pattern : /^(1-0:2\.8\.1)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'electricity received tariff 2' : {
pattern : /^(1-0:2\.8\.2)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'tariff indicator electricity' : {
pattern : /^(0-0:96\.14\.0)\(([0-9]+)/m,
transform : Number
},
'actual electricity power delivered' : {
pattern : /^(1-0:1\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'actual electricity power received' : {
pattern : /^(1-0:2\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'number of power failures in any phase' : {
pattern : /^(0-0:96\.7\.21)\(([0-9.]+)/m,
transform : Number
},
'number of long power failures in any phase' : {
pattern : /^(0-0:96\.7\.9)\(([0-9.]+)/m,
transform : Number
},
'number of voltage sags in L1 phase' : {
pattern : /^(1-0:32\.32\.0)\(([0-9.]+)/m,
transform : Number
},
'number of voltage sags in L2 phase' : {
pattern : /^(1-0:52\.32\.0)\(([0-9.]+)/m,
transform : Number
},
'number of voltage sags in L3 phase' : {
pattern : /^(1-0:72\.32\.0)\(([0-9.]+)/m,
transform : Number
},
'number of voltage swells in L1 phase' : {
pattern : /^(1-0:32\.36\.0)\(([0-9.]+)/m,
transform : Number
},
'number of voltage swells in L2 phase' : {
pattern : /^(1-0:52\.36\.0)\(([0-9.]+)/m,
transform : Number
},
'number of voltage swells in L3 phase' : {
pattern : /^(1-0:72\.36\.0)\(([0-9.]+)/m,
transform : Number
},
'instantaneous voltage L1' : {
pattern : /^(1-0:32\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous voltage L2' : {
pattern : /^(1-0:52\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous voltage L3' : {
pattern : /^(1-0:72\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous current L1' : {
pattern : /^(1-0:31\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous current L2' : {
pattern : /^(1-0:51\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous current L3' : {
pattern : /^(1-0:71\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous active power L1 delivered' : {
pattern : /^(1-0:21\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous active power L2 delivered' : {
pattern : /^(1-0:41\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous active power L3 delivered' : {
pattern : /^(1-0:61\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous active power L1 received' : {
pattern : /^(1-0:22\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous active power L2 received' : {
pattern : /^(1-0:42\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'instantaneous active power L3 received' : {
pattern : /^(1-0:62\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'gas timestamp' : {
pattern : /^(0-[0-9]+:24\.2\.1)\(([0-9.]+[WS])\)/m,
transform : utils.parseTimestamp
},
'gas delivered' : {
pattern : /^(0-[0-9]+:24\.2\.1)\([0-9.]+[WS]\)\(([0-9.]+)/m,
transform : Number
},
'logical device name': {
pattern: /^(0-0:42\.0\.0)\(([0-9.]+)/m,
transform: String
},
'total imported energy register (Q+)': {
pattern: /^(1-0:3\.8\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform: Number
},
'total imported energy register (Q+) tariff 1': {
pattern: /^1-0:3\.8\.1\(([0-9.]+)/m,
transform: Number
},
'total imported energy register (Q+) tariff 2': {
pattern: /^1-0:3\.8\.2\(([0-9.]+)/m,
transform: Number
},
'total exported energy register (Q-)': {
pattern: /^(1-0:4\.8\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform: Number
},
'total exported energy register (Q-) tariff 1': {
pattern: /^1-0:4\.8\.1\(([0-9.]+)/m,
transform: Number
},
'total exported energy register (Q-) tariff 2': {
pattern: /^1-0:4\.8\.2\(([0-9.]+)/m,
transform: Number
},
'instantaneous imported reactive power (Q+)': {
pattern: /^(1-0:3\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform: Number
},
'instantaneous exported reactive power (Q-)': {
pattern: /^(1-0:4\.7\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform: Number
},
'actual theshold electricity in kW': {
pattern: /^(0-0:17\.0\.0)\(([0-9.]+)/m,
transform: Number
},
'switch position electricity' : {
pattern : /^(0-0:96\.3\.10)\(([0-9.]+)/m,
transform : Number
},
'electricity received tariff 0' : {
pattern : /^(1-0:2\.8\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
'electricity delivered tariff 0' : {
pattern : /^(1-0:1\.8\.0)\((([0-9.]+)(\*([a-zA-Z]+))?)/m,
transform : Number
},
};
module.exports = {
parse(telegram, parseMode) {
return this.validate(telegram, parseMode);
},
validate(telegram, parseMode) {
return Telegram(telegram, parseMode);
},
ParseTypes,
};
function Telegram(telegram, parseMode) {
if (! (this instanceof Telegram)) return new Telegram(telegram, parseMode);
this.telegram = telegram;
if (typeof(parseMode) == 'undefined') {
parseMode = ParseTypes.TextBased;
}
this.parse(parseMode);
};
Telegram.prototype.parse = function(parseMode) {
var match = this.telegram.match(patterns.TELEGRAM);
// Validate the overall telegram structure.
if (! match) throw new Error('INVALID_TELEGRAM');
// Validate the checksum.
if (this.checksum() !== match[3]) throw new Error('CHECKSUM_MISMATCH');
// Parse properties and data.
this.identifier = match[1];
this.crc = match[3];
this.objects = this.parseObjects(match[2], parseMode);
};
Telegram.prototype.checksum = function() {
var data = this.telegram.match(patterns.CHECKSUM)[1];
return ('0000' + crc.crc16(data).toString(16)).slice(-4).toUpperCase();
}
Telegram.prototype.parseObjects = function(data, parseMode) {
var objectsByText = {};
var objectsById = {};
data.split('\r\n').forEach(line => {
var processed = false;
// see if there is a known parser
Object.keys(object_patterns).forEach((key) => {
var obj = object_patterns[key];
var match = obj.pattern.exec(line);
if (! match) return;
var valueIdx = match.length > 5 ? 3 : 2;
var parsed = (parseMode & ParseTypes.ReadUnits) == 0
? obj.transform ? obj.transform.apply(obj, match.slice(valueIdx, valueIdx + 1)) : match[valueIdx]
: {
value: obj.transform ? obj.transform.apply(obj, match.slice(valueIdx, valueIdx + 1)) : match[valueIdx],
unit: match.length > 5 ? match[5] : null,
};
if ((parseMode & ParseTypes.TextBased) != 0 || key == 'timestamp') objectsByText[key] = parsed;
if ((parseMode & ParseTypes.NumericIDs) != 0) objectsById[match[1]] = parsed;
processed = true;
});
if (processed) return;
if ((parseMode & ParseTypes.NumericIDs) != 0) {
var matches = /^([0-9]+-[0-9]+:[0-9.]+)\((.+)\)$/m.exec(line);
if (!matches) return;
objectsById[matches[1]] = (parseMode & ParseTypes.ReadUnits) == 0 ? matches[2].split(')(') : { value: matches[2].split(')(') }
}
})
return Object.assign({}, objectsByText, objectsById);;
}