--- a/MqttMonitor/MqttMonitorWidget.py Mon Jun 28 17:44:07 2021 +0200 +++ b/MqttMonitor/MqttMonitorWidget.py Sun Jul 18 18:30:15 2021 +0200 @@ -22,7 +22,8 @@ from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget from .MqttClient import ( - MqttClient, mqttConnackMessage, mqttErrorMessage, mqttLogLevelString + MqttClient, MqttProtocols, mqttConnackMessage, mqttErrorMessage, + mqttLogLevelString ) import UI.PixmapCache @@ -184,19 +185,35 @@ self.__statusLoadValues = collections.defaultdict( self.__loadDefaultDictFactory) + + ####################################################################### + ## Slots handling MQTT related signals + ####################################################################### + + def __createClient(self, protocol=MqttProtocols.MQTTv311): + """ + Private method to instantiate a MQTT client for a given protocol. - self.__client = MqttClient() + @param protocol MQTT protocol version to be used (defaults to + MqttProtocols.MQTTv311) + @type MqttProtocols (optional) + @return created and connected MQTT client object + @rtype MqttClient + """ + client = MqttClient(protocol=protocol) # 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) + client.onConnect.connect(self.__brokerConnected) + client.onDisconnected.connect(self.__brokerDisconnected) + client.onLog.connect(self.__clientLog) + client.onMessage.connect(self.__messageReceived) + client.onPublish.connect(self.__messagePublished) + client.onSubscribe.connect(self.__topicSubscribed) + client.onUnsubscribe.connect(self.__topicUnsubscribed) - self.__client.connectTimeout.connect(self.__connectTimeout) + client.connectTimeout.connect(self.__connectTimeout) + + return client ####################################################################### ## Slots handling MQTT related signals @@ -446,8 +463,7 @@ MqttConnectionProfilesDialog ) dlg = MqttConnectionProfilesDialog( - self.__client, self.__plugin.getPreferences("BrokerProfiles"), - parent=self) + self.__plugin.getPreferences("BrokerProfiles"), parent=self) if dlg.exec() == QDialog.DialogCode.Accepted: profilesDict = dlg.getProfiles() self.__plugin.setPreferences("BrokerProfiles", profilesDict) @@ -457,7 +473,7 @@ MqttConnectionOptionsDialog ) dlg = MqttConnectionOptionsDialog( - self.__client, self.__connectionOptions, parent=self) + self.__connectionOptions, parent=self) if dlg.exec() == QDialog.DialogCode.Accepted: self.__connectionOptions = dlg.getConnectionOptions() if self.__connectionOptions["TlsEnable"]: @@ -1026,8 +1042,11 @@ self.__addBrokerToRecent(host, port) self.connectButton.setEnabled(False) if self.__connectionOptions is None: + self.__client = self.__createClient() self.__client.connectToServer(host, port=port) else: + self.__client = self.__createClient( + protocol=self.__connectionOptions["Protocol"]) self.__client.connectToServerWithOptions( host, port=port, options=self.__connectionOptions) @@ -1040,9 +1059,13 @@ self.__plugin.setPreferences("MostRecentProfile", profileName) profilesDict = self.__plugin.getPreferences("BrokerProfiles") - profile = copy.copy(profilesDict[profileName]) # play it save - host = profile["BrokerAddress"] - port = profile["BrokerPort"] + connectionProfile = copy.copy(profilesDict[profileName]) + host = connectionProfile["BrokerAddress"] + port = connectionProfile["BrokerPort"] + try: + protocol = connectionProfile["Protocol"] + except KeyError: + protocol = MqttProtocols.MQTTv311 self.brokerStatusLabel.setText( self.tr("Connecting to {0}:{1} ...").format( @@ -1050,5 +1073,7 @@ self.brokerStatusLabel.show() self.connectButton.setEnabled(False) - self.__client.connectToServerWithOptions(host, port=port, - options=profile) + + self.__client = self.__createClient(protocol=protocol) + self.__client.connectToServerWithOptions( + host, port=port, options=connectionProfile)