Add some logs for debugging
This commit is contained in:
@@ -1,6 +1,3 @@
|
|||||||
import json
|
|
||||||
import time
|
|
||||||
|
|
||||||
from src.services.mqtt_service import MQTTService
|
from src.services.mqtt_service import MQTTService
|
||||||
|
|
||||||
|
|
||||||
@@ -16,7 +13,10 @@ class MQTTForwarder:
|
|||||||
self.central_broker = central_mqtt
|
self.central_broker = central_mqtt
|
||||||
|
|
||||||
def start(self, src_topic: str, dst_topic: str):
|
def start(self, src_topic: str, dst_topic: str):
|
||||||
|
try:
|
||||||
def forward_handler(topic: str, msg: str):
|
def forward_handler(topic: str, msg: str):
|
||||||
self.central_broker.publish(dst_topic, msg)
|
self.central_broker.publish(dst_topic, msg)
|
||||||
|
|
||||||
self.local_broker.subscribe(src_topic, forward_handler)
|
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}")
|
||||||
@@ -27,7 +27,6 @@ class MQTTService:
|
|||||||
def _on_connect(self, client, userdata, flags, rc):
|
def _on_connect(self, client, userdata, flags, rc):
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
self._connected = True
|
self._connected = True
|
||||||
# Resubscribe all topics
|
|
||||||
for topic in self._subscriptions:
|
for topic in self._subscriptions:
|
||||||
self.client.subscribe(topic)
|
self.client.subscribe(topic)
|
||||||
else:
|
else:
|
||||||
@@ -40,13 +39,14 @@ class MQTTService:
|
|||||||
if not self._connected:
|
if not self._connected:
|
||||||
self.client.connect(self.address, self.port)
|
self.client.connect(self.address, self.port)
|
||||||
self.client.loop_start()
|
self.client.loop_start()
|
||||||
# Wait for connection
|
|
||||||
timeout = 5
|
timeout = 5
|
||||||
start = time.time()
|
start = time.time()
|
||||||
while not self._connected and time.time() - start < timeout:
|
while not self._connected and time.time() - start < timeout:
|
||||||
|
print(f"Connecting to {self.address}...")
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
if not self._connected:
|
if not self._connected:
|
||||||
raise ConnectionError(f"Cannot connect to MQTT broker at {self.address}:{self.port}")
|
raise ConnectionError(f"Cannot connect to MQTT broker at {self.address}:{self.port}")
|
||||||
|
print(f"Successfully connected to {self.address}")
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
if self._connected:
|
if self._connected:
|
||||||
@@ -55,6 +55,7 @@ class MQTTService:
|
|||||||
self._connected = False
|
self._connected = False
|
||||||
|
|
||||||
def publish(self, topic: str, data: str, qos: int = 0):
|
def publish(self, topic: str, data: str, qos: int = 0):
|
||||||
|
try:
|
||||||
self.connect()
|
self.connect()
|
||||||
payload = {
|
payload = {
|
||||||
"timestamp": int(time.time()),
|
"timestamp": int(time.time()),
|
||||||
@@ -62,8 +63,11 @@ class MQTTService:
|
|||||||
}
|
}
|
||||||
result = self.client.publish(topic, json.dumps(payload), qos=qos)
|
result = self.client.publish(topic, json.dumps(payload), qos=qos)
|
||||||
result.wait_for_publish()
|
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):
|
def subscribe(self, topic: str, handler: Callable[[str, str], None], qos: int = 0):
|
||||||
|
try:
|
||||||
self.connect()
|
self.connect()
|
||||||
self._subscriptions[topic] = handler
|
self._subscriptions[topic] = handler
|
||||||
|
|
||||||
@@ -74,3 +78,5 @@ class MQTTService:
|
|||||||
|
|
||||||
self.client.on_message = on_message
|
self.client.on_message = on_message
|
||||||
self.client.subscribe(topic, qos=qos)
|
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}")
|
||||||
Reference in New Issue
Block a user