forked from unloquer/ESP32-Paxcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plain_decoder.js
86 lines (74 loc) · 2.54 KB
/
plain_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Decoder for device payload encoder "PLAIN"
// copy&paste to TTN Console -> Applications -> PayloadFormat -> Decoder
function Decoder(bytes, port) {
var decoded = {};
if (port === 1) {
var i = 0;
if (bytes.length >= 2) {
decoded.wifi = (bytes[i++] << 8) | bytes[i++];
}
if (bytes.length === 4 || bytes.length > 15) {
decoded.ble = (bytes[i++] << 8) | bytes[i++];
}
if (bytes.length > 4) {
decoded.latitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.longitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.sats = bytes[i++];
decoded.hdop = (bytes[i++] << 8) | (bytes[i++]);
decoded.altitude = ((bytes[i++] << 8) | (bytes[i++]));
}
}
if (port === 2) {
var i = 0;
decoded.battery = ((bytes[i++] << 8) | bytes[i++]);
decoded.uptime = ((bytes[i++] << 56) | (bytes[i++] << 48) | (bytes[i++] << 40) | (bytes[i++] << 32) |
(bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.temp = bytes[i++];
decoded.memory = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.reset0 = bytes[i++];
decoded.reset1 = bytes[i++];
}
if (port === 4) {
var i = 0;
decoded.latitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.longitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.sats = bytes[i++];
decoded.hdop = (bytes[i++] << 8) | (bytes[i++]);
decoded.altitude = ((bytes[i++] << 8) | (bytes[i++]));
}
if (port === 5) {
var i = 0;
decoded.button = bytes[i++];
}
if (port === 6) {
var i = 0;
decoded.rssi = bytes[i++];
decoded.beacon = bytes[i++];
}
if (port === 7) {
var i = 0;
decoded.temperature = ((bytes[i++] << 8) | bytes[i++]);
decoded.pressure = ((bytes[i++] << 8) | bytes[i++]);
decoded.humidity = ((bytes[i++] << 8) | bytes[i++]);
decoded.air = ((bytes[i++] << 8) | bytes[i++]);
}
if (port === 8) {
var i = 0;
if (bytes.length >= 2) {
decoded.battery = (bytes[i++] << 8) | bytes[i++];
}
}
if (port === 9) {
// timesync request
if (bytes.length === 1) {
decoded.timesync_seqno = bytes[0];
}
// epoch time answer
if (bytes.length === 5) {
var i = 0;
decoded.time = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.timestatus = bytes[i++];
}
}
return decoded;
}