MqttMonitor/MqttClient.py

branch
eric7
changeset 97
21f9c010dc42
parent 95
d830314cca87
child 98
85d56e77e9df
equal deleted inserted replaced
96:06d1cba236eb 97:21f9c010dc42
5 5
6 """ 6 """
7 Module implementing a PyQt wrapper around the paho MQTT client. 7 Module implementing a PyQt wrapper around the paho MQTT client.
8 """ 8 """
9 9
10 import enum
11
10 from PyQt6.QtCore import ( 12 from PyQt6.QtCore import (
11 pyqtSignal, pyqtSlot, QObject, QCoreApplication, QTimer 13 pyqtSignal, pyqtSlot, QObject, QCoreApplication, QTimer
12 ) 14 )
13 15
14 import paho.mqtt.client as mqtt 16 import paho.mqtt.client as mqtt
15 17
16 from Utilities.crypto import pwConvert 18 from Utilities.crypto import pwConvert
19
20
21 class MqttProtocols(enum.IntEnum):
22 """
23 Class defining the supported MQTT protocol versions.
24 """
25 MQTTv31 = mqtt.MQTTv31
26 MQTTv311 = mqtt.MQTTv311
27 MQTTv5 = mqtt.MQTTv5
17 28
18 29
19 class MqttClient(QObject): 30 class MqttClient(QObject):
20 """ 31 """
21 Class implementing a PyQt wrapper around the paho MQTT client. 32 Class implementing a PyQt wrapper around the paho MQTT client.
71 @param cleanSession flag indicating to start a clean session 82 @param cleanSession flag indicating to start a clean session
72 @type bool 83 @type bool
73 @param userdata user data 84 @param userdata user data
74 @type any 85 @type any
75 @param protocol version of the MQTT protocol to use 86 @param protocol version of the MQTT protocol to use
76 @type int, one of mqtt.MQTTv3, mqtt.MQTTv311 or mqtt.MQTTv5 87 @type int, one of mqtt.MQTTv31, mqtt.MQTTv311 or mqtt.MQTTv5
77 @param transport transport to be used 88 @param transport transport to be used
78 @type str, one of "tcp" or "websockets" 89 @type str, one of "tcp" or "websockets"
79 @param parent reference to the parent object 90 @param parent reference to the parent object
80 @type QObject 91 @type QObject
81 """ 92 """
89 MqttClient.DefaultConnectTimeout * 1000) 100 MqttClient.DefaultConnectTimeout * 1000)
90 self.__connectTimeoutTimer.timeout.connect(self.__connectTimeout) 101 self.__connectTimeoutTimer.timeout.connect(self.__connectTimeout)
91 102
92 self.onConnect.connect(self.__connectTimeoutTimer.stop) 103 self.onConnect.connect(self.__connectTimeoutTimer.stop)
93 104
94 # TODO: MQTTv5: set clean_session to None and remember cleanSession 105 self.__cleanSession = cleanSession
106 if protocol == MqttProtocols.MQTTv5:
107 cleanSession = None
108
95 self.__mqttClient = mqtt.Client( 109 self.__mqttClient = mqtt.Client(
96 client_id=clientId, clean_session=cleanSession, userdata=None, 110 client_id=clientId, clean_session=cleanSession, userdata=userdata,
97 protocol=mqtt.MQTTv311, transport="tcp") 111 protocol=int(protocol), transport=transport)
98 112
99 self.__initCallbacks() 113 self.__initCallbacks()
100 114
101 def __initCallbacks(self): 115 def __initCallbacks(self):
102 """ 116 """
369 else: 383 else:
370 keepalive = self.defaultConnectionOptions["Keepalive"] 384 keepalive = self.defaultConnectionOptions["Keepalive"]
371 self.connectToServer(host, port=port, keepalive=keepalive, 385 self.connectToServer(host, port=port, keepalive=keepalive,
372 bindAddress=bindAddress) 386 bindAddress=bindAddress)
373 387
374 def defaultConnectionOptions(self): 388 @classmethod
375 """ 389 def defaultConnectionOptions(cls):
376 Public method to get a connection options dictionary with default 390 """
391 Class method to get a connection options dictionary with default
377 values. 392 values.
378 393
379 @return dictionary containing the default connection options. It has 394 @return dictionary containing the default connection options. It has
380 the keys "ClientId", "Keepalive", "CleanSession", "Username", 395 the keys "ClientId", "Protocol", "ConnectionTimeout", "Keepalive",
381 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain", 396 "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
382 "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey", 397 "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", "TlsClientCert",
383 "ConnectionTimeout". 398 "TlsClientKey".
384 @rtype dict 399 @rtype dict
385 """ 400 """
386 return { 401 return {
387 "ClientId": "ERIC7_MQTT_MONITOR_CLIENT", 402 "ClientId": "ERIC7_MQTT_MONITOR_CLIENT",
403 "Protocol": MqttProtocols.MQTTv311,
388 "ConnectionTimeout": MqttClient.DefaultConnectTimeout, 404 "ConnectionTimeout": MqttClient.DefaultConnectTimeout,
389 "Keepalive": 60, 405 "Keepalive": 60,
390 "CleanSession": True, 406 "CleanSession": True,
391 "Username": "", 407 "Username": "",
392 "Password": "", 408 "Password": "",

eric ide

mercurial