src.nth.io/

summaryrefslogtreecommitdiff
path: root/npmjs-package/python/doord.py
blob: cf29d595acf305768d9634e9435f3dd8ef0c0262 (plain)
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
#!/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()