Ursalink (Milesight) UC1122 Decoder - Help needed

Ursalink (Milesight) UC1122 Decoder - Help needed

@Kiran

I have the following ‘standard’ decoder that works in TTN but trying to get it to work in Tago’s payload parser does not convert the payload into the buckets for dout1 etc.

I know almost zero about javascript and JSON - the end goal is to create a connector for this device into my dashboard.

Can anybody offer any assistance? Thanks.

//UC1122 Decoder
function Decoder(bytes, port) {
var decoded = {};

for (i = 0; i < bytes.length;) {
    // GPIO INPUT 1
    if (bytes[i] == 0x01) {
        decoded.din1 = bytes[i + 2] === 0 ? "0" : "1";
        i += 3;
        continue;
    }

    // GPIO INPUT 2
    if (bytes[i] == 0x02) {
        decoded.din2 = bytes[i + 2] === 0 ? "0" : "1";
        i += 3;
        continue;
    }

    // GPIO OUTPUT 1
    if (bytes[i] == 0x09) {
        decoded.dout1 = bytes[i + 2] === 0 ? "0" : "1";
        i += 3;
        continue;
    }

    // GPIO OUTPUT 2
    if (bytes[i] == 0x0a) {
        decoded.dout2 = bytes[i + 2] === 0 ? "0" : "1";
        i += 3;
        continue;
    }

    // ADC OUTPUT 1
    if (bytes[i] == 0x11) {
        decoded.ain1_cur = readInt16LE(bytes.slice(i + 2, i + 4)) / 100;
        decoded.ain1_min = readInt16LE(bytes.slice(i + 4, i + 6)) / 100;
        decoded.ain1_max = readInt16LE(bytes.slice(i + 6, i + 8)) / 100;
        decoded.ain1_avg = readInt16LE(bytes.slice(i + 8, i + 10)) / 100;
        i += 10;
        continue;
    }

    // ADC OUTPUT 2
    if (bytes[i] == 0x12) {
        decoded.ain2_cur = readInt16LE(bytes.slice(i + 2, i + 4)) / 100;
        decoded.ain2_min = readInt16LE(bytes.slice(i + 4, i + 6)) / 100;
        decoded.ain2_max = readInt16LE(bytes.slice(i + 6, i + 8)) / 100;
        decoded.ain2_avg = readInt16LE(bytes.slice(i + 8, i + 10)) / 100;
        i += 10;
        continue;
    }

}

return decoded;

}

/* ******************************************

  • bytes to number
    ********************************************/
    function readUInt8LE(bytes) {
    return (bytes & 0xFF);
    }

function readInt8LE(bytes) {
var ref = readUInt8LE(bytes);
return (ref > 0x7F) ? ref - 0x100 : ref;
}

function readUInt16LE(bytes) {
var value = (bytes[1] << 8) + bytes[0];
return (value & 0xFFFF);
}

function readInt16LE(bytes) {
var ref = readUInt16LE(bytes);
return (ref > 0x7FFF) ? ref - 0x10000 : ref;
}

function readUInt32LE(bytes) {
var value = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];
return (value & 0xFFFFFFFF);
}

function readInt32LE(bytes) {
var ref = readUInt32LE(bytes);
return (ref > 0x7FFFFFFF) ? ref - 0x100000000 : ref;
}

function readFloatLE(bytes) {
// JavaScript bitwise operators yield a 32 bits integer, not a float.
// Assume LSB (least significant byte first).
var bits = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
var sign = (bits >>> 31 === 0) ? 1.0 : -1.0;
var e = bits >>> 23 & 0xff;
var m = (e === 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;
var f = sign * m * Math.pow(2, e - 150);
return f;
}