STS_RR_R125/as923_1_jp.js

105 lines
2.8 KiB
JavaScript

// 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) {
// O5 Door contact sensor
if (fPort === 4) {
return [
{
led_state: bytes[0]===0 ? "Off":"On",
mtm_code_1: bytes[1],
mtm_code_2: bytes[2],
hw_code: bytes[3],
battery_level: bytes[4]+"%",
size_value: bytes[5],
door_state: bytes[6] === 1 ? "Closed": "Open",
}
];
}
// heart-beat of O5
else if (fPort === 5) {
return [
{
led_state: (bytes[0] & 0x7f) === 0 ? "Off" : "On",
battery_level: bytes[1] + " %",
}
];
}
// R4 soap/sanitizer sensor
else if (fPort === 7) {
return [
{
led_state: bytes[0]===0 ? "Off":"On",
mtm_code_1: bytes[1],
mtm_code_2: bytes[2],
hw_code: bytes[3],
battery_level: bytes[4]+"%",
size_value: bytes[5],
measure_tech:bytes[6] === 0? "Capacitive":"Other",
liquid_level_event: bytes[7] === 0? "Liquid Detected":"No Liquid",
}
];
}
// R4 soap/sanitizer heart-beat
else if (fPort === 8) {
return [
{
led_state: (bytes[0] & 0x7f) === 0 ? "Off" : "On",
battery_level: bytes[1] + " %",
}
];
}
// R1D dual roll toilet paper sensor
else if (fPort === 57) {
return [
{
led_state:bytes[0]===0 ? "Off":"On",
mtm_code_1:bytes[1],
mtm_code_2:bytes[2],
hw_code:bytes[3],
battery_level:bytes[4]+"%",
size_value:bytes[5],
distance_1_mm:bytes[6]*256+bytes[7],
distance_2_mm:bytes[8]*256+bytes[9],
distance_unit: "mm",
}
];
}
// R1D, Heart-beat
else if (fPort === 58) {
return [
{
led_state: (bytes[0] & 0x7f) === 0 ? "Off" : "On",
battery_level: bytes[1] + " %",
}
];
}
// R5 waste bin sensor
else if (fPort === 11) {
return [
{
led_state:bytes[0]===0 ? "Off":"On",
mtm_code_1:bytes[1],
mtm_code_2:bytes[2],
hw_code:bytes[3],
battery_level:bytes[4]+"%",
size_value:bytes[5],
distance_mm: bytes[6]*256 + bytes[7],
distance_unit: "mm",
}
];
}
// R5, Heart-beat
else if (fPort === 12) {
return [
{
led_state: (bytes[0] & 0x7f) === 0 ? "Off" : "On",
battery_level: bytes[1] + " %",
}
];
}
}