MqttMonitor/MqttMonitorWidget.py

changeset 50
8e9e83221c15
parent 48
41dd2bfee4e4
child 52
cc8734657162
equal deleted inserted replaced
49:a7e370d20ab3 50:8e9e83221c15
16 16
17 import os 17 import os
18 import collections 18 import collections
19 import copy 19 import copy
20 20
21 from PyQt5.QtCore import pyqtSlot, Qt, QTimer 21 from PyQt5.QtCore import pyqtSlot, Qt, QTimer, QFileInfo
22 from PyQt5.QtGui import QFont, QTextCursor, QBrush 22 from PyQt5.QtGui import QFont, QTextCursor, QBrush
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, E5FileDialog
26 from E5Gui.E5PathPicker import E5PathPickerModes 26 from E5Gui.E5PathPicker import E5PathPickerModes
27 27
28 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget 28 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
29 29
30 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage, \ 30 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage, \
73 73
74 self.__isAlternate = False 74 self.__isAlternate = False
75 75
76 # TODO: Messages Edit improvements: 76 # TODO: Messages Edit improvements:
77 # 1. add capability to search 77 # 1. add capability to search
78 # 2. add capability to save contents to a file
79 78
80 for logLevel in (MqttClient.LogDisabled, 79 for logLevel in (MqttClient.LogDisabled,
81 MqttClient.LogDebug, 80 MqttClient.LogDebug,
82 MqttClient.LogInfo, 81 MqttClient.LogInfo,
83 MqttClient.LogNotice, 82 MqttClient.LogNotice,
88 self.logLevelComboBox.setCurrentIndex( 87 self.logLevelComboBox.setCurrentIndex(
89 self.logLevelComboBox.count() - 1) 88 self.logLevelComboBox.count() - 1)
90 89
91 # TODO: Log Edit improvements: 90 # TODO: Log Edit improvements:
92 # 1. add capability to search 91 # 1. add capability to search
93 # 2. add configuration capability for colors
94 92
95 self.__logMessagesBackgrounds = { 93 self.__logMessagesBackgrounds = {
96 MqttClient.LogDebug: QBrush(Qt.white), 94 MqttClient.LogDebug: QBrush(Qt.white),
97 MqttClient.LogInfo: QBrush(Qt.lightGray), 95 MqttClient.LogInfo: QBrush(Qt.lightGray),
98 MqttClient.LogNotice: QBrush(Qt.green), 96 MqttClient.LogNotice: QBrush(Qt.green),
553 # payload size limit is 268,435,455 bytes 551 # payload size limit is 268,435,455 bytes
554 try: 552 try:
555 f = open(payloadFile, "rb") 553 f = open(payloadFile, "rb")
556 payloadStr = f.read() 554 payloadStr = f.read()
557 f.close() 555 f.close()
558 except (IOError, OSError) as err: 556 except EnvironmentError as err:
559 E5MessageBox.critical( 557 E5MessageBox.critical(
560 self, 558 self,
561 self.tr("Read Payload from File"), 559 self.tr("Read Payload from File"),
562 self.tr("""<p>The file <b>{0}</b> could not be read.""" 560 self.tr("""<p>The file <b>{0}</b> could not be read."""
563 """ Aborting...</p><p>Reason: {1}</p>""").format( 561 """ Aborting...</p><p>Reason: {1}</p>""").format(
616 rc, _ = self.__client.subscribe( 614 rc, _ = self.__client.subscribe(
617 MqttMonitorWidget.BrokerStatusTopic) 615 MqttMonitorWidget.BrokerStatusTopic)
618 if rc == 0: 616 if rc == 0:
619 # successfully sent 617 # successfully sent
620 self.__setBrokerStatusSubscribed(True) 618 self.__setBrokerStatusSubscribed(True)
619
620 @pyqtSlot(int)
621 def on_messagesEdit_blockCountChanged(self, newBlockCount):
622 """
623 Private slot handling changes of received messages.
624
625 @param newBlockCount (ignored)
626 @type int
627 """
628 enable = not self.messagesEdit.document().isEmpty()
629 self.saveMessagesButton.setEnabled(enable)
630 self.clearMessagesButton.setEnabled(enable)
631
632 @pyqtSlot()
633 def on_saveMessagesButton_clicked(self):
634 """
635 Private slot to save the received messages.
636 """
637 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
638 self,
639 self.tr("Save Messages"),
640 "",
641 self.tr("Messages Files (*.txt);;All Files (*)"),
642 "",
643 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
644
645 if fn:
646 if fn.endswith("."):
647 fn = fn[:-1]
648
649 ext = QFileInfo(fn).suffix()
650 if not ext:
651 ex = selectedFilter.split("(*")[1].split(")")[0]
652 if ex:
653 fn += ex
654 if QFileInfo(fn).exists():
655 res = E5MessageBox.yesNo(
656 self,
657 self.tr("Save Messages"),
658 self.tr("<p>The file <b>{0}</b> already exists."
659 " Overwrite it?</p>").format(fn),
660 icon=E5MessageBox.Warning)
661 if not res:
662 return
663
664 fn = Utilities.toNativeSeparators(fn)
665 try:
666 f = open(fn, "w")
667 f.write(self.messagesEdit.toPlainText())
668 f.close()
669 except EnvironmentError as err:
670 E5MessageBox.critical(
671 self,
672 self.tr("Save Messages"),
673 self.tr("""<p>The file <b>{0}</b> could not be written."""
674 """</p><p>Reason: {1}</p>""").format(
675 fn, str(err)))
676
677 @pyqtSlot(int)
678 def on_logEdit_blockCountChanged(self, newBlockCount):
679 """
680 Private slot handling changes of received messages.
681
682 @param newBlockCount (ignored)
683 @type int
684 """
685 enable = not self.logEdit.document().isEmpty()
686 self.saveLogMessagesButton.setEnabled(enable)
687 self.clearLogMessagesButton.setEnabled(enable)
688
689 @pyqtSlot()
690 def on_saveLogMessagesButton_clicked(self):
691 """
692 Private slot to save the log messages.
693 """
694 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
695 self,
696 self.tr("Save Log Messages"),
697 "",
698 self.tr("Log Files (*.log);;All Files (*)"),
699 "",
700 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
701
702 if fn:
703 if fn.endswith("."):
704 fn = fn[:-1]
705
706 ext = QFileInfo(fn).suffix()
707 if not ext:
708 ex = selectedFilter.split("(*")[1].split(")")[0]
709 if ex:
710 fn += ex
711 if QFileInfo(fn).exists():
712 res = E5MessageBox.yesNo(
713 self,
714 self.tr("Save Log Messages"),
715 self.tr("<p>The file <b>{0}</b> already exists."
716 " Overwrite it?</p>").format(fn),
717 icon=E5MessageBox.Warning)
718 if not res:
719 return
720
721 fn = Utilities.toNativeSeparators(fn)
722 try:
723 f = open(fn, "w")
724 f.write(self.logEdit.toPlainText())
725 f.close()
726 except EnvironmentError as err:
727 E5MessageBox.critical(
728 self,
729 self.tr("Save Log Messages"),
730 self.tr("""<p>The file <b>{0}</b> could not be written."""
731 """</p><p>Reason: {1}</p>""").format(
732 fn, str(err)))
621 733
622 ####################################################################### 734 #######################################################################
623 ## Utility methods 735 ## Utility methods
624 ####################################################################### 736 #######################################################################
625 737

eric ide

mercurial