Thu, 30 Dec 2021 11:47:53 +0100
Updated version info to indicate relation to eric7.
<!DOCTYPE html> <html><head> <title>Plugin_Mqtt_Monitor.MqttMonitor.MqttClient</title> <meta charset="UTF-8"> <link rel="stylesheet" href="styles.css"> </head> <body> <a NAME="top" ID="top"></a> <h1>Plugin_Mqtt_Monitor.MqttMonitor.MqttClient</h1> <p> Module implementing a PyQt wrapper around the paho MQTT client. </p> <h3>Global Attributes</h3> <table> <tr><td>None</td></tr> </table> <h3>Classes</h3> <table> <tr> <td><a href="#MqttClient">MqttClient</a></td> <td>Class implementing a PyQt wrapper around the paho MQTT client.</td> </tr> </table> <h3>Functions</h3> <table> <tr> <td><a href="#mqttConnackMessage">mqttConnackMessage</a></td> <td>Module function to get the string associated with a CONNACK result.</td> </tr> <tr> <td><a href="#mqttErrorMessage">mqttErrorMessage</a></td> <td>Module function to get the error string associated with an MQTT error number.</td> </tr> <tr> <td><a href="#mqttLogLevelString">mqttLogLevelString</a></td> <td>Module function to get the log level string associated with a log level.</td> </tr> </table> <hr /> <hr /> <a NAME="MqttClient" ID="MqttClient"></a> <h2>MqttClient</h2> <p> Class implementing a PyQt wrapper around the paho MQTT client. </p> <h3>Signals</h3> <dl> <dt>connectTimeout()</dt> <dd> emitted to indicate, that a connection attempt timed out </dd> <dt>onConnectV3(flags, rc)</dt> <dd> emitted after the client has connected to the broker (MQTT v3) </dd> <dt>onConnectV5(flags, rc, packetType, properties emitted after theclient has connected to the broker (MQTT v5)</dt> <dd> </dd> <dt>onDisconnectedV3(rc)</dt> <dd> emitted after the client has disconnected from the broker (MQTT v3) </dd> <dt>onDisconnectedV5(rc, packetType)</dt> <dd> emitted after the client has disconnected from the broker (MQTT v5) </dd> <dt>onLog(level, message)</dt> <dd> emitted to send client log data </dd> <dt>onMessageV3(topic, payload, qos, retain)</dt> <dd> emitted after a message has been received by the client (MQTT v3) </dd> <dt>onMessageV5(topic, payload, qos, retain, properties)</dt> <dd> emitted after a message has been received by the client (MQTT v5) </dd> <dt>onPublish(mid)</dt> <dd> emitted after a message has been published </dd> <dt>onSubscribeV3(mid, grantedQos)</dt> <dd> emitted after the client has subscribed to some topics (MQTT v3) </dd> <dt>onSubscribeV5(mid, reasonCodes, properties)</dt> <dd> emitted after the client has subscribed to some topics (MQTT v5) </dd> <dt>onUnsubscribeV3(mid)</dt> <dd> emitted after the client has unsubscribed from some topics (MQTT v3) </dd> <dt>onUnsubscribeV5(mid, rc, packetType, properties)</dt> <dd> emitted after the client has unsubscribed from some topics (MQTT v5) </dd> </dl> <h3>Derived from</h3> QObject <h3>Class Attributes</h3> <table> <tr><td>DefaultConnectTimeout</td></tr><tr><td>LogDebug</td></tr><tr><td>LogDisabled</td></tr><tr><td>LogError</td></tr><tr><td>LogInfo</td></tr><tr><td>LogLevelMap</td></tr><tr><td>LogNotice</td></tr><tr><td>LogWarning</td></tr> </table> <h3>Class Methods</h3> <table> <tr> <td><a href="#MqttClient.defaultConnectionOptions">defaultConnectionOptions</a></td> <td>Class method to get a connection options dictionary with default values.</td> </tr> </table> <h3>Methods</h3> <table> <tr> <td><a href="#MqttClient.__init__">MqttClient</a></td> <td>Constructor</td> </tr> <tr> <td><a href="#MqttClient.__connectTimeout">__connectTimeout</a></td> <td>Private slot handling a failed connection attempt.</td> </tr> <tr> <td><a href="#MqttClient.__createPropertiesObject">__createPropertiesObject</a></td> <td>Private method to assemble the MQTT v5 properties object.</td> </tr> <tr> <td><a href="#MqttClient.__initCallbacks">__initCallbacks</a></td> <td>Private method to initialize the MQTT callback methods.</td> </tr> <tr> <td><a href="#MqttClient.__onDisconnectedV5">__onDisconnectedV5</a></td> <td>Private method to handle the disconnect from the broker.</td> </tr> <tr> <td><a href="#MqttClient.clearLastWill">clearLastWill</a></td> <td>Public method to remove a will that was previously configured with setLastWill().</td> </tr> <tr> <td><a href="#MqttClient.connectToServer">connectToServer</a></td> <td>Public method to connect to a remote MQTT broker.</td> </tr> <tr> <td><a href="#MqttClient.connectToServerWithOptions">connectToServerWithOptions</a></td> <td>Public method to connect to a remote MQTT broker.</td> </tr> <tr> <td><a href="#MqttClient.disconnectFromServer">disconnectFromServer</a></td> <td>Public method to disconnect the client from the remote broker.</td> </tr> <tr> <td><a href="#MqttClient.getProtocol">getProtocol</a></td> <td>Public method to get the MQTT protocol version.</td> </tr> <tr> <td><a href="#MqttClient.publish">publish</a></td> <td>Public method to publish to a topic.</td> </tr> <tr> <td><a href="#MqttClient.reconnectToServer">reconnectToServer</a></td> <td>Public method to reconnect the client with the same parameters.</td> </tr> <tr> <td><a href="#MqttClient.setConnectionTimeout">setConnectionTimeout</a></td> <td>Public method to set the connection timeout value.</td> </tr> <tr> <td><a href="#MqttClient.setLastWill">setLastWill</a></td> <td>Public method to set the last will of the client.</td> </tr> <tr> <td><a href="#MqttClient.setMaxInflightMessages">setMaxInflightMessages</a></td> <td>Public method to set the maximum number of messages with QoS > 0 that can be part way through their network flow at once.</td> </tr> <tr> <td><a href="#MqttClient.setMaxQueuedMessages">setMaxQueuedMessages</a></td> <td>Public method to set the maximum number of messages with QoS > 0 that can be pending in the outgoing message queue.</td> </tr> <tr> <td><a href="#MqttClient.setTLS">setTLS</a></td> <td>Public method to enable secure connections and set the TLS parameters.</td> </tr> <tr> <td><a href="#MqttClient.setUserCredentials">setUserCredentials</a></td> <td>Public method to set the user name and optionally the password.</td> </tr> <tr> <td><a href="#MqttClient.setUserData">setUserData</a></td> <td>Public method to set the user data.</td> </tr> <tr> <td><a href="#MqttClient.startLoop">startLoop</a></td> <td>Public method to start the MQTT client loop.</td> </tr> <tr> <td><a href="#MqttClient.stopLoop">stopLoop</a></td> <td>Public method to stop the MQTT client loop.</td> </tr> <tr> <td><a href="#MqttClient.subscribe">subscribe</a></td> <td>Public method to subscribe to topics with quality of service.</td> </tr> <tr> <td><a href="#MqttClient.unsubscribe">unsubscribe</a></td> <td>Public method to unsubscribe topics.</td> </tr> </table> <h3>Static Methods</h3> <table> <tr><td>None</td></tr> </table> <a NAME="MqttClient.defaultConnectionOptions" ID="MqttClient.defaultConnectionOptions"></a> <h4>MqttClient.defaultConnectionOptions (class method)</h4> <b>defaultConnectionOptions</b>(<i></i>) <p> Class method to get a connection options dictionary with default values. </p> <dl> <dt>Return:</dt> <dd> dictionary containing the default connection options. It has the keys "ClientId", "Protocol", "ConnectionTimeout", "Keepalive", "CleanSession", "Username", "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain", "WillProperties", "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey", "UserProperties". </dd> </dl> <dl> <dt>Return Type:</dt> <dd> dict </dd> </dl> <a NAME="MqttClient.__init__" ID="MqttClient.__init__"></a> <h4>MqttClient (Constructor)</h4> <b>MqttClient</b>(<i>clientId="", cleanSession=True, userdata=None, protocol=mqtt.MQTTv311, transport="tcp", parent=None</i>) <p> Constructor </p> <dl> <dt><i>clientId</i> (str)</dt> <dd> ID to be used for the client </dd> <dt><i>cleanSession</i> (bool)</dt> <dd> flag indicating to start a clean session </dd> <dt><i>userdata</i> (any)</dt> <dd> user data </dd> <dt><i>protocol</i> (int, one of mqtt.MQTTv31, mqtt.MQTTv311 or mqtt.MQTTv5)</dt> <dd> version of the MQTT protocol to use </dd> <dt><i>transport</i> (str, one of "tcp" or "websockets")</dt> <dd> transport to be used </dd> <dt><i>parent</i> (QObject)</dt> <dd> reference to the parent object </dd> </dl> <a NAME="MqttClient.__connectTimeout" ID="MqttClient.__connectTimeout"></a> <h4>MqttClient.__connectTimeout</h4> <b>__connectTimeout</b>(<i></i>) <p> Private slot handling a failed connection attempt. </p> <a NAME="MqttClient.__createPropertiesObject" ID="MqttClient.__createPropertiesObject"></a> <h4>MqttClient.__createPropertiesObject</h4> <b>__createPropertiesObject</b>(<i>packetType, properties</i>) <p> Private method to assemble the MQTT v5 properties object. </p> <dl> <dt><i>packetType</i> (PacketTypes (= int))</dt> <dd> type of the MQTT packet </dd> <dt><i>properties</i> (list of tuple of (str, str))</dt> <dd> list of user properties </dd> </dl> <dl> <dt>Return:</dt> <dd> MQTT v5 properties object </dd> </dl> <dl> <dt>Return Type:</dt> <dd> Properties </dd> </dl> <a NAME="MqttClient.__initCallbacks" ID="MqttClient.__initCallbacks"></a> <h4>MqttClient.__initCallbacks</h4> <b>__initCallbacks</b>(<i>protocol</i>) <p> Private method to initialize the MQTT callback methods. </p> <dl> <dt><i>protocol</i> (MqttProtocols)</dt> <dd> MQTT protocol version </dd> </dl> <a NAME="MqttClient.__onDisconnectedV5" ID="MqttClient.__onDisconnectedV5"></a> <h4>MqttClient.__onDisconnectedV5</h4> <b>__onDisconnectedV5</b>(<i>client, userdata, rc, properties=None</i>) <p> Private method to handle the disconnect from the broker. </p> <dl> <dt><i>client</i> (paho.mqtt.Client)</dt> <dd> reference to the client object </dd> <dt><i>userdata</i> (Any)</dt> <dd> user data </dd> <dt><i>rc</i> (int or ReasonCodes)</dt> <dd> result code or reason code </dd> <dt><i>properties</i> (dict (optional))</dt> <dd> optional properties (defaults to None) </dd> </dl> <a NAME="MqttClient.clearLastWill" ID="MqttClient.clearLastWill"></a> <h4>MqttClient.clearLastWill</h4> <b>clearLastWill</b>(<i></i>) <p> Public method to remove a will that was previously configured with setLastWill(). </p> <a NAME="MqttClient.connectToServer" ID="MqttClient.connectToServer"></a> <h4>MqttClient.connectToServer</h4> <b>connectToServer</b>(<i>host, port=1883, keepalive=60, bindAddress="", properties=None, clearWill=False</i>) <p> Public method to connect to a remote MQTT broker. </p> <dl> <dt><i>host</i> (str)</dt> <dd> host name or IP address of the remote broker </dd> <dt><i>port</i> (int)</dt> <dd> network port of the server host to connect to (default: 1883, using TLS: 8883) </dd> <dt><i>keepalive</i> (int)</dt> <dd> maximum period in seconds allowed between communications with the broker </dd> <dt><i>bindAddress</i> (str)</dt> <dd> IP address of a local network interface to bind this client to </dd> <dt><i>properties</i> (list of tuple of (str, str))</dt> <dd> list of user properties to be sent with the subscription </dd> <dt><i>clearWill</i> (bool)</dt> <dd> flag indicating to clear the last will previously set </dd> </dl> <a NAME="MqttClient.connectToServerWithOptions" ID="MqttClient.connectToServerWithOptions"></a> <h4>MqttClient.connectToServerWithOptions</h4> <b>connectToServerWithOptions</b>(<i>host, port=1883, bindAddress="", options=None, clearWill=False</i>) <p> Public method to connect to a remote MQTT broker. </p> <dl> <dt><i>host</i> (str)</dt> <dd> host name or IP address of the remote broker </dd> <dt><i>port</i> (int)</dt> <dd> network port of the server host to connect to (default: 1883, using TLS: 8883) </dd> <dt><i>bindAddress</i> (str)</dt> <dd> IP address of a local network interface to bind this client to </dd> <dt><i>options</i> (dict)</dt> <dd> dictionary containing the connection options. This dictionary should contain the keys "ClientId", "ConnectionTimeout", "Keepalive", "CleanSession", "Username", "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain", "WillProperties", "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey", "UserProperties". </dd> <dt><i>clearWill</i> (bool)</dt> <dd> flag indicating to clear the last will previously set </dd> </dl> <a NAME="MqttClient.disconnectFromServer" ID="MqttClient.disconnectFromServer"></a> <h4>MqttClient.disconnectFromServer</h4> <b>disconnectFromServer</b>(<i></i>) <p> Public method to disconnect the client from the remote broker. </p> <a NAME="MqttClient.getProtocol" ID="MqttClient.getProtocol"></a> <h4>MqttClient.getProtocol</h4> <b>getProtocol</b>(<i></i>) <p> Public method to get the MQTT protocol version. </p> <dl> <dt>Return:</dt> <dd> MQTT protocol version in use </dd> </dl> <dl> <dt>Return Type:</dt> <dd> int </dd> </dl> <a NAME="MqttClient.publish" ID="MqttClient.publish"></a> <h4>MqttClient.publish</h4> <b>publish</b>(<i>topic, payload=None, qos=0, retain=False, properties=None</i>) <p> Public method to publish to a topic. </p> <dl> <dt><i>topic</i> (str)</dt> <dd> topic to publish to </dd> <dt><i>payload</i> (str, bytes, int or float)</dt> <dd> data to be published </dd> <dt><i>qos</i> (int, one of 0, 1 or 2)</dt> <dd> quality of service </dd> <dt><i>retain</i> (bool)</dt> <dd> flag indicating to set as the "last known good"/retained message for the topic </dd> <dt><i>properties</i> (list of tuple of (str, str))</dt> <dd> list of user properties to be sent with the subscription </dd> </dl> <dl> <dt>Return:</dt> <dd> message info object </dd> </dl> <dl> <dt>Return Type:</dt> <dd> mqtt.MQTTMessageInfo </dd> </dl> <a NAME="MqttClient.reconnectToServer" ID="MqttClient.reconnectToServer"></a> <h4>MqttClient.reconnectToServer</h4> <b>reconnectToServer</b>(<i></i>) <p> Public method to reconnect the client with the same parameters. </p> <a NAME="MqttClient.setConnectionTimeout" ID="MqttClient.setConnectionTimeout"></a> <h4>MqttClient.setConnectionTimeout</h4> <b>setConnectionTimeout</b>(<i>timeout</i>) <p> Public method to set the connection timeout value. </p> <dl> <dt><i>timeout</i> (int)</dt> <dd> timeout value to be set in seconds </dd> </dl> <a NAME="MqttClient.setLastWill" ID="MqttClient.setLastWill"></a> <h4>MqttClient.setLastWill</h4> <b>setLastWill</b>(<i>topic, payload=None, qos=0, retain=False, properties=None</i>) <p> Public method to set the last will of the client. </p> <dl> <dt><i>topic</i> (str)</dt> <dd> topic the will message should be published on </dd> <dt><i>payload</i> (str, bytes, int or float)</dt> <dd> message to send as a will </dd> <dt><i>qos</i> (int, one of 0, 1 or 2)</dt> <dd> quality of service level to use for the will </dd> <dt><i>retain</i> (bool)</dt> <dd> flag indicating to set as the "last known good"/retained message for the will topic </dd> <dt><i>properties</i> (list of tuple of (str, str))</dt> <dd> list of user properties to be sent with the last will message </dd> </dl> <a NAME="MqttClient.setMaxInflightMessages" ID="MqttClient.setMaxInflightMessages"></a> <h4>MqttClient.setMaxInflightMessages</h4> <b>setMaxInflightMessages</b>(<i>inflight=20</i>) <p> Public method to set the maximum number of messages with QoS > 0 that can be part way through their network flow at once. </p> <dl> <dt><i>inflight</i> (int)</dt> <dd> maximum number of messages in flight </dd> </dl> <a NAME="MqttClient.setMaxQueuedMessages" ID="MqttClient.setMaxQueuedMessages"></a> <h4>MqttClient.setMaxQueuedMessages</h4> <b>setMaxQueuedMessages</b>(<i>queueSize=0</i>) <p> Public method to set the maximum number of messages with QoS > 0 that can be pending in the outgoing message queue. </p> <dl> <dt><i>queueSize</i> (int)</dt> <dd> maximum number of queued messages (0 = unlimited) </dd> </dl> <a NAME="MqttClient.setTLS" ID="MqttClient.setTLS"></a> <h4>MqttClient.setTLS</h4> <b>setTLS</b>(<i>caCerts=None, certFile=None, keyFile=None</i>) <p> Public method to enable secure connections and set the TLS parameters. </p> <dl> <dt><i>caCerts</i> (str)</dt> <dd> path to the Certificate Authority certificates file </dd> <dt><i>certFile</i> (str)</dt> <dd> PEM encoded client certificate file </dd> <dt><i>keyFile</i> (str)</dt> <dd> PEM encoded private key file </dd> </dl> <dl> <dt>Return:</dt> <dd> tuple containing a success flag and the error string of the paho-mqtt library </dd> </dl> <dl> <dt>Return Type:</dt> <dd> tuple of (bool, str) </dd> </dl> <a NAME="MqttClient.setUserCredentials" ID="MqttClient.setUserCredentials"></a> <h4>MqttClient.setUserCredentials</h4> <b>setUserCredentials</b>(<i>username, password=None</i>) <p> Public method to set the user name and optionally the password. </p> <dl> <dt><i>username</i> (str)</dt> <dd> user name to be set </dd> <dt><i>password</i> (str)</dt> <dd> optional password </dd> </dl> <a NAME="MqttClient.setUserData" ID="MqttClient.setUserData"></a> <h4>MqttClient.setUserData</h4> <b>setUserData</b>(<i>userdata</i>) <p> Public method to set the user data. </p> <dl> <dt><i>userdata</i> (any)</dt> <dd> user data </dd> </dl> <a NAME="MqttClient.startLoop" ID="MqttClient.startLoop"></a> <h4>MqttClient.startLoop</h4> <b>startLoop</b>(<i></i>) <p> Public method to start the MQTT client loop. </p> <a NAME="MqttClient.stopLoop" ID="MqttClient.stopLoop"></a> <h4>MqttClient.stopLoop</h4> <b>stopLoop</b>(<i></i>) <p> Public method to stop the MQTT client loop. </p> <a NAME="MqttClient.subscribe" ID="MqttClient.subscribe"></a> <h4>MqttClient.subscribe</h4> <b>subscribe</b>(<i>topic, qos=0, properties=None</i>) <p> Public method to subscribe to topics with quality of service. </p> <dl> <dt><i>topic</i> (str or tuple of (str, int) or list of tuple of (str, int))</dt> <dd> single topic to subscribe to or a tuple with a topic and a QoS or a list of tuples with a topic and a QoS each </dd> <dt><i>qos</i> (int, one of 0, 1 or 2)</dt> <dd> quality of service </dd> <dt><i>properties</i> (list of tuple of (str, str))</dt> <dd> list of user properties to be sent with the subscription </dd> </dl> <dl> <dt>Return:</dt> <dd> tuple containing the result code and the message ID </dd> </dl> <dl> <dt>Return Type:</dt> <dd> tuple of (int, int) </dd> </dl> <a NAME="MqttClient.unsubscribe" ID="MqttClient.unsubscribe"></a> <h4>MqttClient.unsubscribe</h4> <b>unsubscribe</b>(<i>topic, properties=None</i>) <p> Public method to unsubscribe topics. </p> <dl> <dt><i>topic</i> (str or list of str)</dt> <dd> topic or list of topics to unsubscribe </dd> <dt><i>properties</i> (list of tuple of (str, str))</dt> <dd> list of user properties to be sent with the subscription </dd> </dl> <dl> <dt>Return:</dt> <dd> tuple containing the result code and the message ID </dd> </dl> <dl> <dt>Return Type:</dt> <dd> tuple of (int, int) </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="mqttConnackMessage" ID="mqttConnackMessage"></a> <h2>mqttConnackMessage</h2> <b>mqttConnackMessage</b>(<i>connackCode</i>) <p> Module function to get the string associated with a CONNACK result. </p> <dl> <dt><i>connackCode</i> (int)</dt> <dd> result code of the connection request </dd> </dl> <dl> <dt>Return:</dt> <dd> textual representation for the result code </dd> </dl> <dl> <dt>Return Type:</dt> <dd> str </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="mqttErrorMessage" ID="mqttErrorMessage"></a> <h2>mqttErrorMessage</h2> <b>mqttErrorMessage</b>(<i>mqttErrno</i>) <p> Module function to get the error string associated with an MQTT error number. </p> <dl> <dt><i>mqttErrno</i> (int)</dt> <dd> result code of a MQTT request </dd> </dl> <dl> <dt>Return:</dt> <dd> textual representation of the result code </dd> </dl> <dl> <dt>Return Type:</dt> <dd> str </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="mqttLogLevelString" ID="mqttLogLevelString"></a> <h2>mqttLogLevelString</h2> <b>mqttLogLevelString</b>(<i>mqttLogLevel, isMqttLogLevel=True</i>) <p> Module function to get the log level string associated with a log level. </p> <dl> <dt><i>mqttLogLevel</i> (int)</dt> <dd> log level of the paho-mqtt client </dd> <dt><i>isMqttLogLevel</i> (bool)</dt> <dd> flag indicating a MQTT log level is given (if False it is the MqttClient variant, i.e. Debug being lowest) </dd> </dl> <dl> <dt>Return:</dt> <dd> textual representation of the log level </dd> </dl> <dl> <dt>Return Type:</dt> <dd> str </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> </body></html>