U
    ÉZj“-  ã                   @   s†   d Z ddlmZ ddlmZ dd„ Zdd„ Zd	d
„ Zdddddddddejdddfdd„Z	ddddddddddejdddfdd„Z
dS )a  
This module provides some helper functions to allow straightforward subscribing
to topics and retrieving messages. The two functions are simple(), which
returns one or messages matching a set of topics, and callback() which allows
you to pass a callback for processing of messages.
é   )Úmqtté   )Úclientc                 C   s^   |dkrt  t |¡¡‚t|d tƒrF|d D ]}|  ||d ¡ q.n|  |d |d ¡ dS )úInternal callbacké    ÚtopicsÚqosN)r   ZMQTTExceptionÚpahoZconnack_stringÚ
isinstanceÚlistÚ	subscribe)r   ÚuserdataÚflagsZreason_codeÚ
propertiesZtopic© r   úV/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/paho/mqtt/subscribe.pyÚ_on_connect   s    r   c                 C   s   |d | |d |ƒ dS )r   Úcallbackr   Nr   ©r   r   Úmessager   r   r   Ú_on_message_callback&   s    r   c                 C   s„   |d dkrdS |j r"|d s"dS |d d |d< |d dkr^|d dkr^||d< |  ¡  dS |d  |¡ |d dkr€|  ¡  dS )r   Ú	msg_countr   NÚretainedr   Úmessages)ZretainZ
disconnectÚappendr   r   r   r   Ú_on_message_simple+   s    r   r   NÚ	localhosti[  Ú é<   ZtcpTc                 C   s  |dk s|dkrt dƒ‚| |||dœ}tjtjj|||||d�}| ¡  t|_t|_	|dk	rh|j
f |Ž |	rš|	 d¡}|r’|	 d¡}| ||¡ ntd	ƒ‚|dk	r®|jf |Ž |
dk	ròt|
tƒrè|
 d
d¡}|jf |
Ž |rò| |¡ n
| |
¡ | |||¡ | ¡  dS )aT  Subscribe to a list of topics and process them in a callback function.

    This function creates an MQTT client, connects to a broker and subscribes
    to a list of topics. Incoming messages are processed by the user provided
    callback.  This is a blocking function and will never return.

    :param callback: function with the same signature as `on_message` for
               processing the messages received.

    :param topics: either a string containing a single topic to subscribe to, or a
             list of topics to subscribe to.

    :param int qos: the qos to use when subscribing. This is applied to all topics.

    :param userdata: passed to the callback

    :param str hostname: the address of the broker to connect to.
               Defaults to localhost.

    :param int port: the port to connect to the broker on. Defaults to 1883.

    :param str client_id: the MQTT client id to use. If "" or None, the Paho library will
                generate a client id automatically.

    :param int keepalive: the keepalive timeout value for the client. Defaults to 60
                seconds.

    :param will: a dict containing will parameters for the client: will = {'topic':
           "<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
           Topic is required, all other parameters are optional and will
           default to None, 0 and False respectively.

           Defaults to None, which indicates no will should be used.

    :param auth: a dict containing authentication parameters for the client:
           auth = {'username':"<username>", 'password':"<password>"}
           Username is required, password is optional and will default to None
           if not provided.
           Defaults to None, which indicates no authentication is to be used.

    :param tls: a dict containing TLS configuration parameters for the client:
          dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
          'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
          'ciphers':"<ciphers">, 'insecure':"<bool>"}
          ca_certs is required, all other parameters are optional and will
          default to None if not provided, which results in the client using
          the default behaviour - see the paho.mqtt.client documentation.
          Alternatively, tls input can be an SSLContext object, which will be
          processed using the tls_set_context method.
          Defaults to None, which indicates that TLS should not be used.

    :param str transport: set to "tcp" to use the default setting of transport which is
          raw TCP. Set to "websockets" to use WebSockets as the transport.

    :param clean_session: a boolean that determines the client type. If True,
                    the broker will remove all information about this client
                    when it disconnects. If False, the client is a persistent
                    client and subscription information and queued messages
                    will be retained when the client disconnects.
                    Defaults to True.

    :param proxy_args: a dictionary that will be given to the client.
    r   r   zqos must be in the range 0-2)r   r   r   r   )Ú	client_idr   ÚprotocolÚ	transportÚclean_sessionNÚusernameÚpasswordz;The 'username' key was not found, this is required for authÚinsecureF)Ú
ValueErrorr	   ZClientZCallbackAPIVersionZVERSION2Zenable_loggerr   Z
on_messager   Z
on_connectZ	proxy_setÚgetZusername_pw_setÚKeyErrorZwill_setr
   ÚdictÚpopZtls_setZtls_insecure_setZtls_set_contextÚconnectZloop_forever)r   r   r   r   ÚhostnameÚportr   Ú	keepaliveÚwillÚauthÚtlsr    r!   r"   Ú
proxy_argsZcallback_userdatar   r#   r$   r%   r   r   r   r   A   sJ    Düú



r   c                 C   sh   |dk rt dƒ‚|dkrd}ng }|tjkr0d}|||dœ}tt| ||||||||	|
||||ƒ |d S )a�  Subscribe to a list of topics and return msg_count messages.

    This function creates an MQTT client, connects to a broker and subscribes
    to a list of topics. Once "msg_count" messages have been received, it
    disconnects cleanly from the broker and returns the messages.

    :param topics: either a string containing a single topic to subscribe to, or a
             list of topics to subscribe to.

    :param int qos: the qos to use when subscribing. This is applied to all topics.

    :param int msg_count: the number of messages to retrieve from the broker.
                if msg_count == 1 then a single MQTTMessage will be returned.
                if msg_count > 1 then a list of MQTTMessages will be returned.

    :param bool retained: If set to True, retained messages will be processed the same as
               non-retained messages. If set to False, retained messages will
               be ignored. This means that with retained=False and msg_count=1,
               the function will return the first message received that does
               not have the retained flag set.

    :param str hostname: the address of the broker to connect to.
               Defaults to localhost.

    :param int port: the port to connect to the broker on. Defaults to 1883.

    :param str client_id: the MQTT client id to use. If "" or None, the Paho library will
                generate a client id automatically.

    :param int keepalive: the keepalive timeout value for the client. Defaults to 60
                seconds.

    :param will: a dict containing will parameters for the client: will = {'topic':
           "<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
           Topic is required, all other parameters are optional and will
           default to None, 0 and False respectively.
           Defaults to None, which indicates no will should be used.

    :param auth: a dict containing authentication parameters for the client:
           auth = {'username':"<username>", 'password':"<password>"}
           Username is required, password is optional and will default to None
           if not provided.
           Defaults to None, which indicates no authentication is to be used.

    :param tls: a dict containing TLS configuration parameters for the client:
          dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
          'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
          'ciphers':"<ciphers">, 'insecure':"<bool>"}
          ca_certs is required, all other parameters are optional and will
          default to None if not provided, which results in the client using
          the default behaviour - see the paho.mqtt.client documentation.
          Alternatively, tls input can be an SSLContext object, which will be
          processed using the tls_set_context method.
          Defaults to None, which indicates that TLS should not be used.

    :param protocol: the MQTT protocol version to use. Defaults to MQTTv311.

    :param transport: set to "tcp" to use the default setting of transport which is
          raw TCP. Set to "websockets" to use WebSockets as the transport.

    :param clean_session: a boolean that determines the client type. If True,
                    the broker will remove all information about this client
                    when it disconnects. If False, the client is a persistent
                    client and subscription information and queued messages
                    will be retained when the client disconnects.
                    Defaults to True. If protocol is MQTTv50, clean_session
                    is ignored.

    :param proxy_args: a dictionary that will be given to the client.
    r   zmsg_count must be > 0N)r   r   r   r   )r&   r	   ZMQTTv5r   r   )r   r   r   r   r,   r-   r   r.   r/   r0   r1   r    r!   r"   r2   r   r   r   r   r   Úsimpleº   s(    K
       þr3   )Ú__doc__r   r   r   r	   r   r   r   ZMQTTv311r   r3   r   r   r   r   Ú<module>   s:          ý
y       ý