/** * Payload Decoder for The Things Network * * Copyright 2023 Milesight IoT * * @product EM300-MLD */ function Decoder(bytes, port) { 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; } // LEAKAGE STATUS else if (channel_id === 0x05 && channel_type === 0x00) { decoded.leakage_status = bytes[i] === 0 ? "normal" : "leak"; i += 1; } // TEMPERATURE, HUMIDITY & LEAKAGE STATUS HISTROY else if (channel_id === 0x20 && channel_type === 0xce) { var point = {}; point.timestamp = readUInt32LE(bytes.slice(i, i + 4)); point.leakage_status = bytes[i + 7] === 0 ? "normal" : "leak"; decoded.history = decoded.history || []; decoded.history.push(point); i += 8; } else { break; } } return decoded; } /* ****************************************** * bytes to number ********************************************/ 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) >>> 0; }