[
{
"id": "8696d997d6705a77",
"type": "mqtt in",
"z": "dc7ab9e9cd303614",
"name": "ew11-in",
"topic": "ew11/data",
"qos": "0",
"datatype": "buffer",
"broker": "9e9d11862399ce26",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 70,
"y": 680,
"wires": [
[
"d0afcc36a99fdea7"
]
]
},
{
"id": "d0afcc36a99fdea7",
"type": "function",
"z": "dc7ab9e9cd303614",
"name": "解析4个客厅灯开关",
"func": "let payload = msg.payload;\n\n// 如果是 Buffer,先转为十六进制字符串\nif (Buffer.isBuffer(payload)) {\n // 每个字节转成两位十六进制,用空格分隔\n payload = Array.from(payload).map(b => b.toString(16).padStart(2,'0')).join(' ').toUpperCase();\n}\n\n// 如果是字符串,去掉尖括号\nelse if (typeof payload === 'string') {\n payload = payload.replace(/^<|>$/g, '').trim().toUpperCase();\n}\n\n// 否则不是可处理的格式\nelse {\n node.warn("Unsupported payload type: " + typeof payload);\n return null;\n}\n\n// 转为数组\nlet data = payload.split(/\\s+/).map(b => parseInt(b,16));\n\n// 定义报文头(16字节示例)\nconst headerPattern = [0x02,0x19,0x01,0x08,0x00,0xC2,0x3D,0x03,\n 0x02,0x19,0x01,0x0D,0x40,0xC2,0x00,0x00,0x04];\n\n// 判断报文是否匹配头部\nlet isMatch = true;\nfor (let i = 0; i < headerPattern.length; i++) {\n if (data[i] !== headerPattern[i]) {\n isMatch = false;\n break;\n }\n}\n\nif (!isMatch) return null;\n\n// 状态字节在 0x04 后\nlet statusByte = data[headerPattern.length];\n\n// 灯1~4按位解析\nlet lights = {\n dingning_light_1: (statusByte & 0x80) ? "turn_on" : "turn_off",\n dingning_light_2: (statusByte & 0x40) ? "turn_on" : "turn_off",\n dingning_light_3: (statusByte & 0x20) ? "turn_on" : "turn_off",\n dingning_light_4: (statusByte & 0x10) ? "turn_on" : "turn_off"\n};\n\nmsg.payload = lights;\n\nreturn msg;\n",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 150,
"y": 620,
"wires": [
[
"bb5f964b6f17dcc8"
]
]
},
{
"id": "9e9d11862399ce26",
"type": "mqtt-broker",
"name": "",
"broker": "192.168.1.2",
"port": 1883,
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": 4,
"keepalive": 60,
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]