-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteohelix-message-decoder_chirpstack
85 lines (72 loc) · 2.05 KB
/
meteohelix-message-decoder_chirpstack
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
81
82
83
84
85
var pos = 0;
var bindata = "";
var ConvertBase = function (num) {
return {
from : function (baseFrom) {
return {
to : function (baseTo) {
return parseInt(num, baseFrom).toString(baseTo);
}
};
}
};
};
function pad(num) {
var s = "0000000" + num;
return s.slice(-8);
}
ConvertBase.dec2bin = function (num) {
return pad(ConvertBase(num).from(10).to(2));
};
ConvertBase.bin2dec = function (num) {
return ConvertBase(num).from(2).to(10);
};
function data2bits(data) {
var binary = "";
for(var i=0; i<data.length; i++) {
binary += ConvertBase.dec2bin(data[i]);
}
return binary;
}
function bitShift(bits) {
var num = ConvertBase.bin2dec(bindata.substr(pos, bits));
pos += bits;
return Number(num);
}
function precisionRound(number, precision) {
var factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}
function Decoder(bytes, port) {
bindata = data2bits(bytes);
//if(bytes.length != 12) return {"status": "ERROR", "describtion": "11 bytes are required"}
Type = bitShift(2);
Battery = precisionRound(bitShift(5)*0.05+3, 2);
Temperature = precisionRound(bitShift(11)*0.1-100, 1);
T_min = precisionRound(Temperature - bitShift(6)*0.1, 1);
T_max = precisionRound(Temperature + bitShift(6)*0.1, 1);
Humidity = precisionRound(bitShift(9)*0.2, 1);
Pressure = bitShift(14)*5+50000;
Irradiation = bitShift(10)*2;
Irr_max = Irradiation + bitShift(9)*2;
Rain = precisionRound(bitShift(8), 1);
Rain_min_time = precisionRound(bitShift(8), 1);
decoded = {
"Type": Type,
"Battery": Battery,
"Temperature": Temperature,
"T_min": T_min ,
"T_max": T_max,
"Humidity": Humidity,
"Pressure": Pressure,
"Irradiation": Irradiation,
"Irr_max": Irr_max,
"Rain": Rain,
"Rain min time": Rain_min_time
};
return decoded;
}
// This is the part needed for it to work with ChirpStack
function Decode(fPort, bytes) {
return Decoder(bytes);
}