expliot.core.protocols.internet.mqtt

Wrapper for the MQTT features.

Submodules

Attributes

DEFAULT_MQTT_PORT

DEFAULT_MQTT_TIMEOUT

Classes

SimpleMqttClient

A wrapper around publish and subscribe methods.

MqttClient

Wrapper on Paho MQTT Client class.

Package Contents

expliot.core.protocols.internet.mqtt.DEFAULT_MQTT_PORT = 1883
expliot.core.protocols.internet.mqtt.DEFAULT_MQTT_TIMEOUT = 5
class expliot.core.protocols.internet.mqtt.SimpleMqttClient

A wrapper around publish and subscribe methods.

It also few implements helper methods.

static sub(topics, **kwargs)

Wrapper around paho-mqtt subscribe.simple() method.

For details on arguments, please refer paho/mqtt/subscribe.py in paho-mqtt project (https://pypi.org/project/paho-mqtt/).

Parameters:
  • topics – Either a string containing a single topic or a list containing multiple topics

  • kwargs – subscribe.simple() keyword arguments

Returns:

List of msg_count messages (from the topics subscribed to) received from the broker. msg_count subscribe.simple() argument is the count of messages to retrieve.

static pub(topic, **kwargs)

Wrapper around paho-mqtt publish.single() method.

For details on arguments, please refer paho/mqtt/publish.py in paho-mqtt project (https://pypi.org/project/paho-mqtt/).

Parameters:
  • topic – Topic to which the messahed will be published.

  • kwargs – publish.single() keyword arguments

Returns:

static pubmultiple(msgs, **kwargs)

Wrapper around paho-mqtt publish.multiple() method.

For details on arguments, please refer paho/mqtt/publish.py in paho-mqtt project (https://pypi.org/project/paho-mqtt/).

Parameters:
  • msgs

    List of messages to publish. Based on paho-mqtt doc,

    each message can either be:

    1. dict: msg = {‘topic’:”<topic>”, ‘payload’:”<payload>”, ‘qos’:<qos>

    2. tuple: (“<topic>”, “<payload>”, qos, retain)

  • kwargs – publish.multiple() keyword arguments

Returns:

static connauth(host, client_id=None, user=None, passwd=None, **kw)

Help to check if a client can connect to a broker.

Test for specific client ID and/or credentials.

Parameters:
  • host – Host to connect to

  • client_id – Client ID to use. If not specified paho-mqtt generates a random ID

  • user – User name of the client. If None or empty, connection is attempted without username and password

  • passwd – Password of the client. If None, only user name is sent

  • kw – Client.connect() keyword arguments (excluding host)

Returns:

Two comma separated values. The result code and its string representation

static _on_connauth(client, userdata, flags, return_code)

Execute method for paho-mqtt client.

The arguments are passed by client object. Details of the arguments are documented in paho/mqtt/client.py (https://pypi.org/project/paho-mqtt/.

This method is internally used for connauth().

Parameters:
  • client – The client instance for this callback

  • userdata – The private user data as set in Client() or userdata_set()

  • flags – Response flags sent by the broker

  • return_code – The connection result

Returns:

None

class expliot.core.protocols.internet.mqtt.MqttClient(client_id='', clean_session=True, userdata=None, protocol=MQTTv311, transport='tcp')

Bases: paho.mqtt.client.Client

Wrapper on Paho MQTT Client class.

For more details on the functionality check, see https://github.com/eclipse/paho.mqtt.python

classmethod rcstr(retcode)

Returns a string representation of the return code.

Args:

retcode (int): MQTT Connection return code

Returns:

A string representation of the return code

on_connectcb(client, userdata, flags, retcode)

Default on_connect callback.

It sets the member with connection return code and disconnects on error. Used by pub and sub plugins.

Args:
client (MqttClient) - The MQTT client object. This is not

used as it is the same as self.

userdata (caller defined): Callback specific data passed in

__init__(). This is not used as we use self members to pass information.

flags (dict): A dict that contains response flags from the broker. retcode (int): MQTT Connection return code.

Returns:

Nothing.

on_publishcb(client, userdata, mid)

Default on_publish callback.

It disconnects the connection assuming the message has been published. Don’t use this if you want to send multiple messages in single conenction. Used by pub and sub plugins

Args:
client (MqttClient): The MQTT client object. This is not

used as it is the same as self.

userdata (caller defined): Callback specific data passed in

__init__(). This is not used as we use self members to pass information.

mid (int): matches the mid variable returned from the corresponding

publish() call

Returns:

Nothing.

on_disconnectcb(client, userdata, retcode)

Default on_disconnect callback.

It sets the member with disconnection return code and disconnects on error. Used by pub and sub plugins. If the retiurn code is not zero i.e. MQTT_ERR_SUCCESS it means that this callback is called because of an unexpected disconnection from the broker. If it is zero, it is called as a result of self.disconnect() call. We call disconnect() to make sure the object stops looping in loop*() methods in the pub sub plugins.

Args:
client (MqttClient) - The MQTT client object. This is not

used as it is the same as self.

userdata (caller defined): Callback specific data passed in

__init__(). This is not used as we use self members to pass information.

retcode (int): MQTT Disconnection return code. This is not a correct

indication of the error as it returns default connection error return code.

Returns:

Nothing.

easy_config(user=None, passwd=None, on_connect=None, on_publish=None, on_subscribe=None, on_message=None, on_disconnect=None)

Easy configuration for MqttClient.

It sets the username, password and default callbacks required by the pub sub plugins.

Args:

user (str): MQTT Username passwd (str): MQTT Password on_connect (callback): On connect Callback to be set on_publish (callback): On publish Callback to be set on_subscribe (callback): On subscribe Callback to be set on_message (callback): On message Callback to be set on_disconnect (callback): On disconnect Callback to be set

Returns:

Nothing.