MqttMonitor/MqttMonitorWidget.py

branch
eric7
changeset 103
5fe4f179975f
parent 102
70b8858199f5
child 104
9a4c9b7f078c
--- a/MqttMonitor/MqttMonitorWidget.py	Wed Jul 21 20:10:36 2021 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Thu Jul 22 19:02:32 2021 +0200
@@ -74,6 +74,8 @@
         self.__messagesTopicFormat.setFontWeight(QFont.Weight.Bold)
         self.__messagesQosFormat = self.messagesEdit.currentCharFormat()
         self.__messagesQosFormat.setFontItalic(True)
+        self.__messagesSubheaderFormat = self.messagesEdit.currentCharFormat()
+        self.__messagesSubheaderFormat.setFontUnderline(True)
         
         self.__propertiesFormat = self.propertiesEdit.currentCharFormat()
         self.__propertiesTopicFormat = self.propertiesEdit.currentCharFormat()
@@ -159,6 +161,10 @@
         self.__publishedTopics = []
         self.__updatePublishTopicComboBox()
         self.publishButton.setEnabled(False)
+        self.publishPropertiesButton.setIcon(
+            UI.PixmapCache.getIcon("listSelection"))
+        self.publishPropertiesButton.setEnabled(False)
+        self.publishPropertiesButton.setVisible(False)
         
         self.__connectionOptions = None
         
@@ -319,6 +325,8 @@
                 self.__client.getProtocol() == MqttProtocols.MQTTv5)
             self.publishGroup.setEnabled(True)
             self.brokerStatusButton.setEnabled(True)
+            self.publishPropertiesButton.setVisible(
+                self.__client.getProtocol() == MqttProtocols.MQTTv5)
             
             self.__statusLoadValues.clear()
             self.__clearBrokerStatusLabels()
@@ -376,6 +384,7 @@
         self.unsubscribeGroup.setEnabled(False)
         self.unsubscribePropertiesButton.setVisible(False)
         self.publishGroup.setEnabled(False)
+        self.publishPropertiesButton.setVisible(False)
         self.brokerStatusButton.setEnabled(False)
         
         self.__statusLoadValues.clear()
@@ -466,6 +475,8 @@
         @param mid ID of the subscribe request
         @type int
         """
+        # TODO: remember the successfully subscribed topic
+        # TODO: max. number of recent topics as a config item
         if mid in self.__topicQueue:
             topic = self.__topicQueue.pop(mid)
             self.__subscribedTopics.append(topic)
@@ -723,6 +734,18 @@
                 topic, properties=properties)
             self.__topicQueue[mid] = topic
     
+    @pyqtSlot()
+    def on_publishPropertiesButton_clicked(self):
+        """
+        Private slot to edit the publish user properties.
+        """
+        topic = self.publishTopicComboBox.currentText()
+        self.__editProperties(
+            "publish",
+            self.tr("PUBLISH: User Properties for '{0}'").format(topic),
+            topic
+        )
+    
     @pyqtSlot(str)
     def on_publishTopicComboBox_editTextChanged(self, topic):
         """
@@ -732,6 +755,7 @@
         @type str
         """
         self.publishButton.setEnabled(bool(topic))
+        self.publishPropertiesButton.setEnabled(bool(topic))
     
     @pyqtSlot()
     def on_publishButton_clicked(self):
@@ -765,8 +789,15 @@
                 # use empty string together with the retain flag to clean
                 # a retained message by sending None instead
                 payloadStr = None
+        properties = (
+            self.__plugin.getPreferences("PublishProperties").get(topic, [])
+            if self.__client.getProtocol() == MqttProtocols.MQTTv5 else
+            None
+        )
         
-        msgInfo = self.__client.publish(topic, payloadStr, qos, retain)
+        msgInfo = self.__client.publish(
+            topic, payload=payloadStr, qos=qos, retain=retain,
+            properties=properties)
         if msgInfo.rc == 0:
             if topic not in self.__publishedTopics:
                 self.__publishedTopics.append(topic)
@@ -780,8 +811,14 @@
         Private slot to clear the retained messages for the topic.
         """
         topic = self.publishTopicComboBox.currentText()
+        properties = (
+            self.__plugin.getPreferences("PublishProperties").get(topic, [])
+            if self.__client.getProtocol() == MqttProtocols.MQTTv5 else
+            None
+        )
         
-        msgInfo = self.__client.publish(topic, payload=None, retain=True)
+        msgInfo = self.__client.publish(
+            topic, payload=None, retain=True, properties=properties)
         if msgInfo.rc == 0:
             if topic not in self.__publishedTopics:
                 self.__publishedTopics.append(topic)
@@ -988,6 +1025,7 @@
             brokerList.remove(hostAndPort)
         brokerList.insert(0, hostAndPort)
         # limit to most recently used 20 entries
+        # TODO: make the amount of recent brokers a config item
         brokerList = brokerList[:20]
         self.__plugin.setPreferences("RecentBrokersWithPort", brokerList)
         
@@ -1112,19 +1150,25 @@
             self.messagesEdit.insertPlainText(self.tr("Retained Message\n"))
         
         if properties:
-            self.messagesEdit.setCurrentCharFormat(self.__messagesTopicFormat)
+            self.messagesEdit.setCurrentCharFormat(
+                self.__messagesSubheaderFormat)
             self.messagesEdit.insertPlainText(self.tr("Properties:\n"))
             self.messagesEdit.setCurrentCharFormat(self.__messagesFormat)
-            for name, value in sorted(properties.items):
+            for name, value in sorted(properties.items()):
                 self.messagesEdit.insertPlainText(
                     self.tr("{0}: {1}\n", "property name, property value")
                     .format(name, value)
                 )
         
+        self.messagesEdit.setCurrentCharFormat(self.__messagesSubheaderFormat)
+        self.messagesEdit.insertPlainText(self.tr("Message:\n"))
         payloadStr = str(payload, encoding="utf-8", errors="replace")
+        payloadStr = Utilities.filterAnsiSequences(payloadStr)
         self.messagesEdit.setCurrentCharFormat(self.__messagesFormat)
-        self.messagesEdit.insertPlainText(
-            Utilities.filterAnsiSequences(payloadStr))
+        if payloadStr:
+            self.messagesEdit.insertPlainText(payloadStr)
+        else:
+            self.messagesEdit.insertPlainText(self.tr("<empty>"))
         
         if self.followMessagesCheckBox.isChecked():
             self.messagesEdit.ensureCursorVisible()
@@ -1273,7 +1317,7 @@
             self.__plugin.setPreferences("MostRecentProfile", profileName)
             
             profilesDict = self.__plugin.getPreferences("BrokerProfiles")
-            connectionProfile = copy.copy(profilesDict[profileName])
+            connectionProfile = copy.deepcopy(profilesDict[profileName])
             host = connectionProfile["BrokerAddress"]
             port = connectionProfile["BrokerPort"]
             try:
@@ -1348,11 +1392,12 @@
         @param key key to retrieve the right properties
         @type str
         """
-        from .MqttUserPropertiesEditor import MqttUserPropertiesEditor
+        from .MqttUserPropertiesEditor import MqttUserPropertiesEditorDialog
         
         preferencesKey = "{0}Properties".format(propertiesType.capitalize())
         properties = self.__plugin.getPreferences(preferencesKey)
-        dlg = MqttUserPropertiesEditor(header, properties.get(key, []), self)
+        dlg = MqttUserPropertiesEditorDialog(
+            header, properties.get(key, []), self)
         if dlg.exec() == QDialog.DialogCode.Accepted:
             properties[key] = dlg.getProperties()
             self.__plugin.setPreferences(preferencesKey, properties)

eric ide

mercurial