// Décodeur LoRaWAN officiel : Milesight UC100 Series IoT Controller (Milesight) // Source : https://github.com/TheThingsNetwork/lorawan-devices/blob/master/vendor/milesight-iot/uc100.js // The Things Network Device Repository, fichier publié par le constructeur, distribué sans modification. // Format : decodeUplink(input) (TTN v3 / ChirpStack v4). Hébergé par smartbuildings.fr. function decodeUplink(input) { var res = Decoder(input.bytes, input.fPort); if (res.error) { return { errors: [res.error], }; } return { data: res, }; } /** * Payload Decoder for The Things Network * * Copyright 2022 Milesight IoT * * @product UC100 series */ function Decoder(bytes, port) { var decoded = {}; for (i = 0; i < bytes.length;) { var channel_id = bytes[i++]; var channel_type = bytes[i++]; // POWER STATE if (channel_id === 0xff && channel_type === 0x0b) { decoded.power = "on"; i += 1; } // IPSO VERSION else if (channel_id === 0xff && channel_type === 0x01) { decoded.ipso_version = readProtocolVersion(bytes[i]); i += 1; } // PRODUCT SERIAL NUMBER else if (channel_id === 0xff && channel_type === 0x16) { decoded.sn = readSerialNumber(bytes.slice(i, i + 8)); i += 8; } // HARDWARE VERSION else if (channel_id === 0xff && channel_type === 0x09) { decoded.hardware_version = readHardwareVersion(bytes.slice(i, i + 2)); i += 2; } // SOFTWARE VERSION else if (channel_id === 0xff && channel_type === 0x0a) { decoded.software_version = readSoftwareVersion(bytes.slice(i, i + 2)); i += 2; } // METRICS DATA REPORT else if (channel_id === 0xff && channel_type === 0x19) { var modbus_chn_id = bytes[i++] + 1; var data_length = bytes[i++]; var data_type = bytes[i++]; var chn = "chn" + modbus_chn_id; switch (data_type) { case 0: decoded[chn] = bytes[i] ? "on" : "off"; i += 1; break; case 1: decoded[chn] = bytes[i]; i += 1; break; case 2: case 3: decoded[chn] = readUInt16LE(bytes.slice(i, i + 2)); i += 2; break; case 4: case 6: case 8: case 9: case 10: case 11: decoded[chn] = readUInt32LE(bytes.slice(i, i + 4)); i += 4; break; case 5: case 7: decoded[chn] = readFloatLE(bytes.slice(i, i + 4)); i += 4; break; } } // MODBUS READ ERROR else if (channel_id === 0xff && channel_type === 0x15) { var modbus_chn_id = bytes[i] + 1; var channel_name = "channel_" + modbus_chn_id + "_error"; decoded[channel_name] = true; i += 1; } else { break; } } 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; } function readProtocolVersion(bytes) { var major = (bytes & 0xf0) >> 4; var minor = bytes & 0x0f; return "v" + major + "." + minor; } function readHardwareVersion(bytes) { var major = bytes[0] & 0xff; var minor = (bytes[1] & 0xff) >> 4; return "v" + major + "." + minor; } function readSoftwareVersion(bytes) { var major = bytes[0] & 0xff; var minor = bytes[1] & 0xff; return "v" + major + "." + minor; } function readSerialNumber(bytes) { var temp = []; for (var idx = 0; idx < bytes.length; idx++) { temp.push(("0" + (bytes[idx] & 0xff).toString(16)).slice(-2)); } return temp.join(""); }