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.22 |
|
44 |
|
45 if analog_value < 7.4: |
|
46 print("doorbell analog value: {}; ringing: {}; ring range: (6.0, 6.22]".format(analog_value, doorbell_on_state)) |
|
47 |
|
48 if doorbell_on_state != thread_local.doorbell_on_state: |
|
49 thread_local.doorbell_on_state = doorbell_on_state |
|
50 print("doorbell on") if doorbell_on_state else print("doorbell off") |
|
51 |
|
52 |
|
53 if __name__ == "__main__": |
|
54 main() |