diff -r 82845c0fd550 -r 991db89bda9c MqttMonitor/MqttMonitorWidget.py --- a/MqttMonitor/MqttMonitorWidget.py Mon Aug 27 19:26:27 2018 +0200 +++ b/MqttMonitor/MqttMonitorWidget.py Tue Aug 28 18:54:48 2018 +0200 @@ -49,6 +49,14 @@ self.__plugin.getPreferences("RecentBrokers")) self.brokerStatusLabel.hide() + self.subscribeButton.setIcon(UI.PixmapCache.getIcon("plus.png")) + self.subscribeButton.setEnabled(False) + self.unsubscribeButton.setIcon(UI.PixmapCache.getIcon("minus.png")) + + self.__subscribedTopics = [] + self.__topicQueue = {} + self.__updateUnsubscribeTopicComboBox() + self.__client = MqttClient() # connect the MQTT client signals @@ -80,6 +88,9 @@ self.__flashBrokerStatusLabel(msg) self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png")) + + self.subscribeGroup.setEnabled(True) + self.unsubscribeGroup.setEnabled(True) @pyqtSlot(int) def __brokerDisconnected(self, rc): @@ -91,6 +102,9 @@ """ self.__connectedToBroker = False + # ensure, the client loop is stopped + self.__client.stopLoop() + if rc != 0: msg = mqttErrorMessage(rc) else: @@ -98,8 +112,13 @@ self.__flashBrokerStatusLabel(msg) self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) + + self.__subscribedTopics = [] + self.__topicQueue = {} + self.__updateUnsubscribeTopicComboBox() - self.__client.stopLoop() + self.subscribeGroup.setEnabled(False) + self.unsubscribeGroup.setEnabled(False) @pyqtSlot(str, bytes, int, bool) def __messageReceived(self, topic, payload, qos, retain): @@ -137,7 +156,12 @@ @param grantedQos tuple of granted quality of service @type tuple of int """ - pass + if mid in self.__topicQueue: + topic = self.__topicQueue.pop(mid) + self.__subscribedTopics.append(topic) + self.subscribeTopicEdit.clear() + + self.__updateUnsubscribeTopicComboBox() @pyqtSlot(int) def __topicUnsubscribed(self, mid): @@ -147,7 +171,14 @@ @param mid ID of the unsubscribe request @type int """ - pass + if mid in self.__topicQueue: + topic = self.__topicQueue.pop(mid) + try: + self.__subscribedTopics.remove(topic) + self.__updateUnsubscribeTopicComboBox() + except ValueError: + # ignore it + pass ####################################################################### ## Slots handling UI interactions @@ -188,8 +219,51 @@ self.__client.disconnectFromServer() else: host = self.brokerComboBox.currentText() - self.__addBrokerToRecent(host) - self.__client.connectToServer(host) # use standard port at 1883 + if host: + self.__addBrokerToRecent(host) + self.__client.connectToServer(host) + # use standard port at 1883 + + @pyqtSlot(str) + def on_subscribeTopicEdit_textChanged(self, topic): + """ + Private slot to handle a change of the entered topic. + + @param topic entered topic text + @type str + """ + self.subscribeButton.setEnabled(bool(topic)) + + @pyqtSlot() + def on_subscribeButton_clicked(self): + """ + Private slot to subscribe to the entered topic. + """ + topic = self.subscribeTopicEdit.text() + qos = self.subscribeQosSpinBox.value() + if topic: + self.__topicQueue[ + self.__client.subscribe(topic, qos)[1]] = topic + + @pyqtSlot(str) + def on_unsubscribeTopicComboBox_currentIndexChanged(self, topic): + """ + Private slot to handle the selection of a topic to unsubscribe from. + + @param topic topic text + @type str + """ + self.unsubscribeButton.setEnabled(bool(topic)) + + @pyqtSlot() + def on_unsubscribeButton_clicked(self): + """ + Private slot to unsubscribe from the selected topic. + """ + topic = self.unsubscribeTopicComboBox.currentText() + if topic: + self.__topicQueue[ + self.__client.unsubscribe(topic)[1]] = topic ####################################################################### ## Utility methods @@ -211,3 +285,11 @@ self.brokerComboBox.clear() self.brokerComboBox.addItems(brokerList) + + def __updateUnsubscribeTopicComboBox(self): + """ + Private method to update the unsubcribe topic combo box. + """ + self.unsubscribeTopicComboBox.clear() + self.unsubscribeTopicComboBox.addItems(sorted(self.__subscribedTopics)) + self.unsubscribeButton.setEnabled(len(self.__subscribedTopics) > 0)