DIY個人智能家庭網關——python篇之推送門磁報警信息到手機

見《通過openwrt推送門磁報警信息到android手機上


python代碼如下

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import serial  
from time import sleep  
import json
import 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/ttyUSB0
9600
{"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"}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章