//V2.0, 23.12.2021,DS
if (msg.data) {
var decoded = decodeFromHex(msg.data);
for (var i = 0; i < decoded.length; i++) {
decoded[i]['values']['rssi'] = msg.rssi;
decoded[i]['values']['snr'] = msg.snr;
decoded[i]['values']['toa'] = msg.toa;
decoded[i]['values']['frequency'] = msg.frequency;
decoded[i]['values']['dr'] = msg.dr;
decoded[i]['values']['bat'] = decodeBattery(msg.bat);
decoded[i]['values']['hex'] = msg.data;
}
return {
msg: decoded,
metadata: metadata,
msgType: msgType
};
} else {
return {
msg: msg,
metadata: metadata,
msgType: msgType
};
}
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;
}
function decodeFromHex(data) {
var telemetry = [];
if (data.substr(0, 2) == "11") {
var dataString = data.substr(8, 2) + data.substr(6,
2) + data.substr(4, 2) + data.substr(2, 2);
waterMeter = parseInt(dataString, 16) / 1000;
telemetry[0] = {
"ts": msg.ts,
"values": {
"waterMeter": waterMeter
}
};
} else if (data.substr(0, 1) == "c") {
var dataString1 = data.substr(12, 2) + data.substr(
10, 2) + data.substr(8, 2) + data.substr(6,
2);
var dataString2 = data.substr(20, 2) + data.substr(
18, 2) + data.substr(16, 2) + data.substr(
14, 2);
var dataString3 = data.substr(28, 2) + data.substr(
26, 2) + data.substr(24, 2) + data.substr(
22, 2);
var waterMeter1 = parseInt(dataString1, 16) /
1000;
var waterMeter2 = parseInt(dataString2, 16) /
1000;
var waterMeter3 = parseInt(dataString3, 16) /
1000;
telemetry[0] = {
"ts": msg.ts - 7200000,
"values": {
"waterMeter": waterMeter1
}
};
telemetry[1] = {
"ts": msg.ts - 3600000,
"values": {
"waterMeter": waterMeter2
}
};
telemetry[2] = {
"ts": msg.ts,
"values": {
"waterMeter": waterMeter3
}
};
}
return telemetry;
}
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';
}
}