:py:mod:`expliot.core.protocols.internet.mqtt` ============================================== .. py:module:: expliot.core.protocols.internet.mqtt .. autoapi-nested-parse:: Wrapper for the MQTT features. Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 aws/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: expliot.core.protocols.internet.mqtt.SimpleMqttClient expliot.core.protocols.internet.mqtt.MqttClient Attributes ~~~~~~~~~~ .. autoapisummary:: expliot.core.protocols.internet.mqtt.DEFAULT_MQTT_PORT expliot.core.protocols.internet.mqtt.DEFAULT_MQTT_TIMEOUT .. py:data:: DEFAULT_MQTT_PORT :annotation: = 1883 .. py:data:: DEFAULT_MQTT_TIMEOUT :annotation: = 5 .. py:class:: SimpleMqttClient A wrapper around simple publish and subscribe methods in paho mqtt. It also few implements helper methods. .. py:method:: sub(topics, **kwargs) :staticmethod: 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/). :param topics: Either a string containing a single topic or a list containing multiple topics :param kwargs: subscribe.simple() keyword arguments :return: 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. .. py:method:: pub(topic, **kwargs) :staticmethod: 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/). :param topic: Topic to which the messahed will be published. :param kwargs: publish.single() keyword arguments :return: .. py:method:: pubmultiple(msgs, **kwargs) :staticmethod: 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/). :param msgs: List of messages to publish. Based on paho-mqtt doc, each message can either be: 1. dict: msg = {'topic':"", 'payload':"", 'qos': 2. tuple: ("", "", qos, retain) :param kwargs: publish.multiple() keyword arguments :return: .. py:method:: connauth(host, client_id=None, user=None, passwd=None, **kw) :staticmethod: Helper to check if a client can connect to a broker with specific client ID and/or credentials. :param host: Host to connect to :param client_id: Client ID to use. If not specified paho-mqtt generates a random id. :param user: User name of the client. If None or empty, connection is attempted without username and password :param passwd: Password of the client. If None, only user name is sent :param kw: Client.connect() keyword arguments (excluding host) :return: Two comma separated values. The result code and its string representation .. py:method:: _on_connauth(client, userdata, flags, return_code) :staticmethod: Callback 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(). :param client: The client instance for this callback :param userdata: The private user data as set in Client() or userdata_set() :param flags: Response flags sent by the broker :param return_code: The connection result :return: None .. py:class:: MqttClient(client_id='', clean_session=True, userdata=None, protocol=MQTTv311, transport='tcp') Bases: :py:obj:`paho.mqtt.client.Client` Wrapper on Paho MQTT Client class. For more details on the functionality check - https://github.com/eclipse/paho.mqtt.python .. py:method:: rcstr(retcode) :classmethod: Returns a string representation of the return code Args: retcode (int): MQTT Connection return code Returns: A string representation of the return code .. py:method:: 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. .. py:method:: 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. .. py:method:: 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. .. py:method:: 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.