added connection to nfc socket for reloading token file and opening and closing the door from the web interface

This commit is contained in:
Simon Pirkelmann 2021-03-21 15:53:33 +01:00
parent a529126b40
commit bf239edf1d
4 changed files with 41 additions and 4 deletions

View File

@ -6,10 +6,11 @@ from imaginaerraum_door_admin.webapp import create_application
parser = argparse.ArgumentParser()
parser.add_argument("--token_file", default="/etc/door_tokens", help="path to the file with door tokens and users")
parser.add_argument("--nfc_socket", default="/tmp/nfc.sock", help="socket for handling NFC reader commands")
parser.add_argument("--template_folder", default="templates", help="path to Flask templates folder")
parser.add_argument("--static_folder", default="static", help="path to Flask static folder")
parser.add_argument("--mqtt_host", default="10.10.21.2", help="IP address of MQTT broker")
parser.add_argument("--port", default=5000, help="Port for running the Flask server")
parser.add_argument("--port", default=80, help="Port for running the Flask server")
parser.add_argument("--mail_server", default="smtp.googlemail.com", help="email server for sending security messages")
parser.add_argument("--mail_port", default=465, help="port for security email server")
parser.add_argument("--mail_use_tls", default=False, help="use TLS for security emails")

View File

@ -1,9 +1,10 @@
import paho.mqtt.client as mqtt
import socket
from pathlib import Path
class DoorHandle:
def __init__(self, token_file, mqtt_host, mqtt_port=1883):
def __init__(self, token_file, mqtt_host, mqtt_port=1883, nfc_socket='/tmp/nfc.sock'):
self.state = None
self.encoder_position = None
@ -19,6 +20,13 @@ class DoorHandle:
self.mqtt_client.connect_async(host=mqtt_host, port=mqtt_port)
self.mqtt_client.loop_start()
self.nfc_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
self.nfc_sock.connect(nfc_socket)
except Exception as e:
print(f"Could not connect to NFC socket at {nfc_socket}. Exception: {e}")
#raise
self.data_fields = ['name', 'organization', 'email', 'valid_thru']
# The callback for when the client receives a CONNACK response from the server.
@ -31,7 +39,7 @@ class DoorHandle:
# The callback for when a PUBLISH message is received from the server.
def on_message(self, client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
#print(msg.topic + " " + str(msg.payload))
if msg.topic == 'door/state/value':
self.state = msg.payload.decode()
elif msg.topic == 'door/position/value':
@ -70,8 +78,16 @@ class DoorHandle:
output += '|'
output += data[key] if data[key] else ''
output += '\n'
# write new tokens to file and trigger reload
with open(self.token_file, 'w') as f:
f.write(output)
self.nfc_sock.send(b'rld\n')
def open_door(self):
self.nfc_sock.send(b'open\n')
def close_door(self):
self.nfc_sock.send(b'close\n')
def get_most_recent_token(self):
# read last invalid token from logfile

View File

@ -6,6 +6,8 @@
<li><a href="{{ url_for('door_lock') }}">Home</a>
<li><a href="{{ url_for('register') }}">Token Registrierung</a>
<li><a href="{{ url_for('list_tokens') }}">Token Übersicht</a>
<li><a href="{{ url_for('open_door') }}">Tür öffnen</a>
<li><a href="{{ url_for('close_door') }}">Tür schließen</a>
{% if current_user.is_authenticated %}
<li><a href="{{ url_for('security.change_password') }}">Passwort ändern</a>
<li><a href="{{ url_for('security.logout') }}">Benutzer <span>{{ current_user.username }}</span> ausloggen</a>

View File

@ -42,7 +42,7 @@ def uia_username_mapper(identity):
def create_application(config):
# create door objects which provides access to the token file and current door state via MQTT
door = DoorHandle(token_file=config.token_file, mqtt_host=config.mqtt_host)
door = DoorHandle(token_file=config.token_file, mqtt_host=config.mqtt_host, nfc_socket=config.nfc_socket)
app = Flask(__name__, template_folder=config.template_folder, static_folder=config.static_folder)
@ -267,4 +267,22 @@ def create_application(config):
door.store_tokens(tokens)
return redirect('/tokens')
@app.route('/open')
@auth_required()
def open_door():
try:
door.open_door()
except Exception as e:
flash(f'Could not open door. Exception: {e}')
return redirect('/')
@app.route('/close')
@auth_required()
def close_door():
try:
door.close_door()
except Exception as e:
flash(f'Could not close door. Exception: {e}')
return redirect('/')
return app