Skip to content

Commit

Permalink
Fixed parsing PAHO error codes (#29)
Browse files Browse the repository at this point in the history
* Improved logger output

- Added log message on successful database or MQTT connection
- Added log message for PAHO error code 7 (connection lost)

* Fixed parsing PAHO error codes
  • Loading branch information
PatrickBaus authored Jan 21, 2025
1 parent d0dab0d commit 2e9e17a
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions database_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ async def mqtt_producer(
username=username,
password=password,
) as client:
self.__logger.info("Connected to MQTT broker at '%s:%i'", hostname, port)
await client.subscribe("sensors/#", qos=2)
async for message in client.messages:
# if message.topic.matches("sensors/+/+/+"):
Expand All @@ -219,18 +220,25 @@ async def mqtt_producer(
# and here (bottom):
# https://github.com/sbtinstruments/aiomqtt/blob/main/aiomqtt/exceptions.py
reason_code = exc.rc
self.__logger.error(
"PAHO MQTT code error: %s. Reconnecting.",
reason_code,
)
if reason_code == 7:
self.__logger.error(
"Connection to MQTT server (%s:%i) lost. Reconnecting.",
hostname,
port,
)
else:
self.__logger.error(
"PAHO MQTT code error: %s. Reconnecting.",
reason_code,
)
except ConnectionRefusedError:
self.__logger.warning(
"Connection refused by MQTT server (%s:%i). Retrying.",
hostname,
port,
)
except aiomqtt.MqttError as exc:
error = re.search(r"^\[Errno (\d+)\]", str(exc))
error = re.search(r"\[Errno (\d+)]", str(exc))
if error is not None:
error_code = int(error.group(1))
if error_code == 111:
Expand Down Expand Up @@ -282,6 +290,12 @@ async def mqtt_consumer(
database_config["port"],
)
async with self.database_connector(**database_config) as conn:
self.__logger.info(
"Connected consumer (%s) to database at '%s:%i",
worker_name,
database_config["hostname"],
database_config["port"],
)
while "queue not done":
if item is None:
# only get new data if we have pushed everything to the DB
Expand Down

0 comments on commit 2e9e17a

Please sign in to comment.