This commit is contained in:
Lynn Ochs 2021-03-10 17:17:12 +01:00
parent b2d7c5a4cc
commit 2efc2343b6

36
door.py
View File

@ -1,5 +1,6 @@
#!/usr/bin/python #!/usr/bin/python
import os, serial, socket, subprocess, select, datetime, sys import os, serial, socket, subprocess, select, datetime, sys
import paho.mqtt.client as mqcl
import argparse import argparse
OPEN_THRESHOLD = 35 OPEN_THRESHOLD = 35
@ -14,16 +15,25 @@ parser.add_argument("--valid_tokens", default="/etc/door_tokens")
parser.add_argument("--log_file", default="/tmp/nfc.log") parser.add_argument("--log_file", default="/tmp/nfc.log")
parser.add_argument("--state_timeout", type=float, default=10) parser.add_argument("--state_timeout", type=float, default=10)
parser.add_argument("--repeat_time", type=float, default=5) parser.add_argument("--repeat_time", type=float, default=5)
parser.add_argument("--mqtt_host", default="10.10.21.2")
config = parser.parse_args() 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 # Log data to stdout and the log file
def log(*args): def log(*args):
data = "%s %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), " ".join(str(i) for i in args)) data = "%s %s" % (timestamp(), " ".join(str(i) for i in args))
lgf.write(data + "\n") lgf.write(data + "\n")
lgf.flush() lgf.flush()
print(data) 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 # Opens the socket that can control the daemon
# Commands are TBD # Commands are TBD
def open_control_socket(): def open_control_socket():
@ -122,14 +132,18 @@ def poll_door_state():
data = serial_port.readline().strip().split() data = serial_port.readline().strip().split()
if data[0] == b'pos:': if data[0] == b'pos:':
data = int(data[1]) data = int(data[1])
if state_pos != data:
mqtt("position/value", data, True)
state_pos = data state_pos = data
changed = False changed = False
if data < OPEN_THRESHOLD and state != OPEN: if data < OPEN_THRESHOLD and state != OPEN:
state = OPEN state = OPEN
changed = True changed = True
mqtt("state/value", "open", True)
elif data > CLOSED_THRESHOLD and state != CLOSE: elif data > CLOSED_THRESHOLD and state != CLOSE:
state = CLOSE state = CLOSE
changed = True changed = True
mqtt("state/value", "closed", True)
return state return state
# Check the door state and send commands through a state machine # Check the door state and send commands through a state machine
@ -209,6 +223,7 @@ def open_door():
log("Opening the door") log("Opening the door")
action = OPEN action = OPEN
start_time = None start_time = None
mqtt("state/target", "open", True)
handle_door_state() handle_door_state()
def close_door(): def close_door():
@ -217,6 +232,7 @@ def close_door():
log("Closing the door") log("Closing the door")
action = CLOSE action = CLOSE
start_time = None start_time = None
mqtt("state/target", "closed", True)
handle_door_state() handle_door_state()
def toggle_door_state(): def toggle_door_state():
@ -245,12 +261,7 @@ def handle_nfc_token(token = None):
toggle_door_state() toggle_door_state()
else: else:
log("Invalid token:", token) 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): class LineBuffer(object):
def __init__(self, f, handler): def __init__(self, f, handler):
@ -269,7 +280,6 @@ class LineBuffer(object):
self.handler(self.f, i) self.handler(self.f, i)
return True return True
def handle_cmd(comm, data): def handle_cmd(comm, data):
cmd = data.decode('utf8').split() cmd = data.decode('utf8').split()
cmd, args = cmd[0], cmd[1:] cmd, args = cmd[0], cmd[1:]
@ -300,6 +310,16 @@ def handle_cmd(comm, data):
action_names[action], action_names[action],
(datetime.datetime.now() - start_time).total_seconds())) (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 = {} buffers = {}
while True: while True:
readable = select.select([ nfc_fifo, control_socket ] + comm_channels, [], [], 1)[0] readable = select.select([ nfc_fifo, control_socket ] + comm_channels, [], [], 1)[0]