MqttMonitor/MqttConnectionProfilesDialog.py

branch
eric7
changeset 97
21f9c010dc42
parent 95
d830314cca87
child 103
5fe4f179975f
equal deleted inserted replaced
96:06d1cba236eb 97:21f9c010dc42
18 from EricWidgets import EricMessageBox 18 from EricWidgets import EricMessageBox
19 from EricWidgets.EricPathPicker import EricPathPickerModes 19 from EricWidgets.EricPathPicker import EricPathPickerModes
20 20
21 from .Ui_MqttConnectionProfilesDialog import Ui_MqttConnectionProfilesDialog 21 from .Ui_MqttConnectionProfilesDialog import Ui_MqttConnectionProfilesDialog
22 22
23 from .MqttClient import MqttClient, MqttProtocols
24
23 import UI.PixmapCache 25 import UI.PixmapCache
24 from Utilities.crypto import pwConvert 26 from Utilities.crypto import pwConvert
25 27
26 28
27 class MqttConnectionProfilesDialog(QDialog, Ui_MqttConnectionProfilesDialog): 29 class MqttConnectionProfilesDialog(QDialog, Ui_MqttConnectionProfilesDialog):
28 """ 30 """
29 Class implementing a dialog to edit the MQTT connection profiles. 31 Class implementing a dialog to edit the MQTT connection profiles.
30 """ 32 """
31 def __init__(self, client, profiles, parent=None): 33 def __init__(self, profiles, parent=None):
32 """ 34 """
33 Constructor 35 Constructor
34 36
35 @param client reference to the MQTT client object
36 @type MqttClient
37 @param profiles dictionary containing dictionaries containing the 37 @param profiles dictionary containing dictionaries containing the
38 connection parameters. Each entry must have the keys 38 connection parameters. Each entry must have the keys
39 "BrokerAddress", "BrokerPort", "ClientId", 39 "BrokerAddress", "BrokerPort", "ClientId", "Protocol",
40 "Keepalive", "CleanSession", "Username", "Password", "WillTopic", 40 "ConnectionTimeout", "Keepalive", "CleanSession", "Username",
41 "WillMessage", "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", 41 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain",
42 "TlsClientCert", "TlsClientKey", "ConnectionTimeout". 42 "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey".
43 @type dict 43 @type dict
44 @param parent reference to the parent widget 44 @param parent reference to the parent widget
45 @type QWidget 45 @type QWidget
46 """ 46 """
47 super().__init__(parent) 47 super().__init__(parent)
48 self.setupUi(self) 48 self.setupUi(self)
49
50 self.__client = client
51 49
52 self.__profiles = collections.defaultdict(self.__defaultProfile) 50 self.__profiles = collections.defaultdict(self.__defaultProfile)
53 self.__profiles.update(profiles) 51 self.__profiles.update(profiles)
54 self.__profilesChanged = False 52 self.__profilesChanged = False
55 53
237 """ 235 """
238 Public method to return a dictionary of profiles. 236 Public method to return a dictionary of profiles.
239 237
240 @return dictionary containing dictionaries containing the defined 238 @return dictionary containing dictionaries containing the defined
241 connection profiles. Each entry have the keys "BrokerAddress", 239 connection profiles. Each entry have the keys "BrokerAddress",
242 "BrokerPort", "ClientId", "Keepalive", "CleanSession", "Username", 240 "BrokerPort", "ClientId", "Protocol", "ConnectionTimeout",
243 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain", 241 "Keepalive", "CleanSession", "Username", "Password", "WillTopic",
244 "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey", 242 "WillMessage", "WillQos", "WillRetain", "TlsEnable", "TlsCaCert",
245 "ConnectionTimeout". 243 "TlsClientCert", "TlsClientKey".
246 @rtype dict 244 @rtype dict
247 """ 245 """
248 profilesDict = {} 246 profilesDict = {}
249 profilesDict.update(self.__profiles) 247 profilesDict.update(self.__profiles)
250 return profilesDict 248 return profilesDict
254 Private method to apply the entered data to the list of profiles. 252 Private method to apply the entered data to the list of profiles.
255 253
256 @return name of the applied profile 254 @return name of the applied profile
257 @rtype str 255 @rtype str
258 """ 256 """
257 if self.mqttv31Button.isChecked():
258 protocol = MqttProtocols.MQTTv31
259 elif self.mqttv311Button.isChecked():
260 protocol = MqttProtocols.MQTTv311
261 elif self.mqttv5Button.isChecked():
262 protocol = MqttProtocols.MQTTv5
263 else:
264 protocol = MqttProtocols.MQTTv311
265
259 profileName = self.profileEdit.text() 266 profileName = self.profileEdit.text()
260 profile = { 267 connectionProfile = {
261 "BrokerAddress": self.brokerAddressEdit.text(), 268 "BrokerAddress": self.brokerAddressEdit.text(),
262 "BrokerPort": self.brokerPortSpinBox.value(), 269 "BrokerPort": self.brokerPortSpinBox.value(),
263 "ClientId": self.clientIdEdit.text(), 270 "ClientId": self.clientIdEdit.text(),
271 "Protocol": protocol,
264 "ConnectionTimeout": self.connectionTimeoutSpinBox.value(), 272 "ConnectionTimeout": self.connectionTimeoutSpinBox.value(),
265 "Keepalive": self.keepaliveSpinBox.value(), 273 "Keepalive": self.keepaliveSpinBox.value(),
266 "CleanSession": self.cleanSessionCheckBox.isChecked(), 274 "CleanSession": self.cleanSessionCheckBox.isChecked(),
267 "Username": self.usernameEdit.text(), 275 "Username": self.usernameEdit.text(),
268 "Password": pwConvert(self.passwordEdit.text(), encode=True), 276 "Password": pwConvert(self.passwordEdit.text(), encode=True),
273 "TlsEnable": self.tlsGroupBox.isChecked(), 281 "TlsEnable": self.tlsGroupBox.isChecked(),
274 "TlsCaCert": "", 282 "TlsCaCert": "",
275 "TlsClientCert": "", 283 "TlsClientCert": "",
276 "TlsClientKey": "", 284 "TlsClientKey": "",
277 } 285 }
278 if profile["TlsEnable"]: 286 if connectionProfile["TlsEnable"]:
279 if self.tlsCertsFileButton.isChecked(): 287 if self.tlsCertsFileButton.isChecked():
280 profile["TlsCaCert"] = self.tlsCertsFilePicker.text() 288 connectionProfile["TlsCaCert"] = self.tlsCertsFilePicker.text()
281 elif self.tlsSelfSignedCertsButton.isChecked(): 289 elif self.tlsSelfSignedCertsButton.isChecked():
282 profile["TlsCaCert"] = ( 290 connectionProfile["TlsCaCert"] = (
283 self.tlsSelfSignedCertsFilePicker.text()) 291 self.tlsSelfSignedCertsFilePicker.text())
284 profile["TlsClientCert"] = ( 292 connectionProfile["TlsClientCert"] = (
285 self.tlsSelfSignedClientCertFilePicker.text()) 293 self.tlsSelfSignedClientCertFilePicker.text())
286 profile["TlsClientKey"] = ( 294 connectionProfile["TlsClientKey"] = (
287 self.tlsSelfSignedClientKeyFilePicker.text()) 295 self.tlsSelfSignedClientKeyFilePicker.text())
288 296
289 self.__profiles[profileName] = profile 297 self.__profiles[profileName] = connectionProfile
290 self.__profilesChanged = True 298 self.__profilesChanged = True
291 299
292 return profileName 300 return profileName
293 301
294 def __defaultProfile(self): 302 def __defaultProfile(self):
296 Private method to populate non-existing profile items. 304 Private method to populate non-existing profile items.
297 305
298 @return default dictionary entry 306 @return default dictionary entry
299 @rtype dict 307 @rtype dict
300 """ 308 """
301 defaultProfile = self.__client.defaultConnectionOptions() 309 defaultProfile = MqttClient.defaultConnectionOptions()
302 defaultProfile["BrokerAddress"] = "" 310 defaultProfile["BrokerAddress"] = ""
303 if defaultProfile["TlsEnable"]: 311 if defaultProfile["TlsEnable"]:
304 defaultProfile["BrokerPort"] = 8883 312 defaultProfile["BrokerPort"] = 8883
305 else: 313 else:
306 defaultProfile["BrokerPort"] = 1883 314 defaultProfile["BrokerPort"] = 1883
336 Private method to populate the profile data entry fields. 344 Private method to populate the profile data entry fields.
337 345
338 @param profileName name of the profile to get data from 346 @param profileName name of the profile to get data from
339 @type str 347 @type str
340 """ 348 """
341 profile = self.__defaultProfile() 349 connectionProfile = self.__defaultProfile()
342 if profileName: 350 if profileName:
343 profile.update(self.__profiles[profileName]) 351 connectionProfile.update(self.__profiles[profileName])
344 352
345 self.__populatingProfile = True 353 self.__populatingProfile = True
346 if profileName is not None: 354 if profileName is not None:
347 self.profileEdit.setText(profileName) 355 self.profileEdit.setText(profileName)
348 self.brokerAddressEdit.setText(profile["BrokerAddress"]) 356 self.brokerAddressEdit.setText(connectionProfile["BrokerAddress"])
349 self.brokerPortSpinBox.setValue(profile["BrokerPort"]) 357 self.brokerPortSpinBox.setValue(connectionProfile["BrokerPort"])
350 self.clientIdEdit.setText(profile["ClientId"]) 358 self.clientIdEdit.setText(connectionProfile["ClientId"])
351 self.connectionTimeoutSpinBox.setValue(profile["ConnectionTimeout"]) 359 self.mqttv31Button.setChecked(
352 self.keepaliveSpinBox.setValue(profile["Keepalive"]) 360 connectionProfile["Protocol"] == MqttProtocols.MQTTv31)
353 self.cleanSessionCheckBox.setChecked(profile["CleanSession"]) 361 self.mqttv311Button.setChecked(
354 self.usernameEdit.setText(profile["Username"]) 362 connectionProfile["Protocol"] == MqttProtocols.MQTTv311)
355 self.passwordEdit.setText(pwConvert(profile["Password"], encode=False)) 363 self.mqttv5Button.setChecked(
356 self.willTopicEdit.setText(profile["WillTopic"]) 364 connectionProfile["Protocol"] == MqttProtocols.MQTTv5)
357 self.willMessageEdit.setPlainText(profile["WillMessage"]) 365 self.connectionTimeoutSpinBox.setValue(
358 self.willQosSpinBox.setValue(profile["WillQos"]) 366 connectionProfile["ConnectionTimeout"])
359 self.willRetainCheckBox.setChecked(profile["WillRetain"]) 367 self.keepaliveSpinBox.setValue(connectionProfile["Keepalive"])
360 self.tlsGroupBox.setChecked(profile["TlsEnable"]) 368 self.cleanSessionCheckBox.setChecked(connectionProfile["CleanSession"])
361 if profile["TlsCaCert"] and profile["TlsClientCert"]: 369 self.usernameEdit.setText(connectionProfile["Username"])
370 self.passwordEdit.setText(
371 pwConvert(connectionProfile["Password"], encode=False))
372 self.willTopicEdit.setText(connectionProfile["WillTopic"])
373 self.willMessageEdit.setPlainText(connectionProfile["WillMessage"])
374 self.willQosSpinBox.setValue(connectionProfile["WillQos"])
375 self.willRetainCheckBox.setChecked(connectionProfile["WillRetain"])
376 self.tlsGroupBox.setChecked(connectionProfile["TlsEnable"])
377 if (
378 connectionProfile["TlsCaCert"] and
379 connectionProfile["TlsClientCert"]
380 ):
362 self.tlsSelfSignedCertsButton.setChecked(True) 381 self.tlsSelfSignedCertsButton.setChecked(True)
363 self.tlsSelfSignedCertsFilePicker.setText(profile["TlsCaCert"]) 382 self.tlsSelfSignedCertsFilePicker.setText(
383 connectionProfile["TlsCaCert"])
364 self.tlsSelfSignedClientCertFilePicker.setText( 384 self.tlsSelfSignedClientCertFilePicker.setText(
365 profile["TlsClientCert"]) 385 connectionProfile["TlsClientCert"])
366 self.tlsSelfSignedClientKeyFilePicker.setText( 386 self.tlsSelfSignedClientKeyFilePicker.setText(
367 profile["TlsClientKey"]) 387 connectionProfile["TlsClientKey"])
368 elif profile["TlsCaCert"]: 388 elif connectionProfile["TlsCaCert"]:
369 self.tlsCertsFileButton.setChecked(True) 389 self.tlsCertsFileButton.setChecked(True)
370 self.tlsCertsFilePicker.setText(profile["TlsCaCert"]) 390 self.tlsCertsFilePicker.setText(connectionProfile["TlsCaCert"])
371 else: 391 else:
372 self.tlsDefaultCertsButton.setChecked(True) 392 self.tlsDefaultCertsButton.setChecked(True)
373 self.__populatingProfile = False 393 self.__populatingProfile = False
374 394
375 self.showPasswordButton.setChecked(False) 395 self.showPasswordButton.setChecked(False)
383 self.__populatingProfile = True 403 self.__populatingProfile = True
384 self.profileEdit.setText("") 404 self.profileEdit.setText("")
385 self.brokerAddressEdit.setText("") 405 self.brokerAddressEdit.setText("")
386 self.brokerPortSpinBox.setValue(1883) 406 self.brokerPortSpinBox.setValue(1883)
387 self.clientIdEdit.setText("") 407 self.clientIdEdit.setText("")
408 self.mqttv311Button.setChecked(True)
388 self.keepaliveSpinBox.setValue(60) 409 self.keepaliveSpinBox.setValue(60)
389 self.cleanSessionCheckBox.setChecked(True) 410 self.cleanSessionCheckBox.setChecked(True)
390 self.usernameEdit.setText("") 411 self.usernameEdit.setText("")
391 self.passwordEdit.setText("") 412 self.passwordEdit.setText("")
392 self.willTopicEdit.setText("") 413 self.willTopicEdit.setText("")
434 profileName = self.profileEdit.text() 455 profileName = self.profileEdit.text()
435 if profileName == "": 456 if profileName == "":
436 return False 457 return False
437 458
438 elif profileName in self.__profiles: 459 elif profileName in self.__profiles:
439 profile = self.__defaultProfile() 460 if self.mqttv31Button.isChecked():
440 profile.update(self.__profiles[profileName]) 461 protocol = MqttProtocols.MQTTv31
462 elif self.mqttv311Button.isChecked():
463 protocol = MqttProtocols.MQTTv311
464 elif self.mqttv5Button.isChecked():
465 protocol = MqttProtocols.MQTTv5
466 else:
467 protocol = MqttProtocols.MQTTv311
468
469 connectionProfile = self.__defaultProfile()
470 connectionProfile.update(self.__profiles[profileName])
441 changed = ( 471 changed = (
442 self.brokerAddressEdit.text() != profile["BrokerAddress"] or 472 self.brokerAddressEdit.text() !=
443 self.brokerPortSpinBox.value() != profile["BrokerPort"] or 473 connectionProfile["BrokerAddress"] or
444 self.clientIdEdit.text() != profile["ClientId"] or 474 self.brokerPortSpinBox.value() !=
475 connectionProfile["BrokerPort"] or
476 self.clientIdEdit.text() != connectionProfile["ClientId"] or
477 protocol != connectionProfile["Protocol"] or
445 self.connectionTimeoutSpinBox.value() != 478 self.connectionTimeoutSpinBox.value() !=
446 profile["ConnectionTimeout"] or 479 connectionProfile["ConnectionTimeout"] or
447 self.keepaliveSpinBox.value() != profile["Keepalive"] or 480 self.keepaliveSpinBox.value() !=
481 connectionProfile["Keepalive"] or
448 self.cleanSessionCheckBox.isChecked() != 482 self.cleanSessionCheckBox.isChecked() !=
449 profile["CleanSession"] or 483 connectionProfile["CleanSession"] or
450 self.usernameEdit.text() != profile["Username"] or 484 self.usernameEdit.text() != connectionProfile["Username"] or
451 self.passwordEdit.text() != 485 self.passwordEdit.text() !=
452 pwConvert(profile["Password"], encode=False) or 486 pwConvert(connectionProfile["Password"], encode=False) or
453 self.willTopicEdit.text() != profile["WillTopic"] or 487 self.willTopicEdit.text() != connectionProfile["WillTopic"] or
454 self.willMessageEdit.toPlainText() != profile["WillMessage"] or 488 self.willMessageEdit.toPlainText() !=
455 self.willQosSpinBox.value() != profile["WillQos"] or 489 connectionProfile["WillMessage"] or
456 self.willRetainCheckBox.isChecked() != profile["WillRetain"] or 490 self.willQosSpinBox.value() != connectionProfile["WillQos"] or
457 self.tlsGroupBox.isChecked() != profile["TlsEnable"] 491 self.willRetainCheckBox.isChecked() !=
492 connectionProfile["WillRetain"] or
493 self.tlsGroupBox.isChecked() != connectionProfile["TlsEnable"]
458 ) 494 )
459 # check TLS stuff only, if not yet changed 495 # check TLS stuff only, if not yet changed
460 if not changed: 496 if not changed:
461 if self.tlsCertsFileButton.isChecked(): 497 if self.tlsCertsFileButton.isChecked():
462 changed |= ( 498 changed |= (
463 self.tlsCertsFilePicker.text() != profile["TlsCaCert"] 499 self.tlsCertsFilePicker.text() !=
500 connectionProfile["TlsCaCert"]
464 ) 501 )
465 elif self.tlsSelfSignedCertsButton.isChecked(): 502 elif self.tlsSelfSignedCertsButton.isChecked():
466 changed |= ( 503 changed |= (
467 self.tlsSelfSignedCertsFilePicker.text() != 504 self.tlsSelfSignedCertsFilePicker.text() !=
468 profile["TlsCaCert"] or 505 connectionProfile["TlsCaCert"] or
469 self.tlsSelfSignedClientCertFilePicker.text() != 506 self.tlsSelfSignedClientCertFilePicker.text() !=
470 profile["TlsClientCert"] or 507 connectionProfile["TlsClientCert"] or
471 self.tlsSelfSignedClientKeyFilePicker.text() != 508 self.tlsSelfSignedClientKeyFilePicker.text() !=
472 profile["TlsClientKey"] 509 connectionProfile["TlsClientKey"]
473 ) 510 )
474 return changed 511 return changed
475 512
476 else: 513 else:
477 return True 514 return True

eric ide

mercurial