MqttMonitor/MqttMonitorWidget.py

branch
eric7
changeset 97
21f9c010dc42
parent 92
2fb5c08019fd
child 98
85d56e77e9df
equal deleted inserted replaced
96:06d1cba236eb 97:21f9c010dc42
20 from EricWidgets.EricPathPicker import EricPathPickerModes 20 from EricWidgets.EricPathPicker import EricPathPickerModes
21 21
22 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget 22 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
23 23
24 from .MqttClient import ( 24 from .MqttClient import (
25 MqttClient, mqttConnackMessage, mqttErrorMessage, mqttLogLevelString 25 MqttClient, MqttProtocols, mqttConnackMessage, mqttErrorMessage,
26 mqttLogLevelString
26 ) 27 )
27 28
28 import UI.PixmapCache 29 import UI.PixmapCache
29 import Utilities 30 import Utilities
30 31
182 prefix + "load/sockets": self.loadSocketsLabel, 183 prefix + "load/sockets": self.loadSocketsLabel,
183 } 184 }
184 185
185 self.__statusLoadValues = collections.defaultdict( 186 self.__statusLoadValues = collections.defaultdict(
186 self.__loadDefaultDictFactory) 187 self.__loadDefaultDictFactory)
187 188
188 self.__client = MqttClient() 189 #######################################################################
190 ## Slots handling MQTT related signals
191 #######################################################################
192
193 def __createClient(self, protocol=MqttProtocols.MQTTv311):
194 """
195 Private method to instantiate a MQTT client for a given protocol.
196
197 @param protocol MQTT protocol version to be used (defaults to
198 MqttProtocols.MQTTv311)
199 @type MqttProtocols (optional)
200 @return created and connected MQTT client object
201 @rtype MqttClient
202 """
203 client = MqttClient(protocol=protocol)
189 204
190 # connect the MQTT client signals 205 # connect the MQTT client signals
191 self.__client.onConnect.connect(self.__brokerConnected) 206 client.onConnect.connect(self.__brokerConnected)
192 self.__client.onDisconnected.connect(self.__brokerDisconnected) 207 client.onDisconnected.connect(self.__brokerDisconnected)
193 self.__client.onLog.connect(self.__clientLog) 208 client.onLog.connect(self.__clientLog)
194 self.__client.onMessage.connect(self.__messageReceived) 209 client.onMessage.connect(self.__messageReceived)
195 self.__client.onPublish.connect(self.__messagePublished) 210 client.onPublish.connect(self.__messagePublished)
196 self.__client.onSubscribe.connect(self.__topicSubscribed) 211 client.onSubscribe.connect(self.__topicSubscribed)
197 self.__client.onUnsubscribe.connect(self.__topicUnsubscribed) 212 client.onUnsubscribe.connect(self.__topicUnsubscribed)
198 213
199 self.__client.connectTimeout.connect(self.__connectTimeout) 214 client.connectTimeout.connect(self.__connectTimeout)
215
216 return client
200 217
201 ####################################################################### 218 #######################################################################
202 ## Slots handling MQTT related signals 219 ## Slots handling MQTT related signals
203 ####################################################################### 220 #######################################################################
204 221
444 if self.__connectionModeProfile: 461 if self.__connectionModeProfile:
445 from .MqttConnectionProfilesDialog import ( 462 from .MqttConnectionProfilesDialog import (
446 MqttConnectionProfilesDialog 463 MqttConnectionProfilesDialog
447 ) 464 )
448 dlg = MqttConnectionProfilesDialog( 465 dlg = MqttConnectionProfilesDialog(
449 self.__client, self.__plugin.getPreferences("BrokerProfiles"), 466 self.__plugin.getPreferences("BrokerProfiles"), parent=self)
450 parent=self)
451 if dlg.exec() == QDialog.DialogCode.Accepted: 467 if dlg.exec() == QDialog.DialogCode.Accepted:
452 profilesDict = dlg.getProfiles() 468 profilesDict = dlg.getProfiles()
453 self.__plugin.setPreferences("BrokerProfiles", profilesDict) 469 self.__plugin.setPreferences("BrokerProfiles", profilesDict)
454 self.__populateProfileComboBox() 470 self.__populateProfileComboBox()
455 else: 471 else:
456 from .MqttConnectionOptionsDialog import ( 472 from .MqttConnectionOptionsDialog import (
457 MqttConnectionOptionsDialog 473 MqttConnectionOptionsDialog
458 ) 474 )
459 dlg = MqttConnectionOptionsDialog( 475 dlg = MqttConnectionOptionsDialog(
460 self.__client, self.__connectionOptions, parent=self) 476 self.__connectionOptions, parent=self)
461 if dlg.exec() == QDialog.DialogCode.Accepted: 477 if dlg.exec() == QDialog.DialogCode.Accepted:
462 self.__connectionOptions = dlg.getConnectionOptions() 478 self.__connectionOptions = dlg.getConnectionOptions()
463 if self.__connectionOptions["TlsEnable"]: 479 if self.__connectionOptions["TlsEnable"]:
464 port = self.brokerPortComboBox.currentText().strip() 480 port = self.brokerPortComboBox.currentText().strip()
465 if port == "1883": 481 if port == "1883":
1024 self.brokerStatusLabel.show() 1040 self.brokerStatusLabel.show()
1025 1041
1026 self.__addBrokerToRecent(host, port) 1042 self.__addBrokerToRecent(host, port)
1027 self.connectButton.setEnabled(False) 1043 self.connectButton.setEnabled(False)
1028 if self.__connectionOptions is None: 1044 if self.__connectionOptions is None:
1045 self.__client = self.__createClient()
1029 self.__client.connectToServer(host, port=port) 1046 self.__client.connectToServer(host, port=port)
1030 else: 1047 else:
1048 self.__client = self.__createClient(
1049 protocol=self.__connectionOptions["Protocol"])
1031 self.__client.connectToServerWithOptions( 1050 self.__client.connectToServerWithOptions(
1032 host, port=port, options=self.__connectionOptions) 1051 host, port=port, options=self.__connectionOptions)
1033 1052
1034 def __profileConnectToBroker(self): 1053 def __profileConnectToBroker(self):
1035 """ 1054 """
1038 profileName = self.profileComboBox.currentText() 1057 profileName = self.profileComboBox.currentText()
1039 if profileName: 1058 if profileName:
1040 self.__plugin.setPreferences("MostRecentProfile", profileName) 1059 self.__plugin.setPreferences("MostRecentProfile", profileName)
1041 1060
1042 profilesDict = self.__plugin.getPreferences("BrokerProfiles") 1061 profilesDict = self.__plugin.getPreferences("BrokerProfiles")
1043 profile = copy.copy(profilesDict[profileName]) # play it save 1062 connectionProfile = copy.copy(profilesDict[profileName])
1044 host = profile["BrokerAddress"] 1063 host = connectionProfile["BrokerAddress"]
1045 port = profile["BrokerPort"] 1064 port = connectionProfile["BrokerPort"]
1065 try:
1066 protocol = connectionProfile["Protocol"]
1067 except KeyError:
1068 protocol = MqttProtocols.MQTTv311
1046 1069
1047 self.brokerStatusLabel.setText( 1070 self.brokerStatusLabel.setText(
1048 self.tr("Connecting to {0}:{1} ...").format( 1071 self.tr("Connecting to {0}:{1} ...").format(
1049 host, port)) 1072 host, port))
1050 self.brokerStatusLabel.show() 1073 self.brokerStatusLabel.show()
1051 1074
1052 self.connectButton.setEnabled(False) 1075 self.connectButton.setEnabled(False)
1053 self.__client.connectToServerWithOptions(host, port=port, 1076
1054 options=profile) 1077 self.__client = self.__createClient(protocol=protocol)
1078 self.__client.connectToServerWithOptions(
1079 host, port=port, options=connectionProfile)

eric ide

mercurial