阅读背景:

DIY个人智能家庭网关——python篇之推送手机上下线事件

来源:互联网 
#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import time
import subprocess
from time import sleep
import json

mac = "a8:1b:5a:b7:e0:03"
last_state = ""
msg = ""


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 \"e5068f518ccb04cc92ac601f:9dd7a8d37bbfa2814648a6c5\""
    curl_cmdline = '%s -d \'%s\''%(cmd,json_str)
    print curl_cmdline
    rc = subprocess.call(curl_cmdline, shell=True); 
    
cmd = 'iw dev wlan0 station dump | grep Station'
while True:
  obj = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
  obj.wait()
  state = 'offline'
  lines = obj.stdout.readlines()
  for str in lines:										
  	if str.find(mac) > 0:
  		state = 'online'
  		break
  if last_state != state:
    if state == 'online':
      msg = "Welcome home!"
    else:
      msg = "Goodbye"
    push_msg(msg)
  last_state = state
  time.sleep(2)#!/usr/bin/env python
# -*- coding: utf-8 -*- 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: