-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLora-Payload-Decoder.html
675 lines (631 loc) · 28.1 KB
/
Lora-Payload-Decoder.html
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
<html>
<head>
<title>EMU Professional II Lora-Payload Decoder</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
#box_all {
margin: auto;
width: 60%;
display: none;
margin-left: auto;
margin-right: auto;
/*background-color: #CCCCCC;*/
}
#box_content {
background-color: #FFF;
margin: auto;
padding: 20px;
padding-top: 10px;
}
#main {
border: 1px solid #CCCCCC;
clear: both;
padding-top: 2em;
/* background-color: #83888c;*/
color: #666;
}
#footer {
font-size: 8pt;
padding-left: 20px;
padding-top: 8px;
padding-right: 20px;
padding-bottom: 20px;
height: 100px;
background-repeat: no-repeat;
background-position: center center;
background-color: #FFF;
}
body {
font-family: 'Raleway';
}
.left {
float: left;
}
.right {
float: right;
}
.error {
border-color: red;
}
#lin {
font-size: 8pt;
text-align: right;
}
h1 {
text-align: left;
font-size: 18pt;
f font-weight: bold;
margin-bottom: 2px;
}
h2 {
text-align: left;
font-size: 14pt;
f font-weight: bold;
margin-bottom: 2px;
}
#header {
position: relative;
width: 800px;
height: 3em;
}
#header ul#primary {
margin: 0;
padding: 0;
position: absolute;
bottom: -1px;
width: 45em;
cursor: pointer;
}
#header ul#primary li {
display: inline;
list-style: none;
}
#header ul#primary a,
#header ul#primary span,
#header ul#primary a.current {
width: 8em;
display: block;
float: left;
padding: 4px 0;
margin: 1px 2px 0 0;
text-align: center;
font-size: 85%;
text-decoration: none;
color: #FFFFFF;
}
#header ul#primary span,
#header ul#primary a.current,
#header ul#primary a.current:hover {
border: 1px solid #CCCCCC;
border-bottom: none;
background-color: #0069b4;
background-repeat: no-repeat;
padding-bottom: 6px;
margin-top: 0;
}
#header ul#primary a {
background-color: #b6b9be;
background-repeat: no-repeat;
border: 1px solid #AAA;
border-bottom: none;
}
#header ul#primary a:hover {
margin-top: 0;
border-color: #666;
background-color: #0069b4;
padding-bottom: 5px;
}
#header ul#secondary {
color: #cccccc;
position: absolute;
margin: 0;
padding: 0;
bottom: -1.4em;
left: 1px;
width: 50em;
}
#header ul#secondary li a,
#header ul#secondary li span {
width: auto;
display: block;
float: left;
padding: 0 10px;
margin: 0;
border: none;
border-right: 2px #AAA;
background: none;
font-size: 85%;
color: #FFFFFF;
}
#header ul#secondary li a {
color: #CCCCCC;
text-decoration: none;
}
#header ul#secondary li a:hover {
color: #FFFFFF;
background-color: #0069b4;
background-repeat: no-repeat;
padding: 0 10px;
border: none;
border-right: 1px dotted #AAA;
}
#header ul#secondary li a:active {
color: #000;
background-color: #0069b4;
background-repeat: no-repeat;
}
table {
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
width: 750px;
text-align: left;
border-collapse: collapse;
background-color: #0069b4;
background-repeat: no-repeat;
}
th {
font-size: 13px;
font-weight: normal;
padding: 8px;
color: #FFF;
background-color: #0069b4;
background-repeat: no-repeat;
border-top-style: none;
border-bottom-style: none;
border-top-color: #aabcfe;
border-bottom-color: #fff;
}
td {
padding: 8px;
color: #333;
background-color: #b6b9be;
border-top-style: none;
border-bottom-style: none;
border-bottom-color: #fff;
background-size: 100% 100%;
}
td.title {
color: #fff;
font-weight: bold;
background-color: #676;
}
tr:hover td {
color: #FFF;
background-color: #83888c;
background-repeat: no-repeat;
}
form fieldset {
border-color: #000;
border-width: 1px;
border-style: solid;
padding: 10px;
margin: 0;
}
.metervalue {
float: left;
width: 120px;
text-align: right;
}
input,
textarea {
margin-bottom: 5px;
width: 185px;
}
input[type='checkbox'] {
width: 15px;
}
textarea {
width: 250px;
height: 150px;
}
.boxes {
background-image: none;
width: 1em;
}
.boxbottom {
float: left;
padding: 5px;
}
.boxbottom-left {
float: left;
padding: 5px;
width: 20em;
}
.boxergebnis {
display: inline-flex;
min-width: 50%;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="right">
<img alt="EMULogo" width="150px" height="100px" src="https://www.emuag.ch/profII/EMU.svg" />
</div>
<h1>EMU Professional II Lora-Payload Decoder</h1>
<form onsubmit="doWork(); return false;">
<textarea id="data" type="text" style="width: 90%; "></textarea>
<input type="submit">
<div></div>
<pre id="output" style="width: 90%;"></pre>
</form>
</body>
<script lang="javascript">
function doWork() {
//mHRPZwCqkAcAAZh0T2cDAAAAAAQAAAAAGg==
//98744f6700aa9007000198744f67030000000004000000001a
let input = document.getElementById('data').value;
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
var bytes = [];
//check if its base64
if (base64regex.test(input)) {
const binaryString = atob(input);
const length = binaryString.length;
//const bytes = new Uint8Array(length);
for (let i = 0; i < length; i++) {
bytes.push(binaryString.charCodeAt(i));
}
}
else {
for (var bytes = [], c = 0; c < input.length; c += 2) {
bytes.push(parseInt(input.substr(c, 2), 16));
}
}
erg = decodeUplink(bytes);
document.getElementById('output').innerHTML = JSON.stringify(erg, null, 4);
}
function decodeUplink(data) {
//uplink with only 2 bytes is only status update, ignore it
if (data.length <= 2) {
return {
data: 'not enough data to process'
};
}
var obj = {};
//check CRC-8 which resides at the end
let crc8Received = data[data.length - 1];
dataToCheck = [];
for (var i = 0; i < data.length - 1; i++) {
dataToCheck.push(data[i]);
}
if (crc8_encode(dataToCheck).toString(16) === crc8Received.toString(16)) {
//crc-8 seems ok,
} else {
obj.warnings = ['crc-8 wrong'];
//perhaps decide to stop further processing if crc-8 is wrong
}
//first 4 bytes are allways the timestamp, this is the timestamp from the datalogger
var timeStamp = getUint32(data);
obj.data = parsePayload(data);
//for TTN we strip unused information
for (var property in obj.data) {
delete obj.data[property].cfgdescription;
delete obj.data[property].cfgtariff;
delete obj.data[property].cfgunit;
delete obj.data[property].cfgorder;
delete obj.data[property].order;
}
obj.data.timeStamp = timeStamp;
const options = {minute:'numeric', hour:'numeric', second:'numeric', day: 'numeric', year: 'numeric', month: 'numeric', day: 'numeric', timeZoneName: 'long' };
obj.TimeStampReadable = new Date(timeStamp * 1000).toLocaleString( navigator.language, options);
obj.data.medium = {
"type": 1,
"desc": "Electricity"
};
return obj;
}
function crc8_encode(data) {
var xorOut = 0x0000;
var table = [
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B,
0x12, 0x15, 0x38, 0x3F, 0x36, 0x31,
0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77,
0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
0x48, 0x4F, 0x46, 0x41, 0x54, 0x53,
0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9,
0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF,
0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B,
0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1,
0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0,
0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4,
0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE,
0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88,
0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C,
0x35, 0x32, 0x1F, 0x18, 0x11, 0x16,
0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50,
0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
0x6F, 0x68, 0x61, 0x66, 0x73, 0x74,
0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80,
0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6,
0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2,
0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8,
0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E,
0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A,
0x43, 0x44, 0x19, 0x1E, 0x17, 0x10,
0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26,
0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
0x4E, 0x49, 0x40, 0x47, 0x52, 0x55,
0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F,
0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39,
0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D,
0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7,
0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91,
0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5,
0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF,
0xFA, 0xFD, 0xF4, 0xF3
];
var crc = 0x0000;
for (var j = 0; j < data.length; j++) {
crc = table[crc ^ data[j]];
}
return (crc ^ xorOut) & 0xFFFF;
}
/**
* read 1 byte of data an convert it to an Uint8
* @param {*} data
* @returns
*/
function getUint8(data) {
var value = data >>> 0;
return value;
}
function flip(n) {
var x = [];
n = Number(n);
//will work only for positive numbers
var single = n.toString(2).split("");
for (var i = 0; i < single.length; i++) {
x.push(single[i] == 1 ? 0 : 1);
}
var tmp = x.join("");
var y = (parseInt(tmp, 2) + 1) * -1;
return y;
}
/**
* read 1 byte of data an convert it to an Int8
* @param {*} data
* @returns
*/
function getInt8(data) {
if (data === 0) { return 0; }
if (data >> 7 == 1) {
return flip(data);
}
var value = data >>> 0;
return value;
}
/**
* read 2 bytes of data an convert it to an Int16
* @param {*} data
* @returns
*/
function getInt16(data) {
value = (data[1] << 8 | data[0]);
return value;
}
/**
* read 2 bytes of data an convert it to an Uint16
* @param {*} data
* @returns
*/
function getUint16(data) {
value = (data[1] << 8 | data[0]) >>> 0;
return value;
}
/**
* read 4 bytes of data an convert it to an Int32
* @param {*} data
* @returns
*/
function getInt32(data) {
value = (data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]);
return value;
}
/**
* * read 4 bytes of data an convert it to an Uint32
* @param {*} data
* @returns
*/
function getUint32(data) {
value = (data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]) >>> 0;
return value;
}
/**
* read 8 bytes of data an convert it to an Int64
* @param {*} data
* @returns
*/
function getInt64(data) {
//JS can't handle bitwise operation with more than 32bit !
//so this won't work
//if Chirpstack will use another javascript engine we could use typearray's
var value = Number((data[7] << 56 | data[6] << 48 | data[5] << 40 | data[4] << 32 | data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]));
return value;
}
/**
* * read 8 bytes of data an convert it to an Uint32
* @param {*} data
* @returns
*/
function getUint64(data) {
//JS can't handle bitwise operation with more than 32bit !
//so this won't work
//if Chirpstack will use another javascript engine we could use typearray's
value = Number((data[7] << 56 | data[6] << 48 | data[5] << 40 | data[4] << 32 | data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]) >>> 0);
return value;
}
function getBCD(data) {
var bcd = "";
for (var i = 0; i < data.length; i++) {
bcd = bcd + "" + data[i];
}
return bcd;
}
function getASCII(data) {
var ascii = "";
for (var i = 0; i < data.length; i++) {
entry = getUint8(data[i]);
if (entry != 0x00) {
ascii = ascii + String.fromCharCode(entry.toString());
}
}
return ascii;
}
function parsePayload(data) {
var dataTypes = [];
//be sure to fill the complete array
//if we receive an invalid datatype we skip the rest of the data
for (i = 0; i < 256; i++) {
dataTypes[i] = { 'len': 255, 'description': 'invalid data-type' };
}
//the "order" is assigned according to the entry
dataTypes[0x00] = { 'len': 4, 'description': 'data-logger-index', 'dataType': 'Uint32' };
dataTypes[0x01] = { 'len': 4, 'description': 'timestamp', 'dataType': 'Uint32', 'unit': 'seconds' };
dataTypes[0x02] = { 'len': 4, 'description': 'timestamp-previous', 'dataType': 'Uint32', 'unit': 'seconds' };
dataTypes[0x03] = { 'len': 4, 'description': 'Active Energy Import T1', 'dataType': 'Uint32', 'unit': 'Wh', 'cfgdescription': 3, "cfgunit": 1, 'cfgtariff': 1 };
dataTypes[0x04] = { 'len': 4, 'description': 'Active Energy Import T2', 'dataType': 'Uint32', 'unit': 'Wh', 'cfgdescription': 3, "cfgunit": 1, 'cfgtariff': 2 };
dataTypes[0x05] = { 'len': 4, 'description': 'Active Energy Export T1', 'dataType': 'Uint32', 'unit': 'Wh', 'cfgdescription': 5, "cfgunit": 1, 'cfgtariff': 1 };
dataTypes[0x06] = { 'len': 4, 'description': 'Active Energy Export T2', 'dataType': 'Uint32', 'unit': 'Wh', 'cfgdescription': 5, "cfgunit": 1, 'cfgtariff': 2 };
dataTypes[0x07] = { 'len': 4, 'description': 'Reactive Energy Import T1', 'dataType': 'Uint32', 'unit': 'varh', 'cfgdescription': 10, "cfgunit": 5, 'cfgtariff': 1 };
dataTypes[0x08] = { 'len': 4, 'description': 'Reactive Energy Import T2', 'dataType': 'Uint32', 'unit': 'varh', 'cfgdescription': 10, "cfgunit": 5, 'cfgtariff': 2 };
dataTypes[0x09] = { 'len': 4, 'description': 'Reactive Energy Export T1', 'dataType': 'Uint32', 'unit': 'varh', 'cfgdescription': 13, "cfgunit": 5, 'cfgtariff': 1 };
dataTypes[0x0A] = { 'len': 4, 'description': 'Reactive Energy Export T2', 'dataType': 'Uint32', 'unit': 'varh', 'cfgdescription': 13, "cfgunit": 5, 'cfgtariff': 2 };
dataTypes[0x0B] = { 'len': 4, 'description': 'Active Power L123', 'dataType': 'Int32', 'unit': 'W', 'cfgdescription': 25, "cfgunit": 13 };
dataTypes[0x0C] = { 'len': 4, 'description': 'Active Power L1', 'dataType': 'Int32', 'unit': 'W', 'cfgdescription': 25, "cfgunit": 13, 'cfgphase': 1 };
dataTypes[0x0D] = { 'len': 4, 'description': 'Active Power L2', 'dataType': 'Int32', 'unit': 'W', 'cfgdescription': 25, "cfgunit": 13, 'cfgphase': 2 };
dataTypes[0x0E] = { 'len': 4, 'description': 'Active Power L3', 'dataType': 'Int32', 'unit': 'W', 'cfgdescription': 25, "cfgunit": 13, 'cfgphase': 3 };
dataTypes[0x0F] = { 'len': 4, 'description': 'Current L123', 'dataType': 'Int32', 'unit': 'mA', 'cfgdescription': 31, "cfgunit": 28 };
dataTypes[0x10] = { 'len': 4, 'description': 'Current L1', 'dataType': 'Int32', 'unit': 'mA', 'cfgdescription': 31, "cfgunit": 28, 'cfgphase': 1 };
dataTypes[0x11] = { 'len': 4, 'description': 'Current L2', 'dataType': 'Int32', 'unit': 'mA', 'cfgdescription': 31, "cfgunit": 28, 'cfgphase': 2 };
dataTypes[0x12] = { 'len': 4, 'description': 'Current L3', 'dataType': 'Int32', 'unit': 'mA', 'cfgdescription': 31, "cfgunit": 28, 'cfgphase': 3 };
dataTypes[0x13] = { 'len': 4, 'description': 'Current N', 'dataType': 'Int32', 'unit': 'mA', 'cfgdescription': 31, "cfgunit": 28, 'cfgphase': 4 };
dataTypes[0x14] = { 'len': 4, 'description': 'Voltage L1-N', 'dataType': 'Int32', 'unit': 'V/10', 'unit_calculated': 'V', 'factor': 0.1, 'fixed': 1, 'cfgdescription': 30, "cfgunit": 26, 'cfgphase': 1 };
dataTypes[0x15] = { 'len': 4, 'description': 'Voltage L2-N', 'dataType': 'Int32', 'unit': 'V/10', 'unit_calculated': 'V', 'factor': 0.1, 'fixed': 1, 'cfgdescription': 30, "cfgunit": 26, 'cfgphase': 2 };
dataTypes[0x16] = { 'len': 4, 'description': 'Voltage L3-N', 'dataType': 'Int32', 'unit': 'V/10', 'unit_calculated': 'V', 'factor': 0.1, 'fixed': 1, 'cfgdescription': 30, "cfgunit": 26, 'cfgphase': 3 };
dataTypes[0x17] = { 'len': 1, 'description': 'Powerfactor L1', 'dataType': 'Int8', 'unit': 'Cos', 'factor': 0.01, 'fixed': 2, 'cfgdescription': 32, "cfgunit": 31, 'cfgphase': 1 };
dataTypes[0x18] = { 'len': 1, 'description': 'Powerfactor L2', 'dataType': 'Int8', 'unit': 'Cos', 'factor': 0.01, 'fixed': 2, 'cfgdescription': 32, "cfgunit": 31, 'cfgphase': 2 };
dataTypes[0x19] = { 'len': 1, 'description': 'Powerfactor L3', 'dataType': 'Int8', 'unit': 'Cos', 'factor': 0.01, 'fixed': 2, 'cfgdescription': 32, "cfgunit": 31, 'cfgphase': 3 };
dataTypes[0x1A] = { 'len': 2, 'description': 'Frequency', 'dataType': 'Int16', 'unit': 'Hz', 'factor': 0.1, 'fixed': 1, 'cfgdescription': 33, "cfgunit": 32 };
dataTypes[0x1B] = { 'len': 4, 'description': 'Active Power average', 'dataType': 'Int32', 'unit': 'W', "cfgunit": 13, };
dataTypes[0x1C] = { 'len': 4, 'description': 'Active Energy Import T1 kWh', 'dataType': 'Uint32', 'unit': 'kWh', 'cfgdescription': 3, "cfgunit": 2, 'cfgtariff': 1 };
dataTypes[0x1D] = { 'len': 4, 'description': 'Active Energy Import T2 kWh', 'dataType': 'Uint32', 'unit': 'kWh', 'cfgdescription': 3, "cfgunit": 2, 'cfgtariff': 2 };
dataTypes[0x1E] = { 'len': 4, 'description': 'Active Energy Export T1 kWh', 'dataType': 'Uint32', 'unit': 'kWh', 'cfgdescription': 5, "cfgunit": 2, 'cfgtariff': 1 };
dataTypes[0x1F] = { 'len': 4, 'description': 'Active Energy Export T2 kWh', 'dataType': 'Uint32', 'unit': 'kWh', 'cfgdescription': 5, "cfgunit": 2, 'cfgtariff': 2 };
dataTypes[0x20] = { 'len': 4, 'description': 'Reactive Energy Import T1 kvarh', 'dataType': 'Uint32', 'unit': 'kvarh', 'cfgdescription': 10, "cfgunit": 6, 'cfgtariff': 1 };
dataTypes[0x21] = { 'len': 4, 'description': 'Reactive Energy Import T2 kvarh', 'dataType': 'Uint32', 'unit': 'kvarh', 'cfgdescription': 10, "cfgunit": 6, 'cfgtariff': 2 };
dataTypes[0x22] = { 'len': 4, 'description': 'Reactive Energy Export T1 kvarh', 'dataType': 'Uint32', 'unit': 'kvarh', 'cfgdescription': 13, "cfgunit": 6, 'cfgtariff': 1 };
dataTypes[0x23] = { 'len': 4, 'description': 'Reactive Energy Export T2 kvarh', 'dataType': 'Uint32', 'unit': 'kvarh', 'cfgdescription': 13, "cfgunit": 6, 'cfgtariff': 2 };
dataTypes[0x24] = { 'len': 8, 'description': 'Active Energy Import T1 64bit', 'dataType': 'uInt64', 'unit': 'Wh', 'cfgdescription': 3, "cfgunit": 1, 'cfgtariff': 1 };
dataTypes[0x25] = { 'len': 8, 'description': 'Active Energy Import T2 64bit', 'dataType': 'uInt64', 'unit': 'Wh', 'cfgdescription': 3, "cfgunit": 1, 'cfgtariff': 2 };
dataTypes[0x26] = { 'len': 8, 'description': 'Active Energy Export T1 64bit', 'dataType': 'uInt64', 'unit': 'Wh', 'cfgdescription': 5, "cfgunit": 1, 'cfgtariff': 1 };
dataTypes[0x27] = { 'len': 8, 'description': 'Active Energy Export T2 64bit', 'dataType': 'uInt64', 'unit': 'Wh', 'cfgdescription': 5, "cfgunit": 1, 'cfgtariff': 2 };
dataTypes[0x28] = { 'len': 8, 'description': 'Reactive Energy Import T1 64bit', 'dataType': 'uInt64', 'unit': 'varh', 'cfgdescription': 10, "cfgunit": 5, 'cfgtariff': 1 };
dataTypes[0x29] = { 'len': 8, 'description': 'Reactive Energy Import T2 64bit', 'dataType': 'uInt64', 'unit': 'varh', 'cfgdescription': 10, "cfgunit": 5, 'cfgtariff': 2 };
dataTypes[0x2A] = { 'len': 8, 'description': 'Reactive Energy Export T1 64bit', 'dataType': 'uInt64', 'unit': 'varh', 'cfgdescription': 13, "cfgunit": 5, 'cfgtariff': 1 };
dataTypes[0x2B] = { 'len': 8, 'description': 'Reactive Energy Export T2 64bit', 'dataType': 'uInt64', 'unit': 'varh', 'cfgdescription': 13, "cfgunit": 5, 'cfgtariff': 2 };
dataTypes[0xF0] = { 'len': 1, 'description': 'errorcode', 'dataType': 'ErrorCode' };
dataTypes[0xF1] = { 'len': 4, 'description': 'serial-number', 'dataType': 'MeterSerial' };
dataTypes[0xF2] = { 'len': 4, 'description': 'factor-number', 'dataType': 'MeterSerial' };
dataTypes[0xF3] = { 'len': 2, 'description': 'current-transformer primary', 'dataType': 'Uint16', "cfgunit": 72, };
dataTypes[0xF4] = { 'len': 2, 'description': 'current-transformer secondary', 'dataType': 'Uint16', "cfgunit": 72, };
dataTypes[0xF5] = { 'len': 2, 'description': 'voltage-transformer primary', 'dataType': 'Uint16', "cfgunit": 72, };
dataTypes[0xF6] = { 'len': 2, 'description': 'voltage-transformer secondary', 'dataType': 'Uint16', "cfgunit": 72, };
dataTypes[0xF7] = { 'len': 1, 'description': 'meter-typ', 'dataType': 'Uint8' };
dataTypes[0xF8] = { 'len': 4, 'description': 'MID year', 'dataType': 'BCD', };
dataTypes[0xF9] = { 'len': 4, 'description': 'factory year', 'dataType': 'BCD', };
dataTypes[0xFA] = { 'len': 4, 'description': 'firmware version', 'dataType': 'ASCII' };
dataTypes[0xFB] = { 'len': 4, 'description': 'mid-Version', 'dataType': 'ASCII' };
dataTypes[0xFC] = { 'len': 4, 'description': 'manufacturer', 'dataType': 'ASCII' };
dataTypes[0xFD] = { 'len': 4, 'description': 'hw-index', 'dataType': 'ASCII' };
dataTypes[0xFE] = { 'len': 4, 'description': 'systemtime', 'dataType': 'Uint32' };
var obj = {};
var i = 4; //the first 4 bytes is allways the timestamp
//the last byte is the crc-code so ignore this one
while (i < (data.length - 1)) {
//extract signature byte
indexOfDataType = data[i];
dataType = dataTypes[indexOfDataType];
i++;
//also save the sort-order value
dataType.order = indexOfDataType;
switch (dataType.dataType) {
case 'Int8':
dataType.value = Number(getInt8([data[i++]]));
break;
case 'Uint8':
dataType.value = Number(getUint8([data[i++]]));
break;
case 'Int16':
dataType.value = Number(getInt16([data[i++], data[i++]]));
break;
case 'Uint16':
dataType.value = Number(getUint16([data[i++], data[i++]]));
break;
case 'Uint32':
dataType.value = Number(getUint32([data[i++], data[i++], data[i++], data[i++]]));
break;
case 'Int32':
dataType.value = Number(getInt32([data[i++], data[i++], data[i++], data[i++]]));
break;
case 'uInt64':
dataType.value = Number(getUint64([data[i++], data[i++], data[i++], data[i++], data[i++], data[i++], data[i++], data[i++]]));
break;
case 'Int64':
dataType.value = Number(getInt64([data[i++], data[i++], data[i++], data[i++], data[i++], data[i++], data[i++], data[i++]]));
break;
case 'MeterSerial':
dataType.value = ('0' + Number(getUint8([data[i++]])).toString(16)).slice(-2);
dataType.value = ('0' + Number(getUint8([data[i++]])).toString(16)).slice(-2) + dataType.value;
dataType.value = ('0' + Number(getUint8([data[i++]])).toString(16)).slice(-2) + dataType.value;
dataType.value = ('0' + Number(getUint8([data[i++]])).toString(16)).slice(-2) + dataType.value;
break;
case 'BCD':
dataType.value = getBCD([data[i++], data[i++], data[i++], data[i++]]);
break;
case 'ASCII':
dataType.value = getASCII([data[i++], data[i++], data[i++], data[i++]]);
break;
case 'ErrorCode':
dataType.value = Number(getUint8([data[i++]]));
//also encode the error
dataType.TimeChanged = dataType.value & 0x01 ? true : false;
dataType.CTRatioChange = dataType.value & 0x02 ? true : false;
dataType.VTRatioChange = dataType.value & 0x04 ? true : false;
dataType.ImpulseWidthChange = dataType.value & 0x08 ? true : false;
dataType.ImpulseRatioChange = dataType.value & 0x10 ? true : false;
dataType.PowerFail = dataType.value & 0x20 ? true : false;
dataType.LogbookFull = dataType.value & 0x80 ? true : false;
break;
default:
break;
}
//if we have a factor apply it but keep the old value
if (dataType.factor && !isNaN(dataType.factor)) {
var fixed = 0;
if (dataType.fixed && !isNaN(dataType.fixed)) {
fixed = dataType.fixed;
}
//save the value which was sent by the meter (perhaps needed later)
dataType.value_raw = dataType.value;
//calculate the new value using the factor
dataType.value = Number((dataType.value * dataType.factor).toFixed(fixed));
}
obj[dataType.description] = dataType;
//remove all unused infos like dataType, description, len
delete dataType.len;
delete dataType.description;
delete dataType.dataType;
delete dataType.factor;
delete dataType.fixed;
}
return obj;
}
</script>
</html>