/** * Payload Encoder * * Copyright 2025 Milesight IoT * * @product WS503 */ var RAW_VALUE = 0x00; // Chirpstack v4 function encodeDownlink(input) { var encoded = milesightDeviceEncode(input.data); return { bytes: encoded }; } // Chirpstack v3 function Encode(fPort, obj) { return milesightDeviceEncode(obj); } // The Things Network function Encoder(obj, port) { return milesightDeviceEncode(obj); } function milesightDeviceEncode(payload) { var encoded = []; if ("reboot" in payload) { encoded = encoded.concat(reboot(payload.reboot)); } if ("report_interval" in payload) { encoded = encoded.concat(setReportInterval(payload.report_interval)); } if ("switch_1" in payload || "switch_2" in payload || "switch_3" in payload) { encoded = encoded.concat(updateSwitch(payload)); } return encoded; } /** * reboot * @param {number} reboot values: (0: no, 1: yes) * @example { "reboot": 1 } */ function reboot(reboot) { var yes_no_map = { 0: "no", 1: "yes" }; var yes_no_values = getValues(yes_no_map); if (yes_no_values.indexOf(reboot) === -1) { throw new Error("reboot must be one of: " + yes_no_values.join(", ")); } if (getValue(yes_no_map, reboot) === 0) { return []; } return [0xff, 0x10, 0xff]; } /** * report interval configuration * @param {number} report_interval uint: second, range: [60, 64800] * @example { "report_interval": 1200 } */ function setReportInterval(report_interval) { if (typeof report_interval !== "number") { throw new Error("report_interval must be a number"); } if (report_interval < 60 || report_interval > 64800) { throw new Error("report_interval must be in the range of [60, 64800]"); } var buffer = new Buffer(4); buffer.writeUInt8(0xff); buffer.writeUInt8(0x03); buffer.writeUInt16LE(report_interval); return buffer.toBytes(); } /** * button control * @param {number} switch_1 values: (0: off, 1: on) * @param {number} switch_2 values: (0: off, 1: on) * @param {number} switch_3 values: (0: off, 1: on) * @example { "switch_1": 1 } */ function updateSwitch(switch_data) { var data = 0x00; var switch_bit_offset = { switch_1: 0, switch_2: 1, switch_3: 2 }; for (var key in switch_bit_offset) { if (key in switch_data) { data |= 1 << (switch_bit_offset[key] + 4); data |= switch_data[key] << switch_bit_offset[key]; } } var buffer = new Buffer(3); buffer.writeUInt8(0xff); buffer.writeUInt8(0x29); buffer.writeUInt8(data); return buffer.toBytes(); } function getValues(map) { var values = []; if (RAW_VALUE) { for (var key in map) { values.push(parseInt(key)); } } else { for (var key in map) { values.push(map[key]); } } return values; } function getValue(map, value) { if (RAW_VALUE) return value; for (var key in map) { if (map[key] === value) { return parseInt(key); } } throw new Error("not match in " + JSON.stringify(map)); } function Buffer(size) { this.buffer = new Array(size); this.offset = 0; for (var i = 0; i < size; i++) { this.buffer[i] = 0; } } Buffer.prototype._write = function (value, byteLength, isLittleEndian) { var offset = 0; for (var index = 0; index < byteLength; index++) { offset = isLittleEndian ? index << 3 : (byteLength - 1 - index) << 3; this.buffer[this.offset + index] = (value >> offset) & 0xff; } }; Buffer.prototype.writeUInt8 = function (value) { this._write(value, 1, true); this.offset += 1; }; Buffer.prototype.writeInt8 = function (value) { this._write(value < 0 ? value + 0x100 : value, 1, true); this.offset += 1; }; Buffer.prototype.writeUInt16LE = function (value) { this._write(value, 2, true); this.offset += 2; }; Buffer.prototype.writeInt16LE = function (value) { this._write(value < 0 ? value + 0x10000 : value, 2, true); this.offset += 2; }; Buffer.prototype.writeUInt32LE = function (value) { this._write(value, 4, true); this.offset += 4; }; Buffer.prototype.writeInt32LE = function (value) { this._write(value < 0 ? value + 0x100000000 : value, 4, true); this.offset += 4; }; Buffer.prototype.toBytes = function () { return this.buffer; };