Copyright Micropelt

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

// 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'],
            };
    }

}
  • No labels