//V1.0, 20.09.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 rawValue1 = data.substr(8, 8);
telemetry.powerL1 = hexStringToFloat(rawValue1);
var rawValue2 = data.substr(16, 8);
telemetry.powerL2 = hexStringToFloat(rawValue2);
var rawValue3 = data.substr(24, 8);
telemetry.powerL3 = hexStringToFloat(rawValue3);
var rawValue4 = data.substr(32, 8);
telemetry.power = hexStringToFloat(rawValue4);
var rawValue5 = data.substr(48, 8);
telemetry.electricityMeterEnergyHt = hexStringToFloat(rawValue5);
var rawValue6 = data.substr(56, 8);
telemetry.electricityMeterEnergyNt = hexStringToFloat(rawValue6);
/*
byte[] floatVals1 = BitConverter.GetBytes(num1);
float Value1 = BitConverter.ToSingle(floatVals1, 0);
var rawValue2 = data.substr(16, 8);
uint num2 = uint.Parse(rawValue2, NumberStyles
.AllowHexSpecifier);
byte[] floatVals2 = BitConverter.GetBytes(num2);
float Value2 = BitConverter.ToSingle(floatVals2, 0);
var rawValue3 = data.substr(24, 8);
uint num3 = uint.Parse(rawValue3, NumberStyles
.AllowHexSpecifier);
byte[] floatVals3 = BitConverter.GetBytes(num3);
float Value3 = BitConverter.ToSingle(floatVals3, 0);
var rawValue4 = data.substr(32, 8);
uint num4 = uint.Parse(rawValue4, NumberStyles
.AllowHexSpecifier);
byte[] floatVals4 = BitConverter.GetBytes(num4);
float Value4 = BitConverter.ToSingle(floatVals4, 0);
var rawValue5 = data.substr(40, 8);
uint num5 = uint.Parse(rawValue5, NumberStyles
.AllowHexSpecifier);
//byte[] floatVals5 = BitConverter.GetBytes(num5);
//float Value5 = BitConverter.ToSingle(floatVals5, 0);
string rawValue6 = data.substr(48, 8);
uint num6 = uint.Parse(rawValue6, NumberStyles
.AllowHexSpecifier);
byte[] floatVals6 = BitConverter.GetBytes(num6);
float Value6 = BitConverter.ToSingle(floatVals6, 0);
string rawValue7 = data.substr(56, 8);
uint num7 = uint.Parse(rawValue7, NumberStyles
.AllowHexSpecifier);
byte[] floatVals7 = BitConverter.GetBytes(num7);
float Value7 = BitConverter.ToSingle(floatVals7, 0);
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 hexStringToFloat(hexString) {
var hex = parseInt(hexString, 16);
var sign = hex >> 31 ? -1 : 1;
var exponent = (hex >> 23) & 0xFF;
return sign * (hex & 0x7fffff | 0x800000) * 1.0 / Math.pow(2, 23) * Math.pow(2, (exponent - 127));
}
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;
}