-
Notifications
You must be signed in to change notification settings - Fork 0
/
chirpstack-decoder.js
65 lines (57 loc) · 2.05 KB
/
chirpstack-decoder.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
// Decode FieldTester Payload and LinkCheck payload.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// Decoding function depends on fPort
// - fPort == 1 ==> Decode FieldTester payload
// - fPort == 2 ==> Decode Linkcheck payload (returns an ASCII string of the payload)
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(fPort, bytes, variables) {
var decoded = {};
// avoid sending Downlink ACK to integration (Cargo)
if (fPort === 1) {
var lonSign = (bytes[0] >> 7) & 0x01 ? -1 : 1;
var latSign = (bytes[0] >> 6) & 0x01 ? -1 : 1;
var encLat = ((bytes[0] & 0x3f) << 17) +
(bytes[1] << 9) +
(bytes[2] << 1) +
(bytes[3] >> 7);
var encLon = ((bytes[3] & 0x7f) << 16) +
(bytes[4] << 8) +
bytes[5];
var hdop = bytes[8] / 10;
var sats = bytes[9];
var maxHdop = 2;
var minSats = 5;
if ((hdop < maxHdop) && (sats >= minSats)) {
// Send only acceptable quality of position to mappers
decoded.latitude = latSign * (encLat * 108 + 53) / 10000000;
decoded.longitude = lonSign * (encLon * 215 + 107) / 10000000;
decoded.altitude = ((bytes[6] << 8) + bytes[7]) - 1000;
decoded.accuracy = (hdop * 5 + 5) / 10
decoded.hdop = hdop;
decoded.sats = sats;
} else {
decoded.error = "Need more GPS precision (hdop must be <" + maxHdop +
" & sats must be >= " + minSats + ") current hdop: " + hdop + " & sats:" + sats;
decoded.latitude = latSign * (encLat * 108 + 53) / 10000000;
decoded.longitude = lonSign * (encLon * 215 + 107) / 10000000;
decoded.altitude = ((bytes[6] << 8) + bytes[7]) - 1000;
decoded.accuracy = (hdop * 5 + 5) / 10
decoded.hdop = hdop;
decoded.sats = sats;
}
return decoded;
} else if (fPort === 2) {
decoded.payload = Array.from(bytes, function (byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
return decoded;
}
return null;
}
// Chirpstack v3 to v4 compatibility wrapper
function decodeUplink(input) {
return {
data: Decode(input.fPort, input.bytes, input.variables)
};
}