diff options
| author | Luke Hoersten <[email protected]> | 2020-06-02 19:11:08 -0500 |
|---|---|---|
| committer | Luke Hoersten <[email protected]> | 2020-06-02 19:11:08 -0500 |
| commit | 4a7f13ec6c0fd59bed009f31b62674f279a58213 (patch) | |
| tree | 274f1d4728acab943e235733e57cf4ea4bd56c5b /npmjs-package/python | |
| parent | 247670f0d69b3269f88630071b246d38238f26ca (diff) | |
Updated homekit door for Raspbian Buster.
Diffstat (limited to 'npmjs-package/python')
| -rwxr-xr-x | npmjs-package/python/doord.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/npmjs-package/python/doord.py b/npmjs-package/python/doord.py new file mode 100755 index 0000000..cf29d59 --- /dev/null +++ b/npmjs-package/python/doord.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +import time +import automationhat +import sys + +import queue +import threading + + +def main(): + command_queue = queue.LifoQueue() + read_thread = threading.Thread(target=read_loop, args=[command_queue]) + read_thread.start() + run_loop(command_queue) + + +def read_loop(command_queue): + while True: + command_queue.put_nowait(sys.stdin.readline().rstrip('\n')) + + +def run_loop(command_queue): + thread_local = threading.local() + thread_local.doorbell_on_state = False + + while True: + run_command(command_queue) + read_doorbell(thread_local) + + +def run_command(command_queue): + try: + command = command_queue.get(timeout=0.5) + except queue.Empty: + pass + else: + automationhat.relay.on() if command == "unlock" else automationhat.relay.off() + + +def read_doorbell(thread_local): + analog_value = automationhat.analog.one.read() + doorbell_on_state = 6.0 < analog_value and analog_value <= 6.22 + + if analog_value < 7.4: + print("doorbell analog value: {}; ringing: {}; ring range: (6.0, 6.22]".format(analog_value, doorbell_on_state)) + + if doorbell_on_state != thread_local.doorbell_on_state: + thread_local.doorbell_on_state = doorbell_on_state + print("doorbell on") if doorbell_on_state else print("doorbell off") + + +if __name__ == "__main__": + main() |
