Flood Sensor Uplink Decoder
1
function Decoder(bytes){
2
let byteArray = bytes.match(/.{1,2}/g).map(byte =>
3
(parseInt(byte, 16).toString(2)).padStart(8, '0')
4
)
5
let messageTypes = ['keepalive', 'testButtonPressed', 'floodDetected', 'fraudDetected','fraudDetected'];
6
const toBool = value => value == '1';
7
const shortPackage = (byteArray) => {
8
return {
9
reason: messageTypes[parseInt(byteArray[0].slice(0, 3),2)],
10
boxTamper: toBool(byteArray[0][4]),
11
flood: toBool(byteArray[0][6]),
12
battery: (parseInt(byteArray[1], 2) * 16)/1000,
13
}
14
}
15
const longPackage = (byteArray) => {
16
return {
17
reason: messageTypes[parseInt(byteArray[0].slice(0, 3),2)],
18
boxTamper: toBool(byteArray[0][4]),
19
flood: toBool(byteArray[0][6]),
20
battery: (parseInt(byteArray[1], 2) * 16)/1000,
21
temp1: parseInt(byteArray[2], 2),
22
}
23
}
24
if(byteArray.length > 2){
25
return longPackage(byteArray);
26
}else{
27
return shortPackage(byteArray);
28
}
29
}
Copied!
1
function Decoder(bytes){
2
var messageTypes = ['keepalive', 'testButtonPressed', 'floodDetected', 'fraudDetected','fraudDetected'];
3
var decbin = function(number) {
4
return parseInt(number, 10).toString(2)
5
}
6
var byteArray = bytes.map(function (byte) {
7
var number = decbin(byte);
8
return Array(9 - number.length).join("0") + number;
9
});
10
function toBool (value) { return value == '1'};
11
function shortPackage(byteArray) {
12
return {
13
reason: messageTypes[parseInt(byteArray[0].slice(0, 3),2)],
14
boxTamper: toBool(byteArray[0][4]),
15
flood: toBool(byteArray[0][6]),
16
battery: (parseInt(byteArray[1], 2) * 16)/1000,
17
}
18
}
19
function longPackage (byteArray) {
20
return {
21
reason: messageTypes[parseInt(byteArray[0].slice(0, 3),2)],
22
boxTamper: toBool(byteArray[0][4]),
23
flood: toBool(byteArray[0][6]),
24
battery: (parseInt(byteArray[1], 2) * 16)/1000,
25
temp1: parseInt(byteArray[2], 2),
26
}
27
}
28
if(byteArray.length > 2){
29
return longPackage(byteArray);
30
}else{
31
return shortPackage(byteArray);
32
}
33
}
Copied!
Last modified 5mo ago