summaryrefslogtreecommitdiff
path: root/doorwatch.py
blob: c88ac6aecdceb05ba9bd3532f4a020d3df92eb48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# This program plays a "little" sound when the door
# is opened. This is helpful e.g. when sitting back
# in the smokers lounge but wanting the door to be
# unlocked.

import subprocess
import os
import urllib
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

    while True:
        time.sleep(1)

        try:
            r = None
            r = urllib.urlopen('http://172.22.83.5/subcan.json')
            data = json.loads(r.read())
        except Exception:
            sys.excepthook(*sys.exc_info())
            continue
        finally:
            if r is not None:
                r.close()

        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."
        was_closed = closed