scp10011 / xiaoai_hook

小爱拦截器基于go实现
11 stars 7 forks source link

NodeRed Error #1

Open plutosherry opened 4 years ago

plutosherry commented 4 years ago

当添加了nodered的流程后,部署完毕测试 debug中报日志错误如下: TypeError: item.indexOf is not a function

scp10011 commented 4 years ago

试试这个

[{"id":"b942fdc6.591d3","type":"tab","label":"流程1","disabled":false,"info":""},{"id":"e09c6f09.32737","type":"http in","z":"b942fdc6.591d3","name":"","url":"/xiaoai_hook/event","method":"post","upload":false,"swaggerDoc":"","x":1950,"y":320,"wires":[["da9d03b7.b6914","df4d043e.f5c8a8","65a073c8.be1aec"]]},{"id":"5dc48208.d6b39c","type":"debug","z":"b942fdc6.591d3","name":"全局调试","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2420,"y":400,"wires":[]},{"id":"f3cb369d.333f08","type":"http in","z":"b942fdc6.591d3","name":"","url":"/xiaoai_hook/keyword","method":"get","upload":false,"swaggerDoc":"","x":1950,"y":480,"wires":[["483fc3e5.f5c01c"]]},{"id":"496a57cf.81fe68","type":"http response","z":"b942fdc6.591d3","name":"","statusCode":"200","headers":{},"x":2420,"y":480,"wires":[]},{"id":"483fc3e5.f5c01c","type":"function","z":"b942fdc6.591d3","name":"小爱拦截词","func":"// 多个关键词以竖线分割\nmsg.payload = `厕所|卫生间`\nreturn msg","outputs":1,"noerr":0,"x":2230,"y":480,"wires":[["496a57cf.81fe68"]]},{"id":"da9d03b7.b6914","type":"function","z":"b942fdc6.591d3","name":"处理捕获指令","func":"// 指令词列表\n// 一行一个设备,对于一个设备的多个指令词可用,分割\nconst list = [\n    '厕所有人吗,卫生间有人吗',\n]\n\n\n// === 以下内容大多数情况不用改 ====\nlet queries = []\ntry {\n    queries = JSON.parse(msg.payload.res).response.answer[0].text;\n} catch (e) {\n}\nmsg.queries = queries\nmsg.list = list\nmsg.matched = !!list.filter((item, index) => {\n    matched = ~item.indexOf(queries).length\n    matched && (msg.matched_index = index)\n    return matched\n}).length\n\nreturn msg;","outputs":1,"noerr":0,"x":2240,"y":320,"wires":[["b4e8cf73.06b12"]]},{"id":"f2c6ccc0.d5c51","type":"switch","z":"b942fdc6.591d3","name":"","property":"matched_index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":2590,"y":620,"wires":[["96560f39.c35b4"]]},{"id":"ff73c9ae.50b1c8","type":"comment","z":"b942fdc6.591d3","name":"在这里统一配置响应词以及TTS文案","info":"","x":2300,"y":240,"wires":[]},{"id":"b4e8cf73.06b12","type":"switch","z":"b942fdc6.591d3","name":"","property":"matched","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":2430,"y":320,"wires":[["d5b7d6e4.6bf618"],["f2c6ccc0.d5c51"]]},{"id":"b2076bf3.868a28","type":"comment","z":"b942fdc6.591d3","name":"在这里配置小爱的拦截词","info":"","x":2270,"y":520,"wires":[]},{"id":"15903827.e9afb8","type":"comment","z":"b942fdc6.591d3","name":"卫生间有人吗","info":"","x":2770,"y":400,"wires":[]},{"id":"d5b7d6e4.6bf618","type":"function","z":"b942fdc6.591d3","name":"默认回复","func":"answer =  msg.payload.answer ||  '你说的是啥'\nmsg.payload ={method: \"TTS\", jsonrpc: \"2.0\", id: 0, params: {token: \"123456\", msg: answer}}\nreturn msg","outputs":1,"noerr":0,"x":2920,"y":320,"wires":[["6bca25c3.7220cc"]]},{"id":"df4d043e.f5c8a8","type":"http response","z":"b942fdc6.591d3","name":"","statusCode":"200","headers":{},"x":2220,"y":280,"wires":[]},{"id":"96560f39.c35b4","type":"api-current-state","z":"b942fdc6.591d3","name":"厕所灯","server":"5811c904.4fe268","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"switch.wall_switch_right_158d0003a23208","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":true,"x":2770,"y":440,"wires":[["a54d97d9.082348"],["ea7bcb5c.c72188"]]},{"id":"a54d97d9.082348","type":"function","z":"b942fdc6.591d3","name":"有人","func":"place = msg.queries.startsWith(\"厕\") ? \"厕所\" : \"卫生间\"\nmsg.payload ={method: \"TTS\", jsonrpc: \"2.0\", id: 0, params: {token: \"123456\", msg: `${place}有人空闲时我会提醒你的`}}\nreturn msg;","outputs":1,"noerr":0,"x":2930,"y":420,"wires":[["6bca25c3.7220cc","8f95fa01.0c74c8"]]},{"id":"2fe9023a.b51dee","type":"debug","z":"b942fdc6.591d3","name":"全局调试","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":3300,"y":620,"wires":[]},{"id":"6bca25c3.7220cc","type":"http request","z":"b942fdc6.591d3","name":"TTS","method":"POST","ret":"obj","paytoqs":false,"url":"http://172.16.1.200:18888/","tls":"","persist":false,"proxy":"","authType":"","x":3150,"y":620,"wires":[["2fe9023a.b51dee"]]},{"id":"ea7bcb5c.c72188","type":"function","z":"b942fdc6.591d3","name":"没人","func":"place = msg.queries.startsWith(\"厕\") ? \"厕所\" : \"卫生间\"\nmsg.payload ={method: \"TTS\", jsonrpc: \"2.0\", id: 0, params: {token: \"123456\", msg: `${place}没人`}}\n\nreturn msg;","outputs":1,"noerr":0,"x":2930,"y":460,"wires":[["6bca25c3.7220cc"]]},{"id":"65a073c8.be1aec","type":"json","z":"b942fdc6.591d3","name":"","property":"payload.res","action":"","pretty":false,"x":2230,"y":400,"wires":[["5dc48208.d6b39c"]]},{"id":"8f95fa01.0c74c8","type":"ha-wait-until","z":"b942fdc6.591d3","name":"等待厕所没人","server":"5811c904.4fe268","outputs":1,"entityId":"switch.wall_switch_right_158d0003a23208","entityIdFilterType":"exact","property":"state","comparator":"is","value":"off","valueType":"str","timeout":0,"timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":3140,"y":420,"wires":[["6c37670c.990088"]]},{"id":"6c37670c.990088","type":"function","z":"b942fdc6.591d3","name":"提示停止占用","func":"place = msg.queries.startsWith(\"厕\") ? \"厕所\" : \"卫生间\"\nmsg.payload ={method: \"TTS\", jsonrpc: \"2.0\", id: 0, params: {token: \"123456\", msg: `${place}没人了快去吧`}}\nreturn msg;","outputs":1,"noerr":0,"x":3320,"y":420,"wires":[["6bca25c3.7220cc"]]},{"id":"2dc6130b.20bbcc","type":"inject","z":"b942fdc6.591d3","name":"","topic":"","payload":"{\"method\": \"TTS\", \"jsonrpc\": \"2.0\", \"id\": 0, \"params\": {\"token\": \"123456\", \"msg\": \"hello\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2830,"y":620,"wires":[["6bca25c3.7220cc"]]},{"id":"5811c904.4fe268","type":"server","z":"","name":"Home Assistant","addon":true}]
plutosherry commented 4 years ago

params: {token: "123456", msg: answer} 这里的token是小爱音响的token吗? 另外 其中一个TTS流程中的 http://172.16.1.200:18888/ 是需要替换成什么地址?

plutosherry commented 4 years ago

另外,我是打算用来控制自定义的灯开或关,这其中的流程关于:厕所有人没人的状态部分还不理解 是什么原理取得状态的?

scp10011 commented 4 years ago

params:{token:“ 123456”,msg:answer}这里的令牌是小爱音响的令牌吗?另外 其中一个TTS流程中的http://172.16.1.200:18888/是需要替换成什么地址?

http://172.16.1.200:18888/ 输入小爱音箱的ip token 输入和启动xiaoai_hook 传入的token一样的值

scp10011 commented 4 years ago

另外,我是打算用来控制自定义的灯开或关,这其中的流程关于:厕所有人没人的状态部分还不理解是什么原理取得状态的?

卫生间这个只是为了演示多段逻辑,我用的获取厕所灯的状态

plutosherry commented 4 years ago

感谢回复!尝试了下在小爱音响固件版本是:1.54.13 型号:S12A(绿板)下测试,所有的指令(无论是米家设备还是自定义设备)都会被拦截,并转发给nodered服务器(如此影响了米家原生设备的控制)。此外,nodered中”处理捕获指令“这部分的函数可能逻辑上有问题,只要是list中给出的自定义设备名,都会被match匹配,若list中没有的名称也会被匹配。

scp10011 commented 4 years ago

所有的指令(无论是米家设备还是自定义设备)都会被拦截

可以的话 吧/tmp/mipns/mibrain/mibrain_txt_RESULT_NLP.log /tmp/mipns/mibrain/mibrain_nlp_answer.log 发给我

scp10011 commented 4 years ago

此外,nodered中”处理捕获指令“这部分的函数可能逻辑上有问题,只要是list中给出的自定义设备名,都会被match匹配,若list中没有的名称也会被匹配。

下载最新的二进制文件和node-red流程试试

plutosherry commented 3 years ago

最近忙,一直没弄,这两天回家发现小爱偷偷升级固件了,/tmp/mipns/mibrain/mibrain_txt_RESULT_NLP.log/tmp/mipns/mibrain/mibrain_nlp_answer.log 都没了,连/tmp/mipns下的mibrain文件夹都消失了,不知道是不是固件升级导致的。没这两个文件就没办法拦截了。。。。