-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoderTTN3.js
60 lines (55 loc) · 1.65 KB
/
decoderTTN3.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
function Decoder(bytes, fPort) {
var decoded = {};
if (fPort == 1) {
payloadversion = bytes[0];
decoded.length = bytes.length;
// byte0: payloadversion
// byte1: sensor status
for (i = 2; i < bytes.length-1; i++) {
switch (bytes[i]) {
case 0x01:
decoded.battery = (bytes[i+1] + 256) / 100.0;
i = i+1;
break;
case 0x10:
decoded.temperature = ((bytes[i+1] & 0x80 ? 0xffff << 16 : 0) + (bytes[i+1] << 8) + bytes[i+2]) / 100.0;
i = i+2;
break;
case 0x11:
decoded.humidity = bytes[i+1];
i = i+1;
break;
case 0x12:
decoded.pressure = ((bytes[i+1] << 8) + bytes[i+2]) / 10.0;
i = i+2;
break;
case 0x50:
decoded.pm25 = ((bytes[i+1] << 8) + bytes[i+2]) / 10.0;
i = i+2;
break;
case 0x51:
decoded.pm10 = ((bytes[i+1] << 8) + bytes[i+2]) / 10.0;
i = i+2;
break;
}
}
}
return decoded;
}
function decodeUplink(input) {
var data = input.bytes;
var valid = true;
if (typeof Decoder === "function") {
data = Decoder(data, input.fPort);
}
if (valid) {
return {
data: data
};
} else {
return {
data: {},
errors: ["Invalid data received"]
};
}
}