Wed, 29 Aug 2018 19:59:02 +0200
MqttMonitorWidget: implemented part 1 of the status tab.
MqttMonitor/MqttMonitorWidget.py | file | annotate | diff | comparison | revisions | |
MqttMonitor/MqttMonitorWidget.ui | file | annotate | diff | comparison | revisions |
--- 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
--- a/MqttMonitor/MqttMonitorWidget.ui Tue Aug 28 19:29:52 2018 +0200 +++ b/MqttMonitor/MqttMonitorWidget.ui Wed Aug 29 19:59:02 2018 +0200 @@ -79,7 +79,7 @@ <item> <widget class="QTabWidget" name="brokerWidget"> <property name="currentIndex"> - <number>1</number> + <number>2</number> </property> <widget class="QWidget" name="pubSubTab"> <attribute name="title"> @@ -244,6 +244,560 @@ <attribute name="title"> <string>Status</string> </attribute> + <layout class="QVBoxLayout" name="verticalLayout_4"> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_5"> + <item> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="brokerStatusButton"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="toolTip"> + <string>Press to activate the status display</string> + </property> + <property name="text"> + <string>Subscribe</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QScrollArea" name="scrollArea"> + <property name="widgetResizable"> + <bool>true</bool> + </property> + <widget class="QWidget" name="scrollAreaWidgetContents"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>344</width> + <height>598</height> + </rect> + </property> + <layout class="QFormLayout" name="formLayout"> + <property name="labelAlignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + <item row="0" column="0" colspan="2"> + <layout class="QHBoxLayout" name="horizontalLayout_7"> + <item> + <widget class="QLabel" name="label_22"> + <property name="text"> + <string><b>Broker</b></string> + </property> + </widget> + </item> + <item> + <widget class="Line" name="line_2"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + </layout> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_5"> + <property name="text"> + <string>Version</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLabel" name="versionLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="label_6"> + <property name="text"> + <string>Timestamp</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLabel" name="timestampLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="3" column="0"> + <widget class="QLabel" name="label_7"> + <property name="text"> + <string>Uptime</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QLabel" name="uptimeLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="4" column="0"> + <widget class="QLabel" name="label_8"> + <property name="text"> + <string>Subscriptions</string> + </property> + </widget> + </item> + <item row="4" column="1"> + <widget class="QLabel" name="subscriptionsLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="5" column="0" colspan="2"> + <layout class="QHBoxLayout" name="horizontalLayout_8"> + <item> + <widget class="QLabel" name="label_23"> + <property name="text"> + <string><b>Clients</b></string> + </property> + </widget> + </item> + <item> + <widget class="Line" name="line_3"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + </layout> + </item> + <item row="6" column="0"> + <widget class="QLabel" name="label_9"> + <property name="text"> + <string>Connected</string> + </property> + </widget> + </item> + <item row="6" column="1"> + <widget class="QLabel" name="clientsConnectedLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="7" column="0"> + <widget class="QLabel" name="label_10"> + <property name="text"> + <string>Disconnected</string> + </property> + </widget> + </item> + <item row="7" column="1"> + <widget class="QLabel" name="clientsDisconnectedLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="8" column="0"> + <widget class="QLabel" name="label_11"> + <property name="text"> + <string>Expired</string> + </property> + </widget> + </item> + <item row="8" column="1"> + <widget class="QLabel" name="clientsExpiredLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="9" column="0"> + <widget class="QLabel" name="label_12"> + <property name="text"> + <string>Maximum</string> + </property> + </widget> + </item> + <item row="9" column="1"> + <widget class="QLabel" name="clientsMaximumLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="10" column="0"> + <widget class="QLabel" name="label_13"> + <property name="text"> + <string>Total</string> + </property> + </widget> + </item> + <item row="10" column="1"> + <widget class="QLabel" name="clientsTotalLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="11" column="0" colspan="2"> + <layout class="QHBoxLayout" name="horizontalLayout_9"> + <item> + <widget class="QLabel" name="label_24"> + <property name="text"> + <string><b>Messages</b></string> + </property> + </widget> + </item> + <item> + <widget class="Line" name="line_4"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + </layout> + </item> + <item row="12" column="0"> + <widget class="QLabel" name="label_14"> + <property name="text"> + <string>Sent</string> + </property> + </widget> + </item> + <item row="12" column="1"> + <widget class="QLabel" name="messagesSentLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="13" column="0"> + <widget class="QLabel" name="label_15"> + <property name="text"> + <string>Received</string> + </property> + </widget> + </item> + <item row="13" column="1"> + <widget class="QLabel" name="messagesReceivedLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="14" column="0"> + <widget class="QLabel" name="label_16"> + <property name="text"> + <string>Stored</string> + </property> + </widget> + </item> + <item row="14" column="1"> + <widget class="QLabel" name="messagesStoredLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="15" column="0"> + <widget class="QLabel" name="label_17"> + <property name="text"> + <string>Inflight</string> + </property> + </widget> + </item> + <item row="15" column="1"> + <widget class="QLabel" name="messagesInflightLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="16" column="0"> + <widget class="QLabel" name="label_18"> + <property name="text"> + <string>Retained</string> + </property> + </widget> + </item> + <item row="16" column="1"> + <widget class="QLabel" name="messagesRetainedLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="17" column="0" colspan="2"> + <layout class="QHBoxLayout" name="horizontalLayout_10"> + <item> + <widget class="QLabel" name="label_28"> + <property name="text"> + <string><b>PUBLISH Messages</b></string> + </property> + </widget> + </item> + <item> + <widget class="Line" name="line_5"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + </layout> + </item> + <item row="18" column="0"> + <widget class="QLabel" name="label_25"> + <property name="text"> + <string>Sent</string> + </property> + </widget> + </item> + <item row="18" column="1"> + <widget class="QLabel" name="publishMessagesSentLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="19" column="0"> + <widget class="QLabel" name="label_26"> + <property name="text"> + <string>Received</string> + </property> + </widget> + </item> + <item row="19" column="1"> + <widget class="QLabel" name="publishMessagesReceivedLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="20" column="0"> + <widget class="QLabel" name="label_27"> + <property name="text"> + <string>Dropped</string> + </property> + </widget> + </item> + <item row="20" column="1"> + <widget class="QLabel" name="publishMessagesDroppedLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="21" column="0" colspan="2"> + <layout class="QHBoxLayout" name="horizontalLayout_6"> + <item> + <widget class="QLabel" name="label_21"> + <property name="text"> + <string><b>Traffic</b></string> + </property> + </widget> + </item> + <item> + <widget class="Line" name="line"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + </layout> + </item> + <item row="22" column="0"> + <widget class="QLabel" name="label_19"> + <property name="text"> + <string>Bytes Sent</string> + </property> + </widget> + </item> + <item row="22" column="1"> + <widget class="QLabel" name="bytesSentLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="23" column="0"> + <widget class="QLabel" name="label_20"> + <property name="text"> + <string>Bytes Received</string> + </property> + </widget> + </item> + <item row="23" column="1"> + <widget class="QLabel" name="bytesReceivedLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + </layout> + </widget> + </widget> + </item> + </layout> </widget> </widget> </item>