MqttMonitor/MqttMonitorWidget.py

changeset 6
d22f5ce3a07a
parent 5
7162c838cfc9
child 7
63e046d95702
equal deleted inserted replaced
5:7162c838cfc9 6:d22f5ce3a07a
18 18
19 from PyQt5.QtCore import pyqtSlot, QTimer 19 from PyQt5.QtCore import pyqtSlot, QTimer
20 from PyQt5.QtGui import QTextCursor 20 from PyQt5.QtGui import QTextCursor
21 from PyQt5.QtWidgets import QWidget 21 from PyQt5.QtWidgets import QWidget
22 22
23 from E5Gui import E5MessageBox
24
23 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget 25 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
24 26
25 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage 27 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage
26 28
27 import UI.PixmapCache 29 import UI.PixmapCache
30 32
31 class MqttMonitorWidget(QWidget, Ui_MqttMonitorWidget): 33 class MqttMonitorWidget(QWidget, Ui_MqttMonitorWidget):
32 """ 34 """
33 Class implementing the MQTT Monitor widget. 35 Class implementing the MQTT Monitor widget.
34 """ 36 """
37 BrokerStatusTopicPrefix = "$SYS/broker/"
38 BrokerStatusTopic = "$SYS/broker/#"
39
35 def __init__(self, plugin, parent=None): 40 def __init__(self, plugin, parent=None):
36 """ 41 """
37 Constructor 42 Constructor
38 43
39 @param plugin reference to the plug-in object 44 @param plugin reference to the plug-in object
45 self.setupUi(self) 50 self.setupUi(self)
46 51
47 self.__plugin = plugin 52 self.__plugin = plugin
48 53
49 self.__connectedToBroker = False 54 self.__connectedToBroker = False
55 self.__brokerStatusTopicSubscribed = False
50 56
51 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap( 57 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap(
52 os.path.join("MqttMonitor", "icons", "mqtt48.png"))) 58 os.path.join("MqttMonitor", "icons", "mqtt48.png")))
53 59
54 self.brokerWidget.setCurrentIndex(0) 60 self.brokerWidget.setCurrentIndex(0)
63 self.unsubscribeButton.setIcon(UI.PixmapCache.getIcon("minus.png")) 69 self.unsubscribeButton.setIcon(UI.PixmapCache.getIcon("minus.png"))
64 70
65 self.__subscribedTopics = [] 71 self.__subscribedTopics = []
66 self.__topicQueue = {} 72 self.__topicQueue = {}
67 self.__updateUnsubscribeTopicComboBox() 73 self.__updateUnsubscribeTopicComboBox()
74
75 self.__statusLabelMapping = {
76 MqttMonitorWidget.BrokerStatusTopicPrefix + "version":
77 self.versionLabel,
78 MqttMonitorWidget.BrokerStatusTopicPrefix + "timestamp":
79 self.timestampLabel,
80 MqttMonitorWidget.BrokerStatusTopicPrefix + "uptime":
81 self.uptimeLabel,
82 MqttMonitorWidget.BrokerStatusTopicPrefix + "subscriptions/count":
83 self.subscriptionsLabel,
84 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/connected":
85 self.clientsConnectedLabel,
86 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/disconnected":
87 self.clientsDisconnectedLabel,
88 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/expired":
89 self.clientsExpiredLabel,
90 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/maximum":
91 self.clientsMaximumLabel,
92 MqttMonitorWidget.BrokerStatusTopicPrefix + "clients/total":
93 self.clientsTotalLabel,
94 MqttMonitorWidget.BrokerStatusTopicPrefix + "messages/sent":
95 self.messagesSentLabel,
96 MqttMonitorWidget.BrokerStatusTopicPrefix + "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,
113 MqttMonitorWidget.BrokerStatusTopicPrefix + \
114 "publish/messages/dropped":
115 self.publishMessagesDroppedLabel,
116 MqttMonitorWidget.BrokerStatusTopicPrefix + "bytes/sent":
117 self.bytesSentLabel,
118 MqttMonitorWidget.BrokerStatusTopicPrefix + "bytes/received":
119 self.bytesReceivedLabel,
120 ## MqttMonitorWidget.BrokerStatusTopicPrefix + "":
121 ## self.versionLabel,
122 }
68 123
69 self.__client = MqttClient() 124 self.__client = MqttClient()
70 125
71 # connect the MQTT client signals 126 # connect the MQTT client signals
72 self.__client.onConnect.connect(self.__brokerConnected) 127 self.__client.onConnect.connect(self.__brokerConnected)
98 153
99 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png")) 154 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png"))
100 155
101 self.subscribeGroup.setEnabled(True) 156 self.subscribeGroup.setEnabled(True)
102 self.unsubscribeGroup.setEnabled(True) 157 self.unsubscribeGroup.setEnabled(True)
158 self.brokerStatusButton.setEnabled(True)
103 159
104 @pyqtSlot(int) 160 @pyqtSlot(int)
105 def __brokerDisconnected(self, rc): 161 def __brokerDisconnected(self, rc):
106 """ 162 """
107 Private slot to handle a disconnection from the broker. 163 Private slot to handle a disconnection from the broker.
126 self.__topicQueue = {} 182 self.__topicQueue = {}
127 self.__updateUnsubscribeTopicComboBox() 183 self.__updateUnsubscribeTopicComboBox()
128 184
129 self.subscribeGroup.setEnabled(False) 185 self.subscribeGroup.setEnabled(False)
130 self.unsubscribeGroup.setEnabled(False) 186 self.unsubscribeGroup.setEnabled(False)
187 self.brokerStatusButton.setEnabled(False)
131 188
132 @pyqtSlot(str, bytes, int, bool) 189 @pyqtSlot(str, bytes, int, bool)
133 def __messageReceived(self, topic, payload, qos, retain): 190 def __messageReceived(self, topic, payload, qos, retain):
134 """ 191 """
135 Private slot to handle the receipt of a message. 192 Private slot to handle the receipt of a message.
141 @param qos quality of service indicator 198 @param qos quality of service indicator
142 @type int 199 @type int
143 @param retain flag indicating a retained message 200 @param retain flag indicating a retained message
144 @type bool 201 @type bool
145 """ 202 """
146 if topic.startswith("$SYS/broker/"): 203 if topic.startswith(MqttMonitorWidget.BrokerStatusTopicPrefix):
147 # handle broker status messages 204 # handle broker status messages
148 self.__handleBrokerStatusMessage(topic, payload) 205 self.__handleBrokerStatusMessage(topic, payload)
149 else: 206 else:
150 self.__appendMessage(topic, payload) 207 self.__appendMessage(topic, payload)
151 208
253 Private slot to subscribe to the entered topic. 310 Private slot to subscribe to the entered topic.
254 """ 311 """
255 topic = self.subscribeTopicEdit.text() 312 topic = self.subscribeTopicEdit.text()
256 qos = self.subscribeQosSpinBox.value() 313 qos = self.subscribeQosSpinBox.value()
257 if topic: 314 if topic:
258 self.__topicQueue[ 315 if topic.startswith(MqttMonitorWidget.BrokerStatusTopicPrefix):
259 self.__client.subscribe(topic, qos)[1]] = topic 316 E5MessageBox.warning(
317 self,
318 self.tr("Subscribe to Topic"),
319 self.tr("Subscriptions to the Status topic '$SYS' shall"
320 " be done on the Status tab."))
321 else:
322 self.__topicQueue[
323 self.__client.subscribe(topic, qos)[1]] = topic
260 324
261 @pyqtSlot(str) 325 @pyqtSlot(str)
262 def on_unsubscribeTopicComboBox_currentIndexChanged(self, topic): 326 def on_unsubscribeTopicComboBox_currentIndexChanged(self, topic):
263 """ 327 """
264 Private slot to handle the selection of a topic to unsubscribe from. 328 Private slot to handle the selection of a topic to unsubscribe from.
275 """ 339 """
276 topic = self.unsubscribeTopicComboBox.currentText() 340 topic = self.unsubscribeTopicComboBox.currentText()
277 if topic: 341 if topic:
278 self.__topicQueue[ 342 self.__topicQueue[
279 self.__client.unsubscribe(topic)[1]] = topic 343 self.__client.unsubscribe(topic)[1]] = topic
344
345 @pyqtSlot()
346 def on_brokerStatusButton_clicked(self):
347 """
348 Private slot to subscribe or unsubscribe the broker status topic.
349 """
350 if self.__brokerStatusTopicSubscribed:
351 # unsubscribe status topic
352 rc, _ = self.__client.unsubscribe(
353 MqttMonitorWidget.BrokerStatusTopic)
354 if rc == 0:
355 # successfully sent
356 self.__brokerStatusTopicSubscribed = False
357 self.brokerStatusButton.setText(self.tr("Subscribe"))
358 self.brokerStatusButton.setToolTip(
359 self.tr("Press to activate the status display"))
360 else:
361 # subscribe status topic
362 rc, _ = self.__client.subscribe(
363 MqttMonitorWidget.BrokerStatusTopic)
364 if rc == 0:
365 # successfully sent
366 self.__brokerStatusTopicSubscribed = True
367 self.brokerStatusButton.setText(self.tr("Unsubscribe"))
368 self.brokerStatusButton.setToolTip(
369 self.tr("Press to deactivate the status display"))
280 370
281 ####################################################################### 371 #######################################################################
282 ## Utility methods 372 ## Utility methods
283 ####################################################################### 373 #######################################################################
284 374
334 @param topic topic of the received message 424 @param topic topic of the received message
335 @type str 425 @type str
336 @param payload payload of the received message 426 @param payload payload of the received message
337 @type bytes 427 @type bytes
338 """ 428 """
339 payloadStr = str(payload, encoding="utf-8", errors="replace") 429 payloadStr = str(payload, encoding="utf-8", errors="replace").strip()
430
431 try:
432 label = self.__statusLabelMapping[topic.strip()]
433 label.setText(payloadStr)
434 except KeyError:
435 # ignore topics not shown in display
436 pass

eric ide

mercurial