-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoor_lib.py
80 lines (68 loc) · 2 KB
/
door_lib.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
70
71
72
73
74
75
76
77
78
79
80
import contextlib
import logging
from systemd.journal import JournalHandler
import requests
import serial
from time import sleep
import config
base_url = "https://api.flipdot.org/sensors/door/locked/"
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(JournalHandler())
def close():
with get_serial() as s:
try:
if is_open(s):
s.write('0')
return True
else:
return False
except Exception as e:
logger.exception("close")
pass
def open():
with get_serial() as s:
try:
if not is_open(s):
s.write('1')
return True
else:
return False
except Exception as e:
logger.exception("open")
pass
def update_api(locked=None):
logger.debug("update api")
if locked is None:
with get_serial() as s:
locked = not is_open(s)
logger.debug("door is {}".format(locked))
value = 1 if locked else 0
logger.debug("update api to {}".format(value))
try:
logger.debug("posted api: {}".format(requests.get(base_url + str(value), timeout=3).content))
except Exception as e:
logger.exception("error posting api status")
def get_serial():
s = serial.Serial(config.SERIAL, baudrate=9600, timeout=10)
return contextlib.closing(s)
def get_state(s):
for i in range(5):
line = s.readline()
logger.debug("line: {}".format(line))
if not line or len(line) < 3:
continue
first = line.split(" ")[0]
try:
adc = int(first)
except ValueError:
msg = "ValueError: '{}' is not a valid door state.".format(first)
logger.error(msg)
sleep(1)
continue
logger.debug("adc: {}".format(adc))
return adc > 500, adc
return None, "unk"
def is_open(s):
door_is_open, _ = get_state(s)
return door_is_open