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)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值