-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTE_TtnDecoder.js
More file actions
1460 lines (1228 loc) · 53.1 KB
/
TE_TtnDecoder.js
File metadata and controls
1460 lines (1228 loc) · 53.1 KB
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Function reserved for TTN use ONLY
*
* @param {*} input
* @returns
*/
// eslint-disable-next-line
function decodeUplink(input) {
// input has the following structure:
// {
// "bytes": [1, 2, 3], // FRMPayload (byte array)
// "fPort": 1
// }
// Should RETURN :
//
// data: { bytes: input.bytes , },
// warnings: ["warning 1", "warning 2"], // optional
// errors: ["error 1", "error 2"], // optional (if set, the decoding failed)
// };
return te_decoder(input.bytes, input.fPort)
}
// Safe console wrapper (works in TTN and locally). TTN does not export console in its runtime
const debug = (typeof console !== "undefined" && console.log)
? console.log.bind(console)
: function () { };
/**
*
* @param {Uint8Array} bytes
* @param {number} port
* @returns Decoded object
*/
function te_decoder(bytes, port) {
var ttn_output = { data: {}, errors: [] }
var decode = ttn_output.data
var error_dict = ttn_output.errors
decode.size = bytes.length
port = parseInt(port)
if (DecodeFwRevision(decode, port, bytes) === false)
if (Decode8911EX(decode, port, bytes) === false)
if (Decode8931EX(decode, port, bytes) === false)
if (DecodeU8900(decode, port, bytes) === false)
if (DecodeU8900Pof(decode, port, bytes) === false)
if (DecodeSinglePointOrMultiPoint(decode, port, bytes, error_dict) === false)
if (DecodeProtocolV2(decode, port, bytes, error_dict) === false)
if (DecodeTiltSensor(decode, port, bytes) === false)
if (DecodeKeepAlive(decode, port, bytes) === false) {
decode.val = 'Unknown frame';
decode.port = port;
decode.bytes = arrayToString(bytes);
}
return ttn_output;
}
function Decode8931EX(decode, port, bytes) {
if (port === 5) {
var BW_MODE_RESOLUTION = {
0x00: 0.125,
0x01: 0.25,
0x02: 0.5,
0x03: 1,
0x04: 2,
0x05: 3,
0x06: 4,
0x07: 5,
0x08: 6,
0x09: 7,
0x0A: 8,
0x0B: 9,
0x0C: 10,
0x0D: 11,
0x0E: 12,
0x0F: 13,
}
decode.bat = (bytes[1] & 0x0F) === 0xF ? 'err' : (((bytes[1] & 0x0F) * 10) + '%');
decode.devstat = {};
decode.devstat.rotEn = (bitfield(bytes[1], 4) === 1) ? 'enabled' : 'disabled';
decode.devstat.temp = (bitfield(bytes[1], 6) === 0) ? 'ok' : 'err';
decode.devstat.acc = (bitfield(bytes[1], 7) === 0) ? 'ok' : 'err';
decode.presetId = bytes[0];
decode.temp = bytes[2] * 0.5 - 40 + '°C';
decode.fftInfo = {};
decode.fftInfo.BwMode = bytes[3] & 0x0F;
decode.axisInfo = {};
decode.axisInfo.Axis = String.fromCharCode(88 + (bytes[4] >> 6));
decode.axisInfo.PeakNb = bytes[4] & 0x3F;
decode.axisInfo.SigRms = dBDecompression(bytes[5]);
decode.peaksList = [];
var peakVal = 0;
var bitCount = 0;
for (var i = 0; i < decode.axisInfo.PeakNb * 19; i++) {
peakVal |= ((bytes[6 + Math.trunc((i / 8))] >> (8 - 1 - (i % 8))) & 0x01) << (19 - bitCount - 1);
bitCount++;
if (bitCount === 19) {
var peak = {};
peak.Freq = peakVal >> 8;
peak.Freq_Hz = peak.Freq * BW_MODE_RESOLUTION[decode.fftInfo.BwMode]
peak.Mag = dBDecompression(peakVal & 0xFF);
decode.peaksList.push(peak);
bitCount = 0;
peakVal = 0;
}
}
return true;
}
else if (port === 133 || port === 197) {
// 133 start fragment, 197 end fragment
decode.val = '8931 : Fragmented frame NOT SUPPORTED by TTN Live Decoder';
decode.port = port;
decode.bytes = arrayToString(bytes);
return true;
}
else {
return false;
}
}
function Decode8911EX(decode, port, bytes) {
if (port === 1) {
if (bytes.length >= 1) {
decode.bat = bytes[0] + '%';
}
if (bytes.length >= 2) {
decode.peak_nb = bytes[1];
}
if (bytes.length >= 4) {
decode.temp = arrayConverter(bytes, 2, 2);
decode.temp = decode.temp === 0x7FFF ? 'err' : round(((decode.temp / 10.0) - 100), 1);
}
if (bytes.length >= 6) {
decode.sig_rms = round(arrayConverter(bytes, 4, 2) / 1000.0, 3);
}
if (bytes.length >= 7) {
decode.preset = bytes[6];
}
if (bytes.length >= 8) {
decode.devstat = {};
decode.devstat.acc = (bitfield(bytes[7], 5) === 0) ? 'ok' : 'err';
decode.devstat.temp = (bitfield(bytes[7], 6) === 0) ? 'ok' : 'err';
decode.devstat.rotEn = (bitfield(bytes[7], 7) === 1) ? 'enabled' : 'disabled';
decode.devstat.com = (bitfield(bytes[7], 3) === 0) ? 'ok' : 'err';
decode.devstat.battery = (bitfield(bytes[7], 0) === 0) ? 'ok' : 'err';
}
decode.peaks = [];
var peaks_start_index = 8;
var bytes_per_peak = 5;
for (var i = 0; i < decode.peak_nb; i++) {
var peak_start = peaks_start_index + (i * bytes_per_peak);
var peak_end = peak_start + bytes_per_peak - 1;
// Check if we have enough bytes for this complete peak
if (peak_end >= bytes.length) {
break;
}
var peak = {};
peak.freq = arrayConverter(bytes, peak_start, 2);
peak.mag = round(arrayConverter(bytes, peak_start + 2, 2) / 1000.0, 3);
peak.ratio = bytes[peak_start + 4];
decode.peaks.push(peak);
}
return true;
}
else if (port === 129 || port === 193) {
// 129 start fragment, 193 end fragment
decode.val = '8911 : Fragmented frame NOT SUPPORTED by TTN Live Decoder';
decode.port = port;
decode.bytes = arrayToString(bytes);
return true;
}
return false;
}
function DecodeFwRevision(decode, port, bytes) {
if (port === 2) {
decode.firmware_version = arrayToAscii(bytes);
return true;
}
return false;
}
function DecodeU8900(decode, port, bytes) {
if (port === 4) {
decode.bat = (bytes[1] & 0x0F) === 0xF ? 'err' : (((bytes[1] & 0x0F) * 10) + '%');
if (bytes[0] === 0x00) {
decode.devstat = 'normal';
}
else {
decode.devstat = {};
decode.devstat.Meas = (bitfield(bytes[0], 7) === 0) ? 'ok' : 'err';
decode.devstat.Cal = (bitfield(bytes[0], 6) === 0) ? 'ok' : 'err';
decode.devstat.Unk = (bitfield(bytes[0], 5) === 0) ? 'ok' : 'err';
decode.devstat.Unsup = (bitfield(bytes[0], 4) === 0) ? 'ok' : 'err';
}
decode.temp = isNaN(arrayToFloat(bytes, 2)) ? 'err' : round(arrayToFloat(bytes, 2), 1) + '°C';
decode.pres = isNaN(arrayToFloat(bytes, 6)) ? 'err' : round(arrayToFloat(bytes, 6), 3) + 'Bar';
return true;
}
return false;
}
function DecodeU8900Pof(decode, port, bytes) {
if (port === 104) {
// MCU Flags
decode.pream = bytes[0] === 0x3C ? 'OK' : 'KO !!!';
decode.rst_cnt = arrayConverter(bytes, 1, 2, true);
decode.pof_tx = (bytes[3] & 0x01) === 0x01 ? 'MCU POF !!!' : 'OK';
decode.pof_idle = (bitfield(bytes[4], 0) === 0) ? 'OK' : 'MCU POF !!!';
decode.pof_snsmeas = (bitfield(bytes[4], 1) === 0) ? 'OK' : 'MCU POF !!!';
decode.pof_batmeas = (bitfield(bytes[4], 2) === 0) ? 'OK' : 'MCU POF !!!';
decode.batt = arrayConverter(bytes, 5, 2, true) + 'mV';
if (bytes[7] === 0x00) {
decode.devstat = 'ok';
}
else {
decode.devstat = {};
decode.devstat.Meas = (bitfield(bytes[7], 7) === 0) ? 'OK' : 'err';
decode.devstat.Cal = (bitfield(bytes[7], 6) === 0) ? 'OK' : 'err';
decode.devstat.Unk = (bitfield(bytes[7], 5) === 0) ? 'OK' : 'err';
decode.devstat.Unsup = (bitfield(bytes[7], 4) === 0) ? 'OK' : 'err';
}
decode.batt_lvl = (bytes[8] & 0x0F) === 0xF ? 'ERROR' : (((bytes[8] & 0x0F) * 10) + '%');
decode.patbatt = bytes[9] === 0xA5 ? 'OK' : 'Corrupted';
decode.pattemp = bytes[9] === 0xA5 ? 'OK' : 'Corrupted';
decode.mcu_temp = arrayConverter(bytes, 10, 2, true, true) / 100.0 + '°C';
decode.pres = isNaN(arrayToFloat(bytes, 13)) ? 'ERROR' : round(arrayToFloat(bytes, 13), 3) + ' Bar';
decode.patend = bytes[17] === 0x5A ? 'OK' : 'KO !!! ';
var i = 0;
decode.zdata = [];
for (i = 0; i < bytes.length; i++) {
decode.zdata.push(bytes[i].toString(16));
}
return true;
}
return false;
}
//port 10 EyEALQhjCgw/gHNj 1321002d08630a0c3f807363 data
//port 20 AComZGU4ZWFiMTdhZDVm 00 2a 26 64 65 38 65 61 62 31 37 61 64 35 66 fw rev
//port 30 /EyEARwhj keep alive 132100470863
function DecodeOperationResponses(decode, port, bytes) {
var OperationRepsType = {
0: "Read",
1: "Write",
2: "Write+Read"
}
var OperationFlag = {
7: "UuidErr",
6: "OpErr/PayloadErr",
5: "ReadOnly/WrongOp",
4: "NetwErr",
}
decode.op = OperationRepsType[bytes[0] & 0x3];
decode.opFlag = [];
for (var i = 7; i > 4; i--) {
if (bitfield(bytes[0], i) === 1) {
decode.opFlag.push(OperationFlag[i]);
}
}
var uuid = arrayToUint16(bytes, 1, false)
decode.uuid = uuid.toString(16);
var payload = bytes.slice(3)
switch (uuid) {
case 0x2A24:
decode.model = arrayToAscii(payload);
break;
case 0x2A25:
decode.sn = arrayToAscii(payload);
break;
case 0x2A26:
decode.fwrev = arrayToAscii(payload);
break;
case 0x2A27:
decode.hwrev = arrayToAscii(payload);
break;
case 0x2A29:
decode.manuf = arrayToAscii(payload);
break;
// case 0xFC01: // Device Status
// getDevstat(payload);
// break;
case 0xCF01: // Sensor Diagnosis
var SensorDiagFlag = {
7: "TEMP16_OoR_ERR",
4: "TEMP16_CIR_ERR",
3: "SENSOR32_OoR_ERR",
0: "SENSOR32_CIR_ERR",
}
decode.sensor_diagnosis = extract_bitfield(payload[0], SensorDiagFlag)
break;
case 0xCF02: // Comm Diagnosis
var CommDiagFlag = {
2: "LoRa_Network_Join_Error",
1: "LoRa_Power_Error",
0: "LoRa_Regional_Restriction",
}
decode.comm_diagnosis = extract_bitfield(payload[0], CommDiagFlag)
break;
case 0xCF03: // Battery Diagnosis
var BatteryDiagFlag = {
2: "Battery_Warning",
1: "Battery_Low",
0: "Battery_Change",
}
decode.comm_diagnosis = extract_bitfield(payload[0], BatteryDiagFlag)
break;
case 0xB301: // Meas Counter
decode.meas_counter = arrayToInt16(payload, 0, false)
break;
case 0xB302: // Meas interval
decode.measInt = payload[0].toString() + 'h ' + payload[1].toString() + ' min' + payload[2].toString() + ' sec';
break;
case 0x2A19: // Battery
decode.batt = payload[0];
break;
case 0xCE01: // Keepalive
var KeepAliveInterval = {
0: "24h",
1: "12h",
2: "8h",
3: "4h",
4: "2h"
}
var KeepAliveMode = {
0: "AnyTime",
1: "IfSilent",
2: "Disable"
}
decode.kaCfg = {};
decode.kaCfg.mode = KeepAliveMode[(payload[0] >> 3) & 0x3];
decode.kaCfg.interval = KeepAliveInterval[payload[0] & 0x7];
break;
case 0xB201: // Threshold
var ThsSrc = {
0: "MainRaw",
1: "MainDelta",
2: "SecondaryRaw",
3: "SecondaryDelta",
0xFF: "Error"
}
var ThsSel = {
0: "Config",
1: "Level",
2: "MeasInterval",
3: "ComMode",
0xFF: "Error"
}
decode.ThsCfg = {};
decode.ThsCfg.Src = ThsSrc[payload[0]];
decode.ThsCfg.Sel = ThsSel[payload[1]];
switch (decode.ThsCfg.Sel) {
case "Config":
decode.ThsCfg.cfg = {};
decode.ThsCfg.cfg.eventFlag = bitfield(payload[2], 7);
decode.ThsCfg.cfg.enable = bitfield(payload[2], 6);
decode.ThsCfg.cfg.condition = (bitfield(payload[2], 5) === 1) ? 'above (data>level)' : 'below (data<level)';
decode.ThsCfg.cfg.autoclr = bitfield(payload[2], 4);
decode.ThsCfg.cfg.actionMeasIntEn = bitfield(payload[2], 3)
decode.ThsCfg.cfg.actionAdvModeEn = bitfield(payload[2], 2)
decode.ThsCfg.cfg.actionUplModeEn = bitfield(payload[2], 1)
break;
case "Level":
decode.ThsCfg.lvl = {};
if (payload.length - 2 >= 4) {
decode.ThsCfg.lvl.valf32 = arrayToFloat(payload, 2, false);
decode.ThsCfg.lvl.vali32 = arrayToInt32(payload, 2, false) / 100.0;
decode.ThsCfg.lvl.vali16 = arrayToInt16(payload, 4, false) / 100.0;
}
else {
decode.ThsCfg.lvl.err = "wrong size";
}
break;
case "MeasInterval":
decode.ThsCfg.measInt = payload[2].toString() + 'h ' + payload[3].toString() + ' min' + payload[4].toString() + ' sec';
break;
case "ComMode":
var ThsComBleMode = {
0: "Periodic",
1: "On Measure",
2: "ADV Silent"
}
var ThsComLoraMode = {
0: "On Measurement",
1: "Silent",
2: "Merge"
}
decode.ThsCfg.ComMode = {};
decode.ThsCfg.ComMode.ble = ThsComBleMode[payload[2] & 0x03];
decode.ThsCfg.ComMode.lora = ThsComLoraMode[payload[3] & 0x03];
break;
default:
break;
}
break;
case 0xDB01: // Datalog
{
var DataLogDataType = {
0: "Temperature",
1: "MainData",
2: "Temperature+MainData"
}
var DataLogDataSize = {
0: 2,
1: 4,
2: 6,
}
decode.Datalog = {};
decode.Datalog.type = DataLogDataType[payload[0]];
decode.Datalog.index = arrayToUint16(payload, 1, false);
decode.Datalog.length = payload[3];
let dataSize = DataLogDataSize[payload[0]];
decode.Datalog.data = [];
// eslint-disable-next-line
for (var i = 0; i < decode.Datalog.length && payload.length > (dataSize * (i + 1) + 4); i++) {
var dataN = {};
dataN.index = decode.Datalog.index + i;
switch (decode.Datalog.type) {
case "Temperature":
dataN.temp = arrayToUint16(payload, dataSize * (i) + 4, false) / 100.0;
break;
case "MainData":
dataN.maini32 = arrayToInt32(payload, dataSize * (i) + 4, false) / 100.0;
dataN.mainf32 = arrayToFloat(payload, dataSize * (i) + 4, false);
break;
case "Temperature+MainData":
dataN.temp = arrayToUint16(payload, dataSize * (i) + 4, false) / 100.0;
dataN.maini32 = arrayToInt32(payload, dataSize * (i) + 4 + 2, false) / 100.0;
dataN.mainf32 = arrayToFloat(payload, dataSize * (i) + 4 + 2, false);
break;
default: break;
}
decode.Datalog.data.push(dataN);
}
break;
}
case 0xDA03: // Vibration Raw Data
{
var Axis = {
1: "Z",
2: "Y",
4: "X"
}
decode.RawData = {};
decode.RawData.axis = Axis[payload[0]];
decode.RawData.index = arrayToUint16(payload, 1, false);
decode.RawData.length = payload[3];
let dataSize = 2 // Each value is two bytes
decode.RawData.data = [];
for (let i = 0; i < decode.RawData.length; i++) {
let data_sample = {};
data_sample.index = decode.RawData.index + i;
data_sample.value = arrayToInt16(payload, dataSize * (i) + 4, false); // 4 byte offset as we have AXIS+INDEX+LENGTH
decode.RawData.data.push(data_sample);
}
break;
}
case 0xF801: // DevEUI
decode.DevEui = arrayToString(payload);
break;
case 0xF802: // AppEUI
decode.AppEui = arrayToString(payload);
break;
case 0xF803: // Region
var RegionType = {
0: "AS923",
1: "AU915",
2: "CN470",
3: "CN779",
4: "EU433",
5: "EU868",
6: "KR920",
7: "IN865",
8: "US915"
}
decode.Region = RegionType[payload[0] & 0x0F];
break;
case 0xF804: // NetID
decode.netId = arrayToString(payload);
break;
case 0xAA01: // Protocol Version
decode.protocol_version = payload[0]
break;
case 0xDA04: // Measurement Timestamp
const ts32 = arrayConverter(payload, 0, 3, false, false)
const date = new Date(ts32 * 1000);
decode.sensor_timestamp = date.toISOString()
break;
default:
decode.payload = []
decode.payload = arrayToString(payload);
break;
}
return true;
}
function DecodeKeepAlive(decode, port, bytes) {
if (port === 30) {
decode.msgType = "Keep Alive";
decode.devtype = {}
decode.devtype = getDevtype(arrayToUint16(bytes, 0, false));
decode.cnt = arrayToUint16(bytes, 2, false, false);
decode.devstat = []
decode.devstat = getDevstat(bytes[4])
decode.bat = bytes[5];
return true;
}
else {
return false;
}
}
function DecodeTiltSensor(decode, port, bytes) {
decode.size = bytes.length;
if (port === 10)
if (0x2411 === arrayToUint16(bytes, 0, false) && bytes.length === 24) {
decode.cnt = arrayToUint16(bytes, 2, false, false);
var devstat;
devstat = [];
var DevstatDict = {
7: "Com_Err",
6: "Crc_Err",
5: "Timeout_Err",
4: "Sys_Err",
3: "Conf_Err"
}
if (bytes[4] === 0x00) {
devstat = 'ok';
}
else {
for (var i = 7; i >= 3; i--) {
if (bitfield(bytes[4], i) === 1) {
devstat.push(DevstatDict[i]);
}
}
}
decode.devstat = devstat
decode.bat = bytes[5];
decode.temp = (arrayConverter(bytes, 6, 2, false, true) / 100.0).toString() + "°C";
decode.angleX = (arrayConverter(bytes, 8, 2, false, true) / 100.0).toString() + "°";
decode.angleY = (arrayConverter(bytes, 10, 2, false, true) / 100.0).toString() + "°";
decode.dispX = (arrayToFloat(bytes, 12, false)).toString() + "mm";
decode.dispY = (arrayToFloat(bytes, 16, false)).toString() + "mm";
decode.dispZ = (arrayToFloat(bytes, 20, false)).toString() + "mm";
return true;
}
return false;
}
function DecodeSinglePointOrMultiPoint(decode, port, bytes, error) {
if (port === 20) {
DecodeOperationResponses(decode, port, bytes)
return true
}
if (port === 10) {
// Mapping between bw_mode and bin resolution
var BW_MODE_RESOLUTION = {
0x00: 0.125,
0x01: 0.25,
0x02: 0.5,
0x03: 1,
0x04: 2,
0x05: 3,
0x06: 4,
0x07: 5,
0x08: 6,
0x09: 7,
0x0A: 8,
0x0B: 9,
0x0C: 10,
0x0D: 11,
0x0E: 12,
0x0F: 13,
}
// SINGLEPOINT
// 0x1321, 0x1222, 0x1422 map to devtype for pressure and temperature and humidity sensors...
if ([0x1321, 0x1222, 0x1422].includes(arrayToUint16(bytes, 0, false))) {
decode.devtype = {}
decode.devtype = getDevtype(arrayToUint16(bytes, 0, false));
decode.cnt = arrayToUint16(bytes, 2, false, false);
decode.devstat = []
decode.devstat = getDevstat(bytes[4])
decode.bat = bytes[5];
decode.temp = (arrayConverter(bytes, 6, 2, false, true) / 100.0).toString();
if (decode.devtype.Output === "Float") {
decode.data = (arrayToFloat(bytes, 8, false)).toString();
}
else {
decode.data = (arrayToInt32(bytes, 8, false) / 100.0).toString();
}
return true;
}
// MULTIPOINT
// 0x1121 should be single axis and 0x1521 should be tri-axis
else if ([0x1121, 0x1521, 0x152f, 0x112f].includes(arrayToUint16(bytes, 0, false))) {
decode.devtype = {}
decode.devtype = getDevtype(arrayToUint16(bytes, 0, false));
decode.cnt = arrayToUint16(bytes, 2, false, false);
decode.devstat = []
decode.devstat = getDevstat(bytes[4])
decode.bat = bytes[5];
decode.temp = (arrayConverter(bytes, 6, 2, false, true) / 100.0).toString();
decode.vibration_information = {}
decode.vibration_information.vibration_data_format = getBits(bytes[8], 0, 2)
decode.vibration_information.rotating_mode = getBits(bytes[8], 4, 1)
decode.vibration_information.axis = []
if (getBits(bytes[8], 5, 1) === 1)
decode.vibration_information.axis.push("x");
if (getBits(bytes[8], 6, 1) === 1)
decode.vibration_information.axis.push("y");
if (getBits(bytes[8], 7, 1) === 1)
decode.vibration_information.axis.push("z");
decode.preset_id = bytes[9];
decode.bw_mode = bytes[10];
if (decode.bw_mode > 0x0F) {
error.push("BW_Mode must be between 0x00 and 0x0F.")
return false;
}
var isSensorErr = decode.devstat.includes("SnsErr")
if (isSensorErr) {
error.push("Sensor measure is not reliable or is out of range (for more detail see sensor diagnosis).")
return false;
}
decode.vibration_data = {}
switch (decode.vibration_information.vibration_data_format) {
// DATA FORMAT 0
case 0:
var axisSize = 6
// Offset to keep track if an axis existed or not, axis should always come X then Y then Z
var offset = 0
if (decode.vibration_information.axis.includes("x")) {
decode.vibration_data.x = {}
decode.vibration_data.x.time_rms = arrayConverter(bytes, 11, 2, false)
decode.vibration_data.x.time_p2p = arrayConverter(bytes, 13, 2, false)
decode.vibration_data.x.freq_rms = arrayConverter(bytes, 15, 2, false)
offset += 1
}
if (decode.vibration_information.axis.includes("y")) {
decode.vibration_data.y = {}
decode.vibration_data.y.time_rms = arrayConverter(bytes, offset * axisSize + 11, 2, false)
decode.vibration_data.y.time_p2p = arrayConverter(bytes, offset * axisSize + 13, 2, false)
decode.vibration_data.y.freq_rms = arrayConverter(bytes, offset * axisSize + 15, 2, false)
offset += 1
}
if (decode.vibration_information.axis.includes("z")) {
decode.vibration_data.z = {}
decode.vibration_data.z.time_rms = arrayConverter(bytes, offset * axisSize + 11, 2, false)
decode.vibration_data.z.time_p2p = arrayConverter(bytes, offset * axisSize + 13, 2, false)
decode.vibration_data.z.freq_rms = arrayConverter(bytes, offset * axisSize + 15, 2, false)
}
break;
// DATA FORMAT 1 is the default one, because it the format selected in the default preset ID 0
case 1:
decode.vibration_data.spectrum_rms = arrayConverter(bytes, 11, 2, false)
decode.vibration_data.time_p2p = arrayConverter(bytes, 13, 2, false)
decode.vibration_data.velocity = arrayConverter(bytes, 15, 2, false)
decode.vibration_data.windows = []
let windowSize = 14
// Window start from this offset
let offsetStartWindows = 17
// Remove all byte from header and delete last window if fragmented
let windowsNumber = Math.floor((bytes.length - offsetStartWindows) / windowSize)
// Going trhgouh all window
for (let windowIndex = 0; windowIndex < windowsNumber; windowIndex++) {
// Add a key to all windows: "status" with values "enabled" / "disabled"
let window_data = { status: 'disabled' }
// Two first byte of the window
const _rms_window = arrayConverter(bytes, offsetStartWindows + windowIndex * windowSize, 2, false)
if (_rms_window !== 0xFFFF) {
window_data.rms_window = _rms_window
window_data.status = 'enabled'
}
let peak1_bin = arrayConverter(bytes, offsetStartWindows + windowIndex * windowSize + 2, 2, false)
if (peak1_bin !== 0xFFFF) {
window_data.peak1_bin = peak1_bin
window_data.peak1_frequency = window_data.peak1_bin * BW_MODE_RESOLUTION[decode.bw_mode]
window_data.peak1_rms = arrayConverter(bytes, offsetStartWindows + windowIndex * windowSize + 4, 2, false)
}
let peak2_bin = arrayConverter(bytes, offsetStartWindows + windowIndex * windowSize + 6, 2, false)
if (peak2_bin !== 0xFFFF) {
window_data.peak2_bin = peak2_bin
window_data.peak2_frequency = window_data.peak2_bin * BW_MODE_RESOLUTION[decode.bw_mode]
window_data.peak2_rms = arrayConverter(bytes, offsetStartWindows + windowIndex * windowSize + 8, 2, false)
}
let peak3_bin = arrayConverter(bytes, offsetStartWindows + windowIndex * windowSize + 10, 2, false)
if (peak3_bin !== 0xFFFF) {
window_data.peak3_bin = peak3_bin
window_data.peak3_frequency = window_data.peak3_bin * BW_MODE_RESOLUTION[decode.bw_mode]
window_data.peak3_rms = arrayConverter(bytes, offsetStartWindows + windowIndex * windowSize + 12, 2, false)
}
decode.vibration_data.windows.push(window_data);
}
break;
// DATA FORMAT 2
case 2:
decode.vibration_data.spectrum_rms = arrayConverter(bytes, 11, 2, false)
decode.vibration_data.time_p2p = arrayConverter(bytes, 13, 2, false)
decode.vibration_data.velocity = arrayConverter(bytes, 15, 2, false)
decode.vibration_data.peak_cnt = bytes[17]
decode.vibration_data.peaks = []
let peak_size = 19;
/** Les peaks demarrent a partir de cette offset */
let offsetStartPeaks = 18
let peaks_bitfield = ""
// Where to stop looking for peak
const binary_limit = Math.ceil((decode.vibration_data.peak_cnt * peak_size) / 8)
for (let i = 0; i < binary_limit; i++) {
let byte = bytes[offsetStartPeaks + i];
peaks_bitfield += byte.toString(2).padStart(8, '0');
}
// Hold the cursor for bit indexing inside peaks
let current_bit_index = 0;
for (let peakIndex = 0; peakIndex < decode.vibration_data.peak_cnt; peakIndex++) {
if (current_bit_index + 19 > peaks_bitfield.length) {
// Not enough bits left for another (PEAK_BIN, PEAK_MAGNITUDE) pair
break;
}
let peak_data = {}
// Bin index is 11 bit wide
peak_data.bin_index = parseInt(peaks_bitfield.substring(current_bit_index, current_bit_index + 11), 2)
peak_data.frequency = peak_data.bin_index * BW_MODE_RESOLUTION[decode.bw_mode]
current_bit_index += 11;
// Magnitude is just after, 8 bit wide
peak_data.magnitude_compressed = parseInt(peaks_bitfield.substring(current_bit_index, current_bit_index + 8), 2);
peak_data.magnitude_rms = dBDecompression(peak_data.magnitude_compressed)
current_bit_index += 8;
decode.vibration_data.peaks.push(peak_data);
}
break;
default:
break;
}
return true;
}
// Unknown product
else {
return false
}
} else if (port === 138 || port === 202) {
decode.val = 'This LNS does not support fragmented frame. (Vibration Multipoint FW)';
decode.port = port;
decode.bytes = arrayToString(bytes);
return true;
}
return false;
}
/**
*
* @param {Uint8Array} bytes
* @returns Error status (true or false)
*/
function DecodeProtocolV2(decode, port, bytes, error) {
// Check the sensor type, then the frame type, then check what are the optional field, then finally decode the data.
if (port === 21) {
// Check if frame received is of Protocol v2
if ((bytes[0] & 0xF0) !== 0x20) {
error.push("Received invalid frame, please contact TE and provide this exact frame")
return false
}
let sensor_type = bytes[0] & 0x0F
decode.devtype = {}
const sensorTypes = {
0: "Error",
1: "Vibration1Axis",
2: "Temperature",
3: "Pressure",
4: "Humidity",
5: "Vibration3Axis"
}
decode.devtype.sensor = sensorTypes[sensor_type] || "Unknown"
const frameType = {
0x00: "DownlinkResponse",
0x01: "KeepAlive",
0x02: "AdvertisingWithCounter",
0x03: "AdvertisingWithTimestamp",
0x04: "LoRaSingleMeasurement",
0x05: "LoRaMergedMeasurement"
}
decode.frameType = frameType[bytes[1]] || "Unknown"
decode.devstat = getDevstat(bytes[2])
decode.bat = bytes[3];
const payload = bytes.slice(4)
switch (decode.frameType) {
case "DownlinkResponse":
DecodeOperationResponses(decode, port, payload)
break;
case "KeepAlive":
decode.cnt = arrayToInt16(payload)
break;
case "LoRaSingleMeasurement":
decodeLoRaSimpleMeasurement(decode, payload)
break;
case "LoRaMergedMeasurement":
decodeLoRaMergedMeasurement(decode, payload)
break;
default:
break;
}
function decodeLoRaSimpleMeasurement(decode, payload) {
decode.measurement = {}
const OptionMap = {
0: "MeasurementCounter",
1: "Timestamp",
2: "SecondaryTemperature"
}
decode.optional_field = extract_bitfield(payload[0], OptionMap)
// Keep track of optional field offset
let offset = 0
if (decode.optional_field.includes("MeasurementCounter")) {
decode.cnt = arrayConverter(payload, 1, 2, false, false)
offset += 2
}
if (decode.optional_field.includes("Timestamp")) {
decode.timestamp = arrayConverter(payload, offset + 1, 4, false, false)
decode.timestamp_human = new Date(decode.timestamp * 1000).toISOString();
offset += 4
}
if (decode.optional_field.includes("SecondaryTemperature")) {
decode.temp = (arrayToInt16(payload, offset + 1, false) / 100.0).toString()
offset += 2
}
const meas_data = payload.slice(offset + 1)
switch (decode.devtype.sensor) {
case "Vibration1Axis":
case "Vibration3Axis":
decodeVibration(decode, meas_data)
break;
case "Temperature":
case "Humidity":
case "Pressure":
decodeSP(decode, meas_data)
break;
default:
break;
}
}
function decodeSP(decode, meas_data) {
const sensor_data_type = {
0x01: "Float",
0x02: "Int32",
}
decode.sensor_data_type = sensor_data_type[meas_data[0]] || "RFU"
if (decode.sensor_data_type === "Float") {
decode.data = (arrayToFloat(meas_data, 1, false)).toString();
}
else {
decode.data = (arrayToInt32(meas_data, 1, false) / 100.0).toString();
}
}
function decodeVibration(decode, meas_data) {
// Mapping between bw_mode and bin resolution
const BW_MODE_RESOLUTION = {
0x00: 0.125,
0x01: 0.25,
0x02: 0.5,
0x03: 1,
0x04: 2,
0x05: 3,
0x06: 4,
0x07: 5,
0x08: 6,
0x09: 7,
0x0A: 8,
0x0B: 9,
0x0C: 10,
0x0D: 11,
0x0E: 12,
0x0F: 13,
}
decode.vibration_information = {}
decode.vibration_information.frame_format = getBits(meas_data[0], 0, 2)
decode.vibration_information.rotating_mode = getBits(meas_data[0], 4, 1)
decode.vibration_information.axis = []
if (getBits(meas_data[0], 5, 1) === 1)
decode.vibration_information.axis.push("x");
if (getBits(meas_data[0], 6, 1) === 1)
decode.vibration_information.axis.push("y");