您的位置:首页 > 编程语言 > Python开发

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

2017-02-26 23:54 405 查看
见《通过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"}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: