From 5adb0aa38f8f14d288da14017d3302dfa50d806d Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Tue, 6 May 2014 04:32:10 +0200 Subject: doorwatch: add longpoll support --- doorwatch.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/doorwatch.py b/doorwatch.py index c88ac6a..64a9fbd 100644 --- a/doorwatch.py +++ b/doorwatch.py @@ -5,33 +5,39 @@ import subprocess import os -import urllib +import urllib2 import json import sys import time if __name__ == '__main__': workdir = os.path.realpath(os.path.dirname(__file__)) - was_closed = False # Don't ding at startup + was_closed = False # Only react to changes + last_ref = None while True: time.sleep(1) try: r = None - r = urllib.urlopen('http://172.22.83.5/subcan.json') + req_url = 'http://beaglebone.local.sublab.org/longpoll' + if last_ref is not None: + req_url += '?' + last_ref + r = urllib2.urlopen(req_url, timeout=120) data = json.loads(r.read()) except Exception: sys.excepthook(*sys.exc_info()) + last_ref = None continue finally: if r is not None: r.close() + last_ref = data['ref'] closed = data['door.left']['value'] if was_closed and not closed: print "Door has been opened!" - subprocess.call(['paplay', os.path.join(workdir, 'doorwatch.wav')]) + subprocess.call(['paplay', '--volume=32661', os.path.join(workdir, 'doorwatch.wav')]) if not was_closed and closed: print "Door has been closed." was_closed = closed -- cgit v1.2.1