MqttMonitor/MqttMonitorWidget.py

changeset 8
95b56dcfa09b
parent 7
63e046d95702
child 10
7e0e921dc7ea
diff -r 63e046d95702 -r 95b56dcfa09b MqttMonitor/MqttMonitorWidget.py
--- a/MqttMonitor/MqttMonitorWidget.py	Thu Aug 30 18:57:57 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Thu Aug 30 19:45:12 2018 +0200
@@ -74,6 +74,10 @@
         self.__topicQueue = {}
         self.__updateUnsubscribeTopicComboBox()
         
+        self.__publishedTopics = []
+        self.__updatePublishTopicComboBox()
+        self.publishButton.setEnabled(False)
+        
         prefix = MqttMonitorWidget.BrokerStatusTopicPrefix
         self.__statusLabelMapping = {
             # broker
@@ -152,6 +156,7 @@
         
         self.subscribeGroup.setEnabled(True)
         self.unsubscribeGroup.setEnabled(True)
+        self.publishGroup.setEnabled(True)
         self.brokerStatusButton.setEnabled(True)
         
         self.__statusLoadValues.clear()
@@ -180,9 +185,11 @@
         self.__subscribedTopics = []
         self.__topicQueue = {}
         self.__updateUnsubscribeTopicComboBox()
+        self.__updatePublishTopicComboBox()
         
         self.subscribeGroup.setEnabled(False)
         self.unsubscribeGroup.setEnabled(False)
+        self.publishGroup.setEnabled(False)
         self.brokerStatusButton.setEnabled(False)
         
         self.__statusLoadValues.clear()
@@ -233,6 +240,7 @@
             self.subscribeTopicEdit.clear()
             
             self.__updateUnsubscribeTopicComboBox()
+            self.__updatePublishTopicComboBox()
     
     @pyqtSlot(int)
     def __topicUnsubscribed(self, mid):
@@ -247,6 +255,7 @@
             try:
                 self.__subscribedTopics.remove(topic)
                 self.__updateUnsubscribeTopicComboBox()
+                self.__updatePublishTopicComboBox()
             except ValueError:
                 # ignore it
                 pass
@@ -343,6 +352,32 @@
             self.__topicQueue[
                 self.__client.unsubscribe(topic)[1]] = topic
     
+    @pyqtSlot(str)
+    def on_publishTopicComboBox_editTextChanged(self, topic):
+        """
+        Private slot to handle changes of the publish topic name.
+        
+        @param topic topic text
+        @type str
+        """
+        self.publishButton.setEnabled(bool(topic))
+    
+    @pyqtSlot()
+    def on_publishButton_clicked(self):
+        """
+        Private slot to publish the entered message.
+        """
+        topic = self.publishTopicComboBox.currentText()
+        qos = self.publishQosSpinBox.value()
+        retain = self.publishRetainCheckBox.isChecked()
+        payloadStr = self.publishPayloadEdit.toPlainText()
+        
+        msgInfo = self.__client.publish(topic, payloadStr, qos, retain)
+        if msgInfo.rc == 0:
+            if topic not in self.__publishedTopics:
+                self.__publishedTopics.append(topic)
+            self.__updatePublishTopicComboBox()
+    
     @pyqtSlot()
     def on_brokerStatusButton_clicked(self):
         """
@@ -398,6 +433,14 @@
         self.unsubscribeTopicComboBox.addItems(sorted(self.__subscribedTopics))
         self.unsubscribeButton.setEnabled(len(self.__subscribedTopics) > 0)
     
+    def __updatePublishTopicComboBox(self):
+        """
+        Private method to update the publish topic combo box.
+        """
+        self.publishTopicComboBox.clear()
+        self.publishTopicComboBox.addItems(
+            [""] + list(set(self.__publishedTopics + self.__subscribedTopics)))
+    
     def __appendMessage(self, topic, payload):
         """
         Private method to append a received message to the output.
@@ -408,7 +451,7 @@
         @type bytes
         """
         payloadStr = str(payload, encoding="utf-8", errors="replace")
-        txt = "{0} {1}".format(topic, payloadStr)
+        txt = self.tr("{0} -> {1}").format(topic, payloadStr)
         if not txt.endswith(("\r\n", "\r", "\n")):
             txt += "\n"
         

eric ide

mercurial