MqttMonitor/MqttMonitorWidget.py

changeset 31
40582e448c4b
parent 30
17ef10819773
child 32
a71e5b294ebf
equal deleted inserted replaced
30:17ef10819773 31:40582e448c4b
24 24
25 from E5Gui import E5MessageBox 25 from E5Gui import E5MessageBox
26 26
27 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget 27 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
28 28
29 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage 29 from .MqttClient import MqttClient, mqttConnackMessage, mqttErrorMessage, \
30 mqttLogLevelString
30 31
31 import UI.PixmapCache 32 import UI.PixmapCache
32 import Utilities 33 import Utilities
33 34
34 35
57 self.__connectedToBroker = False 58 self.__connectedToBroker = False
58 self.__brokerStatusTopicSubscribed = False 59 self.__brokerStatusTopicSubscribed = False
59 60
60 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap( 61 self.pixmapLabel.setPixmap(UI.PixmapCache.getPixmap(
61 os.path.join("MqttMonitor", "icons", "mqtt48.png"))) 62 os.path.join("MqttMonitor", "icons", "mqtt48.png")))
63
64 for logLevel in (MqttClient.LogDebug,
65 MqttClient.LogInfo,
66 MqttClient.LogNotice,
67 MqttClient.LogWarning,
68 MqttClient.LogError):
69 self.logLevelComboBox.addItem(mqttLogLevelString(
70 logLevel, isMqttLogLevel=False), logLevel)
71 self.logLevelComboBox.setCurrentIndex(
72 self.logLevelComboBox.count() - 1)
62 73
63 self.brokerWidget.setCurrentIndex(0) 74 self.brokerWidget.setCurrentIndex(0)
64 75
65 self.__connectionModeProfile = True 76 self.__connectionModeProfile = True
66 self.__setConnectionMode(True) # initial mode is 'profile connection' 77 self.__setConnectionMode(True) # initial mode is 'profile connection'
133 self.__client = MqttClient() 144 self.__client = MqttClient()
134 145
135 # connect the MQTT client signals 146 # connect the MQTT client signals
136 self.__client.onConnect.connect(self.__brokerConnected) 147 self.__client.onConnect.connect(self.__brokerConnected)
137 self.__client.onDisconnected.connect(self.__brokerDisconnected) 148 self.__client.onDisconnected.connect(self.__brokerDisconnected)
149 self.__client.onLog.connect(self.__clientLog)
138 self.__client.onMessage.connect(self.__messageReceived) 150 self.__client.onMessage.connect(self.__messageReceived)
139 self.__client.onPublish.connect(self.__messagePublished) 151 self.__client.onPublish.connect(self.__messagePublished)
140 self.__client.onSubscribe.connect(self.__topicSubscribed) 152 self.__client.onSubscribe.connect(self.__topicSubscribed)
141 self.__client.onUnsubscribe.connect(self.__topicUnsubscribed) 153 self.__client.onUnsubscribe.connect(self.__topicUnsubscribed)
154
155 self.__client.connectTimeout.connect(self.__connectTimeout)
142 156
143 ####################################################################### 157 #######################################################################
144 ## Slots handling MQTT related signals 158 ## Slots handling MQTT related signals
145 ####################################################################### 159 #######################################################################
146 160
181 self.__clearBrokerStatusLabels() 195 self.__clearBrokerStatusLabels()
182 self.__setBrokerStatusSubscribed(False) 196 self.__setBrokerStatusSubscribed(False)
183 else: 197 else:
184 self.__client.stopLoop() 198 self.__client.stopLoop()
185 199
200 @pyqtSlot()
201 def __connectTimeout(self):
202 """
203 Private slot handling a timeout during a connection attempt.
204 """
205 self.__flashBrokerStatusLabel(self.tr("Connection timed out"))
206 self.__setConnectButtonState()
207
186 @pyqtSlot(int) 208 @pyqtSlot(int)
187 def __brokerDisconnected(self, rc): 209 def __brokerDisconnected(self, rc):
188 """ 210 """
189 Private slot to handle a disconnection from the broker. 211 Private slot to handle a disconnection from the broker.
190 212
201 else: 223 else:
202 msg = self.tr("Connection to Broker shut down cleanly.") 224 msg = self.tr("Connection to Broker shut down cleanly.")
203 self.__flashBrokerStatusLabel(msg) 225 self.__flashBrokerStatusLabel(msg)
204 226
205 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) 227 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
206 self.connectButton.setEnabled(True) 228 self.__setConnectButtonState()
207 229
208 self.__subscribedTopics = [] 230 self.__subscribedTopics = []
209 self.__topicQueue = {} 231 self.__topicQueue = {}
210 self.__updateUnsubscribeTopicComboBox() 232 self.__updateUnsubscribeTopicComboBox()
211 self.__updatePublishTopicComboBox() 233 self.__updatePublishTopicComboBox()
214 self.unsubscribeGroup.setEnabled(False) 236 self.unsubscribeGroup.setEnabled(False)
215 self.publishGroup.setEnabled(False) 237 self.publishGroup.setEnabled(False)
216 self.brokerStatusButton.setEnabled(False) 238 self.brokerStatusButton.setEnabled(False)
217 239
218 self.__statusLoadValues.clear() 240 self.__statusLoadValues.clear()
241
242 @pyqtSlot(int, str)
243 def __clientLog(self, level, message):
244 """
245 Private slot to handle the receipt of a log message.
246
247 @param level log level
248 @type int
249 @param message log message
250 @type str
251 """
252 try:
253 if MqttClient.LogLevelMap[level] < self.logLevelComboBox.itemData(
254 self.logLevelComboBox.currentIndex()):
255 return
256 except KeyError:
257 # always show unknown log levels
258 pass
259
260 txt = self.tr("{0}: {1}").format(mqttLogLevelString(level), message)
261 if not txt.endswith(("\r\n", "\r", "\n")):
262 txt += "\n"
263
264 tc = self.logEdit.textCursor()
265 tc.movePosition(QTextCursor.End)
266 self.logEdit.setTextCursor(tc)
267 self.logEdit.insertPlainText(Utilities.filterAnsiSequences(txt))
268 self.logEdit.ensureCursorVisible()
219 269
220 @pyqtSlot(str, bytes, int, bool) 270 @pyqtSlot(str, bytes, int, bool)
221 def __messageReceived(self, topic, payload, qos, retain): 271 def __messageReceived(self, topic, payload, qos, retain):
222 """ 272 """
223 Private slot to handle the receipt of a message. 273 Private slot to handle the receipt of a message.
305 """ 355 """
306 Private slot to switch between connection profiles and direct 356 Private slot to switch between connection profiles and direct
307 connection mode. 357 connection mode.
308 """ 358 """
309 self.__setConnectionMode(not self.__connectionModeProfile) 359 self.__setConnectionMode(not self.__connectionModeProfile)
360
361 @pyqtSlot(str)
362 def on_profileComboBox_currentIndexChanged(self, profileName):
363 """
364 Private slot handling the change of the selected profile.
365
366 @param profileName name of the selected profile
367 @type str
368 """
369 self.__setConnectButtonState()
310 370
311 @pyqtSlot(str) 371 @pyqtSlot(str)
312 def on_brokerComboBox_editTextChanged(self, host): 372 def on_brokerComboBox_editTextChanged(self, host):
313 """ 373 """
314 Private slot to handling entering or selecting a broker host name. 374 Private slot to handling entering or selecting a broker host name.
728 # use standard port at 1883 788 # use standard port at 1883
729 port = 1883 789 port = 1883
730 if host: 790 if host:
731 self.brokerStatusLabel.setText( 791 self.brokerStatusLabel.setText(
732 self.tr("Connecting to {0}:{1} ...").format( 792 self.tr("Connecting to {0}:{1} ...").format(
733 host, port)) 793 host, port))
734 self.brokerStatusLabel.show() 794 self.brokerStatusLabel.show()
735 795
736 self.__addBrokerToRecent(host, port) 796 self.__addBrokerToRecent(host, port)
737 self.connectButton.setEnabled(False) 797 self.connectButton.setEnabled(False)
738 if self.__connectionOptions is None: 798 if self.__connectionOptions is None:
754 host = profile["BrokerAddress"] 814 host = profile["BrokerAddress"]
755 port = profile["BrokerPort"] 815 port = profile["BrokerPort"]
756 816
757 self.brokerStatusLabel.setText( 817 self.brokerStatusLabel.setText(
758 self.tr("Connecting to {0}:{1} ...").format( 818 self.tr("Connecting to {0}:{1} ...").format(
759 host, port)) 819 host, port))
760 self.brokerStatusLabel.show() 820 self.brokerStatusLabel.show()
761 821
762 self.connectButton.setEnabled(False) 822 self.connectButton.setEnabled(False)
763 self.__client.connectToServerWithOptions(host, port=port, 823 self.__client.connectToServerWithOptions(host, port=port,
764 options=profile) 824 options=profile)

eric ide

mercurial