//V1.0, 23.08.2021,DS
if (msg.port == "3") {
var decoded = decodeFromHex(msg.data);
decoded.ts = msg.ts;
decoded.rssi = msg.rssi;
decoded.snr = msg.snr;
decoded.toa = msg.toa;
decoded.frequency = msg.frequency;
decoded.dr = msg.dr;
decoded.bat = decodeBattery(msg.bat);
decoded.hex = msg.data;
return {
msg: decoded,
metadata: metadata,
msgType: msgType
};
} else {
return {
msg: msg,
metadata: metadata,
msgType: msgType
};
}
function decodeFromHex(data) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var telemetry = {};
var bytes = parseHexStringToBytes(data);
var test = data.substr(8, 8);
telemetry.heatMeterEnergy = HexToFloat32(data
.substr(8, 8));
telemetry.power = HexToFloat32(data.substr(
32, 8));
telemetry.flowRate = HexToFloat32(data.substr(40, 8));
telemetry.flowTemperature = HexToFloat32(data.substr(
48, 8));
telemetry.returnFlowTemperature = HexToFloat32(data
.substr(56, 8));
return telemetry;
}
function parseFloat(str) {
var float = 0,
sign, order, mantiss, exp,
int = 0,
multi = 1;
if (/^0x/.exec(str)) {
int = parseInt(str, 16);
} else {
for (var i = str.length - 1; i >= 0; i -= 1) {
if (str.charCodeAt(i) > 255) {
console.log('Wrong string parametr');
return false;
}
int += str.charCodeAt(i) * multi;
multi *= 256;
}
}
sign = (int >>> 31) ? -1 : 1;
exp = (int >>> 23 & 0xff) - 127;
mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
for (i = 0; i < mantissa.length; i += 1) {
float += parseInt(mantissa[i]) ? Math.pow(2, exp) :
0;
exp--;
}
return float * sign;
}
function findRegister(bytes, registersActive) {
var registers = new Array(4);
var start = 3;
for (var i = 0; i < registersActive.length; i++) {
var current = start * (i + 1);
if (registersActive[i] == 1) {
registers[i] = bytes[current] + "." + bytes[
current + 1] + "." + bytes[current + 2];
}
}
return registers;
}
function decodeBattery(byte) {
if (byte == 0) {
return 'External power source';
} else if (byte > 0 && byte < 255) {
return byte / 254 * 100;
} else {
return 'Unknown battery state';
}
}
function calculateBattery(nibbles) {
var result = nibbles[2] * 10;
return result;
}
function parseHexStringToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function parseHexStringToNibbles(hex) {
for (var nibbles = [], c = 0; c < hex.length; c += 1)
nibbles.push(parseInt(hex.substr(c, 1), 16));
return nibbles;
}
function convertToBinary(x) {
var bin = 0;
var rem, i = 1,
step = 1;
while (x != 0) {
rem = x % 2;
x = parseInt(x / 2);
bin = bin + rem * i;
i = i * 10;
}
return bin;
}
function decodeToJson(payload) {
// covert payload to string.
var str = decodeToString(payload);
// parse string to JSON
var data = JSON.parse(str);
return data;
}
function HexToFloat32(str){
var int = parseInt(str, 16);
if (int > 0 || int < 0) {
var sign = (int >>> 31) ? -1 : 1;
var exp = (int >>> 23 & 0xff) - 127;
var mantissa = ((int & 0x7fffff) + 0x800000)
.toString(2);
var float32 = 0;
for (i = 0; i < mantissa.length; i += 1) {
float32 += parseInt(mantissa[i]) ? Math.pow(
2, exp) : 0;
exp--;
}
return float32 * sign;
} else return 0;
}