T-Valve 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
6
let messageTypes = ['keepalive', 'testButtonPressed', 'floodDetected', 'controlButtonPressed', 'fraudDetected'];
7
const toBool = value => value == '1';
8
const shortPackage = (byteArray) => {
9
return {
10
reason: messageTypes[parseInt(byteArray[0].slice(0, 2))],
11
valveState: toBool(byteArray[0][3]),
12
boxTamper: toBool(byteArray[0][4]),
13
floodDetectionWireState: toBool(byteArray[0][5]),
14
flood: toBool(byteArray[0][6]),
15
magnet: toBool(byteArray[0][7]),
16
alarmValidated: toBool(byteArray[1][0]),
17
manualOpenIndicator: toBool(byteArray[1][1]),
18
manualCloseIndicator: toBool(byteArray[1][2]),
19
}
20
}
21
const longPackage = (byteArray) => {
22
return {
23
reason: messageTypes[parseInt(byteArray[0].slice(0, 2))],
24
valveState: toBool(byteArray[0][3]),
25
boxTamper: toBool(byteArray[0][4]),
26
floodDetectionWireState: toBool(byteArray[0][5]),
27
flood: toBool(byteArray[0][6]),
28
magnet: toBool(byteArray[0][7]),
29
alarmValidated: toBool(byteArray[1][0]),
30
manualOpenIndicator: toBool(byteArray[1][1]),
31
manualCloseIndicator: toBool(byteArray[1][2]),
32
closeTime: parseInt(byteArray[2], 2),
33
openTime: parseInt(byteArray[3], 2),
34
temp1: parseInt(byteArray[4], 2),
35
temp2: parseInt(byteArray[5], 2),
36
battery: (parseInt(byteArray[6], 2) * 4) + 3000,
37
}
38
}
39
if(byteArray.length > 2){
40
return longPackage(byteArray);
41
}else{
42
return shortPackage(byteArray);
43
}
44
}
Copied!
Last modified 5mo ago
Copy link