
    >qa%                         d Z ddlmZ ddlZ	 ddlmZ n# e$ r	 ddlmZ Y nw xY wddlmZ ddlm	Z
 d	 Zd
 Zd Zd Zddddddde
j        ddf
dZdddddddddde
j        ddf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                     | j                                         }t          |t                    r | j        di | dS t          |t
          t          f          r | j        |  dS t          d          )zInternal functionz&message must be a dict, tuple, or listN )	_userdatapopleft
isinstancedictpublishtuplelist	TypeError)r   messages     E/opt/windhager/venv/lib/python3.11/site-packages/paho/mqtt/publish.py_do_publishr   "   s     &&((G'4   B!!!!!!!	GeT]	+	+ B    @AAA    c                     |dk    r&t          |          dk    rt          |            dS dS t          j        t	          j        |                    zInternal callbackr   N)lenr   r   MQTTExceptionpahoconnack_string)r   userdataflagsrcs       r   _on_connectr    /   sX     
Qwwx==1   !4R!8!8999r   c                 *    t          | |||           dS )zInternal v5 callbackN)r    )r   r   r   r   
propertiess        r   _on_connect_v5r#   9   s    %,,,,,r   c                 v    t          |          dk    r|                                  dS t          |            dS r   )r   
disconnectr   )r   r   mids      r   _on_publishr'   =   sA     8}}Fr   	localhosti[   <   tcpc                    t          | t                    st          d          t          j        |t          j        |           ||	          }t          |_        |t          j
        j        k    rt          |_        nt          |_        |
 |j        d	i |
 |rR|                    d          }|r,|                    d          }|                    ||           nt%          d          | |j        d	i | |et          |t(                    r;|                    dd          } |j        d	i | |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insecureFr
   )r   r   r   r   Clientcollectionsdequer'   
on_publishr   r   MQTTv5r#   
on_connectr    	proxy_setgetusername_pw_setKeyErrorwill_setr   poptls_settls_insecure_settls_set_contextconnectloop_forever)msgshostnameportr-   	keepalivewillauthtlsr.   r/   
proxy_argsr   r0   r1   r2   s                  r   multiplerL   G   s   B dH%% 42333 [9{7H7N7N"*iA A AF $F4;%%%*'&&:&&& 088J'' 	0xx
++H""8X6666 / 0 0 0 $
c4   		(wwz511HFN!!S!!! 2 ''111 ""3'''
NN8T9---
r   Fc                 H    | |||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)rL   )rN   rO   rP   rQ   rE   rF   r-   rG   rH   rI   rJ   r.   r/   rK   msgs                  r   singlerS      sK    t G3
H
HCcUHdIy$cy*. . . . .r   )__doc__
__future__r   r4   collections.abcr   ImportErrorr)   r   r   r   r   r    r#   r'   MQTTv311rL   rS   r
   r   r   <module>rY      sT    ' & & & & &    %((((((( % % %$$$$$$$$%            
B 
B 
B: : :- - -   (dbBTtdmj j j jZ Aekbt$dmu=. =. =. =. =. =.s    ##