Some smaller improvements and added some TODOs.

Sun, 09 Sep 2018 12:21:19 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 09 Sep 2018 12:21:19 +0200
changeset 30
17ef10819773
parent 28
0f02baed8308
child 31
40582e448c4b

Some smaller improvements and added some TODOs.

MqttMonitor/MqttClient.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttConnectionOptionsDialog.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttConnectionProfilesDialog.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
PluginMqttMonitor.py file | annotate | diff | comparison | revisions
diff -r 0f02baed8308 -r 17ef10819773 MqttMonitor/MqttClient.py
--- a/MqttMonitor/MqttClient.py	Sat Sep 08 16:55:42 2018 +0200
+++ b/MqttMonitor/MqttClient.py	Sun Sep 09 12:21:19 2018 +0200
@@ -160,6 +160,13 @@
         self.__mqttClient.will_set(topic, payload=payload, qos=qos,
                                    retain=retain)
     
+    def clearLastWill(self):
+        """
+        Public method to remove a will that was previously configured with
+        setLastWill().
+        """
+        self.__mqttClient.will_clear()
+    
     def setTLS(self, caCerts=None, certFile=None, keyFile=None):
         """
         Public method to enable secure connections and set the TLS parameters.
@@ -170,9 +177,16 @@
         @type str
         @param keyFile PEM encoded private key file
         @type str
+        @return tuple containing a success flag and the error string of the
+            paho-mqtt library
+        @rtype tuple of (bool, str)
         """
-        self.__mqttClient.tls_set(ca_certs=caCerts, certfile=certFile,
-                                  keyfile=keyFile)
+        try:
+            self.__mqttClient.tls_set(ca_certs=caCerts, certfile=certFile,
+                                      keyfile=keyFile)
+            return True, ""
+        except ValueError as err:
+            return False, str(err)
     
     def startLoop(self):
         """
@@ -204,6 +218,8 @@
             this client to
         @type str
         """
+        # TODO: get this fixed or allow to interrupt
+        self.__mqttClient.reconnect_delay_set(max_delay=16)
         self.__mqttClient.connect_async(
             host, port=port, keepalive=keepalive, bind_address=bindAddress)
         
diff -r 0f02baed8308 -r 17ef10819773 MqttMonitor/MqttConnectionOptionsDialog.py
--- a/MqttMonitor/MqttConnectionOptionsDialog.py	Sat Sep 08 16:55:42 2018 +0200
+++ b/MqttMonitor/MqttConnectionOptionsDialog.py	Sun Sep 09 12:21:19 2018 +0200
@@ -24,6 +24,7 @@
     """
     Class implementing a dialog to enter MQTT connection options.
     """
+    # TODO: add 'Clear Will'
     def __init__(self, client, options=None, parent=None):
         """
         Constructor
diff -r 0f02baed8308 -r 17ef10819773 MqttMonitor/MqttConnectionProfilesDialog.py
--- a/MqttMonitor/MqttConnectionProfilesDialog.py	Sat Sep 08 16:55:42 2018 +0200
+++ b/MqttMonitor/MqttConnectionProfilesDialog.py	Sun Sep 09 12:21:19 2018 +0200
@@ -28,6 +28,7 @@
     """
     Class implementing a dialog to edit the MQTT connection profiles.
     """
+    # TODO: add 'Clear Will'
     def __init__(self, client, profiles, parent=None):
         """
         Constructor
diff -r 0f02baed8308 -r 17ef10819773 MqttMonitor/MqttMonitorWidget.py
--- a/MqttMonitor/MqttMonitorWidget.py	Sat Sep 08 16:55:42 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Sun Sep 09 12:21:19 2018 +0200
@@ -154,6 +154,9 @@
         @param rc CONNACK result code
         @type int
         """
+        self.brokerStatusLabel.hide()
+        
+        # TODO: add support for flags[‘session present’]
         if rc == 0:
             self.__connectedToBroker = True
             self.__connectionOptions = None
@@ -161,16 +164,24 @@
         msg = mqttConnackMessage(rc)
         self.__flashBrokerStatusLabel(msg)
         
-        self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png"))
-        
-        self.subscribeGroup.setEnabled(True)
-        self.unsubscribeGroup.setEnabled(True)
-        self.publishGroup.setEnabled(True)
-        self.brokerStatusButton.setEnabled(True)
-        
-        self.__statusLoadValues.clear()
-        self.__clearBrokerStatusLabels()
-        self.__setBrokerStatusSubscribed(False)
+        self.connectButton.setEnabled(True)
+        if rc == 0:
+            self.__connectedToBroker = True
+            self.__connectionOptions = None
+            
+            self.connectButton.setIcon(
+                UI.PixmapCache.getIcon("ircDisconnect.png"))
+            
+            self.subscribeGroup.setEnabled(True)
+            self.unsubscribeGroup.setEnabled(True)
+            self.publishGroup.setEnabled(True)
+            self.brokerStatusButton.setEnabled(True)
+            
+            self.__statusLoadValues.clear()
+            self.__clearBrokerStatusLabels()
+            self.__setBrokerStatusSubscribed(False)
+        else:
+            self.__client.stopLoop()
     
     @pyqtSlot(int)
     def __brokerDisconnected(self, rc):
@@ -185,13 +196,14 @@
         # ensure, the client loop is stopped
         self.__client.stopLoop()
         
-        if rc != 0:
+        if rc > 0:
             msg = mqttErrorMessage(rc)
         else:
             msg = self.tr("Connection to Broker shut down cleanly.")
         self.__flashBrokerStatusLabel(msg)
         
         self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
+        self.connectButton.setEnabled(True)
 
         self.__subscribedTopics = []
         self.__topicQueue = {}
@@ -424,6 +436,8 @@
         """
         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()
@@ -545,9 +559,14 @@
         Private method to populate the profiles selection box.
         """
         profilesDict = self.__plugin.getPreferences("BrokerProfiles")
+        mostRecentProfile = self.__plugin.getPreferences("MostRecentProfile")
         
         self.profileComboBox.clear()
         self.profileComboBox.addItems(sorted(profilesDict.keys()))
+        if mostRecentProfile:
+            index = self.profileComboBox.findText(mostRecentProfile)
+            if index >= 0:
+                self.profileComboBox.setCurrentIndex(index)
         
         self.__setConnectButtonState()
     
@@ -709,7 +728,13 @@
             # use standard port at 1883
             port = 1883
         if host:
+            self.brokerStatusLabel.setText(
+                self.tr("Connecting to {0}:{1} ...").format(
+                host, port))
+            self.brokerStatusLabel.show()
+            
             self.__addBrokerToRecent(host, port)
+            self.connectButton.setEnabled(False)
             if self.__connectionOptions is None:
                 self.__client.connectToServer(host, port=port)
             else:
@@ -722,10 +747,18 @@
         """
         profileName = self.profileComboBox.currentText()
         if profileName:
+            self.__plugin.setPreferences("MostRecentProfile", profileName)
+            
             profilesDict = self.__plugin.getPreferences("BrokerProfiles")
             profile = copy.copy(profilesDict[profileName])      # play it save
             host = profile["BrokerAddress"]
             port = profile["BrokerPort"]
             
+            self.brokerStatusLabel.setText(
+                self.tr("Connecting to {0}:{1} ...").format(
+                host, port))
+            self.brokerStatusLabel.show()
+            
+            self.connectButton.setEnabled(False)
             self.__client.connectToServerWithOptions(host, port=port,
                                                      options=profile)
diff -r 0f02baed8308 -r 17ef10819773 PluginMqttMonitor.py
--- a/PluginMqttMonitor.py	Sat Sep 08 16:55:42 2018 +0200
+++ b/PluginMqttMonitor.py	Sun Sep 09 12:21:19 2018 +0200
@@ -96,6 +96,7 @@
             "RecentBrokersWithPort": "[]",      # JSON formatted empty list
             "BrokerProfiles": "{}",             # JSON formatted empty dict
             # __IGNORE_WARNING_M613__
+            "MostRecentProfile": "",            # most recently used profile
         }
         
         self.__translator = None

eric ide

mercurial