MqttMonitor/MqttMonitorWidget.py

changeset 31
40582e448c4b
parent 30
17ef10819773
child 32
a71e5b294ebf
--- a/MqttMonitor/MqttMonitorWidget.py	Sun Sep 09 12:21:19 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Sun Sep 09 17:32:54 2018 +0200
@@ -26,7 +26,8 @@
 
 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
 
-from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage
+from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage, \
+    mqttLogLevelString
 
 import UI.PixmapCache
 import Utilities
@@ -60,6 +61,16 @@
         self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap(
             os.path.join("MqttMonitor", "icons", "mqtt48.png")))
         
+        for logLevel in (MqttClient.LogDebug,
+                         MqttClient.LogInfo,
+                         MqttClient.LogNotice,
+                         MqttClient.LogWarning,
+                         MqttClient.LogError):
+            self.logLevelComboBox.addItem(mqttLogLevelString(
+                logLevel, isMqttLogLevel=False), logLevel)
+        self.logLevelComboBox.setCurrentIndex(
+            self.logLevelComboBox.count() - 1)
+        
         self.brokerWidget.setCurrentIndex(0)
         
         self.__connectionModeProfile = True
@@ -135,10 +146,13 @@
         # connect the MQTT client signals
         self.__client.onConnect.connect(self.__brokerConnected)
         self.__client.onDisconnected.connect(self.__brokerDisconnected)
+        self.__client.onLog.connect(self.__clientLog)
         self.__client.onMessage.connect(self.__messageReceived)
         self.__client.onPublish.connect(self.__messagePublished)
         self.__client.onSubscribe.connect(self.__topicSubscribed)
         self.__client.onUnsubscribe.connect(self.__topicUnsubscribed)
+        
+        self.__client.connectTimeout.connect(self.__connectTimeout)
     
     #######################################################################
     ## Slots handling MQTT related signals
@@ -183,6 +197,14 @@
         else:
             self.__client.stopLoop()
     
+    @pyqtSlot()
+    def __connectTimeout(self):
+        """
+        Private slot handling a timeout during a connection attempt.
+        """
+        self.__flashBrokerStatusLabel(self.tr("Connection timed out"))
+        self.__setConnectButtonState()
+    
     @pyqtSlot(int)
     def __brokerDisconnected(self, rc):
         """
@@ -203,7 +225,7 @@
         self.__flashBrokerStatusLabel(msg)
         
         self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
-        self.connectButton.setEnabled(True)
+        self.__setConnectButtonState()
 
         self.__subscribedTopics = []
         self.__topicQueue = {}
@@ -217,6 +239,34 @@
         
         self.__statusLoadValues.clear()
     
+    @pyqtSlot(int, str)
+    def __clientLog(self, level, message):
+        """
+        Private slot to handle the receipt of a log message.
+        
+        @param level log level
+        @type int
+        @param message log message
+        @type str
+        """
+        try:
+            if MqttClient.LogLevelMap[level] < self.logLevelComboBox.itemData(
+                    self.logLevelComboBox.currentIndex()):
+                return
+        except KeyError:
+            # always show unknown log levels
+            pass
+        
+        txt = self.tr("{0}: {1}").format(mqttLogLevelString(level), message)
+        if not txt.endswith(("\r\n", "\r", "\n")):
+            txt += "\n"
+        
+        tc = self.logEdit.textCursor()
+        tc.movePosition(QTextCursor.End)
+        self.logEdit.setTextCursor(tc)
+        self.logEdit.insertPlainText(Utilities.filterAnsiSequences(txt))
+        self.logEdit.ensureCursorVisible()
+    
     @pyqtSlot(str, bytes, int, bool)
     def __messageReceived(self, topic, payload, qos, retain):
         """
@@ -309,6 +359,16 @@
         self.__setConnectionMode(not self.__connectionModeProfile)
     
     @pyqtSlot(str)
+    def on_profileComboBox_currentIndexChanged(self, profileName):
+        """
+        Private slot handling the change of the selected profile.
+        
+        @param profileName name of the selected profile
+        @type str
+        """
+        self.__setConnectButtonState()
+    
+    @pyqtSlot(str)
     def on_brokerComboBox_editTextChanged(self, host):
         """
         Private slot to handling entering or selecting a broker host name.
@@ -730,7 +790,7 @@
         if host:
             self.brokerStatusLabel.setText(
                 self.tr("Connecting to {0}:{1} ...").format(
-                host, port))
+                    host, port))
             self.brokerStatusLabel.show()
             
             self.__addBrokerToRecent(host, port)
@@ -756,7 +816,7 @@
             
             self.brokerStatusLabel.setText(
                 self.tr("Connecting to {0}:{1} ...").format(
-                host, port))
+                    host, port))
             self.brokerStatusLabel.show()
             
             self.connectButton.setEnabled(False)

eric ide

mercurial