diff -r f75a385e9127 -r 7e0e921dc7ea MqttMonitor/MqttMonitorWidget.py --- a/MqttMonitor/MqttMonitorWidget.py Fri Aug 31 19:28:28 2018 +0200 +++ b/MqttMonitor/MqttMonitorWidget.py Sat Sep 01 20:18:11 2018 +0200 @@ -19,7 +19,7 @@ from PyQt5.QtCore import pyqtSlot, QTimer from PyQt5.QtGui import QTextCursor -from PyQt5.QtWidgets import QWidget +from PyQt5.QtWidgets import QWidget, QDialog from E5Gui import E5MessageBox @@ -62,8 +62,9 @@ self.brokerWidget.setCurrentIndex(0) self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) - self.brokerComboBox.addItems( - self.__plugin.getPreferences("RecentBrokers")) + self.brokerConnectionOptionsButton.setIcon(UI.PixmapCache.getIcon( + os.path.join("MqttMonitor", "icons", "connectionOptions.png"))) + self.__populateBrokerComboBoxes() self.brokerStatusLabel.hide() self.subscribeButton.setIcon(UI.PixmapCache.getIcon("plus.png")) @@ -78,6 +79,8 @@ self.__updatePublishTopicComboBox() self.publishButton.setEnabled(False) + self.__connectionOptions = None + prefix = MqttMonitorWidget.BrokerStatusTopicPrefix self.__statusLabelMapping = { # broker @@ -148,6 +151,7 @@ """ if rc == 0: self.__connectedToBroker = True + self.__connectionOptions = None msg = mqttConnackMessage(rc) self.__flashBrokerStatusLabel(msg) @@ -291,6 +295,17 @@ self.connectButton.setEnabled(True) @pyqtSlot() + def on_brokerConnectionOptionsButton_clicked(self): + """ + Private slot to show a dialog to modify connection options. + """ + from .MqttConnectionOptionsDialog import MqttConnectionOptionsDialog + dlg = MqttConnectionOptionsDialog( + self.__client, self.__connectionOptions, parent=self) + if dlg.exec_() == QDialog.Accepted: + self.__connectionOptions = dlg.getConnectionOptions() + + @pyqtSlot() def on_connectButton_clicked(self): """ Private slot to handle a connect or disconnect request. @@ -299,10 +314,15 @@ self.__client.disconnectFromServer() else: host = self.brokerComboBox.currentText() + port = self.brokerPortComboBox.currentText().strip() + try: + port = int(port) + except ValueError: + # use standard port at 1883 + port = 1883 if host: - self.__addBrokerToRecent(host) - self.__client.connectToServer(host) - # use standard port at 1883 + self.__addBrokerToRecent(host, port) + self.__client.connectToServer(host, port=port) @pyqtSlot(str) def on_subscribeTopicEdit_textChanged(self, topic): @@ -371,6 +391,10 @@ qos = self.publishQosSpinBox.value() retain = self.publishRetainCheckBox.isChecked() payloadStr = self.publishPayloadEdit.toPlainText() + if not payloadStr: + # use empty string together with the retain flag to clean + # a retained message by sending None instead + payloadStr = None msgInfo = self.__client.publish(topic, payloadStr, qos, retain) if msgInfo.rc == 0: @@ -408,22 +432,50 @@ ## Utility methods ####################################################################### - def __addBrokerToRecent(self, host): + def __addBrokerToRecent(self, host, port): """ Private method to add a host name to the list of recently connected brokers. @param host host name of broker @type str + @param port port number of the connection + @type int """ - brokerList = self.__plugin.getPreferences("RecentBrokers") - if host in brokerList: - brokerList.remove(host) - brokerList.insert(0, host) - self.__plugin.setPreferences("RecentBrokers", brokerList) + brokerList = self.__plugin.getPreferences("RecentBrokersWithPort") + hostAndPort = [host, port] + if hostAndPort in brokerList: + brokerList.remove(hostAndPort) + brokerList.insert(0, hostAndPort) + self.__plugin.setPreferences("RecentBrokersWithPort", brokerList) + + self.__populateBrokerComboBoxes() + + def __populateBrokerComboBoxes(self): + """ + Private method to populate the broker name and port combo boxes. + """ + brokerList = self.__plugin.getPreferences("RecentBrokersWithPort") + # step 1: clear combo boxes self.brokerComboBox.clear() - self.brokerComboBox.addItems(brokerList) + self.brokerPortComboBox.clear() + + # step 2a: populate the broker name list + self.brokerComboBox.addItems([b[0].strip() for b in brokerList]) + + # step 2b: populate the broker ports list + if brokerList: + currentPort = brokerList[0][1] + else: + currentPort = 1883 + currentPortStr = "{0:5}".format(currentPort) + portsSet = set([b[1] for b in brokerList]) + portsSet.update([1883, 8883]) + self.brokerPortComboBox.addItems( + sorted(["{0:5}".format(p) for p in portsSet])) + index = self.brokerPortComboBox.findText(currentPortStr) + self.brokerPortComboBox.setCurrentIndex(index) def __updateUnsubscribeTopicComboBox(self): """