import paho.mqtt.client as mqtt import uuid import time import threading import socket class MqttClient: """ Handles the connection and message processing for a single MQTT station. This is a standard Python class, with no GUI dependencies. """ def __init__(self, broker, port, user, password, station_id, on_message_callback): self.broker = broker self.port = port self.user = user self.password = password self.station_id = station_id self.on_message_callback = on_message_callback unique_id = str(uuid.uuid4()) self.client_id = f"WebApp-Backend-{self.station_id}-{unique_id}" self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, self.client_id) # Assign callback functions self.client.on_connect = self.on_connect self.client.on_message = self.on_message self.client.on_disconnect = self.on_disconnect if self.user and self.password: self.client.username_pw_set(self.user, self.password) self.is_connected = False self.reconnect_delay = 1 self.max_reconnect_delay = 60 self.stop_thread = False # --- CORRECTED CALLBACK SIGNATURES --- def on_connect(self, client, userdata, flags, reason_code, properties): """Callback for when the client connects to the broker.""" if reason_code == 0: self.is_connected = True self.reconnect_delay = 1 print(f"Successfully connected to MQTT broker for station: {self.station_id}") topic_base = f"VEC/batterySmartStation/v100/{self.station_id}/#" # topic_base = f"VEC/batterySmartStation/v100/+/+" self.client.subscribe(topic_base) print(f"Subscribed to: {topic_base}") else: print(f"Failed to connect to MQTT for station {self.station_id}, return code {reason_code}") def on_disconnect(self, client, userdata, disconnect_flags, reason_code, properties): """Callback for when the client disconnects.""" print(f"Disconnected from MQTT for station {self.station_id}. Will attempt to reconnect...") def on_message(self, client, userdata, msg): """Callback for when a message is received from the broker.""" try: self.on_message_callback(self.station_id, msg.topic, msg.payload) except Exception as e: print(f"Error processing message in callback for topic {msg.topic}: {e}") def connect(self): """Connects the client to the MQTT broker.""" print(f"Attempting to connect to {self.broker}:{self.port} with client ID: {self.client_id}") try: self.client.connect(self.broker, self.port, 60) except Exception as e: print(f"Error connecting to MQTT for station {self.station_id}: {e}") def start(self): """Starts the MQTT client's network loop in a separate thread.""" self.connect() self.client.loop_start() def stop(self): """Stops the MQTT client's network loop.""" print(f"Stopping MQTT client for station: {self.station_id}") self.client.loop_stop()