-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload-decoder.js
79 lines (68 loc) · 2.04 KB
/
payload-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
// Payload decoder for TTN v3 & Chirpstack v4, JS code based on ATTNode v3 example (https://www.attno.de/)
function bytesToInt16(bytes, start) {
var out = ((bytes[start]) | (bytes[start + 1] << 8));
var sign = bytes[start + 1] & (1 << 7);
if (sign) {
out = 0xFFFF0000 | out;
}
return out;
}
function bytesToUInt16(bytes, start) {
return ((bytes[start]) | (bytes[start + 1] << 8));
}
function bytesToInt32(bytes, start) {
return ((bytes[start]) | (bytes[start + 1] << 8) | (bytes[start + 2] << 16) | (bytes[start + 3] << 24));
}
// 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 = {};
var i = 0;
if (input.bytes.length >= 4) {
// Batt (uint16_t)
decoded.battery = bytesToUInt16(input.bytes, i) / 1000;
i += 2;
// Count (uint16_t)
decoded.count = bytesToUInt16(input.bytes, i);
decoded.mm = decoded.count * 0.45;
i += 2;
}
if (input.bytes.length >= 12) {
// Temperature (int32_t)
decoded.temperature = bytesToInt32(input.bytes, i) / 100;
i += 4;
// Atmospheric Pressure (int32_t)
decoded.pressure = bytesToInt32(input.bytes, i) / 100;
i += 4;
}
if (input.bytes.length >= 14) {
// Humidity (int32_t)
decoded.humidity = bytesToInt32(input.bytes, i) / 1000;
i += 4;
}
return {
data: decoded,
warnings: [],
errors: []
};
}
// 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: []
};
}