/** * Payload Decoder for Milesight Network Server * * Copyright 2023 Milesight IoT * * @product WT101 */ 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; } // TEMPERATURE else if (channel_id === 0x03 && channel_type === 0x67) { decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10; i += 2; } // TEMPERATURE TARGET else if (channel_id === 0x04 && channel_type === 0x67) { decoded.temperature_target = readInt16LE(bytes.slice(i, i + 2)) / 10; i += 2; } // VALVE OPENING else if (channel_id === 0x05 && channel_type === 0x92) { decoded.valve_opening = readUInt8(bytes[i]); i += 1; } // INSTALLATION STATUS else if (channel_id === 0x06 && channel_type === 0x00) { decoded.tamper_status = bytes[i] === 0 ? "installed" : "uninstalled"; i += 1; } // FEENSTRATION STATUS else if (channel_id === 0x07 && channel_type === 0x00) { decoded.window_detection = bytes[i] === 0 ? "normal" : "open"; i += 1; } // MOTOR STORKE CALIBRATION STATUS else if (channel_id === 0x08 && channel_type === 0xe5) { decoded.motor_calibration_result = readMotorCalibration(bytes[i]); i += 1; } // MOTOR STROKE else if (channel_id === 0x09 && channel_type === 0x90) { decoded.motor_storke = readUInt16LE(bytes.slice(i, i + 2)); i += 2; } // FROST PROTECTION else if (channel_id === 0x0a && channel_type === 0x00) { decoded.freeze_protection = bytes[i] === 0 ? "normal" : "triggered"; i += 1; } // MOTOR CURRENT POSTION else if (channel_id === 0x0b && channel_type === 0x90) { decoded.motor_position = readUInt16LE(bytes.slice(i, i + 2)); i += 2; } else { break; } } return decoded; } function readUInt8(bytes) { return bytes & 0xff; } function readInt8(bytes) { var ref = readUInt8(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 readMotorCalibration(type) { switch (type) { case 0x00: return "success"; case 0x01: return "fail: out of range"; case 0x02: return "fail: uninstalled"; case 0x03: return "calibration cleared"; case 0x04: return "temperature control disabled"; default: return "unknown"; } }