diff -r cf158867ed89 -r 51bc5bcc672a MqttMonitor/MqttClient.py --- a/MqttMonitor/MqttClient.py Sat Jan 06 13:11:01 2024 +0100 +++ b/MqttMonitor/MqttClient.py Fri Apr 19 15:15:01 2024 +0200 @@ -22,44 +22,31 @@ """ Class implementing a PyQt wrapper around the paho MQTT client. - @signal onConnectV3(flags, rc) emitted after the client has connected to - the broker (MQTT v3) - @signal onConnectV5(flags, rc, packetType, properties emitted after the - client has connected to the broker (MQTT v5) - @signal onDisconnectedV3(rc) emitted after the client has disconnected from - the broker (MQTT v3) - @signal onDisconnectedV5(rc, packetType) emitted after the client has - disconnected from the broker (MQTT v5) + @signal onConnect(connectFlags, rc, packetType, properties) emitted after the + client has connected to the broker + @signal onDisconnected(rc, packetType) emitted after the client has + disconnected from the broker @signal onLog(level, message) emitted to send client log data - @signal onMessageV3(topic, payload, qos, retain) emitted after a message - has been received by the client (MQTT v3) - @signal onMessageV5(topic, payload, qos, retain, properties) emitted after - a message has been received by the client (MQTT v5) + @signal onMessage(topic, payload, qos, retain, properties) emitted after + a message has been received by the client @signal onPublish(mid) emitted after a message has been published - @signal onSubscribeV3(mid, grantedQos) emitted after the client has - subscribed to some topics (MQTT v3) - @signal onSubscribeV5(mid, reasonCodes, properties) emitted after the - client has subscribed to some topics (MQTT v5) - @signal onUnsubscribeV3(mid) emitted after the client has unsubscribed from + @signal onSubscribe(mid, reasonCodes, properties) emitted after the + client has subscribed to some topics + @signal onUnsubscribe(mid) emitted after the client has unsubscribed from some topics (MQTT v3) - @signal onUnsubscribeV5(mid, rc, packetType, properties) emitted after the + @signal onUnsubscribe(mid, rc, packetType, properties) emitted after the client has unsubscribed from some topics (MQTT v5) @signal connectTimeout() emitted to indicate, that a connection attempt timed out """ - onConnectV3 = pyqtSignal(dict, int) - onConnectV5 = pyqtSignal(dict, int, int, dict) - onDisconnectedV3 = pyqtSignal(int) - onDisconnectedV5 = pyqtSignal(int, int) + onConnect = pyqtSignal(bool, int, int, dict) + onDisconnected = pyqtSignal(int, int) onLog = pyqtSignal(int, str) - onMessageV3 = pyqtSignal(str, bytes, int, bool) - onMessageV5 = pyqtSignal(str, bytes, int, bool, dict) + onMessage = pyqtSignal(str, bytes, int, bool, dict) onPublish = pyqtSignal(int) - onSubscribeV3 = pyqtSignal(int, tuple) - onSubscribeV5 = pyqtSignal(int, list, dict) - onUnsubscribeV3 = pyqtSignal(int) - onUnsubscribeV5 = pyqtSignal(int, int, int, dict) + onSubscribe = pyqtSignal(int, list, dict) + onUnsubscribe = pyqtSignal((int,), (int, int, int, dict)) connectTimeout = pyqtSignal() @@ -113,8 +100,7 @@ self.__connectTimeoutTimer.setInterval(MqttClient.DefaultConnectTimeout * 1000) self.__connectTimeoutTimer.timeout.connect(self.__connectTimeout) - self.onConnectV3.connect(self.__connectTimeoutTimer.stop) - self.onConnectV5.connect(self.__connectTimeoutTimer.stop) + self.onConnect.connect(self.__connectTimeoutTimer.stop) self.__cleanSession = cleanSession self.__protocol = protocol @@ -124,6 +110,7 @@ cleanSession = None self.__mqttClient = mqtt.Client( + mqtt.CallbackAPIVersion.VERSION2, client_id=clientId, clean_session=cleanSession, userdata=userdata, @@ -131,179 +118,68 @@ transport=transport, ) - self.__initCallbacks(protocol) + self.__initCallbacks() - def __initCallbacks(self, protocol): + def __initCallbacks(self): """ Private method to initialize the MQTT callback methods. - - @param protocol MQTT protocol version - @type MqttProtocols """ - if protocol in (MqttProtocols.MQTTv31, MqttProtocols.MQTTv311): - self.__mqttClient.on_connect = self.__onConnectV3 - self.__mqttClient.on_disconnect = self.__onDisconnectedV3 - self.__mqttClient.on_subscribe = self.__onSubscribeV3 - self.__mqttClient.on_unsubscribe = self.__onUnsubscribeV3 - self.__mqttClient.on_message = self.__onMessageV3 - else: - self.__mqttClient.on_connect = self.__onConnectV5 - self.__mqttClient.on_disconnect = self.__onDisconnectedV5 - self.__mqttClient.on_subscribe = self.__onSubscribeV5 - self.__mqttClient.on_unsubscribe = self.__onUnsubscribeV5 - self.__mqttClient.on_message = self.__onMessageV5 - + self.__mqttClient.on_connect = self.__onConnect + self.__mqttClient.on_disconnect = self.__onDisconnected self.__mqttClient.on_log = self.__onLog + self.__mqttClient.on_message = self.__onMessage self.__mqttClient.on_publish = self.__onPublish - - def __onConnectV3( - self, - client, # noqa: U100 - userdata, # noqa: U100 - flags, - rc, - properties=None, # noqa: U100 - ): - """ - Private method to handle the connect to the broker (MQTT v3.1 and v3.1.1). + self.__mqttClient.on_subscribe = self.__onSubscribe + self.__mqttClient.on_unsubscribe = self.__onUnsubscribe - @param client reference to the client object - @type paho.mqtt.Client - @param userdata user data - @type Any - @param flags dictionary containing the response flags sent by the broker - @type dict - @param rc result code - @type int - @param properties optional properties (defaults to None) - @type dict (optional) - """ - self.onConnectV3.emit(flags, rc) - - def __onDisconnectedV3( + def __onConnect( self, - client, # noqa: U100 - userdata, # noqa: U100 + _client, + _userdata, + connectFlags, rc, - ): - """ - Private method to handle the disconnect from the broker (MQTT v3.1 and v3.1.1). - - @param client reference to the client object - @type paho.mqtt.Client - @param userdata user data - @type Any - @param rc result code - @type int - """ - self.onDisconnectedV3.emit(rc) - - def __onSubscribeV3( - self, - client, # noqa: U100 - userdata, # noqa: U100 - mid, - grantedQos, + properties, ): """ - Private method to handle a subscribe event (MQTT v3.1 and v3.1.1). + Private method to handle the connect to the broker. - @param client reference to the client object + @param _client reference to the client object (unused) @type paho.mqtt.Client - @param userdata user data + @param _userdata user data (unused) @type Any - @param mid message ID - @type int - @param grantedQos list of granted QoS for each subscription request - @type list of int + @param connectFlags response flags sent by the broker + @type mqtt.ConnectFlags + @param rc reason code + @type paho.mqtt.ReasonCodes + @param properties MQTT v5.0 properties received from the broker + @type dict """ - self.onSubscribeV3.emit(mid, grantedQos) + self.onConnect.emit( + connectFlags.session_present, rc.value, rc.packetType, properties.json() + ) - def __onUnsubscribeV3( + def __onDisconnected( self, - client, # noqa: U100 - userdata, # noqa: U100 - mid, - ): - """ - Private method to handle an unsubscribe event (MQTT v3.1 and v3.1.1). - - @param client reference to the client object - @type paho.mqtt.Client - @param userdata user data - @type Any - @param mid message ID - @type int - """ - self.onUnsubscribeV3.emit(mid) - - def __onMessageV3( - self, - client, # noqa: U100 - userdata, # noqa: U100 - message, + _client, + _userdata, + _flags, + rc, + _properties=None, ): """ - Private method to handle a new message received from the broker (MQTT v3.1 - and v3.1.1). - - @param client reference to the client object - @type paho.mqtt.Client - @param userdata user data - @type Any - @param message received message object - @type paho.mqtt.MQTTMessage - """ - self.onMessageV3.emit( - message.topic, message.payload, message.qos, message.retain - ) + Private method to handle the disconnect from the broker. - def __onConnectV5( - self, - client, # noqa: U100 - userdata, # noqa: U100 - flags, - rc, - properties=None, - ): - """ - Private method to handle the connect to the broker (MQTT v5.0). - - @param client reference to the client object + @param _client reference to the client object (unused) @type paho.mqtt.Client - @param userdata user data + @param _userdata user data (unused) @type Any - @param flags dictionary containing the response flags sent by the broker + @param _flags dictionary containing the response flags sent by the broker + (unused) @type dict - @param rc reason code - @type paho.mqtt.ReasonCodes - @param properties optional properties (defaults to None) - @type dict (optional) - """ - self.onConnectV5.emit( - flags, - rc.value, - rc.packetType, - properties.json() if properties is not None else {}, - ) - - def __onDisconnectedV5( - self, - client, # noqa: U100 - userdata, # noqa: U100 - rc, - properties=None, # noqa: U100 - ): - """ - Private method to handle the disconnect from the broker (MQTT v5.0). - - @param client reference to the client object - @type paho.mqtt.Client - @param userdata user data - @type Any @param rc result code or reason code @type int or paho.mqtt.ReasonCodes - @param properties optional properties (defaults to None) + @param _properties MQTT v5.0 properties received from the broker + (defaults to None) (unused) @type dict (optional) """ if isinstance(rc, int): @@ -312,102 +188,98 @@ else: packetType = rc.packetType resultCode = rc.value - self.onDisconnectedV5.emit(resultCode, packetType) + self.onDisconnected.emit(resultCode, packetType) - def __onSubscribeV5( + def __onSubscribe( self, - client, # noqa: U100 - userdata, # noqa: U100 + _client, + _userdata, mid, reasonCodes, - properties=None, + properties, ): """ - Private method to handle a subscribe event (MQTT v5.0). + Private method to handle a subscribe event. - @param client reference to the client object + @param _client reference to the client object (unused) @type paho.mqtt.Client - @param userdata user data + @param _userdata user data (unused) @type Any @param mid message ID @type int @param reasonCodes list of reason code for each subscribed topic @type list of paho.mqtt.ReasonCodes - @param properties optional properties (defaults to None) - @type dict (optional) + @param properties MQTT v5.0 properties received from the broker + @type dict """ - self.onSubscribeV5.emit( - mid, - reasonCodes, - properties.json() if properties is not None else {}, - ) + self.onSubscribe.emit(mid, reasonCodes, properties.json()) - def __onUnsubscribeV5( + def __onUnsubscribe( self, - client, # noqa: U100 - userdata, # noqa: U100 + _client, + _userdata, mid, + reasonCodes, properties, - reasonCodes, ): """ - Private method to handle an unsubscribe event (MQTT v5.0). + Private method to handle an unsubscribe event. - @param client reference to the client object + @param _client reference to the client object (unused) @type paho.mqtt.Client - @param userdata user data + @param _userdata user data (unused) @type Any @param mid message ID @type int - @param properties optional properties (defaults to None) - @type dict (optional) @param reasonCodes list of reason code for each unsubscribed topic @type list of paho.mqtt.ReasonCodes + @param properties MQTT v5.0 properties received from the broker + @type dict """ - self.onUnsubscribeV5.emit( - mid, - reasonCodes.value, - reasonCodes.packetType, - properties.json() if properties is not None else {}, - ) + if reasonCodes: + self.onUnsubscribe[int, int, int, dict].emit( + mid, reasonCodes[0].value, reasonCodes[0].packetType, properties.json() + ) + else: + self.onUnsubscribe[int].emit(mid) - def __onMessageV5( + def __onMessage( self, - client, # noqa: U100 - userdata, # noqa: U100 + _client, + _userdata, message, ): """ - Private method to handle a new message received from the broker (MQTT v5.0). + Private method to handle a new message received from the broker. - @param client reference to the client object + @param _client reference to the client object (unused) @type paho.mqtt.Client - @param userdata user data + @param _userdata user data (unused) @type Any @param message received message object @type paho.mqtt.MQTTMessage """ - self.onMessageV5.emit( + self.onMessage.emit( message.topic, message.payload, message.qos, message.retain, - message.properties.json(), + message.properties.json() if message.properties is not None else {}, ) def __onLog( self, - client, # noqa: U100 - userdata, # noqa: U100 + _client, + _userdata, level, buf, ): """ - Private method to handle a log event (MQTT v3.1, v3.1.1 and v5.0). + Private method to handle a log event. - @param client reference to the client object + @param _client reference to the client object (unused) @type paho.mqtt.Client - @param userdata user data + @param _userdata user data (unused) @type Any @param level severity of the log message @type int @@ -418,20 +290,25 @@ def __onPublish( self, - client, # noqa: U100 - userdata, # noqa: U100 + _client, + _userdata, mid, + _reasonCode, + _properties, ): """ - Private method to handle the publishing of a message (MQTT v3.1, v3.1.1 - and v5.0). + Private method to handle the publishing of a message. - @param client reference to the client object + @param _client reference to the client object (unused) @type paho.mqtt.Client - @param userdata user data + @param _userdata user data (unused) @type Any @param mid message ID @type int + @param _reasonCode reason code (unused) + @type paho.mqtt.ReasonCodes + @param _properties MQTT v5.0 properties received from the broker (unused) + @type dict """ self.onPublish.emit(mid) @@ -745,7 +622,7 @@ "TlsCaCert", "TlsClientCert", "TlsClientKey", "UserProperties". @rtype dict """ - from PluginMqttMonitor import mqttPluginObject + from PluginMqttMonitor import mqttPluginObject # noqa: I102 return { "ClientId": "ERIC7_MQTT_MONITOR_CLIENT", @@ -882,43 +759,6 @@ return props -def mqttConnackMessage(connackCode): - """ - Module function to get the string associated with a CONNACK result. - - @param connackCode result code of the connection request - @type int - @return textual representation for the result code - @rtype str - """ - if connackCode == mqtt.CONNACK_ACCEPTED: - return QCoreApplication.translate("MqttConnackMessage", "Connection Accepted.") - elif connackCode == mqtt.CONNACK_REFUSED_PROTOCOL_VERSION: - return QCoreApplication.translate( - "MqttConnackMessage", "Connection Refused: unacceptable protocol version." - ) - elif connackCode == mqtt.CONNACK_REFUSED_IDENTIFIER_REJECTED: - return QCoreApplication.translate( - "MqttConnackMessage", "Connection Refused: identifier rejected." - ) - elif connackCode == mqtt.CONNACK_REFUSED_SERVER_UNAVAILABLE: - return QCoreApplication.translate( - "MqttConnackMessage", "Connection Refused: broker unavailable." - ) - elif connackCode == mqtt.CONNACK_REFUSED_BAD_USERNAME_PASSWORD: - return QCoreApplication.translate( - "MqttConnackMessage", "Connection Refused: bad user name or password." - ) - elif connackCode == mqtt.CONNACK_REFUSED_NOT_AUTHORIZED: - return QCoreApplication.translate( - "MqttConnackMessage", "Connection Refused: not authorised." - ) - else: - return QCoreApplication.translate( - "MqttConnackMessage", "Connection Refused: unknown reason." - ) - - def mqttErrorMessage(mqttErrno): """ Module function to get the error string associated with an MQTT error