Hello, I would like to know if it’s possible to have 3 decoder functions using an IF THEN ELSE IF structure, within one code block
.
I have LoRa sensors sending pulse counts, that are being decoded using this existing and working decoder. Within the same TTN Application I also have 2 LPN Modbus brigdes from Comtac, one with 2 registers and one with 10 registers, both need their own custom decoder. Can this decoder be expanded and check for the format of the raw payload to determine which of the 3 decoder functions needs to be applied on said payload?
If that can be done, how would this decoder have to be edited? (leaving space for the 2 custom ones). I absolutely want to avoid using 3 separate applications in TTN.
function Decoder(bytes, port) {
if (bytes[0] == 12){
var pulse1 = (bytes[3] << 24) + (bytes[4] << 16) + (bytes[5] << 8) + (bytes[6]);
}
if (bytes[0] == 13){
var pulse2 = (bytes[3] << 24) + (bytes[4] << 16) + (bytes[5] << 8) + (bytes[6]);
}
if (bytes[0] === 0){
var battery = (bytes[1])
if (battery > 100){
battery = 100;
var empty = 0;
}
else if (battery < 68){
empty = 1;
}
else empty = 0;
}
return{
pulse1: pulse1,
pulse2: pulse2,
batterylow: empty,
batterystate: battery
}
}