
    (\i-                         d Z ddlmZ ddlmZ d Zd Zd Zdd	d
dddd	d	d	ej                  ddd	fdZ	dddd
dddd	d	d	ej                  ddd	fdZ
y	)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                     |dk7  r(t        j                  t        j                  |            t	        |d   t
              r |d   D ]  }| j                  ||d           y| j                  |d   |d          y)Internal callback    topicsqosN)r   MQTTExceptionpahoconnack_string
isinstancelist	subscribe)r   userdataflagsreason_code
propertiestopics         c/home/devuser/fomin/mqttuser/mqtt_project/myenv/lib/python3.12/site-packages/paho/mqtt/subscribe.py_on_connectr      sx    a  !4!4[!ABB(8$d+h' 	5EUHUO4	5 	(+Xe_=    c                 $     |d   | |d   |       y)r   callbackr   N r   r   messages      r   _on_message_callbackr   &   s    HZ*!5w?r   c                     |d   dk(  ry|j                   r|d   sy|d   dz
  |d<   |d   |d   dk(  r||d<   | j                          y|d   j                  |       |d   dk(  r| j                          yy)r   	msg_countr   Nretainedr   messages)retain
disconnectappendr   s      r   _on_message_simpler&   +   s     ! ~~hz2$[1A5H[
#(=(B&Z(! "r   r   N	localhosti[   <   tcpTc                    |dk  s|dkD  rt        d      | |||d}t        j                  t        j                  j                  |||||      }|j                          t        |_        t        |_	        | |j                  di | |	rB|	j                  d      }|r$|	j                  d      }|j                  ||       nt        d	      | |j                  di | |
Yt        |
t               r8|
j#                  d
d      } |j$                  di |
 |r#|j'                  |       n|j)                  |
       |j+                  |||       |j-                          y)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insecureFr   )
ValueErrorr   ClientCallbackAPIVersionVERSION2enable_loggerr   
on_messager   
on_connect	proxy_setgetusername_pw_setKeyErrorwill_setr   dictpoptls_settls_insecure_settls_set_contextconnectloop_forever)r   r	   r
   r   hostnameportr,   	keepalivewillauthtlsr-   r.   r/   
proxy_argscallback_userdatar   r0   r1   r2   s                       r   r   r   A   se   H Qw#'788 	 [[(("#F ,F#F&:&88J'xx
+H""8X6 / 0 0 $
c4 wwz51HFNN!S! ''1 ""3'
NN8T9-
r   c                     |dk  rt        d      |dk(  rd}ng }|t        j                  k(  rd}|||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"   )r3   r   MQTTv5r   r&   )r	   r
   r    r!   rF   rG   r,   rH   rI   rJ   rK   r-   r.   r/   rL   r"   r   s                    r   simplerP      s    V 1}011 A~ 4;;#xPHh$	4sHiJ( Jr   )__doc__r(   r   r   r   r   r   r&   MQTTv311r   rP   r   r   r   <module>rS      sy     	>@
, $%tk"DDvr A{bt$dmmu$_ r   