首页 > 编程 > Python > 正文

DIY个人智能家庭网关——python篇之推送门磁报警信息到手机

2019-11-06 09:27:26
字体:
来源:转载
供稿:网友

见《通过openwrt推送门磁报警信息到android手机上》

python代码如下

#!/usr/bin/env python# -*- coding: utf-8 -*- import serial  from time import sleep  import jsonimport subPRocess  ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0.5)     print ser.port  print ser.baudrate  my_addr = "0x5555"def push_msg(msg):    content = {"platform":"all","audience":"all", "notification":{"alert":msg}}    print content    json_str = json.dumps(content)    print json_str    cmd = "curl -X POST  --cacert /etc/ssl/certs/ca-certificates.crt -v https://api.jpush.cn/v3/push/ -H /"Content-Type: application/json/" -u /"xxx:xxx/""    curl_cmdline = '%s -d /'%s/''%(cmd,json_str)    print curl_cmdline    rc = subprocess.call(curl_cmdline, shell=True);   def recv(serial):      while True:        data =serial.read(64)        if data == '':          continue      else:        break      sleep(0.02)     return data     while True:      data =recv(ser)     if data != '':       print data    s = json.loads(data)      print s["addr"]    if s["addr"] == my_addr:      push_msg("Alarm!!!")门磁触发后输出结果

root@OpenWrt:/tmp# ./pushalarm.py &root@OpenWrt:/tmp# /dev/ttyUSB09600{"type":"trigger", "addr":"0x5555","data":"0xc0"}0x5555{'platform': 'all', 'audience': 'all', 'notification': {'alert': 'Alarm!!!'}}{"platform": "all", "audience": "all", "notification": {"alert": "Alarm!!!"}}curl -X POST  --cacert /etc/ssl/certs/ca-certificates.crt -v https://api.jpush.cn/v3/push/ -H "Content-Type: application/json" -u "xxx:xxx" -d '{"platform": "all", "audience": "all", "notification": {"alert": "Alarm!!!"}}'> POST /v3/push/ HTTP/1.1> Authorization: Basic ZTUwNjhmNTE4Y2NiMDRjYzkyYWM2MDFmOjlkZDdhOGQzN2JiZmEyODE0NjQ4YTZjNQ==> User-Agent: curl/7.40.0> Host: api.jpush.cn> Accept: */*> Content-Type: application/json> Content-Length: 77> < HTTP/1.1 200 OK< Server: nginx< Date: Sun, 26 Feb 2017 15:49:53 GMT< Content-Type: application/json< Transfer-Encoding: chunked< Connection: keep-alive< X-Rate-Limit-Limit: 600< X-Rate-Limit-Remaining: 599< X-Rate-Limit-Reset: 60< X-JPush-MsgId: 1562770329< {"sendno":"0","msg_id":"1562770329"}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表