MqttMonitorWidget: added functionality to receive messages and show them in a text edit.

Tue, 28 Aug 2018 19:29:52 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 28 Aug 2018 19:29:52 +0200
changeset 5
7162c838cfc9
parent 4
991db89bda9c
child 6
d22f5ce3a07a

MqttMonitorWidget: added functionality to receive messages and show them in a text edit.

MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.ui file | annotate | diff | comparison | revisions
--- 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")
--- a/MqttMonitor/MqttMonitorWidget.ui	Tue Aug 28 18:54:48 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.ui	Tue Aug 28 19:29:52 2018 +0200
@@ -79,7 +79,7 @@
    <item>
     <widget class="QTabWidget" name="brokerWidget">
      <property name="currentIndex">
-      <number>0</number>
+      <number>1</number>
      </property>
      <widget class="QWidget" name="pubSubTab">
       <attribute name="title">
@@ -197,6 +197,48 @@
       <attribute name="title">
        <string>Messages</string>
       </attribute>
+      <layout class="QVBoxLayout" name="verticalLayout_3">
+       <item>
+        <widget class="QPlainTextEdit" name="messagesEdit">
+         <property name="tabChangesFocus">
+          <bool>true</bool>
+         </property>
+         <property name="lineWrapMode">
+          <enum>QPlainTextEdit::NoWrap</enum>
+         </property>
+         <property name="readOnly">
+          <bool>true</bool>
+         </property>
+         <property name="textInteractionFlags">
+          <set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <layout class="QHBoxLayout" name="horizontalLayout_4">
+         <item>
+          <spacer name="horizontalSpacer">
+           <property name="orientation">
+            <enum>Qt::Horizontal</enum>
+           </property>
+           <property name="sizeHint" stdset="0">
+            <size>
+             <width>40</width>
+             <height>20</height>
+            </size>
+           </property>
+          </spacer>
+         </item>
+         <item>
+          <widget class="QPushButton" name="pushButton">
+           <property name="text">
+            <string>Clear</string>
+           </property>
+          </widget>
+         </item>
+        </layout>
+       </item>
+      </layout>
      </widget>
      <widget class="QWidget" name="statusTab">
       <attribute name="title">
@@ -225,5 +267,22 @@
   <tabstop>unsubscribeButton</tabstop>
  </tabstops>
  <resources/>
- <connections/>
+ <connections>
+  <connection>
+   <sender>pushButton</sender>
+   <signal>clicked()</signal>
+   <receiver>messagesEdit</receiver>
+   <slot>clear()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>231</x>
+     <y>382</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>179</x>
+     <y>272</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
 </ui>

eric ide

mercurial