MqttMonitor/MqttMonitorWidget.py

branch
eric7
changeset 99
420cb8adbf7e
parent 98
85d56e77e9df
child 100
9c29cfbd96c3
--- a/MqttMonitor/MqttMonitorWidget.py	Sun Jul 18 19:32:16 2021 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Mon Jul 19 20:00:17 2021 +0200
@@ -25,6 +25,7 @@
     MqttClient, MqttProtocols, mqttConnackMessage, mqttErrorMessage,
     mqttLogLevelString
 )
+from .MqttReasonCodes import mqttReasonCode
 
 import UI.PixmapCache
 import Utilities
@@ -211,12 +212,15 @@
                             protocol=protocol)
         
         # connect the MQTT client signals
-        client.onConnect.connect(self.__brokerConnected)
-        client.onDisconnected.connect(self.__brokerDisconnected)
+        client.onConnectV3.connect(self.__brokerConnected)
+        client.onConnectV5.connect(self.__brokerConnected)
+        client.onDisconnectedV3.connect(self.__brokerDisconnected)
+        client.onDisconnectedV5.connect(self.__brokerDisconnected)
         client.onLog.connect(self.__clientLog)
         client.onMessage.connect(self.__messageReceived)
         client.onPublish.connect(self.__messagePublished)
-        client.onSubscribe.connect(self.__topicSubscribed)
+        client.onSubscribeV3.connect(self.__topicSubscribed)
+        client.onSubscribeV5.connect(self.__topicSubscribedV5)
         client.onUnsubscribe.connect(self.__topicUnsubscribed)
         
         client.connectTimeout.connect(self.__connectTimeout)
@@ -229,14 +233,16 @@
     
     # TODO: change to accept ReasonCode for rc
     @pyqtSlot(dict, int)
-    def __brokerConnected(self, flags, rc):
+    @pyqtSlot(dict, int, int)
+    def __brokerConnected(self, flags, rc, packetType=None):
         """
         Private slot to handle being connected to a broker.
         
         @param flags flags set for the connection
         @type dict
-        @param rc CONNACK result code
-        @type int
+        @param rc CONNACK result code or tuple containing the result code and
+            the packet type of the MQTTv5 reason code
+        @type int or tuple of (int, int)
         """
         self.brokerStatusLabel.hide()
         
@@ -245,7 +251,10 @@
             self.__connectedToBroker = True
             self.__connectionOptions = None
         
-        msg = mqttConnackMessage(rc)
+        if packetType is not None:
+            msg = mqttReasonCode(rc, packetType)
+        else:
+            msg = mqttConnackMessage(rc)
         self.__flashBrokerStatusLabel(msg)
         
         self.connectButton.setEnabled(True)
@@ -276,7 +285,8 @@
         self.__setConnectButtonState()
     
     @pyqtSlot(int)
-    def __brokerDisconnected(self, rc):
+    @pyqtSlot(int, int)
+    def __brokerDisconnected(self, rc, packetType=None):
         """
         Private slot to handle a disconnection from the broker.
         
@@ -288,11 +298,16 @@
         # ensure, the client loop is stopped
         self.__client.stopLoop()
         
-        msg = (
-            mqttErrorMessage(rc)
-            if rc > 0 else
-            self.tr("Connection to Broker shut down cleanly.")
-        )
+        if packetType is not None:
+            # MQTT v5
+            msg = mqttReasonCode(rc, packetType)
+        else:
+            # MQTT v3
+            msg = (
+                mqttErrorMessage(rc)
+                if rc > 0 else
+                self.tr("Connection to Broker shut down cleanly.")
+            )
         self.__flashBrokerStatusLabel(msg)
         
         self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect"))
@@ -383,10 +398,11 @@
         # TODO: check this 'pass' statement
         pass
     
-    @pyqtSlot(int, tuple)
-    def __topicSubscribed(self, mid, grantedQos):
+    @pyqtSlot(int)
+    def __topicSubscribed(self, mid):
         """
-        Private slot to handle being subscribed to topics.
+        Private slot to handle being subscribed to topics (MQTT v3.1,
+        MQTT v3.1.1).
         
         @param mid ID of the subscribe request
         @type int
@@ -401,6 +417,20 @@
             self.__updateUnsubscribeTopicComboBox()
             self.__updatePublishTopicComboBox()
     
+    @pyqtSlot(int, list)
+    def __topicSubscribedV5(self, mid, reasonCodes):
+        """
+        Private slot to handle being subscribed to topics (MQTT v5).
+        
+        @param mid ID of the subscribe request
+        @type int
+        @param grantedQos tuple of granted quality of service
+        @type tuple of int
+        """
+        msg = mqttReasonCode(reasonCodes[0].value, reasonCodes[0].packetType)
+        self.__flashBrokerStatusLabel(msg)
+        self.__topicSubscribed(mid)
+    
     @pyqtSlot(int)
     def __topicUnsubscribed(self, mid):
         """

eric ide

mercurial