MqttMonitor/MqttMonitorWidget.py

changeset 7
63e046d95702
parent 6
d22f5ce3a07a
child 8
95b56dcfa09b
equal deleted inserted replaced
6:d22f5ce3a07a 7:63e046d95702
13 str = unicode # __IGNORE_EXCEPTION__ 13 str = unicode # __IGNORE_EXCEPTION__
14 except NameError: 14 except NameError:
15 pass 15 pass
16 16
17 import os 17 import os
18 import collections
18 19
19 from PyQt5.QtCore import pyqtSlot, QTimer 20 from PyQt5.QtCore import pyqtSlot, QTimer
20 from PyQt5.QtGui import QTextCursor 21 from PyQt5.QtGui import QTextCursor
21 from PyQt5.QtWidgets import QWidget 22 from PyQt5.QtWidgets import QWidget
22 23
34 """ 35 """
35 Class implementing the MQTT Monitor widget. 36 Class implementing the MQTT Monitor widget.
36 """ 37 """
37 BrokerStatusTopicPrefix = "$SYS/broker/" 38 BrokerStatusTopicPrefix = "$SYS/broker/"
38 BrokerStatusTopic = "$SYS/broker/#" 39 BrokerStatusTopic = "$SYS/broker/#"
40 BrokerStatusTopicLoadPrefix = "$SYS/broker/load/"
39 41
40 def __init__(self, plugin, parent=None): 42 def __init__(self, plugin, parent=None):
41 """ 43 """
42 Constructor 44 Constructor
43 45
70 72
71 self.__subscribedTopics = [] 73 self.__subscribedTopics = []
72 self.__topicQueue = {} 74 self.__topicQueue = {}
73 self.__updateUnsubscribeTopicComboBox() 75 self.__updateUnsubscribeTopicComboBox()
74 76
77 prefix = MqttMonitorWidget.BrokerStatusTopicPrefix
75 self.__statusLabelMapping = { 78 self.__statusLabelMapping = {
76 MqttMonitorWidget.BrokerStatusTopicPrefix + "version": 79 # broker
77 self.versionLabel, 80 prefix + "version": self.versionLabel,
78 MqttMonitorWidget.BrokerStatusTopicPrefix + "timestamp": 81 prefix + "timestamp": self.timestampLabel,
79 self.timestampLabel, 82 prefix + "uptime": self.uptimeLabel,
80 MqttMonitorWidget.BrokerStatusTopicPrefix + "uptime": 83 prefix + "subscriptions/count": self.subscriptionsLabel,
81 self.uptimeLabel, 84 # clients
82 MqttMonitorWidget.BrokerStatusTopicPrefix + "subscriptions/count": 85 prefix + "clients/connected": self.clientsConnectedLabel,
83 self.subscriptionsLabel, 86 prefix + "clients/disconnected": self.clientsDisconnectedLabel,
84 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/connected": 87 prefix + "clients/expired": self.clientsExpiredLabel,
85 self.clientsConnectedLabel, 88 prefix + "clients/maximum": self.clientsMaximumLabel,
86 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/disconnected": 89 prefix + "clients/total": self.clientsTotalLabel,
87 self.clientsDisconnectedLabel, 90 # messages
88 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/expired": 91 prefix + "messages/sent": self.messagesSentLabel,
89 self.clientsExpiredLabel, 92 prefix + "messages/received": self.messagesReceivedLabel,
90 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/maximum": 93 prefix + "messages/stored": self.messagesStoredLabel,
91 self.clientsMaximumLabel, 94 prefix + "store/messages/count": self.messagesStoredLabel,
92 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/total": 95 prefix + "messages/inflight": self.messagesInflightLabel,
93 self.clientsTotalLabel, 96 prefix + "retained messages/count": self.messagesRetainedLabel,
94 MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/sent": 97 # publish messages
95 self.messagesSentLabel, 98 prefix + "publish/messages/sent": self.publishMessagesSentLabel,
96 MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/received": 99 prefix + "publish/messages/received":
97 self.messagesReceivedLabel,
98 MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/stored":
99 self.messagesStoredLabel,
100 MqttMonitorWidget.BrokerStatusTopicPrefix + "store/messages/count":
101 self.messagesStoredLabel,
102 MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/inflight":
103 self.messagesInflightLabel,
104 MqttMonitorWidget.BrokerStatusTopicPrefix + \
105 "retained messages/count":
106 self.messagesRetainedLabel,
107 MqttMonitorWidget.BrokerStatusTopicPrefix + \
108 "publish/messages/sent":
109 self.publishMessagesSentLabel,
110 MqttMonitorWidget.BrokerStatusTopicPrefix + \
111 "publish/messages/received":
112 self.publishMessagesReceivedLabel, 100 self.publishMessagesReceivedLabel,
113 MqttMonitorWidget.BrokerStatusTopicPrefix + \ 101 prefix + "publish/messages/dropped":
114 "publish/messages/dropped":
115 self.publishMessagesDroppedLabel, 102 self.publishMessagesDroppedLabel,
116 MqttMonitorWidget.BrokerStatusTopicPrefix + "bytes/sent": 103 # traffic
117 self.bytesSentLabel, 104 prefix + "bytes/sent": self.bytesSentLabel,
118 MqttMonitorWidget.BrokerStatusTopicPrefix + "bytes/received": 105 prefix + "bytes/received": self.bytesReceivedLabel,
119 self.bytesReceivedLabel, 106 # load
120 ## MqttMonitorWidget.BrokerStatusTopicPrefix + "": 107 prefix + "load/bytes/sent": self.loadBytesSentLabel,
121 ## self.versionLabel, 108 prefix + "load/bytes/received": self.loadBytesReceivedLabel,
109 prefix + "load/messages/sent": self.loadMessagesSentLabel,
110 prefix + "load/messages/received": self.loadMessagesReceivedLabel,
111 prefix + "load/publish/sent": self.loadPublishSentLabel,
112 prefix + "load/publish/received": self.loadPublishReceivedLabel,
113 prefix + "load/publish/dropped": self.loadPublishDroppedLabel,
114 prefix + "load/connections": self.loadConnectionsLabel,
115 prefix + "load/sockets": self.loadSocketsLabel,
122 } 116 }
117
118 self.__statusLoadValues = collections.defaultdict(
119 self.__loadDefaultDictFactory)
123 120
124 self.__client = MqttClient() 121 self.__client = MqttClient()
125 122
126 # connect the MQTT client signals 123 # connect the MQTT client signals
127 self.__client.onConnect.connect(self.__brokerConnected) 124 self.__client.onConnect.connect(self.__brokerConnected)
154 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png")) 151 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png"))
155 152
156 self.subscribeGroup.setEnabled(True) 153 self.subscribeGroup.setEnabled(True)
157 self.unsubscribeGroup.setEnabled(True) 154 self.unsubscribeGroup.setEnabled(True)
158 self.brokerStatusButton.setEnabled(True) 155 self.brokerStatusButton.setEnabled(True)
156
157 self.__statusLoadValues.clear()
159 158
160 @pyqtSlot(int) 159 @pyqtSlot(int)
161 def __brokerDisconnected(self, rc): 160 def __brokerDisconnected(self, rc):
162 """ 161 """
163 Private slot to handle a disconnection from the broker. 162 Private slot to handle a disconnection from the broker.
183 self.__updateUnsubscribeTopicComboBox() 182 self.__updateUnsubscribeTopicComboBox()
184 183
185 self.subscribeGroup.setEnabled(False) 184 self.subscribeGroup.setEnabled(False)
186 self.unsubscribeGroup.setEnabled(False) 185 self.unsubscribeGroup.setEnabled(False)
187 self.brokerStatusButton.setEnabled(False) 186 self.brokerStatusButton.setEnabled(False)
187
188 self.__statusLoadValues.clear()
188 189
189 @pyqtSlot(str, bytes, int, bool) 190 @pyqtSlot(str, bytes, int, bool)
190 def __messageReceived(self, topic, payload, qos, retain): 191 def __messageReceived(self, topic, payload, qos, retain):
191 """ 192 """
192 Private slot to handle the receipt of a message. 193 Private slot to handle the receipt of a message.
315 if topic.startswith(MqttMonitorWidget.BrokerStatusTopicPrefix): 316 if topic.startswith(MqttMonitorWidget.BrokerStatusTopicPrefix):
316 E5MessageBox.warning( 317 E5MessageBox.warning(
317 self, 318 self,
318 self.tr("Subscribe to Topic"), 319 self.tr("Subscribe to Topic"),
319 self.tr("Subscriptions to the Status topic '$SYS' shall" 320 self.tr("Subscriptions to the Status topic '$SYS' shall"
320 " be done on the Status tab.")) 321 " be done on the 'Status' tab."))
321 else: 322 else:
322 self.__topicQueue[ 323 self.__topicQueue[
323 self.__client.subscribe(topic, qos)[1]] = topic 324 self.__client.subscribe(topic, qos)[1]] = topic
324 325
325 @pyqtSlot(str) 326 @pyqtSlot(str)
425 @type str 426 @type str
426 @param payload payload of the received message 427 @param payload payload of the received message
427 @type bytes 428 @type bytes
428 """ 429 """
429 payloadStr = str(payload, encoding="utf-8", errors="replace").strip() 430 payloadStr = str(payload, encoding="utf-8", errors="replace").strip()
431 topic = topic.strip()
432
433 if topic.startswith(MqttMonitorWidget.BrokerStatusTopicLoadPrefix):
434 self.__handleBrokerLoadStatusMessage(topic, payloadStr)
435 else:
436 try:
437 label = self.__statusLabelMapping[topic]
438 label.setText(payloadStr)
439 except KeyError:
440 # ignore topics not shown in display
441 pass
442
443 def __handleBrokerLoadStatusMessage(self, topic, payloadStr):
444 """
445 Private method to append a received message to the output.
446
447 @param topic topic of the received message
448 @type str
449 @param payloadStr string representation of the payload of the
450 received message
451 @type str
452 """
453 subtopic, topicElement = topic.rsplit("/", 1)
454 self.__statusLoadValues[subtopic][topicElement] = payloadStr
430 455
431 try: 456 try:
432 label = self.__statusLabelMapping[topic.strip()] 457 label = self.__statusLabelMapping[subtopic]
433 label.setText(payloadStr) 458 label.setText("{0} / {1} / {2}".format(
459 self.__statusLoadValues[subtopic]["1min"],
460 self.__statusLoadValues[subtopic]["5min"],
461 self.__statusLoadValues[subtopic]["15min"],
462 ))
434 except KeyError: 463 except KeyError:
435 # ignore topics not shown in display 464 # ignore topics not shown in display
436 pass 465 pass
466
467 def __loadDefaultDictFactory(self):
468 """
469 Private method to populate non-existing load items.
470
471 @return default dictionary entry
472 @rtype dict
473 """
474 return {
475 "1min": "-",
476 "5min": "-",
477 "15min": "-",
478 }

eric ide

mercurial