MqttMonitor/MqttMonitorWidget.py

changeset 6
d22f5ce3a07a
parent 5
7162c838cfc9
child 7
63e046d95702
--- a/MqttMonitor/MqttMonitorWidget.py	Tue Aug 28 19:29:52 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Wed Aug 29 19:59:02 2018 +0200
@@ -20,6 +20,8 @@
 from PyQt5.QtGui import QTextCursor
 from PyQt5.QtWidgets import QWidget
 
+from E5Gui import E5MessageBox
+
 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
 
 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage
@@ -32,6 +34,9 @@
     """
     Class implementing the MQTT Monitor widget.
     """
+    BrokerStatusTopicPrefix = "$SYS/broker/"
+    BrokerStatusTopic = "$SYS/broker/#"
+    
     def __init__(self, plugin, parent=None):
         """
         Constructor
@@ -47,6 +52,7 @@
         self.__plugin = plugin
         
         self.__connectedToBroker = False
+        self.__brokerStatusTopicSubscribed = False
         
         self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap(
             os.path.join("MqttMonitor", "icons", "mqtt48.png")))
@@ -66,6 +72,55 @@
         self.__topicQueue = {}
         self.__updateUnsubscribeTopicComboBox()
         
+        self.__statusLabelMapping = {
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "version":
+                self.versionLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "timestamp":
+                self.timestampLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "uptime":
+                self.uptimeLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "subscriptions/count":
+                self.subscriptionsLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/connected":
+                self.clientsConnectedLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/disconnected":
+                self.clientsDisconnectedLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/expired":
+                self.clientsExpiredLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/maximum":
+                self.clientsMaximumLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/total":
+                self.clientsTotalLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/sent":
+                self.messagesSentLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/received":
+                self.messagesReceivedLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/stored":
+                self.messagesStoredLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "store/messages/count":
+                self.messagesStoredLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/inflight":
+                self.messagesInflightLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + \
+                    "retained messages/count":
+                self.messagesRetainedLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + \
+                    "publish/messages/sent":
+                self.publishMessagesSentLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + \
+                    "publish/messages/received":
+                self.publishMessagesReceivedLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + \
+                    "publish/messages/dropped":
+                self.publishMessagesDroppedLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "bytes/sent":
+                self.bytesSentLabel,
+            MqttMonitorWidget.BrokerStatusTopicPrefix + "bytes/received":
+                self.bytesReceivedLabel,
+##            MqttMonitorWidget.BrokerStatusTopicPrefix + "":
+##                self.versionLabel,
+        }
+        
         self.__client = MqttClient()
         
         # connect the MQTT client signals
@@ -100,6 +155,7 @@
         
         self.subscribeGroup.setEnabled(True)
         self.unsubscribeGroup.setEnabled(True)
+        self.brokerStatusButton.setEnabled(True)
     
     @pyqtSlot(int)
     def __brokerDisconnected(self, rc):
@@ -128,6 +184,7 @@
         
         self.subscribeGroup.setEnabled(False)
         self.unsubscribeGroup.setEnabled(False)
+        self.brokerStatusButton.setEnabled(False)
     
     @pyqtSlot(str, bytes, int, bool)
     def __messageReceived(self, topic, payload, qos, retain):
@@ -143,7 +200,7 @@
         @param retain flag indicating a retained message
         @type bool
         """
-        if topic.startswith("$SYS/broker/"):
+        if topic.startswith(MqttMonitorWidget.BrokerStatusTopicPrefix):
             # handle broker status messages
             self.__handleBrokerStatusMessage(topic, payload)
         else:
@@ -255,8 +312,15 @@
         topic = self.subscribeTopicEdit.text()
         qos = self.subscribeQosSpinBox.value()
         if topic:
-            self.__topicQueue[
-                self.__client.subscribe(topic, qos)[1]] = topic
+            if topic.startswith(MqttMonitorWidget.BrokerStatusTopicPrefix):
+                E5MessageBox.warning(
+                    self,
+                    self.tr("Subscribe to Topic"),
+                    self.tr("Subscriptions to the Status topic '$SYS' shall"
+                            " be done on the Status tab."))
+            else:
+                self.__topicQueue[
+                    self.__client.subscribe(topic, qos)[1]] = topic
     
     @pyqtSlot(str)
     def on_unsubscribeTopicComboBox_currentIndexChanged(self, topic):
@@ -278,6 +342,32 @@
             self.__topicQueue[
                 self.__client.unsubscribe(topic)[1]] = topic
     
+    @pyqtSlot()
+    def on_brokerStatusButton_clicked(self):
+        """
+        Private slot to subscribe or unsubscribe the broker status topic.
+        """
+        if self.__brokerStatusTopicSubscribed:
+            # unsubscribe status topic
+            rc, _ = self.__client.unsubscribe(
+                MqttMonitorWidget.BrokerStatusTopic)
+            if rc == 0:
+                # successfully sent
+                self.__brokerStatusTopicSubscribed = False
+                self.brokerStatusButton.setText(self.tr("Subscribe"))
+                self.brokerStatusButton.setToolTip(
+                    self.tr("Press to activate the status display"))
+        else:
+            # subscribe status topic
+            rc, _ = self.__client.subscribe(
+                MqttMonitorWidget.BrokerStatusTopic)
+            if rc == 0:
+                # successfully sent
+                self.__brokerStatusTopicSubscribed = True
+                self.brokerStatusButton.setText(self.tr("Unsubscribe"))
+                self.brokerStatusButton.setToolTip(
+                    self.tr("Press to deactivate the status display"))
+    
     #######################################################################
     ## Utility methods
     #######################################################################
@@ -336,4 +426,11 @@
         @param payload payload of the received message
         @type bytes
         """
-        payloadStr = str(payload, encoding="utf-8", errors="replace")
+        payloadStr = str(payload, encoding="utf-8", errors="replace").strip()
+        
+        try:
+            label = self.__statusLabelMapping[topic.strip()]
+            label.setText(payloadStr)
+        except KeyError:
+            # ignore topics not shown in display
+            pass

eric ide

mercurial