Copyright Micropelt
MLRTPS Decoder Chirpstack & TTN(The Things Network)
Uplink Decoder
// 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_port_2(input) {
var output = {};
{
var REV_Major = (input[0] & 0xF).toString();
var REV_Minor = ((input[0] >> 4) & 0xF).toString(16);
output.REV = REV_Major + "." + REV_Minor;
}
{
var HW_Major = (input[1] & 0xF).toString();
var HW_Minor = ((input[1] >> 4) & 0xF).toString();
output.HW = HW_Major + "." + HW_Minor;
}
{
var FW_Year = input[2].toString();
var FW_Month = input[3].toString();
var FW_Day = input[4].toString();
var FW_Minor = input[5].toString();
output.FW = "20" + FW_Year + "." + FW_Month + "." + FW_Day + "." + FW_Minor;
}
return output;
}
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 = get_spt_value(bytes);
return { uplink_decoded : output };
}
case 2:
{
var output = {};
output = decode_port_2(input);
return { version: 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 {
};
}