diff --git a/door.py b/door.py index 62e4e92..2dcec04 100644 --- a/door.py +++ b/door.py @@ -2,6 +2,10 @@ import os, serial, socket, subprocess, select, datetime, sys import argparse +OPEN_THRESHOLD = 35 +CLOSED_THRESHOLD = 60 +CLOSED_WANT = 135 + parser = argparse.ArgumentParser() parser.add_argument("--serial_port", default="/dev/serial/by-id/usb-Imaginaerraum.de_DoorControl_43363220195053573A002C0-if01") parser.add_argument("--nfc_fifo", default="/tmp/nfc_fifo") @@ -120,10 +124,10 @@ def poll_door_state(): data = int(data[1]) state_pos = data changed = False - if data < 80 and state != OPEN: + if data < OPEN_THRESHOLD and state != OPEN: state = OPEN changed = True - elif data > 100 and state != CLOSE: + elif data > CLOSED_THRESHOLD and state != CLOSE: state = CLOSE changed = True return state @@ -154,6 +158,12 @@ def handle_door_state(): if action == IDLE: if state != old_state: log("Door changed unexpectedly:", state_names[state]) + start_time = datetime.datetime.now() + if start_time and (datetime.datetime.now() - start_time).total_seconds() >= config.state_timeout: + start_time = None + if state_pos >= CLOSED_THRESHOLD and state_pos < CLOSED_WANT: + log("Closing door a bit more") + serial_port.write(target_state_cmd[CLOSE]) return # Target state, next action, timeout action