equal
deleted
inserted
replaced
|
1 #!/usr/bin/env python3 |
|
2 |
|
3 import time |
|
4 import automationhat |
|
5 import sys |
|
6 |
|
7 import queue |
|
8 import threading |
|
9 |
|
10 |
|
11 def main(): |
|
12 command_queue = queue.LifoQueue() |
|
13 read_thread = threading.Thread(target=read_loop, args=[command_queue]) |
|
14 read_thread.start() |
|
15 run_loop(command_queue) |
|
16 |
|
17 |
|
18 def read_loop(command_queue): |
|
19 while True: |
|
20 command_queue.put_nowait(sys.stdin.readline().rstrip('\n')) |
|
21 |
|
22 |
|
23 def run_loop(command_queue): |
|
24 thread_local = threading.local() |
|
25 thread_local.doorbell_on_state = False |
|
26 |
|
27 while True: |
|
28 run_command(command_queue) |
|
29 read_doorbell(thread_local) |
|
30 |
|
31 |
|
32 def run_command(command_queue): |
|
33 try: |
|
34 command = command_queue.get(timeout=0.5) |
|
35 except queue.Empty: |
|
36 pass |
|
37 else: |
|
38 automationhat.relay.on() if command == "unlock" else automationhat.relay.off() |
|
39 |
|
40 |
|
41 def read_doorbell(thread_local): |
|
42 analog_value = automationhat.analog.one.read() |
|
43 doorbell_on_state = 6.0 < analog_value and analog_value < 6.3 |
|
44 |
|
45 if doorbell_on_state != thread_local.doorbell_on_state: |
|
46 thread_local.doorbell_on_state = doorbell_on_state |
|
47 print("doorbell_on") if doorbell_on_state else print("doorbell_off") |
|
48 |
|
49 |
|
50 if __name__ == "__main__": |
|
51 main() |