8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
11 import collections |
11 import collections |
12 import copy |
12 import copy |
|
13 import contextlib |
13 |
14 |
14 from PyQt5.QtCore import pyqtSlot, Qt, QTimer, QFileInfo |
15 from PyQt5.QtCore import pyqtSlot, Qt, QTimer, QFileInfo |
15 from PyQt5.QtGui import QFont, QTextCursor, QBrush |
16 from PyQt5.QtGui import QFont, QTextCursor, QBrush |
16 from PyQt5.QtWidgets import QWidget, QDialog |
17 from PyQt5.QtWidgets import QWidget, QDialog |
17 |
18 |
45 @param iconSuffix suffix for the icons |
46 @param iconSuffix suffix for the icons |
46 @type str |
47 @type str |
47 @param parent reference to the parent widget |
48 @param parent reference to the parent widget |
48 @type QWidget |
49 @type QWidget |
49 """ |
50 """ |
50 super(MqttMonitorWidget, self).__init__(parent) |
51 super().__init__(parent) |
51 self.setupUi(self) |
52 self.setupUi(self) |
52 |
53 |
53 self.__plugin = plugin |
54 self.__plugin = plugin |
54 self.__iconSuffix = iconSuffix |
55 self.__iconSuffix = iconSuffix |
55 |
56 |
246 self.__connectedToBroker = False |
247 self.__connectedToBroker = False |
247 |
248 |
248 # ensure, the client loop is stopped |
249 # ensure, the client loop is stopped |
249 self.__client.stopLoop() |
250 self.__client.stopLoop() |
250 |
251 |
251 if rc > 0: |
252 msg = ( |
252 msg = mqttErrorMessage(rc) |
253 mqttErrorMessage(rc) |
253 else: |
254 if rc > 0 else |
254 msg = self.tr("Connection to Broker shut down cleanly.") |
255 self.tr("Connection to Broker shut down cleanly.") |
|
256 ) |
255 self.__flashBrokerStatusLabel(msg) |
257 self.__flashBrokerStatusLabel(msg) |
256 |
258 |
257 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) |
259 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) |
258 self.__setConnectButtonState() |
260 self.__setConnectButtonState() |
259 |
261 |
277 @param level log level |
279 @param level log level |
278 @type int |
280 @type int |
279 @param message log message |
281 @param message log message |
280 @type str |
282 @type str |
281 """ |
283 """ |
282 try: |
284 with contextlib.suppress(KeyError): |
283 if MqttClient.LogLevelMap[level] < self.logLevelComboBox.itemData( |
285 if MqttClient.LogLevelMap[level] < self.logLevelComboBox.itemData( |
284 self.logLevelComboBox.currentIndex()): |
286 self.logLevelComboBox.currentIndex()): |
285 return |
287 return |
286 except KeyError: |
|
287 # always show unknown log levels |
|
288 pass |
|
289 |
288 |
290 scrollbarValue = self.logEdit.verticalScrollBar().value() |
289 scrollbarValue = self.logEdit.verticalScrollBar().value() |
291 |
290 |
292 textCursor = self.logEdit.textCursor() |
291 textCursor = self.logEdit.textCursor() |
293 if not self.logEdit.document().isEmpty(): |
292 if not self.logEdit.document().isEmpty(): |
370 @param mid ID of the unsubscribe request |
369 @param mid ID of the unsubscribe request |
371 @type int |
370 @type int |
372 """ |
371 """ |
373 if mid in self.__topicQueue: |
372 if mid in self.__topicQueue: |
374 topic = self.__topicQueue.pop(mid) |
373 topic = self.__topicQueue.pop(mid) |
375 try: |
374 with contextlib.suppress(ValueError): |
376 self.__subscribedTopics.remove(topic) |
375 self.__subscribedTopics.remove(topic) |
377 self.__updateUnsubscribeTopicComboBox() |
376 self.__updateUnsubscribeTopicComboBox() |
378 self.__updatePublishTopicComboBox() |
377 self.__updatePublishTopicComboBox() |
379 except ValueError: |
|
380 # ignore it |
|
381 pass |
|
382 |
378 |
383 ####################################################################### |
379 ####################################################################### |
384 ## Slots handling UI interactions |
380 ## Slots handling UI interactions |
385 ####################################################################### |
381 ####################################################################### |
386 |
382 |
723 return |
719 return |
724 |
720 |
725 fn = Utilities.toNativeSeparators(fn) |
721 fn = Utilities.toNativeSeparators(fn) |
726 try: |
722 try: |
727 with open(fn, "w") as f: |
723 with open(fn, "w") as f: |
728 f.write(self.logEdit.toPlainText()) |
724 f.write(self.logEdit.toPlainText()) |
729 except EnvironmentError as err: |
725 except EnvironmentError as err: |
730 E5MessageBox.critical( |
726 E5MessageBox.critical( |
731 self, |
727 self, |
732 self.tr("Save Log Messages"), |
728 self.tr("Save Log Messages"), |
733 self.tr("""<p>The file <b>{0}</b> could not be written.""" |
729 self.tr("""<p>The file <b>{0}</b> could not be written.""" |
786 # step 1: clear combo boxes |
782 # step 1: clear combo boxes |
787 self.brokerComboBox.clear() |
783 self.brokerComboBox.clear() |
788 self.brokerPortComboBox.clear() |
784 self.brokerPortComboBox.clear() |
789 |
785 |
790 # step 2a: populate the broker name list |
786 # step 2a: populate the broker name list |
791 if brokerPortList: |
787 currentBroker = brokerPortList[0][0] if brokerPortList else "" |
792 currentBroker = brokerPortList[0][0] |
|
793 else: |
|
794 currentBroker = "" |
|
795 brokerSet = {b[0].strip() for b in brokerPortList} |
788 brokerSet = {b[0].strip() for b in brokerPortList} |
796 self.brokerComboBox.addItems(sorted(brokerSet)) |
789 self.brokerComboBox.addItems(sorted(brokerSet)) |
797 index = self.brokerComboBox.findText(currentBroker) |
790 index = self.brokerComboBox.findText(currentBroker) |
798 self.brokerComboBox.setCurrentIndex(index) |
791 self.brokerComboBox.setCurrentIndex(index) |
799 |
792 |
800 # step 2b: populate the broker ports list |
793 # step 2b: populate the broker ports list |
801 if brokerPortList: |
794 currentPort = brokerPortList[0][1] if brokerPortList else 1883 |
802 currentPort = brokerPortList[0][1] |
|
803 else: |
|
804 currentPort = 1883 |
|
805 currentPortStr = "{0:5}".format(currentPort) |
795 currentPortStr = "{0:5}".format(currentPort) |
806 portsSet = {b[1] for b in brokerPortList} |
796 portsSet = {b[1] for b in brokerPortList} |
807 portsSet.update({1883, 8883}) |
797 portsSet.update({1883, 8883}) |
808 self.brokerPortComboBox.addItems( |
798 self.brokerPortComboBox.addItems( |
809 sorted("{0:5}".format(p) for p in portsSet)) |
799 sorted("{0:5}".format(p) for p in portsSet)) |
915 topic = topic.strip() |
905 topic = topic.strip() |
916 |
906 |
917 if topic.startswith(MqttMonitorWidget.BrokerStatusTopicLoadPrefix): |
907 if topic.startswith(MqttMonitorWidget.BrokerStatusTopicLoadPrefix): |
918 self.__handleBrokerLoadStatusMessage(topic, payloadStr) |
908 self.__handleBrokerLoadStatusMessage(topic, payloadStr) |
919 else: |
909 else: |
920 try: |
910 with contextlib.suppress(KeyError): |
921 label = self.__statusLabelMapping[topic] |
911 label = self.__statusLabelMapping[topic] |
922 label.setText(payloadStr) |
912 label.setText(payloadStr) |
923 except KeyError: |
|
924 # ignore topics not shown in display |
|
925 pass |
|
926 |
913 |
927 def __handleBrokerLoadStatusMessage(self, topic, payloadStr): |
914 def __handleBrokerLoadStatusMessage(self, topic, payloadStr): |
928 """ |
915 """ |
929 Private method to append a received message to the output. |
916 Private method to append a received message to the output. |
930 |
917 |
935 @type str |
922 @type str |
936 """ |
923 """ |
937 subtopic, topicElement = topic.rsplit("/", 1) |
924 subtopic, topicElement = topic.rsplit("/", 1) |
938 self.__statusLoadValues[subtopic][topicElement] = payloadStr |
925 self.__statusLoadValues[subtopic][topicElement] = payloadStr |
939 |
926 |
940 try: |
927 with contextlib.suppress(KeyError): |
941 label = self.__statusLabelMapping[subtopic] |
928 label = self.__statusLabelMapping[subtopic] |
942 label.setText("{0} / {1} / {2}".format( |
929 label.setText("{0} / {1} / {2}".format( |
943 self.__statusLoadValues[subtopic]["1min"], |
930 self.__statusLoadValues[subtopic]["1min"], |
944 self.__statusLoadValues[subtopic]["5min"], |
931 self.__statusLoadValues[subtopic]["5min"], |
945 self.__statusLoadValues[subtopic]["15min"], |
932 self.__statusLoadValues[subtopic]["15min"], |
946 )) |
933 )) |
947 except KeyError: |
|
948 # ignore topics not shown in display |
|
949 pass |
|
950 |
934 |
951 def __clearBrokerStatusLabels(self): |
935 def __clearBrokerStatusLabels(self): |
952 """ |
936 """ |
953 Private method to clear the broker status labels. |
937 Private method to clear the broker status labels. |
954 """ |
938 """ |
955 for statusLabelKey in self.__statusLabelMapping: |
939 for statusLabelKey in self.__statusLabelMapping: |
956 if statusLabelKey.startswith( |
940 label = ( |
957 MqttMonitorWidget.BrokerStatusTopicLoadPrefix): |
941 "- / - / -" |
958 label = "- / - / -" |
942 if statusLabelKey.startswith( |
959 else: |
943 MqttMonitorWidget.BrokerStatusTopicLoadPrefix) else |
960 label = "-" |
944 "-" |
|
945 ) |
961 self.__statusLabelMapping[statusLabelKey].setText(label) |
946 self.__statusLabelMapping[statusLabelKey].setText(label) |
962 |
947 |
963 def __loadDefaultDictFactory(self): |
948 def __loadDefaultDictFactory(self): |
964 """ |
949 """ |
965 Private method to populate non-existing load items. |
950 Private method to populate non-existing load items. |