-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexampleDecoderTTN.js
109 lines (100 loc) · 2.19 KB
/
exampleDecoderTTN.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
var hexChar = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
function byteToHex(b) {
return hexChar[(b >> 4) & 0x0f] + hexChar[b & 0x0f];
}
function hexToInt(hex) {
var num = hex;
if (num > 0x7F) {
num = num - 0x100;
}
return num;
}
function Decoder(bytes, port) {
if (port == 2) {
var mac1 = "";
for (i = 0; i < 6; i++) {
mac1 += byteToHex(bytes[i + 1]);
if (i < 5) {
mac1 += ':';
}
}
var rssi1 = hexToInt(bytes[0]);
var mac2 = "";
for (i = 0; i < 6; i++) {
mac2 += byteToHex(bytes[i + 8]);
if (i < 5) {
mac2 += ':';
}
}
var rssi2 = hexToInt(bytes[7]);
var mac3 = "";
for (i = 0; i < 6; i++) {
mac3 += byteToHex(bytes[i + 15]);
if (i < 5) {
mac3 += ':';
}
}
var rssi3 = hexToInt(bytes[14]);
var temp = ((bytes[22] << 8) | bytes[23]);
if ((bytes[22] >> 7) == 1) {
temp = (0xFFFF ^ (temp)) + 1;
temp = temp - temp - temp;
}
var press = ((bytes[24] << 8) | bytes[25]);
var hum = (bytes[26]);
return {
wifiAccessPoints: [
{
"macAddress": mac1,
"signalStrength": rssi1
},
{
"macAddress": mac2,
"signalStrength": rssi2
},
{
"macAddress": mac3,
"signalStrength": rssi3
}
],
battery: String(3.3 / 255) * ((4.7 + 10) / 10) * (bytes[21]),
temperature: String(temp / 100),
pressure: String(press / 10),
humidity: String(hum),
inMotion: String(bytes[27])
};
}
if (port == 3) {
return {
payload: String("GNSS data")
};
}
if (port == 10) {
return {
battery: String(3.3 / 255) * ((4.7 + 10) / 10) * (bytes[0])
};
}
if (port == 199) {
return {
payload: String("LoRa Cloud payload")
};
}
if (port == 202) {
return {
payload: String("Alc sync payload")
};
}
if (port == 44) {
return {
ledActivation: String(bytes[0]),
interval: String(bytes[1] << 8 | bytes[2]),
beacon: String(bytes[3]),
wifiActivation: String(bytes[4]),
gnssActivation: String(bytes[5]),
motionActivation: String(bytes[6]),
motionIntervalDuration: String(bytes[7]),
motionThresholdRegister: String(bytes[8]),
motionDurationRegister: String(bytes[9])
};
}
}