/** * Payload Decoder for Milesight Network Server * * Copyright 2023 Milesight IoT * * @product WS302 */ function Decode(fPort, bytes) { return milesight(bytes); } function milesight(bytes) { var decoded = {}; for (var i = 0; i < bytes.length; ) { var channel_id = bytes[i++]; var channel_type = bytes[i++]; // BATTERY if (channel_id === 0x01 && channel_type === 0x75) { decoded.battery = bytes[i]; i += 1; } // SOUND else if (channel_id === 0x05 && channel_type === 0x5b) { decoded.freq_weight = readFrequecyWeightType(bytes[i]); decoded.time_weight = readTimeWeightType(bytes[i]); decoded.la = readUInt16LE(bytes.slice(i + 1, i + 3)) / 10; decoded.laeq = readUInt16LE(bytes.slice(i + 3, i + 5)) / 10; decoded.lamax = readUInt16LE(bytes.slice(i + 5, i + 7)) / 10; i += 7; } // LoRaWAN Class Type else if (channel_id === 0xff && channel_type === 0x0f) { switch (bytes[i]) { case 0: decoded.class_type = "class-a"; break; case 1: decoded.class_type = "class-b"; break; case 2: decoded.class_type = "class-c"; break; } i += 1; } else { break; } } return decoded; } function readFrequecyWeightType(bytes) { var type = ""; var bits = bytes & 0x03; switch (bits) { case 0: type = "Z"; break; case 1: type = "A"; break; case 2: type = "C"; break; } return type; } function readTimeWeightType(bytes) { var type = ""; var bits = (bytes[0] >> 2) & 0x03; switch (bits) { case 0: type = "impulse"; break; case 1: type = "fast"; break; case 2: type = "slow"; break; } return type; } 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; }