Files
lorawan-device-codecs/barani-meteo-wind-2023.js
2023-07-20 10:24:10 +02:00

123 lines
2.7 KiB
JavaScript

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);
}
function windify(num) {
if (num > 0) {
return num * 0.6335 + 0.3582
} else {
return 0
}
}
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(fPort, bytes, variables) {
bindata = data2bits(bytes);
if(bytes.length != 12) return {"status": "ERROR", "description": "12 bytes are required"};
Index = bitShift(8)*1;
if ((Index % 10) <= 4) {
bitShift(1); // is 9th bit useless?
Battery = (Index % 10) * 0.2 + 3.3;
} else {
bitShift(1); // is 9th bit useless?
Battery = (Index % 10) * 0.2 + 3.3 - 1;
}
Hz_avg = bitShift(12) * 0.02
Wind_10m_avg = precisionRound(windify(Hz_avg), 2);
Hz_3s_gust = bitShift(9) * 0.1 + Hz_avg
Wind_3s_gust = precisionRound(windify(Hz_3s_gust), 1);
Hz_1s_gust = bitShift(8) * 0.1 + Hz_3s_gust;
Wind_1s_gust = precisionRound(windify(Hz_1s_gust), 1);
Hz_3s_min = bitShift(9) * 0.1
Wind_3s_min = precisionRound(windify(Hz_3s_min), 1);
Wind_stdev = precisionRound(bitShift(8) * 0.1, 1);
Dir_10m_avg = bitShift(9);
Dir_1s_gust = bitShift(9);
Dir_stdev = bitShift(8);
Duration_gust = bitShift(7) * 5;
Alarm_sent = bitShift(1);
Debug_flags = bitShift(1);
decoded = {
"Index": Index,
"Battery": Battery,
"Wind_10m_avg": Wind_10m_avg,
"Wind_3s_gust": Wind_3s_gust,
"Wind_1s_gust": Wind_1s_gust,
"Wind_3s_min": Wind_3s_min,
"Wind_stdev": Wind_stdev,
"Dir_10m_avg": Dir_10m_avg,
"Dir_1s_gust": Dir_1s_gust,
"Dir_stdev": Dir_stdev,
"Duration_gust": Duration_gust,
"Alarm_sent": Alarm_sent,
"Debug_flags": Debug_flags,
};
return decoded;
}
function decodeUplink(input) {
return {
data: Decoder(input.fPort, input.bytes, input.variables)
};
}