U
    ÉZj8-  ã                   @  s  d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZ d	d
lmZ ddlmZ e�rXzddlmZmZmZ W n( ek
rÆ   ddlmZmZmZ Y nX zddlmZ W n  ek
rø   ddlmZ Y nX G dd„ dedd�ZG dd„ dedd�ZG dd„ dedd�Ze
e ej!e"e#f Z$e	eee$f  Z%ddœdd„Z&dddœdd„Z'dddd d!d"d#œd$d%„Z(d&d'd(d)dddej)d*df
dd+dd+dd,d-d.d/d0d1d"d2œd3d4„Z*dddd&d'd(d)dddej)d*dfd+d5dd6d+dd+dd,d-d.d/d0d1d"d7œd8d9„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.
é    )ÚannotationsN)ÚIterable)ÚTYPE_CHECKINGÚAnyÚListÚTupleÚUnion)ÚCallbackAPIVersionÚMQTTProtocolVersion)Ú
Properties)Ú
ReasonCodeé   )Úmqtté   )Úclient)ÚNotRequiredÚRequiredÚ	TypedDict)ÚLiteralc                   @  s   e Zd ZU ded< ded< dS )ÚAuthParameterúRequired[str]ÚusernameúNotRequired[str]ÚpasswordN©Ú__name__Ú
__module__Ú__qualname__Ú__annotations__© r   r   úT/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/paho/mqtt/publish.pyr   /   s   
r   F)Útotalc                   @  s>   e Zd ZU ded< ded< ded< ded< ded< d	ed
< dS )ÚTLSParameterr   Úca_certsr   ÚcertfileÚkeyfileúNotRequired[int]Ztls_versionÚciphersúNotRequired[bool]ÚinsecureNr   r   r   r   r    r"   4   s   
r"   c                   @  s.   e Zd ZU ded< ded< ded< ded< d	S )
ÚMessageDictr   ÚtopiczNotRequired[paho.PayloadType]Úpayloadr&   Úqosr(   ÚretainNr   r   r   r   r    r*   =   s   
r*   zpaho.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ÚdictÚpublishÚtupleÚlistÚ	TypeError)r   Úmessager   r   r    Ú_do_publishH   s    

r7   ÚMessagesList)r   Úuserdatac                 C  s2   |dkrt |ƒdkr.t| ƒ nt t |¡¡‚dS )zInternal v5 callbackr   N)Úlenr7   r   ZMQTTExceptionÚpahoZconnack_string)r   r9   ÚflagsZreason_codeÚ
propertiesr   r   r    Ú_on_connectU   s    
r>   zcollections.deque[MessagesList]Úintr   r   ÚNone)r   r9   ÚmidÚreason_codesr=   Úreturnc                 C  s"   t |ƒdkr|  ¡  nt| ƒ dS )zInternal callbackr   N)r:   Z
disconnectr7   )r   r9   rA   rB   r=   r   r   r    Ú_on_publish^   s    
rD   Ú	localhosti[  Ú é<   ZtcpÚstrzMessageDict | NonezAuthParameter | NonezTLSParameter | Noner
   zLiteral[('tcp', 'websockets')]z
Any | None)ÚmsgsÚhostnameÚportÚ	client_idÚ	keepaliveÚwillÚauthÚtlsÚprotocolÚ	transportÚ
proxy_argsrC   c                 C  s  t | tƒstdƒ‚t| ƒdkr&tdƒ‚tjtj|t	 
| ¡||	d�}| ¡  t|_t|_|
dk	rj|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 )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.

    :param 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)

    :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 proxy_args: a dictionary that will be given to the client.
    zmsgs must be an iterabler   zmsgs is empty)rL   r9   rQ   rR   Nr   r   z;The 'username' key was not found, this is required for authr)   F)r0   r   r5   r:   Ú
ValueErrorr;   ZClientr	   ZVERSION2ÚcollectionsÚdequeZenable_loggerrD   Z
on_publishr>   Z
on_connectZ	proxy_setÚgetZusername_pw_setÚKeyErrorZwill_setr1   ÚpopZtls_setZtls_insecure_setZtls_set_contextÚconnectZloop_forever)rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   r   r   r   r)   r   r   r    Úmultiplej   sB    L
û



r[   zpaho.PayloadTypeÚbool)r+   r,   r-   r.   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rC   c                 C  s0   | |||dœ}t |g||||||	|
|||ƒ dS )aŠ
  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.

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

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

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

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

    :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:
           Username is required, password is optional and will default to None
           auth = {'username':"<username>", 'password':"<password>"}
           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.
          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.

    :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 proxy_args: a dictionary that will be given to the client.
    )r+   r,   r-   r.   N)r[   )r+   r,   r-   r.   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   Úmsgr   r   r    Úsingleç   s    H  ÿr^   ),Ú__doc__Ú
__future__r   rU   Úcollections.abcr   Útypingr   r   r   r   r   Zpaho.mqtt.enumsr	   r
   Zpaho.mqtt.propertiesr   Zpaho.mqtt.reasoncodesr   rF   r   r   r;   r   r   r   ÚImportErrorÚtyping_extensionsr   r   r"   r*   rH   ZPayloadTyper?   r\   ZMessageTupler8   r7   r>   rD   ZMQTTv311r[   r^   r   r   r   r    Ú<module>   sf   		õ&ò