Corrected some code style issues and added support for message properties to the on_message callback function. eric7

Tue, 20 Jul 2021 18:10:55 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 20 Jul 2021 18:10:55 +0200
branch
eric7
changeset 101
0eae5f616154
parent 100
9c29cfbd96c3
child 102
70b8858199f5

Corrected some code style issues and added support for message properties to the on_message callback function.

MqttMonitor/MqttClient.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
--- a/MqttMonitor/MqttClient.py	Tue Jul 20 17:48:21 2021 +0200
+++ b/MqttMonitor/MqttClient.py	Tue Jul 20 18:10:55 2021 +0200
@@ -32,17 +32,17 @@
     """
     Class implementing a PyQt wrapper around the paho MQTT client.
     
-    @signal onConnect(flags, rc) emitted after the client has connected to the
-        broker
-    @signal onDisconnected(rc) emitted after the client has disconnected from
+    @signal onConnectV3(flags, rc) emitted after the client has connected to
+        the broker
+    @signal onDisconnectedV3(rc) emitted after the client has disconnected from
         the broker
     @signal onLog(level, message) emitted to send client log data
-    @signal onMessage(topic, payload, qos, retain) emitted after a message has
-        been received by the client
+    @signal onMessageV3(topic, payload, qos, retain) emitted after a message
+        has been received by the client
     @signal onPublish(mid) emitted after a message has been published
-    @signal onSubscribe(mid, grantedQos) emitted after the client has
+    @signal onSubscribeV3(mid, grantedQos) emitted after the client has
         subscribed to some topics
-    @signal onUnsubscribe(mid) emitted after the client has unsubscribed from
+    @signal onUnsubscribeV3(mid) emitted after the client has unsubscribed from
         some topics
     @signal connectTimeout() emitted to indicate, that a connection attempt
         timed out
@@ -53,7 +53,7 @@
     onDisconnectedV5 = pyqtSignal(int, int)
     onLog = pyqtSignal(int, str)
     onMessageV3 = pyqtSignal(str, bytes, int, bool)
-    onMessageV5 = pyqtSignal(str, bytes, int, bool)
+    onMessageV5 = pyqtSignal(str, bytes, int, bool, dict)
     onPublish = pyqtSignal(int)
     onSubscribeV3 = pyqtSignal(int, tuple)
     onSubscribeV5 = pyqtSignal(int, list)
@@ -136,7 +136,7 @@
             self.__mqttClient.on_disconnect = (
                 lambda client, userdata, rc:
                     self.onDisconnectedV3.emit(rc)
-                )
+            )
             self.__mqttClient.on_subscribe = (
                 lambda client, userdata, mid, grantedQos, properties=None:
                     self.onSubscribeV3.emit(mid, grantedQos)
@@ -168,7 +168,8 @@
             self.__mqttClient.on_message = (
                 lambda client, userdata, message:
                     self.onMessageV5.emit(message.topic, message.payload,
-                                          message.qos, message.retain)
+                                          message.qos, message.retain,
+                                          message.properties.json())
             )
         self.__mqttClient.on_log = (
             lambda client, userdata, level, buf:
@@ -324,6 +325,9 @@
     def getProtocol(self):
         """
         Public method to get the MQTT protocol version.
+        
+        @return MQTT protocol version in use
+        @rtype int
         """
         return self.__protocol
     
--- a/MqttMonitor/MqttMonitorWidget.py	Tue Jul 20 17:48:21 2021 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Tue Jul 20 18:10:55 2021 +0200
@@ -253,10 +253,11 @@
             self.__connectedToBroker = True
             self.__connectionOptions = None
         
-        if packetType is not None:
-            msg = mqttReasonCode(rc, packetType)
-        else:
-            msg = mqttConnackMessage(rc)
+            msg = (
+                mqttReasonCode(rc, packetType)
+                if packetType is not None else
+                mqttConnackMessage(rc)
+            )
         self.__flashBrokerStatusLabel(msg)
         
         self.connectButton.setEnabled(True)
@@ -294,22 +295,25 @@
         
         @param rc MQTT error result code
         @type int
+        @param packetType packet type as reported by the client
+        @type int
         """
         self.__connectedToBroker = False
         
         # ensure, the client loop is stopped
         self.__client.stopLoop()
         
-        if packetType is not None:
+        msg = (
             # MQTT v5
-            msg = mqttReasonCode(rc, packetType)
-        else:
+            mqttReasonCode(rc, packetType)
+            if packetType is not None 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"))
@@ -371,6 +375,7 @@
     
     # TODO: add support for MQTT v5 properties
     @pyqtSlot(str, bytes, int, bool)
+    @pyqtSlot(str, bytes, int, bool, dict)
     def __messageReceived(self, topic, payload, qos, retain, properties=None):
         """
         Private slot to handle the receipt of a message.
@@ -412,8 +417,6 @@
         
         @param mid ID of the subscribe request
         @type int
-        @param grantedQos tuple of granted quality of service
-        @type tuple of int
         """
         if mid in self.__topicQueue:
             topic = self.__topicQueue.pop(mid)
@@ -949,9 +952,9 @@
         @param retain flag indicating a retained message
         @type bool
         @param properties properties sent with the message (MQTT v5)
-        @type Properties
+        @type dict
         """
-        # TODO: add Output for retain and properties
+        # TODO: add Output for properties
         scrollbarValue = self.messagesEdit.verticalScrollBar().value()
         
         textCursor = self.messagesEdit.textCursor()

eric ide

mercurial