Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
// 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 get_spt_value(input) {
  var spt_value = input[3];
  switch (spt_value) {
    case 0:
      return "0";
    case 1:
      return "+1";
    case 2:
      return "+2";
    case 3:
      return "+3";
    case 4:
      return "+4";
    case 5:
      return "+5";
    case 12:
      return "-4";
    case 13:
      return "-3";
    case 14:
      return "-2";
    case 15:
      return "-1";
    case 255:
      return "Freeze Protect";
    default:
      return "Invalid SPT value";      
  }
}

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]get_spt_value(bytes);

                return { uplink_decoded: output };
            }
        default:
            return {
                errors: ['unknown FPort'],
            };
    }

}

// Encode downlink function.
//
// Input is an object with the following fields:
// - output = Object representing the payload that must be encoded.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - bytes = Byte array containing the downlink payload.
function encodeDownlink(input) {
    return {
    };
}