MqttMonitor/MqttMonitorWidget.py

changeset 44
ca2e03cb6ed4
parent 43
a0853f7a8b80
child 45
696b5d1a7b97
equal deleted inserted replaced
43:a0853f7a8b80 44:ca2e03cb6ed4
21 from PyQt5.QtCore import pyqtSlot, QTimer 21 from PyQt5.QtCore import pyqtSlot, QTimer
22 from PyQt5.QtGui import QTextCursor 22 from PyQt5.QtGui import QTextCursor
23 from PyQt5.QtWidgets import QWidget, QDialog 23 from PyQt5.QtWidgets import QWidget, QDialog
24 24
25 from E5Gui import E5MessageBox 25 from E5Gui import E5MessageBox
26 from E5Gui.E5PathPicker import E5PathPickerModes
26 27
27 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget 28 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
28 29
29 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage, \ 30 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage, \
30 mqttLogLevelString 31 mqttLogLevelString
68 self.__connectedToBroker = False 69 self.__connectedToBroker = False
69 self.__brokerStatusTopicSubscribed = False 70 self.__brokerStatusTopicSubscribed = False
70 71
71 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap( 72 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap(
72 os.path.join("MqttMonitor", "icons", "mqtt48.png"))) 73 os.path.join("MqttMonitor", "icons", "mqtt48.png")))
74
75 self.publishPayloadFilePicker.setMode(E5PathPickerModes.OpenFileMode)
76 self.publishPayloadFilePicker.setFilters(self.tr("All Files (*)"))
73 77
74 for logLevel in (MqttClient.LogDisabled, 78 for logLevel in (MqttClient.LogDisabled,
75 MqttClient.LogDebug, 79 MqttClient.LogDebug,
76 MqttClient.LogInfo, 80 MqttClient.LogInfo,
77 MqttClient.LogNotice, 81 MqttClient.LogNotice,
505 @pyqtSlot() 509 @pyqtSlot()
506 def on_publishButton_clicked(self): 510 def on_publishButton_clicked(self):
507 """ 511 """
508 Private slot to publish the entered message. 512 Private slot to publish the entered message.
509 """ 513 """
510 # TODO: read message data from file as binary
511 # size of data <= 268435455
512 topic = self.publishTopicComboBox.currentText() 514 topic = self.publishTopicComboBox.currentText()
513 qos = self.publishQosSpinBox.value() 515 qos = self.publishQosSpinBox.value()
514 retain = self.publishRetainCheckBox.isChecked() 516 retain = self.publishRetainCheckBox.isChecked()
515 payloadStr = self.publishPayloadEdit.toPlainText() 517 payloadFile = self.publishPayloadFilePicker.text()
516 if not payloadStr: 518 if bool(payloadFile) and os.path.exists(payloadFile) and \
517 # use empty string together with the retain flag to clean 519 os.path.getsize(payloadFile) <= 268435455:
518 # a retained message by sending None instead 520 # payload size limit is 268,435,455 bytes
519 payloadStr = None 521 try:
522 f = open(payloadFile, "rb")
523 payloadStr = f.read()
524 f.close()
525 except (IOError, OSError) as err:
526 E5MessageBox.critical(
527 self,
528 self.tr("Read Payload from File"),
529 self.tr("""<p>The file <b>{0}</b> could not be read."""
530 """ Aborting...</p><p>Reason: {1}</p>""").format(
531 payloadFile, str(err)))
532 return
533 else:
534 payloadStr = self.publishPayloadEdit.toPlainText()
535 if not payloadStr:
536 # use empty string together with the retain flag to clean
537 # a retained message by sending None instead
538 payloadStr = None
520 539
521 msgInfo = self.__client.publish(topic, payloadStr, qos, retain) 540 msgInfo = self.__client.publish(topic, payloadStr, qos, retain)
522 if msgInfo.rc == 0: 541 if msgInfo.rc == 0:
523 if topic not in self.__publishedTopics: 542 if topic not in self.__publishedTopics:
524 self.__publishedTopics.append(topic) 543 self.__publishedTopics.append(topic)
533 """ 552 """
534 self.publishTopicComboBox.clearEditText() 553 self.publishTopicComboBox.clearEditText()
535 self.publishPayloadEdit.clear() 554 self.publishPayloadEdit.clear()
536 self.publishQosSpinBox.setValue(0) 555 self.publishQosSpinBox.setValue(0)
537 self.publishRetainCheckBox.setChecked(False) 556 self.publishRetainCheckBox.setChecked(False)
557 self.publishPayloadFilePicker.clear()
558
559 @pyqtSlot(str)
560 def on_publishPayloadFilePicker_textChanged(self, path):
561 """
562 Private slot handling a change of path of the payload file.
563
564 @param path path of the payload file
565 @type str
566 """
567 self.publishPayloadEdit.setEnabled(not bool(path))
538 568
539 @pyqtSlot() 569 @pyqtSlot()
540 def on_brokerStatusButton_clicked(self): 570 def on_brokerStatusButton_clicked(self):
541 """ 571 """
542 Private slot to subscribe or unsubscribe the broker status topic. 572 Private slot to subscribe or unsubscribe the broker status topic.

eric ide

mercurial