eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.13k stars 723 forks source link

about on_disconnect callback #755

Closed ajo79 closed 6 months ago

ajo79 commented 9 months ago

I am using PyQt6 and paho mqtt in my application. I have written class MqttClient. in my main application i got the signal self.ConnectionStatus.emit(f"Subscribed") But I am not getting the signal self.ConnectionStatus.emit(f"Disconnected") when i call disconnect

import sys, time
import paho.mqtt.client as mqtt
from PyQt6.QtCore import QObject, pyqtSignal

class MqttClient(QObject):

    messageReceived = pyqtSignal(str)  # Signal to send the received message to the GUI
    ConnectionStatus = pyqtSignal(str)  # Signal to send connection error messages to the GUI

    def __init__(self, broker_host, topic):
        super().__init__()  # Call the base class's __init__ method
        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_message = self.on_message
        self.client.on_disconnect = self.on_disconnect  # Add on_disconnect callback
        self.broker_host = broker_host
        self.topic = topic

        print("mqtt client: Init done ")

    def connect(self):
        try:
            self.client.connect(self.broker_host, 1883, 60)
            self.client.loop_start()
            print("mqtt client: loop started ")
            self.ConnectionStatus.emit(f"Connected")           
        except Exception as e:
            print(f"mqtt client: Connection error: {str(e)}")
            self.ConnectionStatus.emit(f"error: {str(e)}") 
            raise e  

    def on_connect(self, client, userdata, flags, rc):
        if rc == 0:
            #print("mqtt client: Connected to MQTT broker.")
            client.subscribe(self.topic)
            print("mqtt client: Subscribed")
            self.ConnectionStatus.emit(f"Subscribed")

        else:
            #print(f"mqtt client: Failed to connect to MQTT broker. Return code: {rc}")
            self.ConnectionStatus.emit(f"{rc}")

    def on_disconnect(self, client, userdata, rc):
        if rc == 0:
            client.close()
            print("mqtt client: Disconnected")
        else:
            self.ConnectionStatus.emit(f"{rc}")
        self.ConnectionStatus.emit(f"Disconnected")

    def on_message(self, client, userdata, msg):
        current_value = msg.payload.decode()
        #print("mqtt client: Received current value: " + current_value)
        self.display_current_value(current_value)
        self.messageReceived.emit(current_value)  # Emit the signal with the received value

    def display_current_value(self, current_value):
        # Update your GUI to display the current value here
        # For example, update a label or text field
        print("Displaying current value: " + current_value)
MattBrittan commented 7 months ago

Can you please add a print("mqtt client: on_disconnect called") to the start of on_disconnect and confirm if this is output? (just want to narrow down the issue).

MattBrittan commented 6 months ago

I'm going to close this due to the lack of response to my request for further information; please feel free to reopen if you are able to provide the info.