-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx10d.py
executable file
·47 lines (37 loc) · 1.12 KB
/
x10d.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
#!/usr/bin/env python
from daemon import Daemon, SerialDispatcher
from serial import Serial
import api
from threading import Thread
import sys
def callback(event):
if event:
print(str(event))
def listen(daemon):
while True:
house, unit, act = input().split()
unit = int(unit)
if act.upper() == "ON":
daemon.on(house, unit)
elif act.upper() == "OFF":
daemon.off(house, unit)
def main(args):
serial_port = args[1]
baud = 9600
s = Serial(serial_port, baud)
dispatcher = SerialDispatcher(s)
daemon = Daemon(dispatcher)
daemon.subscribe(callback)
daemon_thread = Thread(target=daemon.listen, name="daemon-listener", daemon=True)
daemon_thread.start()
api_thread = Thread(target=api.run_api, args=(daemon,), name="web-api", daemon=True)
api_thread.start()
user_thread = Thread(target=listen, args=(daemon,), name="user-listener")
user_thread.start()
user_thread.join()
daemon_thread.join()
api_thread.join()
s.close()
if __name__ == "__main__":
# TODO: Parse arguments for things
main(sys.argv)