U
    >qav,                     @   s   d Z ddlmZ ddlmZ ddlmZ dd 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.
    )absolute_import   )mqtt   )clientc                 C   s^   |dkrt t|t|d trF|d D ]}| ||d  q.n| |d |d  dS )Internal callbackr   topicsqosN)r   MQTTExceptionpahoZconnack_string
isinstancelist	subscribe)r   userdataflagsrcZ
propertiesZtopic r   V/var/www/html/flasktest/Project_env/lib/python3.8/site-packages/paho/mqtt/subscribe.py_on_connect_v5   s    r   c                 C   s   t | |||d dS )zInternal v5 callbackN)r   )r   r   r   r   r   r   r   _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_simple0   s    r   NZ	localhosti[   <   ZtcpTc                 C   s  |dk s|dkrt d| |||d}tj|||||d}t|_|tjjkrTt|_	nt
|_	|dk	rn|jf | |	r|	d}|r|	d}||| ntd	|dk	r|jf | |
dk	rt|
tr|
d
d}|jf |
 |r|| n
||
 |||| |  dS )a  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.

    callback : function of the form "on_message(client, userdata, message)" for
               processing the messages received.

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

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

    userdata : passed to the callback

    hostname : a string containing the address of the broker to connect to.
               Defaults to localhost.

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

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

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

    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.

    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.

    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.

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

    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.

    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Clientr   Z
on_messager   r   ZMQTTv5r   Z
on_connectr   Z	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   F   sF    C 



r   c                 C   sZ   |dk rt d|dkrd}ng }|||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.

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

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

    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.

    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.

    hostname : a string containing the address of the broker to connect to.
               Defaults to localhost.

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

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

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

    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.

    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.

    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.

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

    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.

    proxy_args: a dictionary that will be given to the client.
    r   zmsg_count must be > 0N)r   r   r   r   )r(   r   r   )r   r	   r   r   r.   r/   r!   r0   r1   r2   r3   r"   r#   r$   r4   r   r   r   r   r   simple   s$    H       r5   )__doc__
__future__r   r   r   r   r   r   r   r   r   ZMQTTv311r   r5   r   r   r   r   <module>   s>          
t       