From 7bca060dabbda4f0a32369365ee1ae3d2319105a Mon Sep 17 00:00:00 2001 From: Kai Kriegel Date: Thu, 16 Jun 2022 19:50:24 +0200 Subject: [PATCH] fixed single --- fabapi/connect.py | 4 ++-- schema | 2 +- single.py | 40 +++++++++++++++++++++++----------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/fabapi/connect.py b/fabapi/connect.py index af0a642..7b23201 100644 --- a/fabapi/connect.py +++ b/fabapi/connect.py @@ -93,7 +93,7 @@ async def connect_with_fabfire_initial(host, port, uid): return auth, response.challenge else: logging.error(f"Auth failed: {response.failed.code}, additional info: {response.failed.additionalData}") - return None + return None, None async def connect_with_fabfire_step(auth, msg): @@ -106,4 +106,4 @@ async def connect_with_fabfire_step(auth, msg): return response.successful.additionalData, response.successful.session # dont care, message, we are done else: logging.error(f"Auth failed: {response.failed.code}, additional info: {response.failed.additionalData}") - return None + return None, None diff --git a/schema b/schema index 18ed9c2..19f20f5 160000 --- a/schema +++ b/schema @@ -1 +1 @@ -Subproject commit 18ed9c2ae6a221f57d19e255165c7ebc4508e9af +Subproject commit 19f20f5154f0eced6288ff56cac840025ee51da1 diff --git a/single.py b/single.py index 6304bbc..7712dea 100644 --- a/single.py +++ b/single.py @@ -1,33 +1,35 @@ import asyncio import logging - +import os from asyncio_mqtt import Client import json import fabapi from timer import Timer -BFFHD_HOST = "127.0.0.1" +BFFHD_HOST = "localhost" MQTT_HOST = "127.0.0.1" -MACHINE_URN = "" -READER_ID = "" +MACHINE_URN = "urn:fabaccess:resource:MachineA2" +READER_ID = "111" + +auth_cap = None +session = None async def main(): - done = False - auth_cap = None - session = None - msg = None + global auth_cap + global session async with Client(MQTT_HOST) as client: - await client.publish("/cmnd/reader", payload='{"Cmd":"haltPICC"}', qos=2, retain=False) - await client.publish("/cmnd/reader", payload='{"Cmd": "message", "MssgID": 0, "AddnTxt":" Karte auflegen"}', qos=2, retain=False) + await client.publish(f"/cmnd/reader/{READER_ID}", payload='{"Cmd":"haltPICC"}', qos=2, retain=False) + await client.publish(f"/cmnd/reader/{READER_ID}", payload='{"Cmd": "message", "MssgID": 0, "AddnTxt":" Karte auflegen"}', qos=2, retain=False) async with client.filtered_messages(f"/rfid_reader/{READER_ID}") as messages: await client.subscribe("/rfid_reader/#") + logging.info(f"Connected to reader {READER_ID} on MQTT Broker {MQTT_HOST}") async for message in messages: response_for_reader = None if not auth_cap: - timeout_timer = Timer(10, lambda: handle_timeout(client)) + timeout_timer = Timer(2, lambda: handle_timeout(client)) auth_cap, response_for_reader = await fabapi.connect_with_fabfire_initial( - BFFHD_HOST, 59961, message.payload) + BFFHD_HOST, 59661, message.payload) elif not session: response_for_reader, session = await fabapi.connect_with_fabfire_step(auth_cap, message.payload) @@ -66,13 +68,17 @@ async def main(): if response_for_reader: await client.publish(f"/cmnd/reader/{READER_ID}", payload=response_for_reader, qos=2, retain=False) - await client.publish("/cmnd/reader", payload=msg, qos=2, retain=False) - -async def handle_timeout(client, reader_id): - await client.publish(f"/cmnd/reader/{reader_id}", payload='{"Cmd":"haltPICC"}', qos=2, +async def handle_timeout(client): + global auth_cap + global session + await client.publish(f"/cmnd/reader/{READER_ID}", payload='{"Cmd":"haltPICC"}', qos=2, retain=False) - logging.critical(f"authentication timed out on reader {reader_id}") + logging.critical(f"authentication timed out on reader {READER_ID}") + auth_cap = None + session = None if __name__ == "__main__": + LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper() + logging.basicConfig(level=LOGLEVEL) loop = asyncio.get_event_loop() loop.run_until_complete(main())