// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// - variables contains the device variables e.g. {“calibration”: “3.5”} (both the key / value are of type string)
// The function must return an object, e.g. {“temperature”: 22.5}
//function Decode(fPort, bytes, variables) {
// return {};
//}
function bytesToFloat(bytes) {
//LSB Format (least significant byte first).
var bits = bytes[3]<<24 | bytes[2]<<16 | bytes[1]<<8 | bytes[0];
var sign = (bits>>>31 === 0) ? 1.0 : -1.0;
var e = bits>>>23 & 0xff;
var m = (e === 0) ? (bits & 0x7fffff)<<1 : (bits & 0x7fffff) | 0x800000;
var f = sign * m * Math.pow(2, e - 150);
return f;
}
function bytesToFloathum(bytes) {
//LSB Format (least significant byte first).
var bits1 = bytes[7]<<24 | bytes[6]<<16 | bytes[5]<<8 | bytes[4];
var sign1 = (bits1>>>31 === 0) ? 1.0 : -1.0;
var e1 = bits1>>>23 & 0xff;
var m1 = (e1 === 0) ? (bits1 & 0x7fffff)<<1 : (bits1 & 0x7fffff) | 0x800000;
var f1 = sign1 * m1 * Math.pow(2, e1 - 150);
return f1;
}
function Decode(fPort, bytes, variables) {
var data = {};
data.temperature = bytesToFloat(bytes.slice(0, 12));
data.temp0 = bytes[0];
data.temp1 = bytes[1];
data.temp2 = bytes[2];
data.temp3 = bytes[3];
data.humidity = bytesToFloathum(bytes.slice(0, 12));
data.hum0 = bytes[4];
data.hum1 = bytes[5];
data.hum2 = bytes[6];
data.hum3 = bytes[7];
return data;
}