summaryrefslogtreecommitdiff
path: root/scripts/status_script.py
blob: a92d5bfd17470edf4d0232d568cfa5ac746666fa (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python

import urllib
import contextlib
import json
import time
import os

class SublabStatus(object):
    json = None
    def get_json(self):
        if self.json is not None:
            return self.json

        with contextlib.closing(urllib.urlopen('http://beaglebone.local.sublab.org/')) as response:
            self.json = json.load(response)
        return self.json

    def door_status(self):
        try:
            self.get_json()
        except Exception:
            return 'unkown'

        door_status = self.json['door.lock']

        if door_status['ts'] + 120 < time.time():
            return 'unknown'

        if not door_status['value']:
            return 'open'
        else:
            return 'closed'

if __name__ == '__main__':
    s = SublabStatus()
    if s.door_status() == 'open':
        colors = ('#0f0', '#222', 'status-open.png')
    elif s.door_status() == 'closed':
        colors = ('#222', '#f00', 'status-closed.png')
    else:
        colors = ('#222', '#222', 'status-unknown.png')

    css = '''
span.sublabopen {
    color: %s;
}

span.sublabclosed {
    color: %s;
}

#statusimage {
    background: url(../img/%s) ;
    height: 100px;
    width: 100px;
    position: absolute;
    right: 20px;
}


''' % colors

    path = os.path.realpath(os.path.dirname(__file__))
    path = os.path.join(path, '..', 'public', 'css', 'sublab.status.css')
    path_new = path + '.new'

    with open(path_new, 'w') as css_file:
        css_file.write(css)
    os.rename(path_new, path)