MqttMonitor/MqttClient.py

branch
eric7
changeset 97
21f9c010dc42
parent 95
d830314cca87
child 98
85d56e77e9df
--- a/MqttMonitor/MqttClient.py	Mon Jun 28 17:44:07 2021 +0200
+++ b/MqttMonitor/MqttClient.py	Sun Jul 18 18:30:15 2021 +0200
@@ -7,6 +7,8 @@
 Module implementing a PyQt wrapper around the paho MQTT client.
 """
 
+import enum
+
 from PyQt6.QtCore import (
     pyqtSignal, pyqtSlot, QObject, QCoreApplication, QTimer
 )
@@ -16,6 +18,15 @@
 from Utilities.crypto import pwConvert
 
 
+class MqttProtocols(enum.IntEnum):
+    """
+    Class defining the supported MQTT protocol versions.
+    """
+    MQTTv31 = mqtt.MQTTv31
+    MQTTv311 = mqtt.MQTTv311
+    MQTTv5 = mqtt.MQTTv5
+
+
 class MqttClient(QObject):
     """
     Class implementing a PyQt wrapper around the paho MQTT client.
@@ -73,7 +84,7 @@
         @param userdata user data
         @type any
         @param protocol version of the MQTT protocol to use
-        @type int, one of mqtt.MQTTv3, mqtt.MQTTv311 or mqtt.MQTTv5
+        @type int, one of mqtt.MQTTv31, mqtt.MQTTv311 or mqtt.MQTTv5
         @param transport transport to be used
         @type str, one of "tcp" or "websockets"
         @param parent reference to the parent object
@@ -91,10 +102,13 @@
         
         self.onConnect.connect(self.__connectTimeoutTimer.stop)
         
-        # TODO: MQTTv5: set clean_session to None and remember cleanSession
+        self.__cleanSession = cleanSession
+        if protocol == MqttProtocols.MQTTv5:
+            cleanSession = None
+        
         self.__mqttClient = mqtt.Client(
-            client_id=clientId, clean_session=cleanSession, userdata=None,
-            protocol=mqtt.MQTTv311, transport="tcp")
+            client_id=clientId, clean_session=cleanSession, userdata=userdata,
+            protocol=int(protocol), transport=transport)
         
         self.__initCallbacks()
 
@@ -371,20 +385,22 @@
             self.connectToServer(host, port=port, keepalive=keepalive,
                                  bindAddress=bindAddress)
     
-    def defaultConnectionOptions(self):
+    @classmethod
+    def defaultConnectionOptions(cls):
         """
-        Public method to get a connection options dictionary with default
+        Class method to get a connection options dictionary with default
         values.
         
         @return dictionary containing the default connection options. It has
-            the keys "ClientId", "Keepalive", "CleanSession", "Username",
-            "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain",
-            "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey",
-            "ConnectionTimeout".
+            the keys "ClientId", "Protocol", "ConnectionTimeout", "Keepalive",
+            "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
+            "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", "TlsClientCert",
+            "TlsClientKey".
         @rtype dict
         """
         return {
             "ClientId": "ERIC7_MQTT_MONITOR_CLIENT",
+            "Protocol": MqttProtocols.MQTTv311,
             "ConnectionTimeout": MqttClient.DefaultConnectTimeout,
             "Keepalive": 60,
             "CleanSession": True,

eric ide

mercurial