// Decode uplink function.
//
// Input is an object with the following fields:
// - bytes = Byte array containing the uplink payload, e.g. [255, 230, 255, 0]
// - fPort = Uplink fPort.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - output = Object representing the decoded payload.
function decodeUplink(input) {
return {
data: Decode(input.fPort, input.bytes, input.variables)
};
}
function Decode(fPort, bytes) {
switch (fPort) {
case 1:
{
var output = {};
output.Ambient_Temperature = bytes[0] * 0.25;
output.PIR_Status = bytes[1] >> 5 & 0x01;
output.Energy_Storage_Low = bytes[1] >> 4 & 0x01;
output.Radio_Comm_Error = bytes[1] >> 3 & 0x01;
output.Radio_Signal_Strength = bytes[1] >> 2 & 0x01;
output.PIR_Sensor_Failure = bytes[1] >> 1 & 0x01;
output.Ambient_Sensor_Failure = bytes[1] & 0x01;
output.Storage_Voltage = Number((bytes[2] * 0.02).toFixed(2));
output.Relative_SPT_Value = bytes[3];
return { uplink_decoded: output };
}
default:
return {
errors: ['unknown FPort'],
};
}
} |