MqttMonitor/MqttClient.py

branch
eric7
changeset 104
9a4c9b7f078c
parent 103
5fe4f179975f
child 105
36ec7431ad04
equal deleted inserted replaced
103:5fe4f179975f 104:9a4c9b7f078c
277 @param userdata user data 277 @param userdata user data
278 @type any 278 @type any
279 """ 279 """
280 self.__mqttClient.user_data_set(userdata) 280 self.__mqttClient.user_data_set(userdata)
281 281
282 # TODO: MQTTv5: add support for WILL properties 282 def setLastWill(self, topic, payload=None, qos=0, retain=False,
283 def setLastWill(self, topic, payload=None, qos=0, retain=False): 283 properties=None):
284 """ 284 """
285 Public method to set the last will of the client. 285 Public method to set the last will of the client.
286 286
287 @param topic topic the will message should be published on 287 @param topic topic the will message should be published on
288 @type str 288 @type str
291 @param qos quality of service level to use for the will 291 @param qos quality of service level to use for the will
292 @type int, one of 0, 1 or 2 292 @type int, one of 0, 1 or 2
293 @param retain flag indicating to set as the "last known good"/retained 293 @param retain flag indicating to set as the "last known good"/retained
294 message for the will topic 294 message for the will topic
295 @type bool 295 @type bool
296 @param properties list of user properties to be sent with the
297 last will message
298 @type list of tuple of (str, str)
296 """ 299 """
297 self.__mqttClient.will_set(topic, payload=payload, qos=qos, 300 self.__mqttClient.will_set(topic, payload=payload, qos=qos,
298 retain=retain) 301 retain=retain, properties=properties)
299 302
300 def clearLastWill(self): 303 def clearLastWill(self):
301 """ 304 """
302 Public method to remove a will that was previously configured with 305 Public method to remove a will that was previously configured with
303 setLastWill(). 306 setLastWill().
349 """ 352 """
350 self.__mqttClient.loop_stop() 353 self.__mqttClient.loop_stop()
351 self.__loopStarted = False 354 self.__loopStarted = False
352 355
353 def connectToServer(self, host, port=1883, keepalive=60, bindAddress="", 356 def connectToServer(self, host, port=1883, keepalive=60, bindAddress="",
354 properties=None): 357 properties=None, clearWill=False):
355 """ 358 """
356 Public method to connect to a remote MQTT broker. 359 Public method to connect to a remote MQTT broker.
357 360
358 @param host host name or IP address of the remote broker 361 @param host host name or IP address of the remote broker
359 @type str 362 @type str
367 this client to 370 this client to
368 @type str 371 @type str
369 @param properties list of user properties to be sent with the 372 @param properties list of user properties to be sent with the
370 subscription 373 subscription
371 @type list of tuple of (str, str) 374 @type list of tuple of (str, str)
372 """ 375 @param clearWill flag indicating to clear the last will previously set
376 @type bool
377 """
378 if clearWill:
379 self.clearLastWill()
380
373 props = ( 381 props = (
374 self.__createPropertiesObject(PacketTypes.CONNECT, properties) 382 self.__createPropertiesObject(PacketTypes.CONNECT, properties)
375 if properties else 383 if properties else
376 None 384 None
377 ) 385 )
383 391
384 if not self.__loopStarted: 392 if not self.__loopStarted:
385 self.startLoop() 393 self.startLoop()
386 394
387 def connectToServerWithOptions(self, host, port=1883, bindAddress="", 395 def connectToServerWithOptions(self, host, port=1883, bindAddress="",
388 options=None): 396 options=None, clearWill=False):
389 """ 397 """
390 Public method to connect to a remote MQTT broker. 398 Public method to connect to a remote MQTT broker.
391 399
392 @param host host name or IP address of the remote broker 400 @param host host name or IP address of the remote broker
393 @type str 401 @type str
398 this client to 406 this client to
399 @type str 407 @type str
400 @param options dictionary containing the connection options. This 408 @param options dictionary containing the connection options. This
401 dictionary should contain the keys "ClientId", "ConnectionTimeout", 409 dictionary should contain the keys "ClientId", "ConnectionTimeout",
402 "Keepalive", "CleanSession", "Username", "Password", "WillTopic", 410 "Keepalive", "CleanSession", "Username", "Password", "WillTopic",
403 "WillMessage", "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", 411 "WillMessage", "WillQos", "WillRetain", "WillProperties",
404 "TlsClientCert", "TlsClientKey", "UserProperties". 412 "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey",
413 "UserProperties".
405 @type dict 414 @type dict
415 @param clearWill flag indicating to clear the last will previously set
416 @type bool
406 """ 417 """
407 if options: 418 if options:
408 parametersDict = self.defaultConnectionOptions() 419 parametersDict = self.defaultConnectionOptions()
409 parametersDict.update(options) 420 parametersDict.update(options)
410 421
418 pwConvert(parametersDict["Password"], encode=False)) 429 pwConvert(parametersDict["Password"], encode=False))
419 else: 430 else:
420 self.setUserCredentials(parametersDict["Username"]) 431 self.setUserCredentials(parametersDict["Username"])
421 432
422 # step 2: set last will data 433 # step 2: set last will data
423 if parametersDict["WillTopic"]: 434 if not clearWill and parametersDict["WillTopic"]:
424 if parametersDict["WillMessage"]: 435 if parametersDict["WillMessage"]:
425 willMessage = parametersDict["WillMessage"] 436 willMessage = parametersDict["WillMessage"]
426 else: 437 else:
427 # empty message to clear the will 438 # empty message to clear the will
428 willMessage = None 439 willMessage = None
440 props = (
441 self.__createPropertiesObject(
442 PacketTypes.WILLMESSAGE,
443 parametersDict["WillProperties"])
444 if (parametersDict["WillProperties"] and
445 self.__protocol == MqttProtocols.MQTTv5) else
446 None
447 )
429 self.setLastWill(parametersDict["WillTopic"], 448 self.setLastWill(parametersDict["WillTopic"],
430 willMessage, 449 payload=willMessage,
431 parametersDict["WillQos"], 450 qos=parametersDict["WillQos"],
432 parametersDict["WillRetain"]) 451 retain=parametersDict["WillRetain"],
452 properties=props)
433 453
434 # step 3: set TLS parameters 454 # step 3: set TLS parameters
435 if parametersDict["TlsEnable"]: 455 if parametersDict["TlsEnable"]:
436 if ( 456 if (
437 parametersDict["TlsCaCert"] and 457 parametersDict["TlsCaCert"] and
464 properties = None 484 properties = None
465 # step 4: connect to server 485 # step 4: connect to server
466 self.__cleanSession = parametersDict["CleanSession"] 486 self.__cleanSession = parametersDict["CleanSession"]
467 self.connectToServer(host, port=port, 487 self.connectToServer(host, port=port,
468 keepalive=parametersDict["Keepalive"], 488 keepalive=parametersDict["Keepalive"],
469 properties=properties) 489 properties=properties,
490 clearWill=clearWill)
470 else: 491 else:
471 keepalive = self.defaultConnectionOptions["Keepalive"] 492 keepalive = self.defaultConnectionOptions["Keepalive"]
472 self.connectToServer(host, port=port, keepalive=keepalive, 493 self.connectToServer(host, port=port, keepalive=keepalive,
473 bindAddress=bindAddress) 494 bindAddress=bindAddress,
495 clearWill=clearWill)
474 496
475 @classmethod 497 @classmethod
476 def defaultConnectionOptions(cls): 498 def defaultConnectionOptions(cls):
477 """ 499 """
478 Class method to get a connection options dictionary with default 500 Class method to get a connection options dictionary with default
479 values. 501 values.
480 502
481 @return dictionary containing the default connection options. It has 503 @return dictionary containing the default connection options. It has
482 the keys "ClientId", "Protocol", "ConnectionTimeout", "Keepalive", 504 the keys "ClientId", "Protocol", "ConnectionTimeout", "Keepalive",
483 "CleanSession", "Username", "Password", "WillTopic", "WillMessage", 505 "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
484 "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", "TlsClientCert", 506 "WillQos", "WillRetain", "WillProperties", "TlsEnable",
485 "TlsClientKey", "UserProperties". 507 "TlsCaCert", "TlsClientCert", "TlsClientKey", "UserProperties".
486 @rtype dict 508 @rtype dict
487 """ 509 """
488 return { 510 return {
489 "ClientId": "ERIC7_MQTT_MONITOR_CLIENT", 511 "ClientId": "ERIC7_MQTT_MONITOR_CLIENT",
490 "Protocol": MqttProtocols.MQTTv311, 512 "Protocol": MqttProtocols.MQTTv311,
495 "Password": "", 517 "Password": "",
496 "WillTopic": "", 518 "WillTopic": "",
497 "WillMessage": "", 519 "WillMessage": "",
498 "WillQos": 0, 520 "WillQos": 0,
499 "WillRetain": False, 521 "WillRetain": False,
522 "WillProperties": [],
500 "TlsEnable": False, 523 "TlsEnable": False,
501 "TlsCaCert": "", 524 "TlsCaCert": "",
502 "TlsClientCert": "", 525 "TlsClientCert": "",
503 "TlsClientKey": "", 526 "TlsClientKey": "",
504 "UserProperties": { 527 "UserProperties": {

eric ide

mercurial