MqttMonitor/MqttMonitorWidget.py

changeset 28
0f02baed8308
parent 27
aeb276d76ec7
parent 25
01d44a4decf5
child 30
17ef10819773
equal deleted inserted replaced
27:aeb276d76ec7 28:0f02baed8308
14 except NameError: 14 except NameError:
15 pass 15 pass
16 16
17 import os 17 import os
18 import collections 18 import collections
19 import copy
19 20
20 from PyQt5.QtCore import pyqtSlot, QTimer 21 from PyQt5.QtCore import pyqtSlot, QTimer
21 from PyQt5.QtGui import QTextCursor 22 from PyQt5.QtGui import QTextCursor
22 from PyQt5.QtWidgets import QWidget, QDialog 23 from PyQt5.QtWidgets import QWidget, QDialog
23 24
58 59
59 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap( 60 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap(
60 os.path.join("MqttMonitor", "icons", "mqtt48.png"))) 61 os.path.join("MqttMonitor", "icons", "mqtt48.png")))
61 62
62 self.brokerWidget.setCurrentIndex(0) 63 self.brokerWidget.setCurrentIndex(0)
64
65 self.__connectionModeProfile = True
66 self.__setConnectionMode(True) # initial mode is 'profile connection'
67 self.__populateProfileComboBox()
63 68
64 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) 69 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
65 self.brokerConnectionOptionsButton.setIcon(UI.PixmapCache.getIcon( 70 self.brokerConnectionOptionsButton.setIcon(UI.PixmapCache.getIcon(
66 os.path.join("MqttMonitor", "icons", "connectionOptions.png"))) 71 os.path.join("MqttMonitor", "icons", "connectionOptions.png")))
67 self.__populateBrokerComboBoxes() 72 self.__populateBrokerComboBoxes()
281 """ 286 """
282 self.brokerStatusLabel.setText(message) 287 self.brokerStatusLabel.setText(message)
283 self.brokerStatusLabel.show() 288 self.brokerStatusLabel.show()
284 QTimer.singleShot(5000, self.brokerStatusLabel.hide) 289 QTimer.singleShot(5000, self.brokerStatusLabel.hide)
285 290
291 @pyqtSlot()
292 def on_modeButton_clicked(self):
293 """
294 Private slot to switch between connection profiles and direct
295 connection mode.
296 """
297 self.__setConnectionMode(not self.__connectionModeProfile)
298
286 @pyqtSlot(str) 299 @pyqtSlot(str)
287 def on_brokerComboBox_editTextChanged(self, host): 300 def on_brokerComboBox_editTextChanged(self, host):
288 """ 301 """
289 Private slot to handling entering or selecting a broker host name. 302 Private slot to handling entering or selecting a broker host name.
290 303
291 @param host host name of the broker 304 @param host host name of the broker
292 @type str 305 @type str
293 """ 306 """
294 if not self.__connectedToBroker and not host: 307 self.__setConnectButtonState()
295 self.connectButton.setEnabled(False)
296 else:
297 self.connectButton.setEnabled(True)
298 308
299 @pyqtSlot() 309 @pyqtSlot()
300 def on_brokerConnectionOptionsButton_clicked(self): 310 def on_brokerConnectionOptionsButton_clicked(self):
301 """ 311 """
302 Private slot to show a dialog to modify connection options. 312 Private slot to show a dialog to modify connection options or a
303 """ 313 dialog to edit connection profiles.
304 from .MqttConnectionOptionsDialog import MqttConnectionOptionsDialog 314 """
305 dlg = MqttConnectionOptionsDialog( 315 if self.__connectionModeProfile:
306 self.__client, self.__connectionOptions, parent=self) 316 from .MqttConnectionProfilesDialog import \
307 if dlg.exec_() == QDialog.Accepted: 317 MqttConnectionProfilesDialog
308 self.__connectionOptions = dlg.getConnectionOptions() 318 dlg = MqttConnectionProfilesDialog(
319 self.__client, self.__plugin.getPreferences("BrokerProfiles"),
320 parent=self)
321 if dlg.exec_() == QDialog.Accepted:
322 profilesDict = dlg.getProfiles()
323 self.__plugin.setPreferences("BrokerProfiles", profilesDict)
324 self.__populateProfileComboBox()
325 else:
326 from .MqttConnectionOptionsDialog import \
327 MqttConnectionOptionsDialog
328 dlg = MqttConnectionOptionsDialog(
329 self.__client, self.__connectionOptions, parent=self)
330 if dlg.exec_() == QDialog.Accepted:
331 self.__connectionOptions = dlg.getConnectionOptions()
332 if self.__connectionOptions["TlsEnable"]:
333 port = self.brokerPortComboBox.currentText().strip()
334 if port == "1883":
335 # it is default non-encrypted port => set to TLS port
336 self.brokerPortComboBox.setEditText("8883")
337 else:
338 port = self.brokerPortComboBox.currentText().strip()
339 if port == "8883":
340 # it is default TLS port => set to non-encrypted port
341 self.brokerPortComboBox.setEditText("1883")
309 342
310 @pyqtSlot() 343 @pyqtSlot()
311 def on_connectButton_clicked(self): 344 def on_connectButton_clicked(self):
312 """ 345 """
313 Private slot to handle a connect or disconnect request. 346 Private slot to handle a connect or disconnect request.
314 """ 347 """
315 if self.__connectedToBroker: 348 if self.__connectedToBroker:
316 self.__client.disconnectFromServer() 349 self.__client.disconnectFromServer()
317 else: 350 else:
318 host = self.brokerComboBox.currentText() 351 if self.__connectionModeProfile:
319 port = self.brokerPortComboBox.currentText().strip() 352 self.__profileConnectToBroker()
320 try: 353 else:
321 port = int(port) 354 self.__directConnectToBroker()
322 except ValueError:
323 # use standard port at 1883
324 port = 1883
325 if host:
326 self.__addBrokerToRecent(host, port)
327 if self.__connectionOptions is None:
328 self.__client.connectToServer(host, port=port)
329 else:
330 self.__client.connectToServerWithOptions(
331 host, port=port, options=self.__connectionOptions)
332 355
333 @pyqtSlot(str) 356 @pyqtSlot(str)
334 def on_subscribeTopicEdit_textChanged(self, topic): 357 def on_subscribeTopicEdit_textChanged(self, topic):
335 """ 358 """
336 Private slot to handle a change of the entered topic. 359 Private slot to handle a change of the entered topic.
500 self.brokerPortComboBox.clear() 523 self.brokerPortComboBox.clear()
501 524
502 # step 2a: populate the broker name list 525 # step 2a: populate the broker name list
503 self.brokerComboBox.addItems([b[0].strip() for b in brokerList]) 526 self.brokerComboBox.addItems([b[0].strip() for b in brokerList])
504 527
528 self.__setConnectButtonState()
529
505 # step 2b: populate the broker ports list 530 # step 2b: populate the broker ports list
506 if brokerList: 531 if brokerList:
507 currentPort = brokerList[0][1] 532 currentPort = brokerList[0][1]
508 else: 533 else:
509 currentPort = 1883 534 currentPort = 1883
512 portsSet.update({1883, 8883}) 537 portsSet.update({1883, 8883})
513 self.brokerPortComboBox.addItems( 538 self.brokerPortComboBox.addItems(
514 sorted("{0:5}".format(p) for p in portsSet)) 539 sorted("{0:5}".format(p) for p in portsSet))
515 index = self.brokerPortComboBox.findText(currentPortStr) 540 index = self.brokerPortComboBox.findText(currentPortStr)
516 self.brokerPortComboBox.setCurrentIndex(index) 541 self.brokerPortComboBox.setCurrentIndex(index)
542
543 def __populateProfileComboBox(self):
544 """
545 Private method to populate the profiles selection box.
546 """
547 profilesDict = self.__plugin.getPreferences("BrokerProfiles")
548
549 self.profileComboBox.clear()
550 self.profileComboBox.addItems(sorted(profilesDict.keys()))
551
552 self.__setConnectButtonState()
517 553
518 def __updateUnsubscribeTopicComboBox(self): 554 def __updateUnsubscribeTopicComboBox(self):
519 """ 555 """
520 Private method to update the unsubcribe topic combo box. 556 Private method to update the unsubcribe topic combo box.
521 """ 557 """
628 return { 664 return {
629 "1min": "-", 665 "1min": "-",
630 "5min": "-", 666 "5min": "-",
631 "15min": "-", 667 "15min": "-",
632 } 668 }
669
670 def __setConnectionMode(self, profileMode):
671 """
672 Private method to set the connection mode.
673
674 @param profileMode flag indicating the profile connection mode
675 @type bool
676 """
677 self.__connectionModeProfile = profileMode
678 if profileMode:
679 self.modeButton.setIcon(UI.PixmapCache.getIcon(
680 os.path.join("MqttMonitor", "icons", "profiles.png")))
681 else:
682 self.modeButton.setIcon(UI.PixmapCache.getIcon(
683 os.path.join("MqttMonitor", "icons", "quickopen.png")))
684
685 self.profileComboBox.setVisible(profileMode)
686 self.brokerConnectionWidget.setVisible(not profileMode)
687 self.__setConnectButtonState()
688
689 def __setConnectButtonState(self):
690 """
691 Private method to set the enabled state of the connect button.
692 """
693 if self.__connectionModeProfile:
694 self.connectButton.setEnabled(
695 bool(self.profileComboBox.currentText()))
696 else:
697 self.connectButton.setEnabled(
698 bool(self.brokerComboBox.currentText()))
699
700 def __directConnectToBroker(self):
701 """
702 Private method to connect to the broker with entered data.
703 """
704 host = self.brokerComboBox.currentText()
705 port = self.brokerPortComboBox.currentText().strip()
706 try:
707 port = int(port)
708 except ValueError:
709 # use standard port at 1883
710 port = 1883
711 if host:
712 self.__addBrokerToRecent(host, port)
713 if self.__connectionOptions is None:
714 self.__client.connectToServer(host, port=port)
715 else:
716 self.__client.connectToServerWithOptions(
717 host, port=port, options=self.__connectionOptions)
718
719 def __profileConnectToBroker(self):
720 """
721 Private method to connect to the broker with selected profile.
722 """
723 profileName = self.profileComboBox.currentText()
724 if profileName:
725 profilesDict = self.__plugin.getPreferences("BrokerProfiles")
726 profile = copy.copy(profilesDict[profileName]) # play it save
727 host = profile["BrokerAddress"]
728 port = profile["BrokerPort"]
729
730 self.__client.connectToServerWithOptions(host, port=port,
731 options=profile)

eric ide

mercurial