/** * Payload Decoder * * Copyright 2025 Milesight IoT * * @product WS503 v1 */ 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++]; // IPSO VERSION if (channel_id === 0xff && channel_type === 0x01) { decoded.ipso_version = readProtocolVersion(bytes[i]); i += 1; } // HARDWARE VERSION else if (channel_id === 0xff && channel_type === 0x09) { decoded.hardware_version = readHardwareVersion(bytes.slice(i, i + 2)); i += 2; } // FIRMWARE VERSION else if (channel_id === 0xff && channel_type === 0x0a) { decoded.firmware_version = readFirmwareVersion(bytes.slice(i, i + 2)); i += 2; } // DEVICE STATUS else if (channel_id === 0xff && channel_type === 0x0b) { decoded.device_status = 1; i += 1; } // LORAWAN CLASS TYPE else if (channel_id === 0xff && channel_type === 0x0f) { decoded.lorawan_class = bytes[i]; i += 1; } // SERIAL NUMBER else if (channel_id === 0xff && channel_type === 0x08) { decoded.sn = readSerialNumber(bytes.slice(i, i + 6)); i += 6; } // SWITCH STATE else if (channel_id === 0xff && channel_type === 0x29) { // payload (0 0 0 0 0 0 0 0) // Switch 3 2 1 3 2 1 // ------- ------- // bit mask change state decoded.switch_1 = bytes[i] & 1; decoded.switch_1_change = (bytes[i] >> 4) & 1; decoded.switch_2 = (bytes[i] >> 1) & 1; decoded.switch_2_change = (bytes[i] >> 5) & 1; decoded.switch_3 = (bytes[i] >> 2) & 1; decoded.switch_3_change = (bytes[i] >> 6) & 1; i += 1; } else { break; } } return decoded; } 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 readFirmwareVersion(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(""); }