Brought some adjustment to mqtt service and forwarder

This commit is contained in:
2025-12-29 13:46:05 +01:00
parent 4110e6823b
commit 3d15e8e26a
3 changed files with 78 additions and 74 deletions

View File

@@ -1,55 +1,76 @@
import json
import time
from typing import Callable
from typing import Callable, Optional
import paho.mqtt.client as mqtt
from paho.mqtt.client import Client
class MQTTService:
client : Client
def __init__(self, address: str, port: int):
def __init__(self, address: str, port: int, client_id: Optional[str] = None,
username: Optional[str] = None, password: Optional[str] = None,
ca_certs: Optional[str] = None):
self.address = address
self.port = port
self.client = mqtt.Client()
self.client = mqtt.Client(client_id=client_id)
if username and password:
self.client.username_pw_set(username, password)
if ca_certs:
self.client.tls_set(ca_certs=ca_certs)
else:
self.client.tls_set()
self.client.tls_insecure_set(True)
self._connected = False
self.client.on_connect = self._on_connect
self.client.on_disconnect = self._on_disconnect
self.client.on_message = None
self._subscriptions = {}
def publish(self, client_id: str, topic: str, data: str, qos: int = 0):
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:
print(f"MQTT connection failed with code {rc}")
try:
self.__connect()
def _on_disconnect(self, client, userdata, rc):
self._connected = False
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("MQTT error:", e)
self.__disconnect()
def subscribe(self, topic: str, handler: Callable[[str], None]):
def on_message(client, userdata, msg):
handler(msg.payload.decode())
try:
self.__connect()
self.client.message_callback_add(topic, on_message)
self.client.subscribe(topic)
except Exception as e:
print("MQTT error:", e)
self.__disconnect()
def __connect(self):
if not self.client.is_connected():
def connect(self):
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:
time.sleep(0.1)
if not self._connected:
raise ConnectionError(f"Cannot connect to MQTT broker at {self.address}:{self.port}")
def __disconnect(self):
if self.client.is_connected():
def disconnect(self):
if self._connected:
self.client.disconnect()
self.client.loop_stop()
self.client.loop_stop()
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()
def subscribe(self, topic: str, handler: Callable[[str, str], None], qos: int = 0):
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())
self.client.on_message = on_message
self.client.subscribe(topic, qos=qos)