U
    >qa%                     @   s   d Z ddlmZ ddlZzddlmZ W n  ek
rH   ddlmZ Y nX 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e
jddf
ddZdddddddddde
jddfddZdS )a  
This module provides some helper functions to allow straightforward publishing
of messages in a one-shot manner. In other words, they are useful for the
situation where you have a single/multiple messages you want to publish to a
broker, then disconnect and nothing else is required.
    )absolute_importN)Iterable   )mqtt   )clientc                 C   sH   | j  }t|tr"| jf | n"t|ttfr<| j|  ntddS )zInternal functionz&message must be a dict, tuple, or listN)Z	_userdatapopleft
isinstancedictZpublishtuplelist	TypeError)r   message r   T/var/www/html/flasktest/Project_env/lib/python3.8/site-packages/paho/mqtt/publish.py_do_publish"   s    

r   c                 C   s2   |dkrt |dkr.t|  ntt|dS zInternal callbackr   N)lenr   r   MQTTExceptionpahoZconnack_string)r   userdataflagsrcr   r   r   _on_connect/   s    
r   c                 C   s   t | ||| dS )zInternal v5 callbackN)r   )r   r   r   r   Z
propertiesr   r   r   _on_connect_v59   s    r   c                 C   s"   t |dkr|   nt|  dS r   )r   Z
disconnectr   )r   r   Zmidr   r   r   _on_publish=   s    
r   Z	localhosti[   <   Ztcpc                 C   s  t | tstdtj|t| ||	d}t|_|t	j
jkrDt|_nt|_|
dk	r^|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  Publish multiple messages to a broker, then disconnect cleanly.

    This function creates an MQTT client, connects to a broker and publishes a
    list of messages. Once the messages have been delivered, it disconnects
    cleanly from the broker.

    msgs : a list of messages to publish. Each message is either a dict or a
           tuple.

           If a dict, only the topic must be present. Default values will be
           used for any missing arguments. The dict must be of the form:

           msg = {'topic':"<topic>", 'payload':"<payload>", 'qos':<qos>,
           'retain':<retain>}
           topic must be present and may not be empty.
           If payload is "", None or not present then a zero length payload
           will be published.
           If qos is not present, the default of 0 is used.
           If retain is not present, the default of False is used.

           If a tuple, then it must be of the form:
           ("<topic>", "<payload>", qos, retain)

    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.
    proxy_args: a dictionary that will be given to the client.
    zmsgs must be an iterable)	client_idr   protocol	transportNusernamepasswordz;The 'username' key was not found, this is required for authinsecureF)r	   r   r   r   ZClientcollectionsdequer   Z
on_publishr   r   ZMQTTv5r   Z
on_connectr   Z	proxy_setgetZusername_pw_setKeyErrorZwill_setr
   popZtls_setZtls_insecure_setZtls_set_contextconnectZloop_forever)Zmsgshostnameportr   	keepalivewillauthtlsr   r    
proxy_argsr   r!   r"   r#   r   r   r   multipleG   s:    A
 



r1   Fc                 C   s0   | |||d}t |g||||||	|
||| dS )a1
  Publish a single message to a broker, then disconnect cleanly.

    This function creates an MQTT client, connects to a broker and publishes a
    single message. Once the message has been delivered, it disconnects cleanly
    from the broker.

    topic : the only required argument must be the topic string to which the
            payload will be published.

    payload : the payload to be published. If "" or None, a zero length payload
              will be published.

    qos : the qos to use when publishing,  default to 0.

    retain : set the message to be retained (True) or not (False).

    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.
          Defaults to None, which indicates that TLS should not be used.
          Alternatively, tls input can be an SSLContext object, which will be
          processed using the tls_set_context method.

    transport : set to "tcp" to use the default setting of transport which is
          raw TCP. Set to "websockets" to use WebSockets as the transport.
    proxy_args: a dictionary that will be given to the client.
    )topicpayloadqosretainN)r1   )r2   r3   r4   r5   r*   r+   r   r,   r-   r.   r/   r   r    r0   msgr   r   r   single   s    :  r7   )__doc__
__future__r   r$   collections.abcr   ImportErrorr   r   r   r   r   r   r   r   ZMQTTv311r1   r7   r   r   r   r   <module>   s>   

    
m       