Repository navigation
Expand file tree
/
Copy pathdecoder.js
More file actions
68 lines (52 loc) · 1.76 KB
/
Copy pathdecoder.js
File metadata and controls
68 lines (52 loc) · 1.76 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
/*
* Decoder function for The Things Network to unpack the payload of Bosch Parking Lot Sensors
*
* More info on the sensors/buy online:
* https://connectedthings.store/lorawan-sensors/bosch-parking-lot-sensors.html
*
* This function was created by Al Bennett at Sensational Systems - al@sensational.systems
*/
function Decoder(bytes, port) {
var decoded = {
"bytes": bytes,
"port": port,
"packet_type": "Unknown"
};
// Startup packets are on port 3
if(3 === port) {
decoded.packet_type = "Startup";
// Bytes 0 - 11 are debug info
decoded.debug = bytes.slice(0, 12);
// Bytes 12 - 14 are firmware version, convert to string
decoded.firmware_version = bytes[12] + '.' + bytes[13] + '.' + bytes[14];
// Byte 15 is the reset reason, convert to string
decoded.reset_reason = 0;
switch (bytes[15]) {
case 1: decoded.reset_reason = "Watchdog";
break;
case 2: decoded.reset_reason = "Power On";
break;
case 3: decoded.reset_reason = "System Request";
break;
case 4: decoded.reset_reason = "Other";
break;
default: decoded.reset_reason = "Unknown";
}
// Final byte, 16, is seven reserved bits with the LSB being the current occupancy state
decoded.occupied = false;
if(1 === (bytes[16] & 1)) {
decoded.occupied = true;
}
}
// Parking status packets are on port 1
// Heartbeat packets are on port 2, but the same as status packets, so handled the same
if(1 === port || 2 === port) {
if(1 === port) {
decoded.packet_type = "Status";
} else {
decoded.packet_type = "Heartbeat";
}
decoded.occupied = (1 === bytes[0]) ? true : false;
}
return decoded;
}