MqttMonitor/MqttClient.py

branch
eric7
changeset 104
9a4c9b7f078c
parent 103
5fe4f179975f
child 105
36ec7431ad04
--- a/MqttMonitor/MqttClient.py	Thu Jul 22 19:02:32 2021 +0200
+++ b/MqttMonitor/MqttClient.py	Fri Jul 23 17:48:22 2021 +0200
@@ -279,8 +279,8 @@
         """
         self.__mqttClient.user_data_set(userdata)
     
-    # TODO: MQTTv5: add support for WILL properties
-    def setLastWill(self, topic, payload=None, qos=0, retain=False):
+    def setLastWill(self, topic, payload=None, qos=0, retain=False,
+                    properties=None):
         """
         Public method to set the last will of the client.
         
@@ -293,9 +293,12 @@
         @param retain flag indicating to set as the "last known good"/retained
             message for the will topic
         @type bool
+        @param properties list of user properties to be sent with the
+            last will message
+        @type list of tuple of (str, str)
         """
         self.__mqttClient.will_set(topic, payload=payload, qos=qos,
-                                   retain=retain)
+                                   retain=retain, properties=properties)
     
     def clearLastWill(self):
         """
@@ -351,7 +354,7 @@
         self.__loopStarted = False
     
     def connectToServer(self, host, port=1883, keepalive=60, bindAddress="",
-                        properties=None):
+                        properties=None, clearWill=False):
         """
         Public method to connect to a remote MQTT broker.
         
@@ -369,7 +372,12 @@
         @param properties list of user properties to be sent with the
             subscription
         @type list of tuple of (str, str)
+        @param clearWill flag indicating to clear the last will previously set
+        @type bool
         """
+        if clearWill:
+            self.clearLastWill()
+        
         props = (
             self.__createPropertiesObject(PacketTypes.CONNECT, properties)
             if properties else
@@ -385,7 +393,7 @@
             self.startLoop()
     
     def connectToServerWithOptions(self, host, port=1883, bindAddress="",
-                                   options=None):
+                                   options=None, clearWill=False):
         """
         Public method to connect to a remote MQTT broker.
         
@@ -400,9 +408,12 @@
         @param options dictionary containing the connection options. This
             dictionary should contain the keys "ClientId", "ConnectionTimeout",
             "Keepalive", "CleanSession", "Username", "Password", "WillTopic",
-            "WillMessage", "WillQos", "WillRetain", "TlsEnable", "TlsCaCert",
-            "TlsClientCert", "TlsClientKey", "UserProperties".
+            "WillMessage", "WillQos", "WillRetain", "WillProperties",
+            "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey",
+            "UserProperties".
         @type dict
+        @param clearWill flag indicating to clear the last will previously set
+        @type bool
         """
         if options:
             parametersDict = self.defaultConnectionOptions()
@@ -420,16 +431,25 @@
                     self.setUserCredentials(parametersDict["Username"])
             
             # step 2: set last will data
-            if parametersDict["WillTopic"]:
+            if not clearWill and parametersDict["WillTopic"]:
                 if parametersDict["WillMessage"]:
                     willMessage = parametersDict["WillMessage"]
                 else:
                     # empty message to clear the will
                     willMessage = None
+                props = (
+                    self.__createPropertiesObject(
+                        PacketTypes.WILLMESSAGE,
+                        parametersDict["WillProperties"])
+                    if (parametersDict["WillProperties"] and
+                        self.__protocol == MqttProtocols.MQTTv5) else
+                    None
+                )
                 self.setLastWill(parametersDict["WillTopic"],
-                                 willMessage,
-                                 parametersDict["WillQos"],
-                                 parametersDict["WillRetain"])
+                                 payload=willMessage,
+                                 qos=parametersDict["WillQos"],
+                                 retain=parametersDict["WillRetain"],
+                                 properties=props)
             
             # step 3: set TLS parameters
             if parametersDict["TlsEnable"]:
@@ -466,11 +486,13 @@
             self.__cleanSession = parametersDict["CleanSession"]
             self.connectToServer(host, port=port,
                                  keepalive=parametersDict["Keepalive"],
-                                 properties=properties)
+                                 properties=properties,
+                                 clearWill=clearWill)
         else:
             keepalive = self.defaultConnectionOptions["Keepalive"]
             self.connectToServer(host, port=port, keepalive=keepalive,
-                                 bindAddress=bindAddress)
+                                 bindAddress=bindAddress,
+                                 clearWill=clearWill)
     
     @classmethod
     def defaultConnectionOptions(cls):
@@ -481,8 +503,8 @@
         @return dictionary containing the default connection options. It has
             the keys "ClientId", "Protocol", "ConnectionTimeout", "Keepalive",
             "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
-            "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", "TlsClientCert",
-            "TlsClientKey", "UserProperties".
+            "WillQos", "WillRetain", "WillProperties", "TlsEnable",
+            "TlsCaCert", "TlsClientCert", "TlsClientKey", "UserProperties".
         @rtype dict
         """
         return {
@@ -497,6 +519,7 @@
             "WillMessage": "",
             "WillQos": 0,
             "WillRetain": False,
+            "WillProperties": [],
             "TlsEnable": False,
             "TlsCaCert": "",
             "TlsClientCert": "",

eric ide

mercurial