MqttMonitor/MqttMonitorWidget.py

changeset 5
7162c838cfc9
parent 4
991db89bda9c
child 6
d22f5ce3a07a
diff -r 991db89bda9c -r 7162c838cfc9 MqttMonitor/MqttMonitorWidget.py
--- a/MqttMonitor/MqttMonitorWidget.py	Tue Aug 28 18:54:48 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Tue Aug 28 19:29:52 2018 +0200
@@ -9,9 +9,15 @@
 
 from __future__ import unicode_literals
 
+try:
+    str = unicode       # __IGNORE_EXCEPTION__
+except NameError:
+    pass
+
 import os
 
 from PyQt5.QtCore import pyqtSlot, QTimer
+from PyQt5.QtGui import QTextCursor
 from PyQt5.QtWidgets import QWidget
 
 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
@@ -19,6 +25,7 @@
 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage
 
 import UI.PixmapCache
+import Utilities
 
 
 class MqttMonitorWidget(QWidget, Ui_MqttMonitorWidget):
@@ -44,6 +51,8 @@
         self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap(
             os.path.join("MqttMonitor", "icons", "mqtt48.png")))
         
+        self.brokerWidget.setCurrentIndex(0)
+        
         self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
         self.brokerComboBox.addItems(
             self.__plugin.getPreferences("RecentBrokers"))
@@ -134,7 +143,11 @@
         @param retain flag indicating a retained message
         @type bool
         """
-        pass
+        if topic.startswith("$SYS/broker/"):
+            # handle broker status messages
+            self.__handleBrokerStatusMessage(topic, payload)
+        else:
+            self.__appendMessage(topic, payload)
     
     @pyqtSlot(int)
     def __messagePublished(self, mid):
@@ -293,3 +306,34 @@
         self.unsubscribeTopicComboBox.clear()
         self.unsubscribeTopicComboBox.addItems(sorted(self.__subscribedTopics))
         self.unsubscribeButton.setEnabled(len(self.__subscribedTopics) > 0)
+    
+    def __appendMessage(self, topic, payload):
+        """
+        Private method to append a received message to the output.
+        
+        @param topic topic of the received message
+        @type str
+        @param payload payload of the received message
+        @type bytes
+        """
+        payloadStr = str(payload, encoding="utf-8", errors="replace")
+        txt = "{0} {1}".format(topic, payloadStr)
+        if not txt.endswith(("\r\n", "\r", "\n")):
+            txt += "\n"
+        
+        tc = self.messagesEdit.textCursor()
+        tc.movePosition(QTextCursor.End)
+        self.messagesEdit.setTextCursor(tc)
+        self.messagesEdit.insertPlainText(Utilities.filterAnsiSequences(txt))
+        self.messagesEdit.ensureCursorVisible()
+    
+    def __handleBrokerStatusMessage(self, topic, payload):
+        """
+        Private method to append a received message to the output.
+        
+        @param topic topic of the received message
+        @type str
+        @param payload payload of the received message
+        @type bytes
+        """
+        payloadStr = str(payload, encoding="utf-8", errors="replace")

eric ide

mercurial