-
Notifications
You must be signed in to change notification settings - Fork 30
/
ChirpStackV4_Decoder.js
80 lines (75 loc) · 2.11 KB
/
ChirpStackV4_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
// Decode uplink function.
//
// Input is an object with the following fields:
// - bytes = Byte array containing the uplink payload, e.g. [255, 230, 255, 0]
// - fPort = Uplink fPort.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - data = Object representing the decoded payload.
function decodeUplink(input) {
var decoded = {
data:[]
};
var type;
var floatNumber;
if ( (input.bytes.length % 5) !== 0) {
decoded.data.push({
type: "error",
Value: -1
});
return { data: decoded }
}
const arrayBuffer = new ArrayBuffer(4);
const dataView = new DataView(arrayBuffer);
for(let j =0 ; j < input.bytes.length; j+=5) {
var num =0;
switch ( input.bytes[j]) {
case 0:
type = 'Temp';
num = 2;
break;
case 1:
type = 'Humidity';
num = 2;
break;
case 2:
type = 'CO2';
num = 0;
break;
case 3:
type = 'TVOC';
num = 0;
break;
case 4:
type = 'Light';
num = 0;
break;
default:
type = 'unknown';
break;
}
for (let i = 0; i < 4; i++) {
dataView.setUint8(i, input.bytes[j+i+1]);
}
floatNumber = dataView.getFloat32(0, true);
decoded.data.push({
type: type,
Value: floatNumber.toFixed(num)
});
}
return { data: decoded }
}
// Encode downlink function.
//
// Input is an object with the following fields:
// - data = Object representing the payload that must be encoded.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - bytes = Byte array containing the downlink payload.
function encodeDownlink(input) {
return {
bytes: [225, 230, 255, 0]
};
}