Compare commits
No commits in common. "2efc2343b68a4b65dd239cbb0426074826d6f1b6" and "e48f0f63385f5990440524c66ba925466f8e292f" have entirely different histories.
2efc2343b6
...
e48f0f6338
50
door.py
50
door.py
|
@ -1,12 +1,7 @@
|
|||
#!/usr/bin/python
|
||||
import os, serial, socket, subprocess, select, datetime, sys
|
||||
import paho.mqtt.client as mqcl
|
||||
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")
|
||||
|
@ -15,25 +10,16 @@ parser.add_argument("--valid_tokens", default="/etc/door_tokens")
|
|||
parser.add_argument("--log_file", default="/tmp/nfc.log")
|
||||
parser.add_argument("--state_timeout", type=float, default=10)
|
||||
parser.add_argument("--repeat_time", type=float, default=5)
|
||||
parser.add_argument("--mqtt_host", default="10.10.21.2")
|
||||
|
||||
config = parser.parse_args()
|
||||
|
||||
mqttc = mqcl.Client()
|
||||
|
||||
def timestamp():
|
||||
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Log data to stdout and the log file
|
||||
def log(*args):
|
||||
data = "%s %s" % (timestamp(), " ".join(str(i) for i in args))
|
||||
data = "%s %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), " ".join(str(i) for i in args))
|
||||
lgf.write(data + "\n")
|
||||
lgf.flush()
|
||||
print(data)
|
||||
|
||||
def mqtt(topic, msg, persistent = True):
|
||||
mqttc.publish("door/" + topic, msg, qos=2, retain=persistent)
|
||||
|
||||
# Opens the socket that can control the daemon
|
||||
# Commands are TBD
|
||||
def open_control_socket():
|
||||
|
@ -132,18 +118,14 @@ def poll_door_state():
|
|||
data = serial_port.readline().strip().split()
|
||||
if data[0] == b'pos:':
|
||||
data = int(data[1])
|
||||
if state_pos != data:
|
||||
mqtt("position/value", data, True)
|
||||
state_pos = data
|
||||
changed = False
|
||||
if data < OPEN_THRESHOLD and state != OPEN:
|
||||
if data < 80 and state != OPEN:
|
||||
state = OPEN
|
||||
changed = True
|
||||
mqtt("state/value", "open", True)
|
||||
elif data > CLOSED_THRESHOLD and state != CLOSE:
|
||||
elif data > 100 and state != CLOSE:
|
||||
state = CLOSE
|
||||
changed = True
|
||||
mqtt("state/value", "closed", True)
|
||||
return state
|
||||
|
||||
# Check the door state and send commands through a state machine
|
||||
|
@ -172,12 +154,6 @@ 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
|
||||
|
@ -223,7 +199,6 @@ def open_door():
|
|||
log("Opening the door")
|
||||
action = OPEN
|
||||
start_time = None
|
||||
mqtt("state/target", "open", True)
|
||||
handle_door_state()
|
||||
|
||||
def close_door():
|
||||
|
@ -232,7 +207,6 @@ def close_door():
|
|||
log("Closing the door")
|
||||
action = CLOSE
|
||||
start_time = None
|
||||
mqtt("state/target", "closed", True)
|
||||
handle_door_state()
|
||||
|
||||
def toggle_door_state():
|
||||
|
@ -261,7 +235,12 @@ def handle_nfc_token(token = None):
|
|||
toggle_door_state()
|
||||
else:
|
||||
log("Invalid token:", token)
|
||||
mqtt("token/last_invalid", "%s;%s" % (timestamp(), token))
|
||||
|
||||
open_logfile()
|
||||
read_valid_tokens()
|
||||
open_nfc_fifo()
|
||||
open_serial_port()
|
||||
open_control_socket()
|
||||
|
||||
class LineBuffer(object):
|
||||
def __init__(self, f, handler):
|
||||
|
@ -280,6 +259,7 @@ class LineBuffer(object):
|
|||
self.handler(self.f, i)
|
||||
return True
|
||||
|
||||
|
||||
def handle_cmd(comm, data):
|
||||
cmd = data.decode('utf8').split()
|
||||
cmd, args = cmd[0], cmd[1:]
|
||||
|
@ -310,16 +290,6 @@ def handle_cmd(comm, data):
|
|||
action_names[action],
|
||||
(datetime.datetime.now() - start_time).total_seconds()))
|
||||
|
||||
open_logfile()
|
||||
read_valid_tokens()
|
||||
open_nfc_fifo()
|
||||
open_serial_port()
|
||||
open_control_socket()
|
||||
|
||||
mqttc.on_connect = lambda client, userdata, flags, rc: log("Connected to mqtt host with result %s" % (str(rc), ))
|
||||
mqttc.connect_async(config.mqtt_host, keepalive = 60)
|
||||
mqttc.loop_start()
|
||||
|
||||
buffers = {}
|
||||
while True:
|
||||
readable = select.select([ nfc_fifo, control_socket ] + comm_channels, [], [], 1)[0]
|
||||
|
|
Loading…
Reference in New Issue
Block a user