MqttMonitor/MqttMonitorWidget.py

changeset 44
ca2e03cb6ed4
parent 43
a0853f7a8b80
child 45
696b5d1a7b97
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):

eric ide

mercurial