SOURCE

function getCmdId(payload, start, end) {
    var sn = "";
    for (i = start; i <= end; i++) {
        sn += payload[i].toString();
    }
    return sn.toUpperCase();
}
function byteToString(arr) {
    if (typeof arr === 'string') {
        return arr;
    }
    var str = '',
        _arr = arr;
    for (var i = 0; i < _arr.length; i++) {
        var one = _arr[i].toString(2),
            v = one.match(/^1+?(?=0)/);
        if (v && one.length == 8) {
            var bytesLength = v[0].length;
            var store = _arr[i].toString(2).slice(7 - bytesLength);
            for (var st = 1; st < bytesLength; st++) {
                store += _arr[st + i].toString(2).slice(2);
            }
            str += String.fromCharCode(parseInt(store, 2));
            i += bytesLength - 1;
        } else {
            str += String.fromCharCode(_arr[i]);
        }
    }
    return str;
}
function hexToSingle(hexNum) {
    var a = hexNum;
    var b = parseInt(a, 16);
    var s = (b & 0x80000000) ? -1 : 1;
    var e = (b & 0x7f800000) / 0x800000 - 127;
    var c = (b & 0x7fffff) / 0x800000;
    var re = s * (1 + c) * Math.pow(2, e);
    return re;
}
function getReverseHexString(payload, start, length) {
    var str = "";
    var end = start + length
    for (i = end; i >= start; i--) {
        if (payload[i] < 16) {
            str += "0" + payload[i].toString(16);
        } else {
            str += payload[i].toString(16);
        }
    }
    return str.toUpperCase();
}
function get4ByteHexStringValue(hexstring) {
    let hexValue = hexstring;
    let signedValue;

    // Check if the value is negative
    if ((hexValue & 0x8000) === 0x8000) {
        // If it is negative, convert it using two's complement
        signedValue = -((~hexValue + 1) & 0xFFFF);
    } else {
        // If it is positive, just use the value as is
        signedValue = hexValue;
    }
    return signedValue;
}
payload=[255,85,4,170,1,1,0,4,161,162,163,16,0,1,1,0,1,0,0,0,249,163,162,161]
var result = {
    sn: '',
    category: '',
    shadow: {}
};
console.log("拆包前: " + payload);
if (!payload instanceof Array) {
    console.log("payload is not array, return");
    console.log(payload);
    //return result;
}
var shadow = {};
while (true) {
    if (payload.length == 0) {
        break;
    }
    if (payload[0] == 161) {
        if (payload.length < 6) {
            break;
        }
        var sensorType = payload[5];
        console.log("msg sensorType=" + sensorType);
        switch (sensorType) {
            case 1:
                shadow.DoorStatus = payload[8].toString();
                break;
            case 2:
                shadow.WaterStatus = payload[8].toString();
                break;
            case 4:
                var hexStr = getReverseHexString(payload, 8, 3);
                var temperature = hexToSingle(hexStr);
                shadow.Temperature = temperature.toFixed(2);
                hexStr = getReverseHexString(payload, 13, 3);
                var humidness = hexToSingle(hexStr);
                shadow.Humidness = humidness.toFixed(2);
                break;
            case 5:
                var hexStr = getReverseHexString(payload, 8, 3);
                var temperature1 = hexToSingle(hexStr);
                var hexStr2 = getReverseHexString(payload, 13, 3);
                var temperature2 = hexToSingle(hexStr2);
                var hexStr3 = getReverseHexString(payload, 18, 3);
                var temperature3 = hexToSingle(hexStr3);
                var hexStr4 = getReverseHexString(payload, 23, 3);
                var temperature4 = hexToSingle(hexStr4);
                shadow.Temp1 = temperature1.toFixed(2);
                shadow.Temp2 = temperature2.toFixed(2);
                shadow.Temp3 = temperature3.toFixed(2);
                shadow.Temp4 = temperature4.toFixed(2);
                break;
            case 6:
                var xAxisStr = getReverseHexString(payload, 8, 3);
                var xAxis = parseInt('0x'+xAxisStr)
                var yAxisStr = getReverseHexString(payload, 13, 3);
                var yAxis = parseInt('0x'+yAxis)
                var zAxisStr = getReverseHexString(payload, 18, 3);
               var zAxis = parseInt('0x'+zAxisStr)
                shadow.XAxis = 0;
                shadow.YAxis = 0;
                shadow.ZAxis = 0;
                break;
            case 10:
                var hexStr = getReverseHexString(payload, 8, 3);
                var voltage = hexToSingle(hexStr);
                shadow.DCVoltage1 = voltage.toFixed(2);
                hexStr = getReverseHexString(payload, 13, 3);
                var voltage2 = hexToSingle(hexStr);
                shadow.DCVoltage2 = voltage2.toFixed(2);
                hexStr = getReverseHexString(payload, 18, 3);
                var voltage3 = hexToSingle(hexStr);
                shadow.DCVoltage3 = voltage3.toFixed(2);
                break;
            case 13:
                var hexStr = getReverseHexString(payload, 8, 3);
                var voltage = hexToSingle(hexStr);
                shadow.Voltage = voltage.toFixed(2);
                hexStr = getReverseHexString(payload, 13, 3);
                var current = hexToSingle(hexStr);
                shadow.Current = current.toFixed(2);
                hexStr = getReverseHexString(payload, 18, 3);
                var power = hexToSingle(hexStr);
                shadow.Power = power.toFixed(2);
                hexStr = getReverseHexString(payload, 23, 3);
                var freq = hexToSingle(hexStr);
                shadow.Freq = freq.toFixed(2);
                hexStr = getReverseHexString(payload, 28, 3);
                var ep = hexToSingle(hexStr);
                shadow.EffectivePower = ep.toFixed(2);
                hexStr = getReverseHexString(payload, 33, 3);
                var ip = hexToSingle(hexStr);
                shadow.InvalidPower = ip.toFixed(2);
                break;
            case 14:
                var hexStr = getReverseHexString(payload, 8, 3);
                var temperature1 = hexToSingle(hexStr);
                var hexStr2 = getReverseHexString(payload, 13, 3);
                var temperature2 = hexToSingle(hexStr2);
                var hexStr3 = getReverseHexString(payload, 18, 3);
                var temperature3 = hexToSingle(hexStr3);
                var hexStr4 = getReverseHexString(payload, 23, 3);
                var temperature4 = hexToSingle(hexStr4);
                var hexStr5 = getReverseHexString(payload, 28, 3);
                var temperature5 = hexToSingle(hexStr5);
                var hexStr6 = getReverseHexString(payload, 33, 3);
                var temperature6 = hexToSingle(hexStr6);
                var hexStr7 = getReverseHexString(payload, 38, 3);
                var temperature7 = hexToSingle(hexStr7);
                var hexStr8 = getReverseHexString(payload, 43, 3);
                var temperature8 = hexToSingle(hexStr8);
                var hexStr9 = getReverseHexString(payload, 48, 3);
                var temperature9 = hexToSingle(hexStr9);
                var hexStr10 = getReverseHexString(payload, 53, 3);
                var temperature10 = hexToSingle(hexStr10);
                var hexStr11 = getReverseHexString(payload, 58, 3);
                var temperature11 = hexToSingle(hexStr11);
                var hexStr12 = getReverseHexString(payload, 63, 3);
                var temperature12 = hexToSingle(hexStr12);
                var hexStr13 = getReverseHexString(payload, 68, 3);
                var temperature13 = hexToSingle(hexStr13);
                var hexStr14 = getReverseHexString(payload, 73, 3);
                var temperature14 = hexToSingle(hexStr14);
                var hexStr15 = getReverseHexString(payload, 78, 3);
                var temperature15 = hexToSingle(hexStr15);
                var hexStr16 = getReverseHexString(payload, 83, 3);
                var temperature16 = hexToSingle(hexStr16);
                shadow.Temperature1 = temperature1.toFixed(2);
                shadow.Temperature2 = temperature2.toFixed(2);
                shadow.Temperature3 = temperature3.toFixed(2);
                shadow.Temperature4 = temperature4.toFixed(2);
                shadow.Temperature5 = temperature5.toFixed(2);
                shadow.Temperature6 = temperature6.toFixed(2);
                shadow.Temperature7 = temperature7.toFixed(2);
                shadow.Temperature8 = temperature8.toFixed(2);
                shadow.Temperature9 = temperature9.toFixed(2);
                shadow.Temperature10 = temperature10.toFixed(2);
                shadow.Temperature11 = temperature11.toFixed(2);
                shadow.Temperature12 = temperature12.toFixed(2);
                shadow.Temperature13 = temperature13.toFixed(2);
                shadow.Temperature14 = temperature14.toFixed(2);
                shadow.Temperature15 = temperature15.toFixed(2);
                shadow.Temperature16 = temperature16.toFixed(2);
                break;
            case 15:
                shadow.FanSpeed1 = payload[11];
                break;
            case 17:
                shadow.MonitoringBoardVersion = byteToString(payload.slice(11, 16));
                if (payload.length > 28) {
                    shadow.MonitoringBootVersion = byteToString(payload.slice(23, 28));
                }
                break;
        }
        var len = payload[4] * 256 + payload[3];
        payload = payload.slice(len);
    } else {
        var cmdId = getCmdId(payload, 0, 3);
        console.log("msg cmdId=" + cmdId);
        switch (cmdId) {
            case "25585432":
                shadow.AutoBrightnessMode = payload[6].toString();
                payload = payload.slice(8);
                break;
            case "25585457":
                shadow.PowerStatus = payload[6].toString();
                payload = payload.slice(8);
                break;
            case "25585458":
                shadow.TimePeriodAutoBrightnessMode = payload[6].toString();
                payload = payload.slice(8);
                break;
            case "25585464":
                shadow.Brightness = payload[6];
                payload = payload.slice(8);
                break;
            case "25585465":
                shadow.Contrast = payload[6];
                payload = payload.slice(8);
                break;
            case "25585466":
                shadow.InputSource = payload[6].toString();
                payload = payload.slice(8);
                break;
            case "25585467":
                shadow.AudioSwitchStatus = payload[6].toString();
                payload = payload.slice(8);
                break;
            case "25585468":
                shadow.Volume = payload[6];
                payload = payload.slice(8);
                break;
            case "25585481":
                shadow.Sharpness = payload[6];
                payload = payload.slice(8);
                break;
            case "25585482":
                shadow.Gama = payload[6].toString();
                payload = payload.slice(8);
                break;
            case "25585483":
                shadow.RedGain = payload[6];
                payload = payload.slice(8);
                break;
            case "25585484":
                shadow.GreenGain = payload[6];
                payload = payload.slice(8);
                break;
            case "25585485":
                shadow.BlueGain = payload[6];
                payload = payload.slice(8);
                break;
            case "25585486":
                shadow.Backlight = payload[6];
                payload = payload.slice(8);
                break;
            case "25585487":
                shadow.InputSignalStatus = payload[6].toString();
                payload = payload.slice(8);
                break;
            case "255854171":
                shadow.AutoReportSwitchStatus = payload[5].toString();
                payload = payload.slice(8);
                break;
            case "255851560":
                shadow.AmbientLight1 = payload[4] + payload[5] * 256;
                shadow.Backlight1 = payload[6];
                shadow.AmbientLight2 = payload[7] + payload[8] * 256;
                shadow.Backlight2 = payload[9];
                shadow.AmbientLight3 = payload[10] + payload[11] * 256;
                shadow.Backlight3 = payload[12];
                shadow.AmbientLight4 = payload[13] + payload[14] * 256;
                shadow.Backlight4 = payload[15];
                shadow.AmbientLight5 = payload[16] + payload[17] * 256;
                shadow.Backlight5 = payload[18];
                payload = payload.slice(20);
                break;
            case "255851540":
                var config = {}
                config.h1 = payload[4];
                config.m1 = payload[5];
                config.backlight1 = payload[6];

                config.h2 = payload[7];
                config.m2 = payload[8];
                config.backlight2 = payload[9];

                config.h3 = payload[10];
                config.m3 = payload[11];
                config.backlight3 = payload[12];

                config.h4 = payload[13];
                config.m4 = payload[14];
                config.backlight4 = payload[15];

                config.h5 = payload[16];
                config.m5 = payload[17];
                config.backlight5 = payload[18];

                shadow.TimePeriodAutoBrightnessConfig = config
                payload = payload.slice(20);
                break;
            case "255855168":
                //sensor data
                var type = payload[4];
                var switchStatus = payload[5].toString();
                var reportCycle = (payload[7] << 8) + payload[6];
                switch (type) {
                    case 1:
                        shadow.DoorOpenAutoReportSwitch = switchStatus;
                        shadow.DoorOpenAutoReportCycle = reportCycle;
                        break;
                    case 2:
                        shadow.WaterSendorAutoReportSwitch = switchStatus;
                        shadow.WaterSendorAutoReportCycle = reportCycle;
                        break;
                    case 6:
                        shadow.GSendorAutoReportSwitch = switchStatus;
                        shadow.GSensorAutoReportCycle = reportCycle;
                        break;
                    case 10:
                        shadow.DCPowerAutoReportSwitch = switchStatus;
                        shadow.DCPowerAutoReportCycle = reportCycle;
                        break;
                    case 14:
                        shadow.TemperatureSendorAutoReportSwitch = switchStatus;
                        shadow.TemperatureSendorAutoReportCycle = reportCycle;
                        break;
                    case 15:
                        shadow.FanSpeedAutoReportSwitch = switchStatus;
                        shadow.FanSpeedAutoReportCycle = reportCycle;
                        break;
                }
                payload = payload.slice(9);
                break;
            default:
                //shadow.ModelName = "ZGS126";
                // return {};
        }
        payload=payload.slice(8);
        console.log(111)
    }
    console.log("拆包后: " + payload)
}
result.shadow = shadow;
console.log(shadow)
console 命令行工具 X clear

                    
>
console