beautify + test
This commit is contained in:
@@ -3,15 +3,15 @@ var bindata = "";
|
||||
|
||||
var ConvertBase = function (num) {
|
||||
return {
|
||||
from : function (baseFrom) {
|
||||
from: function (baseFrom) {
|
||||
return {
|
||||
to : function (baseTo) {
|
||||
to: function (baseTo) {
|
||||
return parseInt(num, baseFrom).toString(baseTo);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function pad(num) {
|
||||
var s = "0000000" + num;
|
||||
@@ -20,24 +20,24 @@ function pad(num) {
|
||||
|
||||
function windify(num) {
|
||||
if (num > 0) {
|
||||
return num * 0.6335 + 0.3582
|
||||
return num * 0.6335 + 0.3582;
|
||||
} else {
|
||||
return 0
|
||||
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++) {
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
binary += ConvertBase.dec2bin(data[i]);
|
||||
}
|
||||
return binary;
|
||||
@@ -50,35 +50,38 @@ function bitShift(bits) {
|
||||
}
|
||||
|
||||
function precisionRound(number, precision) {
|
||||
var factor = Math.pow(10, precision);
|
||||
return Math.round(number * factor) / factor;
|
||||
var factor = Math.pow(10, precision);
|
||||
return Math.round(number * factor) / factor;
|
||||
}
|
||||
|
||||
function Decoder(fPort, bytes, variables) {
|
||||
bindata = data2bits(bytes);
|
||||
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;
|
||||
if (bytes.length != 12) return {
|
||||
"status": "ERROR",
|
||||
"description": "12 bytes are required"
|
||||
}
|
||||
|
||||
Hz_avg = bitShift(12) * 0.02
|
||||
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
|
||||
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
|
||||
Hz_3s_min = bitShift(9) * 0.1;
|
||||
Wind_3s_min = precisionRound(windify(Hz_3s_min), 1);
|
||||
|
||||
Wind_stdev = precisionRound(bitShift(8) * 0.1, 1);
|
||||
@@ -96,27 +99,38 @@ function Decoder(fPort, bytes, variables) {
|
||||
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,
|
||||
};
|
||||
"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;
|
||||
return decoded;
|
||||
|
||||
}
|
||||
|
||||
function decodeUplink(input) {
|
||||
return {
|
||||
data: Decoder(input.fPort, input.bytes, input.variables)
|
||||
};
|
||||
return {
|
||||
data: Decoder(input.fPort, input.bytes, input.variables)
|
||||
}
|
||||
}
|
||||
|
||||
// function hexToBytes(hex) {
|
||||
// let bytes = [];
|
||||
// for (let c = 0; c < hex.length; c += 2)
|
||||
// bytes.push(parseInt(hex.substr(c, 2), 16));
|
||||
// return bytes;
|
||||
// }
|
||||
|
||||
// console.log("Testing decoder with sample data 0x0B80520C1C001F399C852001: ")
|
||||
// console.table(Decoder(1,hexToBytes("0B80520C1C001F399C852001"),1))
|
||||
// console.log("Done!")
|
||||
|
||||
Reference in New Issue
Block a user