Variable thresholds

This commit is contained in:
Lynn Ochs 2021-03-10 17:16:40 +01:00
parent e48f0f6338
commit b2d7c5a4cc

14
door.py
View File

@ -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