From dd57019c99f89f8a17334a7e66ca591870486b2f Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 29 Dec 2025 14:10:13 +0100 Subject: [PATCH] Add some logs for debugging --- .../src/controllers/mqtt_forwarder.py | 12 +++--- api-customer/src/services/mqtt_service.py | 40 +++++++++++-------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/api-customer/src/controllers/mqtt_forwarder.py b/api-customer/src/controllers/mqtt_forwarder.py index 310231cb..25a86b28 100644 --- a/api-customer/src/controllers/mqtt_forwarder.py +++ b/api-customer/src/controllers/mqtt_forwarder.py @@ -1,6 +1,3 @@ -import json -import time - from src.services.mqtt_service import MQTTService @@ -16,7 +13,10 @@ class MQTTForwarder: self.central_broker = central_mqtt def start(self, src_topic: str, dst_topic: str): - def forward_handler(topic: str, msg: str): - self.central_broker.publish(dst_topic, msg) + try: + def forward_handler(topic: str, msg: str): + self.central_broker.publish(dst_topic, msg) - self.local_broker.subscribe(src_topic, forward_handler) \ No newline at end of file + self.local_broker.subscribe(src_topic, forward_handler) + except Exception as e: + print(f"An error occurred while forwarding from {src_topic} to {dst_topic}: {e}") \ No newline at end of file diff --git a/api-customer/src/services/mqtt_service.py b/api-customer/src/services/mqtt_service.py index 959300ae..d792fa56 100644 --- a/api-customer/src/services/mqtt_service.py +++ b/api-customer/src/services/mqtt_service.py @@ -27,7 +27,6 @@ class MQTTService: def _on_connect(self, client, userdata, flags, rc): if rc == 0: self._connected = True - # Resubscribe all topics for topic in self._subscriptions: self.client.subscribe(topic) else: @@ -40,13 +39,14 @@ class MQTTService: if not self._connected: self.client.connect(self.address, self.port) self.client.loop_start() - # Wait for connection timeout = 5 start = time.time() while not self._connected and time.time() - start < timeout: + print(f"Connecting to {self.address}...") time.sleep(0.1) if not self._connected: raise ConnectionError(f"Cannot connect to MQTT broker at {self.address}:{self.port}") + print(f"Successfully connected to {self.address}") def disconnect(self): if self._connected: @@ -55,22 +55,28 @@ class MQTTService: self._connected = False def publish(self, topic: str, data: str, qos: int = 0): - self.connect() - payload = { - "timestamp": int(time.time()), - "data": data - } - result = self.client.publish(topic, json.dumps(payload), qos=qos) - result.wait_for_publish() + try: + self.connect() + payload = { + "timestamp": int(time.time()), + "data": data + } + result = self.client.publish(topic, json.dumps(payload), qos=qos) + result.wait_for_publish() + except Exception as e: + print(f"An error occurred while publishing on {topic} on {self.address} : {e}") def subscribe(self, topic: str, handler: Callable[[str, str], None], qos: int = 0): - self.connect() - self._subscriptions[topic] = handler + try: + self.connect() + self._subscriptions[topic] = handler - def on_message(client, userdata, msg): - for sub_topic, h in self._subscriptions.items(): - if mqtt.topic_matches_sub(sub_topic, msg.topic): - h(msg.topic, msg.payload.decode()) + def on_message(client, userdata, msg): + for sub_topic, h in self._subscriptions.items(): + if mqtt.topic_matches_sub(sub_topic, msg.topic): + h(msg.topic, msg.payload.decode()) - self.client.on_message = on_message - self.client.subscribe(topic, qos=qos) \ No newline at end of file + self.client.on_message = on_message + self.client.subscribe(topic, qos=qos) + except Exception as e: + print(f"An error occurred while trying to subscribe to {topic} on {self.address} : {e}") \ No newline at end of file