Corrected the connection code for MQTTv5. eric7

Sun, 18 Jul 2021 19:32:16 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 18 Jul 2021 19:32:16 +0200
branch
eric7
changeset 98
85d56e77e9df
parent 97
21f9c010dc42
child 99
420cb8adbf7e

Corrected the connection code for MQTTv5.

MqttMonitor/MqttClient.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
diff -r 21f9c010dc42 -r 85d56e77e9df MqttMonitor/MqttClient.py
--- a/MqttMonitor/MqttClient.py	Sun Jul 18 18:30:15 2021 +0200
+++ b/MqttMonitor/MqttClient.py	Sun Jul 18 19:32:16 2021 +0200
@@ -148,22 +148,22 @@
         self.stopLoop()
         self.connectTimeout.emit()
     
-    def reinitialise(self, clientId="", cleanSession=True, userdata=None):
-        """
-        Public method to reinitialize the client with given data.
-        
-        @param clientId ID to be used for the client
-        @type str
-        @param cleanSession flag indicating to start a clean session
-        @type bool
-        @param userdata user data
-        @type any
-        """
-        self.__mqttClient.reinitialise(
-            client_id=clientId, clean_session=cleanSession, userdata=userdata)
-        
-        self.__initCallbacks()
-    
+##    def reinitialise(self, clientId="", cleanSession=True, userdata=None):
+##        """
+##        Public method to reinitialize the client with given data.
+##        
+##        @param clientId ID to be used for the client
+##        @type str
+##        @param cleanSession flag indicating to start a clean session
+##        @type bool
+##        @param userdata user data
+##        @type any
+##        """
+##        self.__mqttClient.reinitialise(
+##            client_id=clientId, clean_session=cleanSession, userdata=userdata)
+##        
+##        self.__initCallbacks()
+##    
     def setConnectionTimeout(self, timeout):
         """
         Public method to set the connection timeout value.
@@ -295,8 +295,8 @@
             trying to connect with the given parameters
         @type bool
         """
-        if reinit:
-            self.reinitialise()
+##        if reinit:
+##            self.reinitialise()
         # TODO: MQTTv5: use 'clean_start' set to the remembered 'cleanSession'
         # TODO: MQTTv5: add support for MQTTv5 properties
         self.__mqttClient.connect_async(
@@ -331,14 +331,9 @@
             parametersDict = self.defaultConnectionOptions()
             parametersDict.update(options)
             
-            # step 1: reinitialize to set the client ID and clean session flag
-            self.reinitialise(
-                clientId=parametersDict["ClientId"],
-                cleanSession=parametersDict["CleanSession"]
-            )
             self.setConnectionTimeout(parametersDict["ConnectionTimeout"])
             
-            # step 2: set username and password
+            # step 1: set username and password
             if parametersDict["Username"]:
                 if parametersDict["Password"]:
                     self.setUserCredentials(
@@ -347,7 +342,7 @@
                 else:
                     self.setUserCredentials(parametersDict["Username"])
             
-            # step 3: set last will data
+            # step 2: set last will data
             if parametersDict["WillTopic"]:
                 if parametersDict["WillMessage"]:
                     willMessage = parametersDict["WillMessage"]
@@ -359,7 +354,7 @@
                                  parametersDict["WillQos"],
                                  parametersDict["WillRetain"])
             
-            # step 4: set TLS parameters
+            # step 3: set TLS parameters
             if parametersDict["TlsEnable"]:
                 if (
                     parametersDict["TlsCaCert"] and
@@ -376,10 +371,10 @@
                     # use default TLS configuration
                     self.setTLS()
             
-            # step 5: connect to server
+            # step 4: connect to server
             self.connectToServer(host, port=port,
-                                 keepalive=parametersDict["Keepalive"],
-                                 reinit=False)
+                                 keepalive=parametersDict["Keepalive"])
+##                                 reinit=False)
         else:
             keepalive = self.defaultConnectionOptions["Keepalive"]
             self.connectToServer(host, port=port, keepalive=keepalive,
diff -r 21f9c010dc42 -r 85d56e77e9df MqttMonitor/MqttMonitorWidget.py
--- a/MqttMonitor/MqttMonitorWidget.py	Sun Jul 18 18:30:15 2021 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Sun Jul 18 19:32:16 2021 +0200
@@ -190,17 +190,25 @@
     ## Slots handling MQTT related signals
     #######################################################################
     
-    def __createClient(self, protocol=MqttProtocols.MQTTv311):
+    # TODO: make MQTT default protocol version a configuration option
+    # (config page)
+    def __createClient(self, clientId="", cleanSession=None,
+                       protocol=MqttProtocols.MQTTv311):
         """
         Private method to instantiate a MQTT client for a given protocol.
         
+        @param clientId ID to be used for the client
+        @type str
+        @param cleanSession flag indicating to start a clean session
+        @type bool
         @param protocol MQTT protocol version to be used (defaults to
             MqttProtocols.MQTTv311)
         @type MqttProtocols (optional)
         @return created and connected MQTT client object
         @rtype MqttClient
         """
-        client = MqttClient(protocol=protocol)
+        client = MqttClient(clientId=clientId, cleanSession=cleanSession,
+                            protocol=protocol)
         
         # connect the MQTT client signals
         client.onConnect.connect(self.__brokerConnected)
@@ -219,6 +227,7 @@
     ## Slots handling MQTT related signals
     #######################################################################
     
+    # TODO: change to accept ReasonCode for rc
     @pyqtSlot(dict, int)
     def __brokerConnected(self, flags, rc):
         """
@@ -1046,7 +1055,10 @@
                 self.__client.connectToServer(host, port=port)
             else:
                 self.__client = self.__createClient(
-                    protocol=self.__connectionOptions["Protocol"])
+                    clientId=self.__connectionOptions["ClientId"],
+                    cleanSession=self.__connectionOptions["CleanSession"],
+                    protocol=self.__connectionOptions["Protocol"]
+                )
                 self.__client.connectToServerWithOptions(
                     host, port=port, options=self.__connectionOptions)
     
@@ -1074,6 +1086,10 @@
             
             self.connectButton.setEnabled(False)
             
-            self.__client = self.__createClient(protocol=protocol)
+            self.__client = self.__createClient(
+                clientId=connectionProfile["ClientId"],
+                cleanSession=connectionProfile["CleanSession"],
+                protocol=protocol
+            )
             self.__client.connectToServerWithOptions(
                 host, port=port, options=connectionProfile)

eric ide

mercurial