-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuerstatus.py
69 lines (51 loc) · 1.44 KB
/
tuerstatus.py
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
#!/usr/bin/env python
import json
import os
import requests
import time
from mpd import MPDClient
LOCK = '334EC1'
URL = 'http://localhost/status/door'
MPD_HOST = os.environ['MPD_HOST']
MPD_PORT = os.environ['MPD_PORT']
MPD_PASS = os.environ['MPD_PASS']
open_last = False
lights = [8, 7, 6, 5, 4, 3, 1]
def handleMPD(is_open):
if not is_open:
print ("Pausing MPD")
client = MPDClient()
client.connect(MPD_HOST, MPD_PORT)
client.password(MPD_PASS)
client.pause(1)
client.close()
client.disconnect()
def handleStateChange(is_open):
handleMPD(is_open)
payload = {'door_open': 1 if is_open else 0}
print("Payload ", payload)
for x in range(1,5):
r = requests.post(URL, data=payload)
print(r)
if r.status_code == requests.codes.ok:
break
if not is_open:
for x in lights:
url = "http://troll.nobreakspace.org/control?cmd=set_state_actuator&number={0}&function=2".format(x)
requests.get(url)
while True:
r = requests.get('https://padlock.nobreakspace.org/api/locks/stream', cert=('tuerstatus.crt', 'tuerstatus.key'), stream=True, verify=False)
for line in r.iter_lines(chunk_size=16):
print("Result line ", line)
if len(line) > 3:
data = line[6:]
data = json.loads(data.decode("UTF-8"))
locks = dict()
for d in data:
locks[d['id']] = d
# print("Locks data", locks)
is_open = not locks[LOCK]['locked']
if open_last != is_open:
open_last = is_open
handleStateChange(is_open)
time.sleep(2)