MqttMonitor/Documentation/source/Plugin_Mqtt_Monitor.MqttMonitor.MqttClient.html

Sun, 30 May 2021 18:21:40 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 30 May 2021 18:21:40 +0200
branch
eric7
changeset 92
2fb5c08019fd
parent 87
fe391e485681
child 108
d7808a71b234
permissions
-rw-r--r--

Ported the plug-in to PyQt6 for eric7.

<!DOCTYPE html>
<html><head>
<title>Plugin_Mqtt_Monitor.MqttMonitor.MqttClient</title>
<meta charset="UTF-8">
<style>
body {
    background: #EDECE6;
    margin: 0em 1em 10em 1em;
    color: black;
}

h1 { color: white; background: #85774A; }
h2 { color: white; background: #85774A; }
h3 { color: white; background: #9D936E; }
h4 { color: white; background: #9D936E; }
    
a { color: #BA6D36; }

</style>
</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>onConnect(flags, rc)</dt>
<dd>
emitted after the client has connected to the
        broker
</dd>
<dt>onDisconnected(rc)</dt>
<dd>
emitted after the client has disconnected from
        the broker
</dd>
<dt>onLog(level, message)</dt>
<dd>
emitted to send client log data
</dd>
<dt>onMessage(topic, payload, qos, retain)</dt>
<dd>
emitted after a message has
        been received by the client
</dd>
<dt>onPublish(mid)</dt>
<dd>
emitted after a message has been published
</dd>
<dt>onSubscribe(mid, grantedQos)</dt>
<dd>
emitted after the client has
        subscribed to some topics
</dd>
<dt>onUnsubscribe(mid)</dt>
<dd>
emitted after the client has unsubscribed from
        some topics
</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>None</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.__initCallbacks">__initCallbacks</a></td>
<td>Private method to initialize the MQTT callback methods.</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.defaultConnectionOptions">defaultConnectionOptions</a></td>
<td>Public method to get a connection options dictionary with default values.</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.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.reinitialise">reinitialise</a></td>
<td>Public method to reinitialize the client with given data.</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.__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.MQTTv3, 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.__initCallbacks" ID="MqttClient.__initCallbacks"></a>
<h4>MqttClient.__initCallbacks</h4>
<b>__initCallbacks</b>(<i></i>)

<p>
        Private method to initialize the MQTT callback methods.
</p>
<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="", reinit=True</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>reinit</i> (bool)</dt>
<dd>
flag indicating to reinitialize the MQTT client before
            trying to connect with the given parameters
</dd>
</dl>
<a NAME="MqttClient.connectToServerWithOptions" ID="MqttClient.connectToServerWithOptions"></a>
<h4>MqttClient.connectToServerWithOptions</h4>
<b>connectToServerWithOptions</b>(<i>host, port=1883, bindAddress="", options=None</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", "Keepalive",
            "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
            "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", "TlsClientCert",
            "TlsClientKey", "ConnectionTimeout"
</dd>
</dl>
<a NAME="MqttClient.defaultConnectionOptions" ID="MqttClient.defaultConnectionOptions"></a>
<h4>MqttClient.defaultConnectionOptions</h4>
<b>defaultConnectionOptions</b>(<i></i>)

<p>
        Public 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", "Keepalive", "CleanSession", "Username",
            "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain",
            "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey",
            "ConnectionTimeout".
</dd>
</dl>
<dl>
<dt>Return Type:</dt>
<dd>
dict
</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.publish" ID="MqttClient.publish"></a>
<h4>MqttClient.publish</h4>
<b>publish</b>(<i>topic, payload=None, qos=0, retain=False</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>
</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.reinitialise" ID="MqttClient.reinitialise"></a>
<h4>MqttClient.reinitialise</h4>
<b>reinitialise</b>(<i>clientId="", cleanSession=True, userdata=None</i>)

<p>
        Public method to reinitialize the client with given data.
</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>
</dl>
<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</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>
</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</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>
</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</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>
</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>

eric ide

mercurial