summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Franke <nobody@nowhere.ws>2014-05-06 04:32:10 +0200
committerChristian Franke <nobody@nowhere.ws>2014-05-06 04:35:50 +0200
commit5adb0aa38f8f14d288da14017d3302dfa50d806d (patch)
tree66027cd33fbf12872f9560d7b628a1d03a578291
parentfc01fffde820738e00fe11d94ea4b3f118de579d (diff)
doorwatch: add longpoll support
-rw-r--r--doorwatch.py14
1 files 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