MqttMonitorWidget: implemented publish functionality.

Thu, 30 Aug 2018 19:45:12 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 30 Aug 2018 19:45:12 +0200
changeset 8
95b56dcfa09b
parent 7
63e046d95702
child 9
f75a385e9127

MqttMonitorWidget: implemented publish functionality.

MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.ui file | annotate | diff | comparison | revisions
--- 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"
         
--- a/MqttMonitor/MqttMonitorWidget.ui	Thu Aug 30 18:57:57 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.ui	Thu Aug 30 19:45:12 2018 +0200
@@ -79,13 +79,13 @@
    <item>
     <widget class="QTabWidget" name="brokerWidget">
      <property name="currentIndex">
-      <number>2</number>
+      <number>0</number>
      </property>
      <widget class="QWidget" name="pubSubTab">
       <attribute name="title">
        <string>Pub/Sub</string>
       </attribute>
-      <layout class="QVBoxLayout" name="verticalLayout_2">
+      <layout class="QVBoxLayout" name="verticalLayout_5">
        <item>
         <widget class="QGroupBox" name="subscribeGroup">
          <property name="enabled">
@@ -179,6 +179,112 @@
         </widget>
        </item>
        <item>
+        <widget class="QGroupBox" name="publishGroup">
+         <property name="enabled">
+          <bool>false</bool>
+         </property>
+         <property name="title">
+          <string>Publish</string>
+         </property>
+         <layout class="QVBoxLayout" name="verticalLayout_2">
+          <item>
+           <layout class="QHBoxLayout" name="horizontalLayout_13">
+            <item>
+             <widget class="QLabel" name="label_39">
+              <property name="text">
+               <string>Topic:</string>
+              </property>
+             </widget>
+            </item>
+            <item>
+             <widget class="E5ClearableComboBox" name="publishTopicComboBox">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="toolTip">
+               <string>Enter the topic to publish to</string>
+              </property>
+              <property name="editable">
+               <bool>true</bool>
+              </property>
+             </widget>
+            </item>
+            <item>
+             <widget class="QLabel" name="label_40">
+              <property name="text">
+               <string>QoS:</string>
+              </property>
+             </widget>
+            </item>
+            <item>
+             <widget class="QSpinBox" name="publishQosSpinBox">
+              <property name="toolTip">
+               <string>Enter the desired QoS value</string>
+              </property>
+              <property name="alignment">
+               <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+              </property>
+              <property name="maximum">
+               <number>2</number>
+              </property>
+             </widget>
+            </item>
+            <item>
+             <widget class="QCheckBox" name="publishRetainCheckBox">
+              <property name="toolTip">
+               <string>Select to retain the published message</string>
+              </property>
+              <property name="text">
+               <string>Retain</string>
+              </property>
+             </widget>
+            </item>
+           </layout>
+          </item>
+          <item>
+           <widget class="QPlainTextEdit" name="publishPayloadEdit">
+            <property name="maximumSize">
+             <size>
+              <width>16777215</width>
+              <height>300</height>
+             </size>
+            </property>
+            <property name="toolTip">
+             <string>Enter the string data to be sent</string>
+            </property>
+           </widget>
+          </item>
+          <item>
+           <layout class="QHBoxLayout" name="horizontalLayout_12">
+            <item>
+             <spacer name="horizontalSpacer_3">
+              <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="publishButton">
+              <property name="text">
+               <string>Publish</string>
+              </property>
+             </widget>
+            </item>
+           </layout>
+          </item>
+         </layout>
+        </widget>
+       </item>
+       <item>
         <spacer name="verticalSpacer">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
@@ -186,7 +292,7 @@
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
-           <height>312</height>
+           <height>40</height>
           </size>
          </property>
         </spacer>
@@ -284,9 +390,9 @@
           <property name="geometry">
            <rect>
             <x>0</x>
-            <y>-266</y>
-            <width>339</width>
-            <height>670</height>
+            <y>0</y>
+            <width>344</width>
+            <height>840</height>
            </rect>
           </property>
           <layout class="QFormLayout" name="formLayout">
@@ -1013,6 +1119,11 @@
    <extends>QLineEdit</extends>
    <header>E5Gui/E5LineEdit.h</header>
   </customwidget>
+  <customwidget>
+   <class>E5ClearableComboBox</class>
+   <extends>QComboBox</extends>
+   <header>E5Gui/E5ComboBox.h</header>
+  </customwidget>
  </customwidgets>
  <tabstops>
   <tabstop>brokerComboBox</tabstop>
@@ -1033,8 +1144,8 @@
    <slot>clear()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>231</x>
-     <y>382</y>
+     <x>378</x>
+     <y>578</y>
     </hint>
     <hint type="destinationlabel">
      <x>179</x>

eric ide

mercurial