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
|
||||
|
||||
|
||||
@@ -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)
|
||||
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):
|
||||
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)
|
||||
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}")
|
||||
Reference in New Issue
Block a user