【搬家】小爱拦截转发NR

官方升级了日志文件以及格式,已调整日志路径以及对应的自动拦截词的判定

前提是已经拥有一台SSH过的小爱
重启就会丢SSH的绿板小爱可以见后面的物理解决方案

一键安装包测试版
在小爱上执行(注意,会验证nodered服务是否正常,需要先导入后文里的流程进行)

curl -s "https://raw.githubusercontent.com/FlashSoft/mico/master/install.sh?`date +%s`" > /root/install.sh
sh /root/install.sh

一键安装包截图

与之匹配的nodered的流程如下, 自己可以发挥自己的想象力去写自己的流

NodeRed流程,可以自己导入进去玩


{"id":"57fc0f09.83c4c","type":"http in","z":"2a168bed.f914f4","name":"","url":"/miai","method":"post","upload":false,"swaggerDoc":"","x":80,"y":225,"wires":"9813ba6d.7261f8","b79563dc.c75a9"]]},{"id":"9813ba6d.7261f8","type":"debug","z":"2a168bed.f914f4","name":"全局调试","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":275,"y":140,"wires":]},{"id":"aa970ca6.ec15a","type":"http in","z":"2a168bed.f914f4","name":"","url":"/miai","method":"get","upload":false,"swaggerDoc":"","x":75,"y":95,"wires":"58f84781.1c1e68"]]},{"id":"42c15019.d03be","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":450,"y":95,"wires":]},{"id":"58f84781.1c1e68","type":"function","z":"2a168bed.f914f4","name":"小爱拦截词","func":"// 多个关键词以竖线分割\nmsg.payload = `没有|未知`\nreturn msg","outputs":1,"noerr":0,"x":280,"y":95,"wires":"42c15019.d03be"]]},{"id":"b79563dc.c75a9","type":"function","z":"2a168bed.f914f4","name":"处理捕获指令","func":"// 指令词列表\n// 一行一个设备,对于一个设备的多个指令词可用,分割\nconst list = \n  '打开新风'],\n  '关闭新风'],\n  '室内空气质量']\n]\n\n\n// === 以下内容大多数情况不用改 ====\nlet queries = ]\ntry { queries = JSON.parse(msg.payload.asr).response.queries.map(it => it.query) } catch (e) { }\nmsg.queries = queries\nmsg.list = list\nmsg.matched = !!list.filter((item, index) => {\n  item.matched = !!queries.filter(it => ~item.indexOf(it)).length\n  item.matched && (msg.matched_index = index)\n  return item.matched\n}).length\n\nreturn msg","outputs":1,"noerr":0,"x":290,"y":225,"wires":"a26ebc66.1c77f","672bee8b.a306"]]},{"id":"4110603.07d34a","type":"switch","z":"2a168bed.f914f4","name":"","property":"matched_index","propertyType":"msg","rules":{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"},{"t":"eq","v":"4","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":630,"y":385,"wires":"4e1a7bcb.af07c4","46fae169.a3aa2"],"42de8cd6.db79e4","231a209b.5a602"],"e8994c49.c91b5","8941d489.2d0158","60d41877.24a8d8"],],]]},{"id":"4e1a7bcb.af07c4","type":"api-call-service","z":"2a168bed.f914f4","name":"打开所有FFU","server":"4ccda836.aef688","service_domain":"switch","service":"turn_on","data":"{\"entity_id\":\"switch.original_xiaomi_mi_smart_wifi_socket,switch.xiaomi_mi_smart_wifi_usb_socket\"}","mergecontext":"","x":860,"y":360,"wires":"fec8efed.c3c45"]]},{"id":"46fae169.a3aa2","type":"api-call-service","z":"2a168bed.f914f4","name":"打开所有Airx","server":"4ccda836.aef688","service_domain":"fan","service":"turn_on","data":"{\"entity_id\":\"fan.airx,fan.airx2\"}","mergecontext":"","x":860,"y":411,"wires":"fec8efed.c3c45"]]},{"id":"42a42cc6.e51e74","type":"function","z":"2a168bed.f914f4","name":"TTS文本","func":"msg.payload = '新风打开成功'\nreturn msg","outputs":1,"noerr":0,"x":1160,"y":360,"wires":"13bb5f0.c37b7a1"]]},{"id":"42de8cd6.db79e4","type":"api-call-service","z":"2a168bed.f914f4","name":"关闭所有FFU","server":"4ccda836.aef688","service_domain":"switch","service":"turn_off","data":"{\"entity_id\":\"switch.original_xiaomi_mi_smart_wifi_socket,switch.xiaomi_mi_smart_wifi_usb_socket\"}","mergecontext":"","x":860,"y":525,"wires":"447d706a.41662"]]},{"id":"231a209b.5a602","type":"api-call-service","z":"2a168bed.f914f4","name":"关闭所有Airx","server":"4ccda836.aef688","service_domain":"fan","service":"turn_off","data":"{\"entity_id\":\"fan.airx,fan.airx2\"}","mergecontext":"","x":860,"y":575,"wires":"447d706a.41662"]]},{"id":"e8994c49.c91b5","type":"api-current-state","z":"2a168bed.f914f4","name":"PM2.5","server":"4ccda836.aef688","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"sensor.m1_pm25","x":845,"y":695,"wires":"442c8451.3029ac"]]},{"id":"8941d489.2d0158","type":"api-current-state","z":"2a168bed.f914f4","name":"温度","server":"4ccda836.aef688","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"sensor.m1_temperature","x":845,"y":750,"wires":"442c8451.3029ac"]]},{"id":"60d41877.24a8d8","type":"api-current-state","z":"2a168bed.f914f4","name":"湿度","server":"4ccda836.aef688","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"sensor.m1_humidity","x":845,"y":805,"wires":"442c8451.3029ac"]]},{"id":"442c8451.3029ac","type":"join","z":"2a168bed.f914f4","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1015,"y":694,"wires":"47e7f395.074fec"]]},{"id":"430c565c.8c74b8","type":"comment","z":"2a168bed.f914f4","name":"在这里统一配置响应词以及TTS文案","info":"","x":350,"y":185,"wires":]},{"id":"a26ebc66.1c77f","type":"debug","z":"2a168bed.f914f4","name":"捕获调试","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":510,"y":140,"wires":]},{"id":"672bee8b.a306","type":"switch","z":"2a168bed.f914f4","name":"","property":"matched","propertyType":"msg","rules":{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":480,"y":225,"wires":"d81bcfed.b26de"],"4110603.07d34a"]]},{"id":"447d706a.41662","type":"join","z":"2a168bed.f914f4","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1025,"y":525,"wires":"cea11ca1.25103"]]},{"id":"fec8efed.c3c45","type":"join","z":"2a168bed.f914f4","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1020,"y":360,"wires":"42a42cc6.e51e74"]]},{"id":"90f763d2.6e01f","type":"comment","z":"2a168bed.f914f4","name":"在这里配置小爱的拦截词","info":"","x":320,"y":50,"wires":]},{"id":"47e7f395.074fec","type":"function","z":"2a168bed.f914f4","name":"TTS文本","func":"msg.payload = `\n当前室内PM2.5为${msg.payload'sensor.m1_pm25']},\n温度${msg.payload'sensor.m1_temperature']}摄氏度,\n湿度${msg.payload'sensor.m1_humidity']}\n`\nreturn msg","outputs":1,"noerr":0,"x":1160,"y":694,"wires":"4fdc4b2b.e28ff4"]]},{"id":"9e028a2b.92f5f8","type":"comment","z":"2a168bed.f914f4","name":"打开净化器","info":"","x":850,"y":320,"wires":]},{"id":"149bebb6.7f1b14","type":"comment","z":"2a168bed.f914f4","name":"关闭净化器","info":"","x":850,"y":485,"wires":]},{"id":"a3d5f3e0.112ac","type":"comment","z":"2a168bed.f914f4","name":"查询空气质量","info":"","x":865,"y":655,"wires":]},{"id":"d81bcfed.b26de","type":"function","z":"2a168bed.f914f4","name":"TTS文本","func":"// 如果是被nodered捕获了,则在小爱提示基础上补了一个嘿嘿嘿,便于辨识\ntry{\n  const res = JSON.parse(msg.payload.res)\n  msg.payload = `嘿嘿嘿,${res.answer[0].text}`\n}\ncatch(e){\n  msg.payload = '嘿嘿嘿,你说的是啥'    \n}\n\nreturn msg","outputs":1,"noerr":0,"x":640,"y":218,"wires":"bce07803.b56008"]]},{"id":"bce07803.b56008","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":840,"y":218,"wires":]},{"id":"13bb5f0.c37b7a1","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":1305,"y":360,"wires":]},{"id":"cea11ca1.25103","type":"function","z":"2a168bed.f914f4","name":"TTS文本","func":"msg.payload = '新风关闭成功'\nreturn msg","outputs":1,"noerr":0,"x":1160,"y":525,"wires":"4b41eae3.ab1c54"]]},{"id":"4b41eae3.ab1c54","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":1305,"y":525,"wires":]},{"id":"4fdc4b2b.e28ff4","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":1305,"y":693,"wires":]},{"id":"4ccda836.aef688","type":"server","z":"","name":"HASS","legacy":false}]


绿板小爱的不用老连着TTL开启dropbear的方案
供大家参考,直接使用ESP8266或其他单片机,直发串口指令给小爱,开启dropbear

// @author FlashSoft

// ESP8266 定时重启dropbear程序
// 用于解决不拆机开启绿板小爱的SSH功能
// 默认密码为空

// 操作流程: 从小爱的12V口接电,用AMS1117或更小的更省电的HT7333降压成3.3V给ESP8266供电
// 按照如下线序接到小爱上,把固件烧写进ESP8266即可,可以使用arduino ide或platformio进行烧写

// 接线顺序:
// rxd GPIO14 (D5) ==> 小爱 TXD
// txd GPIO12 (D6) ==> 小爱 RXD
// gnd ==> 小爱GND
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial swSer(14, 12, false, 256);

void setup() {
  Serial.begin(115200);
  swSer.begin(115200);
}

void loop() {
  Serial.println("执行指令");
  // 向串口发送指令
  // 判断dropbear进程个数,如果是0个,则启动重新生成key并启动dropbear
  swSer.println(" \"`ps|grep 'dropbear -r /etc/dropbear/dropbear_rsa_host_key'|grep -v grep|wc -l`\" -eq \"0\" ]] && rm /etc/dropbear/dropbear_rsa_host_key && dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key && dropbear -r /etc/dropbear/dropbear_rsa_host_key");
  // 10秒执行一次
  delay(10000);
}

关于putty中,程序显示乱码问题可以参考的@windgo 解决方案:
參考連結:https://my.oschina.net/yuyang/blog/176002

打开putty主程序
选择window-〉Appearance-〉Font settings-〉Change…
选择Fixedsys字体,字符集选择CHINESE_GB2312。
在window-〉Appearance -〉Translation中,
Received data assumed to be in which character set 中
把Use font encoding改为UTF-8

================ 历史内容 ====================
在大神们已经研究完成的基础上,简单写了个shell程序,在小爱上运行
主要逻辑是:
1、检测小米服务器响应的日志变化
2、捕获响应日志中,如果是未知设备(也等于你自己的自制设备)
3、则让小爱停止播放找到未知设备的播报
4、通过curl转发asr和res日志内容给自己的远端接口
5、远端接口去跟HA通讯来控制自制设备(这个等大神们写HA插件)
6、远端接口返回需要小爱播报的文本内容
7、如果之前小爱在播放音乐的话就接着播放音乐

填个小坑~!~!~!~!绿板的修改下 mico.sh

asr_file="/tmp/mipns/mibrain/mibrain_asr.log"
res_file="/tmp/mipns/mibrain/mibrain_response.log"

日志名字得修改下~!~!:smiley:

自动更新后改为
asr_file="/tmp/mipns/mibrain/mibrain_asr.log"
res_file="/tmp/mipns/mibrain/mibrain_txt_RESULT_NLP.log"