use MQTT to get last invalid token

This commit is contained in:
Simon Pirkelmann 2021-03-13 18:54:32 +01:00
parent 28f95740af
commit 4d405c66d6
3 changed files with 10 additions and 14 deletions

2
app.py
View File

@ -245,4 +245,4 @@ def deactivate_token(token):
if __name__ == '__main__':
app.run()
app.run(host='0.0.0.0')

View File

@ -1,14 +1,13 @@
import paho.mqtt.client as mqtt
import re
class DoorHandle:
def __init__(self, host, port=1883):
def __init__(self, host, port=1883, token_file='/etc/door_tokens'):
self.state = None
self.encoder_position = None
self.last_invalid = {}
self.token_file = 'door_tokens'
self.nfc_logfile = 'nfc.log'
self.token_file = token_file
self.mqtt_client = mqtt.Client()
self.mqtt_client.on_connect = self.on_connect
@ -33,6 +32,9 @@ class DoorHandle:
self.state = msg.payload.decode()
elif msg.topic == 'door/position/value':
self.encoder_position = int(msg.payload)
elif msg.topic == 'token/last_invalid':
timestamp, token = msg.payload.split(';')
self.last_invalid = {'token': token, 'timestamp': timestamp}
def get_tokens(self):
tokens = {}
@ -68,11 +70,5 @@ class DoorHandle:
f.write(output)
def get_most_recent_token(self):
# read last invalid token from logfile
with open(self.nfc_logfile) as f:
nfc_log = f.read()
match = re.search(r"(?P<date>\d{4}-\d{2}-\d{2}) (?P<time>\d{2}:\d{2}:\d{2}) Invalid token: (?P<token>[[:xdigit:]]{14})",
nfc_log, re.REVERSE)
return match
return self.last_invalid

View File

@ -18,7 +18,7 @@
{% endwith %}
{% if token is not none %}
Letzter unregistrierter Token: {{ token['token'] }} <br>
Gelesen am {{ token['date'] }} um {{ token['time'] }}
Timestamp {{ token['timestamp'] }}
<div>
<p>
@ -78,4 +78,4 @@
});
});
</script>
</html>
</html>