7 Module implementing the MQTT Monitor widget. |
7 Module implementing the MQTT Monitor widget. |
8 """ |
8 """ |
9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
|
12 try: |
|
13 str = unicode # __IGNORE_EXCEPTION__ |
|
14 except NameError: |
|
15 pass |
|
16 |
12 import os |
17 import os |
13 |
18 |
14 from PyQt5.QtCore import pyqtSlot, QTimer |
19 from PyQt5.QtCore import pyqtSlot, QTimer |
|
20 from PyQt5.QtGui import QTextCursor |
15 from PyQt5.QtWidgets import QWidget |
21 from PyQt5.QtWidgets import QWidget |
16 |
22 |
17 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget |
23 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget |
18 |
24 |
19 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage |
25 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage |
20 |
26 |
21 import UI.PixmapCache |
27 import UI.PixmapCache |
|
28 import Utilities |
22 |
29 |
23 |
30 |
24 class MqttMonitorWidget(QWidget, Ui_MqttMonitorWidget): |
31 class MqttMonitorWidget(QWidget, Ui_MqttMonitorWidget): |
25 """ |
32 """ |
26 Class implementing the MQTT Monitor widget. |
33 Class implementing the MQTT Monitor widget. |
41 |
48 |
42 self.__connectedToBroker = False |
49 self.__connectedToBroker = False |
43 |
50 |
44 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap( |
51 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap( |
45 os.path.join("MqttMonitor", "icons", "mqtt48.png"))) |
52 os.path.join("MqttMonitor", "icons", "mqtt48.png"))) |
|
53 |
|
54 self.brokerWidget.setCurrentIndex(0) |
46 |
55 |
47 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) |
56 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) |
48 self.brokerComboBox.addItems( |
57 self.brokerComboBox.addItems( |
49 self.__plugin.getPreferences("RecentBrokers")) |
58 self.__plugin.getPreferences("RecentBrokers")) |
50 self.brokerStatusLabel.hide() |
59 self.brokerStatusLabel.hide() |
132 @param qos quality of service indicator |
141 @param qos quality of service indicator |
133 @type int |
142 @type int |
134 @param retain flag indicating a retained message |
143 @param retain flag indicating a retained message |
135 @type bool |
144 @type bool |
136 """ |
145 """ |
137 pass |
146 if topic.startswith("$SYS/broker/"): |
|
147 # handle broker status messages |
|
148 self.__handleBrokerStatusMessage(topic, payload) |
|
149 else: |
|
150 self.__appendMessage(topic, payload) |
138 |
151 |
139 @pyqtSlot(int) |
152 @pyqtSlot(int) |
140 def __messagePublished(self, mid): |
153 def __messagePublished(self, mid): |
141 """ |
154 """ |
142 Private slot to handle a message being published. |
155 Private slot to handle a message being published. |
291 Private method to update the unsubcribe topic combo box. |
304 Private method to update the unsubcribe topic combo box. |
292 """ |
305 """ |
293 self.unsubscribeTopicComboBox.clear() |
306 self.unsubscribeTopicComboBox.clear() |
294 self.unsubscribeTopicComboBox.addItems(sorted(self.__subscribedTopics)) |
307 self.unsubscribeTopicComboBox.addItems(sorted(self.__subscribedTopics)) |
295 self.unsubscribeButton.setEnabled(len(self.__subscribedTopics) > 0) |
308 self.unsubscribeButton.setEnabled(len(self.__subscribedTopics) > 0) |
|
309 |
|
310 def __appendMessage(self, topic, payload): |
|
311 """ |
|
312 Private method to append a received message to the output. |
|
313 |
|
314 @param topic topic of the received message |
|
315 @type str |
|
316 @param payload payload of the received message |
|
317 @type bytes |
|
318 """ |
|
319 payloadStr = str(payload, encoding="utf-8", errors="replace") |
|
320 txt = "{0} {1}".format(topic, payloadStr) |
|
321 if not txt.endswith(("\r\n", "\r", "\n")): |
|
322 txt += "\n" |
|
323 |
|
324 tc = self.messagesEdit.textCursor() |
|
325 tc.movePosition(QTextCursor.End) |
|
326 self.messagesEdit.setTextCursor(tc) |
|
327 self.messagesEdit.insertPlainText(Utilities.filterAnsiSequences(txt)) |
|
328 self.messagesEdit.ensureCursorVisible() |
|
329 |
|
330 def __handleBrokerStatusMessage(self, topic, payload): |
|
331 """ |
|
332 Private method to append a received message to the output. |
|
333 |
|
334 @param topic topic of the received message |
|
335 @type str |
|
336 @param payload payload of the received message |
|
337 @type bytes |
|
338 """ |
|
339 payloadStr = str(payload, encoding="utf-8", errors="replace") |