T-Valve Uplink Decoder

Recommended decoder (JavaScript ES6)

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!

TTN V3 Decoder (JavaScript ES5):

1
function decodeUplink(input) {
2
var bytes = input.bytes;
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
11
var messageTypes = [ 'keepalive', 'testButtonPressed', 'floodDetected', 'controlButtonPressed', 'fraudDetected' ];
12
toBool = function(value) {
13
return value == '1';
14
};
15
shortPackage = function(byteArray) {
16
return {
17
data: {
18
reason: 'keepalive',
19
waterTemp: parseInt(byteArray[0], 2)/2,
20
valveState: toBool(byteArray[1][0]),
21
ambientTemp: (parseInt(byteArray[1].slice(1, 8), 2)-20)/2,
22
}
23
};
24
};
25
longPackage = function(byteArray) {
26
return {
27
data: {
28
reason: messageTypes[parseInt(byteArray[0].slice(0, 3),2)],
29
boxTamper: toBool(byteArray[0][4]),
30
floodDetectionWireState: toBool(byteArray[0][5]),
31
flood: toBool(byteArray[0][6]),
32
magnet: toBool(byteArray[0][7]),
33
alarmValidated: toBool(byteArray[1][0]),
34
manualOpenIndicator: toBool(byteArray[1][1]),
35
manualCloseIndicator: toBool(byteArray[1][2]),
36
closeTime: parseInt(byteArray[2], 2),
37
openTime: parseInt(byteArray[3], 2),
38
battery: ((parseInt(byteArray[4], 2) * 8) + 1600)/1000,
39
}
40
};
41
};
42
if (byteArray.length > 2) {
43
return longPackage(byteArray);
44
} else {
45
return shortPackage(byteArray);
46
}
47
}
Copied!