diff options
-rw-r--r-- | bot_backend.py | 16 | ||||
-rw-r--r-- | doorwatch.py | 30 |
2 files changed, 33 insertions, 13 deletions
diff --git a/bot_backend.py b/bot_backend.py index b1ccb48..5c3812b 100644 --- a/bot_backend.py +++ b/bot_backend.py @@ -34,10 +34,18 @@ def tmp_set(url, value): def on_pubmsg(self, c, e): message = e.arguments[0] - light_command_prefix = '!light' - if message.startswith(light_command_prefix): - nick = e.source.nick - on_light_command(self, nick, message[len(light_command_prefix) + 1:]) + nick = e.source.nick + commands = { + '!light' : on_light_command, + } + + if message.startswith('!help'): + self.connection.privmsg(self.channel, "The following commands are currently known: %s" % ', '.join(sorted(commands.keys()))) + else: + for key in commands: + if message.startswith(key): + commands[key](self, nick, message[len(key) + 1:]) + break def on_light_command(self, nick, commandline): tokens = commandline.split(' ') diff --git a/doorwatch.py b/doorwatch.py index c88ac6a..cdb22fb 100644 --- a/doorwatch.py +++ b/doorwatch.py @@ -5,33 +5,45 @@ 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 = None # 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') - data = json.loads(r.read()) + 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) + buf = r.read() + if not buf.strip(): + continue + data = json.loads(buf) except Exception: sys.excepthook(*sys.exc_info()) + last_ref = None continue finally: if r is not None: r.close() + last_ref = data['ref'] + data = data['data'] + 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')]) - if not was_closed and closed: - print "Door has been closed." + if was_closed is not None: + if was_closed and not closed: + print "Door has been opened!" + 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 |