|
本帖最后由 leonarddo 于 2022-8-13 06:18 编辑
由于家里使用的tp的易展mesh路由,研究了一下如何用路由实现一个室内定位的效果。做了一个demo出来,抛砖引玉,能实现基本需求。第一次使用node-red,语法或逻辑有什么问题还请指出。如图,画圈部分按需进行修改后即可使用。
图中画圈的三个部分根据实际情况修改
所需额外组件:
homeassistant额外需要安装的组件
Node-RED Companion
node-red使用中额外需要安装的组件
node-red-contrib-loop
node-red-contrib-home-assistant-websocket
实现代码部分:
将该部分复制后导入,再根据实际情况修改即可。
[{"id":"6d323e565cdb008c","type":"function","z":"88f6c9b0caa7d564","name":"","func":"encrypt_password = encrypt_pwd(msg.password);\nheaders = {'Content-Type': 'application/json; charset=UTF-8'};\npayload = {'method':'do','login':{'password':encrypt_password}};\nurl='http://'+msg.ip+'/';\nmsg.url=url;\nmsg.headers = {};\nmsg.headers['Content-Type'] = 'application/json; charset=UTF-8';\nmsg.payload = payload;\n\nfor (const key in msg.room) {\n msg.room[key]=msg.room[key].toLowerCase();\n msg.room[key]=msg.room[key].replace(/:/g, '-')\n}\nfor (const key in msg.user) {\n msg.user[key]=msg.user[key].toLowerCase();\n msg.user[key]=msg.user[key].replace(/:/g, '-')\n}\n\nreturn msg;\n\nfunction encrypt_pwd(password){\n input1 = \"RDpbLfCPsJZ7fiv\";\n input3 = \"yLwVl0zKqws7LgKPRQ84Mdt708T1qQ3Ha7xv3H7NyU84p21BriUWBU43odz3iP4rBL3cD02KZciXTysVXiV8ngg6vL48rPJyAUw0HurW20xqxv9aYb4M9wK1Ae0wlro510qXeU07kV57fQMc8L6aLgMLwygtc0F10a0Dg70TOoouyFhdysuRMO51yY5ZlOZZLEal1h0t9YQW0Ko7oBwmCAHoic4HYbUyVeU3sfQ1xtXcPcf1aT303wAQhv66qzW\";\n len1 = input1.length;\n len2 = msg.password.length;\n dictionary = input3;\n lenDict = dictionary.length;\n output = '';\n if (len1 > len2){\n length = len1;\n }else{\n length = len2;\n }\n index = 0;\n while (index < length){\n cl = 187;\n cr = 187;\n if (index >= len1){\n cr = password[index].charCodeAt(0);\n }else if (index >= len2){\n cl = input1[index].charCodeAt(0);\n }else{\n cl = input1[index].charCodeAt(0);\n cr = password[index].charCodeAt(0);\n }\n index += 1;\n output = output + String.fromCharCode((dictionary[cl^cr]).charCodeAt(0) % lenDict);\n }\n return output;\n}\n\n\n","outputs":1,"noerr":51,"initialize":"","finalize":"","libs":[],"x":370,"y":220,"wires":[["84218d326015b8c4"]]},{"id":"aba4edad6372bed8","type":"inject","z":"88f6c9b0caa7d564","name":"","props":[{"p":"password","v":"你的密码","vt":"str"},{"p":"ip","v":"路由器ip","vt":"str"},{"p":"room","v":"{\"房间1\":\"房间1路由的mac\",\"房间2\":\"aa-bb-cc-dd-ee-ff\"}","vt":"json"},{"p":"user","v":"{\"设备1\":\"设备1的mac\",\"设备2\":\"设备2的mac\",\"设备3\":\"设备3的mac\"}","vt":"json"}],"repeat":"4","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":250,"y":220,"wires":[["6d323e565cdb008c"]]},{"id":"de8511021b63a935","type":"debug","z":"88f6c9b0caa7d564","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":480,"y":140,"wires":[]},{"id":"84218d326015b8c4","type":"http request","z":"88f6c9b0caa7d564","name":"stok","method":"POST","ret":"obj","paytoqs":"body","url":"","tls":"","persist":true,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":490,"y":220,"wires":[["608954138b51145f"]]},{"id":"b978485d3e5bed1f","type":"function","z":"88f6c9b0caa7d564","name":"","func":"stok=msg.payload.stok;\npayload = {\"hosts_info\":{\"table\":\"host_info\"},\"method\":\"get\"};\nurl='http://'+msg.ip+'/stok='+stok+'/ds';\nmsg.url=url;\nmsg.headers = {};\nmsg.headers['Content-Type'] = 'application/json; charset=UTF-8';\nmsg.payload = payload\nreturn msg;","outputs":1,"noerr":6,"initialize":"","finalize":"","libs":[],"x":730,"y":220,"wires":[["d43f2cae1aec66a2"]]},{"id":"d43f2cae1aec66a2","type":"http request","z":"88f6c9b0caa7d564","name":"获取","method":"POST","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":850,"y":220,"wires":[["de9dedbc7485f3df"]]},{"id":"608954138b51145f","type":"switch","z":"88f6c9b0caa7d564","name":"校验","property":"payload.error_code","propertyType":"msg","rules":[{"t":"else"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":220,"wires":[[],["b978485d3e5bed1f"]]},{"id":"5163aec3ea30afad","type":"function","z":"88f6c9b0caa7d564","name":"","func":"for (const key in msg.payload) {\n var obj=msg.payload[key];\n for (const key2 in msg.room) {\n if(obj.parent_mac==msg.room[key2]){\n obj.room=key2;\n }\n }\n msg.payload=obj;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":340,"wires":[["de9dedbc7485f3df","6bec9ac4d4dee396"]]},{"id":"6bec9ac4d4dee396","type":"switch","z":"88f6c9b0caa7d564","name":"","property":"payload.mac","propertyType":"msg","rules":[{"t":"eq","v":"user[\"设备1\"]","vt":"msg"},{"t":"eq","v":"user[\"设备2\"]","vt":"msg"},{"t":"eq","v":"user[\"设备3\"]","vt":"msg"}],"checkall":"true","repair":false,"outputs":3,"x":410,"y":340,"wires":[["de8511021b63a935","599d45cfb1e0d5be","bd3d10759739bcd5"],["2e78ae9e20e1529e","5070d379d2abf55e"],["71a21cf85e38771f","26b55712ea92ea85"]]},{"id":"599d45cfb1e0d5be","type":"function","z":"88f6c9b0caa7d564","name":"","func":"msg.payload.room='not_home';\nmsg.delay=5;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":280,"wires":[["03b6af81eb0170aa"]]},{"id":"03b6af81eb0170aa","type":"trigger","z":"88f6c9b0caa7d564","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"30","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":680,"y":280,"wires":[["bd3d10759739bcd5"]]},{"id":"2e78ae9e20e1529e","type":"function","z":"88f6c9b0caa7d564","name":"","func":"msg.payload.room='not_home';\nmsg.delay=5;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":320,"wires":[["0105021c6bc4635c"]]},{"id":"0105021c6bc4635c","type":"trigger","z":"88f6c9b0caa7d564","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"30","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":680,"y":320,"wires":[["5070d379d2abf55e"]]},{"id":"71a21cf85e38771f","type":"function","z":"88f6c9b0caa7d564","name":"","func":"msg.payload.room='not_home';\nmsg.delay=5;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":360,"wires":[["f6545760de247fe4"]]},{"id":"f6545760de247fe4","type":"trigger","z":"88f6c9b0caa7d564","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"30","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":680,"y":360,"wires":[["26b55712ea92ea85"]]},{"id":"de9dedbc7485f3df","type":"loop","z":"88f6c9b0caa7d564","name":"test","kind":"enum","count":"","initial":"1","step":"1","condition":"","conditionType":"js","when":"before","enumeration":"payload.hosts_info.host_info","enumerationType":"msg","limit":"","loopPayload":"loop-val","finalPayload":"final-orig","x":270,"y":280,"wires":[[],["5163aec3ea30afad"]]},{"id":"bd3d10759739bcd5","type":"ha-entity","z":"88f6c9b0caa7d564","name":"设备1","server":"59488b0fdf1eaf6e","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"设备1"},{"property":"device_class","value":""},{"property":"icon","value":"mdi-cellphone"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.room","stateType":"msg","attributes":[{"property":"room","value":"payload.room","valueType":"msg"},{"property":"ip","value":"payload.ip","valueType":"msg"},{"property":"mac","value":"payload.mac","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":810,"y":280,"wires":[[]]},{"id":"5070d379d2abf55e","type":"ha-entity","z":"88f6c9b0caa7d564","name":"设备2","server":"59488b0fdf1eaf6e","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"设备2"},{"property":"device_class","value":""},{"property":"icon","value":"mdi-cellphone"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.room","stateType":"msg","attributes":[{"property":"room","value":"payload.room","valueType":"msg"},{"property":"ip","value":"payload.ip","valueType":"msg"},{"property":"mac","value":"payload.mac","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":810,"y":320,"wires":[[]]},{"id":"26b55712ea92ea85","type":"ha-entity","z":"88f6c9b0caa7d564","name":"设备3","server":"59488b0fdf1eaf6e","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"设备3"},{"property":"device_class","value":""},{"property":"icon","value":"mdi-cellphone"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.room","stateType":"msg","attributes":[{"property":"room","value":"payload.room","valueType":"msg"},{"property":"ip","value":"payload.ip","valueType":"msg"},{"property":"mac","value":"payload.mac","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":810,"y":360,"wires":[[]]},{"id":"59488b0fdf1eaf6e","type":"server","name":"Home Assistant","version":4,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]
实现效果图
========flows文件========
flow文件
flows.rar
(2.9 KB, 下载次数: 20)
|
|