MqttMonitor/MqttMonitorWidget.py

branch
eric7
changeset 99
420cb8adbf7e
parent 98
85d56e77e9df
child 100
9c29cfbd96c3
equal deleted inserted replaced
98:85d56e77e9df 99:420cb8adbf7e
23 23
24 from .MqttClient import ( 24 from .MqttClient import (
25 MqttClient, MqttProtocols, mqttConnackMessage, mqttErrorMessage, 25 MqttClient, MqttProtocols, mqttConnackMessage, mqttErrorMessage,
26 mqttLogLevelString 26 mqttLogLevelString
27 ) 27 )
28 from .MqttReasonCodes import mqttReasonCode
28 29
29 import UI.PixmapCache 30 import UI.PixmapCache
30 import Utilities 31 import Utilities
31 32
32 33
209 """ 210 """
210 client = MqttClient(clientId=clientId, cleanSession=cleanSession, 211 client = MqttClient(clientId=clientId, cleanSession=cleanSession,
211 protocol=protocol) 212 protocol=protocol)
212 213
213 # connect the MQTT client signals 214 # connect the MQTT client signals
214 client.onConnect.connect(self.__brokerConnected) 215 client.onConnectV3.connect(self.__brokerConnected)
215 client.onDisconnected.connect(self.__brokerDisconnected) 216 client.onConnectV5.connect(self.__brokerConnected)
217 client.onDisconnectedV3.connect(self.__brokerDisconnected)
218 client.onDisconnectedV5.connect(self.__brokerDisconnected)
216 client.onLog.connect(self.__clientLog) 219 client.onLog.connect(self.__clientLog)
217 client.onMessage.connect(self.__messageReceived) 220 client.onMessage.connect(self.__messageReceived)
218 client.onPublish.connect(self.__messagePublished) 221 client.onPublish.connect(self.__messagePublished)
219 client.onSubscribe.connect(self.__topicSubscribed) 222 client.onSubscribeV3.connect(self.__topicSubscribed)
223 client.onSubscribeV5.connect(self.__topicSubscribedV5)
220 client.onUnsubscribe.connect(self.__topicUnsubscribed) 224 client.onUnsubscribe.connect(self.__topicUnsubscribed)
221 225
222 client.connectTimeout.connect(self.__connectTimeout) 226 client.connectTimeout.connect(self.__connectTimeout)
223 227
224 return client 228 return client
227 ## Slots handling MQTT related signals 231 ## Slots handling MQTT related signals
228 ####################################################################### 232 #######################################################################
229 233
230 # TODO: change to accept ReasonCode for rc 234 # TODO: change to accept ReasonCode for rc
231 @pyqtSlot(dict, int) 235 @pyqtSlot(dict, int)
232 def __brokerConnected(self, flags, rc): 236 @pyqtSlot(dict, int, int)
237 def __brokerConnected(self, flags, rc, packetType=None):
233 """ 238 """
234 Private slot to handle being connected to a broker. 239 Private slot to handle being connected to a broker.
235 240
236 @param flags flags set for the connection 241 @param flags flags set for the connection
237 @type dict 242 @type dict
238 @param rc CONNACK result code 243 @param rc CONNACK result code or tuple containing the result code and
239 @type int 244 the packet type of the MQTTv5 reason code
245 @type int or tuple of (int, int)
240 """ 246 """
241 self.brokerStatusLabel.hide() 247 self.brokerStatusLabel.hide()
242 248
243 # TODO: add support for flags[‘session present’] 249 # TODO: add support for flags[‘session present’]
244 if rc == 0: 250 if rc == 0:
245 self.__connectedToBroker = True 251 self.__connectedToBroker = True
246 self.__connectionOptions = None 252 self.__connectionOptions = None
247 253
248 msg = mqttConnackMessage(rc) 254 if packetType is not None:
255 msg = mqttReasonCode(rc, packetType)
256 else:
257 msg = mqttConnackMessage(rc)
249 self.__flashBrokerStatusLabel(msg) 258 self.__flashBrokerStatusLabel(msg)
250 259
251 self.connectButton.setEnabled(True) 260 self.connectButton.setEnabled(True)
252 if rc == 0: 261 if rc == 0:
253 self.__connectedToBroker = True 262 self.__connectedToBroker = True
274 """ 283 """
275 self.__flashBrokerStatusLabel(self.tr("Connection timed out")) 284 self.__flashBrokerStatusLabel(self.tr("Connection timed out"))
276 self.__setConnectButtonState() 285 self.__setConnectButtonState()
277 286
278 @pyqtSlot(int) 287 @pyqtSlot(int)
279 def __brokerDisconnected(self, rc): 288 @pyqtSlot(int, int)
289 def __brokerDisconnected(self, rc, packetType=None):
280 """ 290 """
281 Private slot to handle a disconnection from the broker. 291 Private slot to handle a disconnection from the broker.
282 292
283 @param rc MQTT error result code 293 @param rc MQTT error result code
284 @type int 294 @type int
286 self.__connectedToBroker = False 296 self.__connectedToBroker = False
287 297
288 # ensure, the client loop is stopped 298 # ensure, the client loop is stopped
289 self.__client.stopLoop() 299 self.__client.stopLoop()
290 300
291 msg = ( 301 if packetType is not None:
292 mqttErrorMessage(rc) 302 # MQTT v5
293 if rc > 0 else 303 msg = mqttReasonCode(rc, packetType)
294 self.tr("Connection to Broker shut down cleanly.") 304 else:
295 ) 305 # MQTT v3
306 msg = (
307 mqttErrorMessage(rc)
308 if rc > 0 else
309 self.tr("Connection to Broker shut down cleanly.")
310 )
296 self.__flashBrokerStatusLabel(msg) 311 self.__flashBrokerStatusLabel(msg)
297 312
298 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect")) 313 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect"))
299 self.__setConnectButtonState() 314 self.__setConnectButtonState()
300 315
381 @type int 396 @type int
382 """ 397 """
383 # TODO: check this 'pass' statement 398 # TODO: check this 'pass' statement
384 pass 399 pass
385 400
386 @pyqtSlot(int, tuple) 401 @pyqtSlot(int)
387 def __topicSubscribed(self, mid, grantedQos): 402 def __topicSubscribed(self, mid):
388 """ 403 """
389 Private slot to handle being subscribed to topics. 404 Private slot to handle being subscribed to topics (MQTT v3.1,
405 MQTT v3.1.1).
390 406
391 @param mid ID of the subscribe request 407 @param mid ID of the subscribe request
392 @type int 408 @type int
393 @param grantedQos tuple of granted quality of service 409 @param grantedQos tuple of granted quality of service
394 @type tuple of int 410 @type tuple of int
398 self.__subscribedTopics.append(topic) 414 self.__subscribedTopics.append(topic)
399 self.subscribeTopicEdit.clear() 415 self.subscribeTopicEdit.clear()
400 416
401 self.__updateUnsubscribeTopicComboBox() 417 self.__updateUnsubscribeTopicComboBox()
402 self.__updatePublishTopicComboBox() 418 self.__updatePublishTopicComboBox()
419
420 @pyqtSlot(int, list)
421 def __topicSubscribedV5(self, mid, reasonCodes):
422 """
423 Private slot to handle being subscribed to topics (MQTT v5).
424
425 @param mid ID of the subscribe request
426 @type int
427 @param grantedQos tuple of granted quality of service
428 @type tuple of int
429 """
430 msg = mqttReasonCode(reasonCodes[0].value, reasonCodes[0].packetType)
431 self.__flashBrokerStatusLabel(msg)
432 self.__topicSubscribed(mid)
403 433
404 @pyqtSlot(int) 434 @pyqtSlot(int)
405 def __topicUnsubscribed(self, mid): 435 def __topicUnsubscribed(self, mid):
406 """ 436 """
407 Private slot to handle being unsubcribed from a topic. 437 Private slot to handle being unsubcribed from a topic.

eric ide

mercurial