MqttMonitorWidget: added capability to read the payload data to be published from a file.

Mon, 10 Sep 2018 17:38:39 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 10 Sep 2018 17:38:39 +0200
changeset 44
ca2e03cb6ed4
parent 43
a0853f7a8b80
child 45
696b5d1a7b97

MqttMonitorWidget: added capability to read the payload data to be published from a file.

ChangeLog file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.ui file | annotate | diff | comparison | revisions
diff -r a0853f7a8b80 -r ca2e03cb6ed4 ChangeLog
--- a/ChangeLog	Mon Sep 10 16:52:37 2018 +0200
+++ b/ChangeLog	Mon Sep 10 17:38:39 2018 +0200
@@ -4,6 +4,7 @@
 - bug fixes
 - limited amount of recently used brokers to 20
 - added log level selection to disable logging
+- added capability to read the payload data to be published from a file
 
 Version 1.0.0
 - first stable release
diff -r a0853f7a8b80 -r ca2e03cb6ed4 MqttMonitor/MqttMonitorWidget.py
--- a/MqttMonitor/MqttMonitorWidget.py	Mon Sep 10 16:52:37 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Mon Sep 10 17:38:39 2018 +0200
@@ -23,6 +23,7 @@
 from PyQt5.QtWidgets import QWidget, QDialog
 
 from E5Gui import E5MessageBox
+from E5Gui.E5PathPicker import E5PathPickerModes
 
 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
 
@@ -71,6 +72,9 @@
         self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap(
             os.path.join("MqttMonitor", "icons", "mqtt48.png")))
         
+        self.publishPayloadFilePicker.setMode(E5PathPickerModes.OpenFileMode)
+        self.publishPayloadFilePicker.setFilters(self.tr("All Files (*)"))
+        
         for logLevel in (MqttClient.LogDisabled,
                          MqttClient.LogDebug,
                          MqttClient.LogInfo,
@@ -507,16 +511,31 @@
         """
         Private slot to publish the entered message.
         """
-        # TODO: read message data from file as binary
-        #       size of data <= 268435455
         topic = self.publishTopicComboBox.currentText()
         qos = self.publishQosSpinBox.value()
         retain = self.publishRetainCheckBox.isChecked()
-        payloadStr = self.publishPayloadEdit.toPlainText()
-        if not payloadStr:
-            # use empty string together with the retain flag to clean
-            # a retained message by sending None instead
-            payloadStr = None
+        payloadFile = self.publishPayloadFilePicker.text()
+        if bool(payloadFile) and os.path.exists(payloadFile) and \
+           os.path.getsize(payloadFile) <= 268435455:
+            # payload size limit is 268,435,455 bytes
+            try:
+                f = open(payloadFile, "rb")
+                payloadStr = f.read()
+                f.close()
+            except (IOError, OSError) as err:
+                E5MessageBox.critical(
+                    self,
+                    self.tr("Read Payload from File"),
+                    self.tr("""<p>The file <b>{0}</b> could not be read."""
+                            """ Aborting...</p><p>Reason: {1}</p>""").format(
+                        payloadFile, str(err)))
+                return
+        else:
+            payloadStr = self.publishPayloadEdit.toPlainText()
+            if not payloadStr:
+                # use empty string together with the retain flag to clean
+                # a retained message by sending None instead
+                payloadStr = None
         
         msgInfo = self.__client.publish(topic, payloadStr, qos, retain)
         if msgInfo.rc == 0:
@@ -535,6 +554,17 @@
         self.publishPayloadEdit.clear()
         self.publishQosSpinBox.setValue(0)
         self.publishRetainCheckBox.setChecked(False)
+        self.publishPayloadFilePicker.clear()
+    
+    @pyqtSlot(str)
+    def on_publishPayloadFilePicker_textChanged(self, path):
+        """
+        Private slot handling a change of path of the payload file.
+        
+        @param path path of the payload file
+        @type str
+        """
+        self.publishPayloadEdit.setEnabled(not bool(path))
     
     @pyqtSlot()
     def on_brokerStatusButton_clicked(self):
diff -r a0853f7a8b80 -r ca2e03cb6ed4 MqttMonitor/MqttMonitorWidget.ui
--- a/MqttMonitor/MqttMonitorWidget.ui	Mon Sep 10 16:52:37 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.ui	Mon Sep 10 17:38:39 2018 +0200
@@ -144,7 +144,7 @@
    <item>
     <widget class="QTabWidget" name="brokerWidget">
      <property name="currentIndex">
-      <number>3</number>
+      <number>0</number>
      </property>
      <widget class="QWidget" name="pubSubTab">
       <attribute name="title">
@@ -323,6 +323,33 @@
            </widget>
           </item>
           <item>
+           <layout class="QHBoxLayout" name="horizontalLayout_18">
+            <item>
+             <widget class="QLabel" name="label_42">
+              <property name="text">
+               <string>Payload File:</string>
+              </property>
+             </widget>
+            </item>
+            <item>
+             <widget class="E5PathPicker" name="publishPayloadFilePicker" native="true">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="focusPolicy">
+               <enum>Qt::StrongFocus</enum>
+              </property>
+              <property name="toolTip">
+               <string>Enter the full path to a file containing the message payload</string>
+              </property>
+             </widget>
+            </item>
+           </layout>
+          </item>
+          <item>
            <layout class="QHBoxLayout" name="horizontalLayout_12">
             <item>
              <widget class="QPushButton" name="publishClearButton">
@@ -479,7 +506,7 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>178</width>
+            <width>344</width>
             <height>840</height>
            </rect>
           </property>
@@ -1286,6 +1313,12 @@
    <extends>QComboBox</extends>
    <header>E5Gui/E5ComboBox.h</header>
   </customwidget>
+  <customwidget>
+   <class>E5PathPicker</class>
+   <extends>QWidget</extends>
+   <header>E5Gui/E5PathPicker.h</header>
+   <container>1</container>
+  </customwidget>
  </customwidgets>
  <tabstops>
   <tabstop>modeButton</tabstop>
@@ -1302,6 +1335,7 @@
   <tabstop>unsubscribeButton</tabstop>
   <tabstop>publishTopicComboBox</tabstop>
   <tabstop>publishPayloadEdit</tabstop>
+  <tabstop>publishPayloadFilePicker</tabstop>
   <tabstop>publishQosSpinBox</tabstop>
   <tabstop>publishRetainCheckBox</tabstop>
   <tabstop>publishClearButton</tabstop>
@@ -1311,6 +1345,7 @@
   <tabstop>messagesClearButton</tabstop>
   <tabstop>brokerStatusButton</tabstop>
   <tabstop>scrollArea</tabstop>
+  <tabstop>logLevelComboBox</tabstop>
   <tabstop>logEdit</tabstop>
   <tabstop>logClearButton</tabstop>
  </tabstops>

eric ide

mercurial