
    Vh&                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dlZd dlmZ dZdZ	 d dlZd dlmc mZ d dlmZmZ d d	lmZ d
 Zedk(  r e        yy# e$ r  e
j*                         ZdZY 9w xY w)    )absolute_importdivisionprint_functiona  
module: mqtt
short_description: Publish a message on an MQTT topic for the IoT
description:
  - Publish a message on an MQTT topic.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  server:
    type: str
    description:
      - MQTT broker address/name.
    default: localhost
  port:
    type: int
    description:
      - MQTT broker port number.
    default: 1883
  username:
    type: str
    description:
      - Username to authenticate against the broker.
  password:
    type: str
    description:
      - Password for O(username) to authenticate against the broker.
  client_id:
    type: str
    description:
      - MQTT client identifier.
      - If not specified, a value C(hostname + pid) will be used.
  topic:
    type: str
    description:
      - MQTT topic name.
    required: true
  payload:
    type: str
    description:
      - Payload. The special string V("None") may be used to send a NULL (that is, empty) payload which is useful to simply
        notify with the O(topic) or to clear previously retained messages.
    required: true
  qos:
    type: str
    description:
      - QoS (Quality of Service).
    default: "0"
    choices: ["0", "1", "2"]
  retain:
    description:
      - Setting this flag causes the broker to retain (in other words keep) the message so that applications that subsequently
        subscribe to the topic can received the last retained message immediately.
    type: bool
    default: false
  ca_cert:
    type: path
    description:
      - The path to the Certificate Authority certificate files that are to be treated as trusted by this client. If this
        is the only option given then the client will operate in a similar manner to a web browser. That is to say it will
        require the broker to have a certificate signed by the Certificate Authorities in ca_certs and will communicate using
        TLS v1, but will not attempt any form of authentication. This provides basic network encryption but may not be sufficient
        depending on how the broker is configured.
    aliases: [ca_certs]
  client_cert:
    type: path
    description:
      - The path pointing to the PEM encoded client certificate. If this is not None it will be used as client information
        for TLS based authentication. Support for this feature is broker dependent.
    aliases: [certfile]
  client_key:
    type: path
    description:
      - The path pointing to the PEM encoded client private key. If this is not None it will be used as client information
        for TLS based authentication. Support for this feature is broker dependent.
    aliases: [keyfile]
  tls_version:
    description:
      - Specifies the version of the SSL/TLS protocol to be used.
      - By default (if the python version supports it) the highest TLS version is detected. If unavailable, TLS v1 is used.
    type: str
    choices:
      - tlsv1.1
      - tlsv1.2
requirements: [mosquitto]
notes:
  - This module requires a connection to an MQTT broker such as Mosquitto U(http://mosquitto.org) and the I(Paho) C(mqtt)
    Python client (U(https://pypi.org/project/paho-mqtt/)).
author: "Jan-Piet Mens (@jpmens)"
z
- name: Publish a message on an MQTT topic
  community.general.mqtt:
    topic: 'service/ansible/{{ ansible_hostname }}'
    payload: 'Hello at {{ ansible_date_time.iso8601 }}'
    qos: 0
    retain: false
    client_id: ans001
  delegate_to: localhost
N)LooseVersionTF)AnsibleModulemissing_required_lib)	to_nativec                  t   i } 	 t         j                  | d<   	 t         j                  | d<   t	        t        t        d      t        dd      t        d	      t        d	      t        d       t        d
g d      t        dd      t        d       t        d d      t        d ddg      t        d ddg      t        d ddg      t        d ddg            d      }t        s |j                  t        d      t               |j                  j                  dd      }|j                  j                  dd      }|j                  j                  d      }|j                  j                  d      }|j                  j                  dd      }t        |j                  j                  dd             }|j                  j                  d!      }|j                  j                  d"d       }	|j                  j                  d#d       }
|j                  j                  d$d       }|j                  j                  d%d       }|j                  j                  d&d       }|j                  j                  d'd       }|+t        j                         d(t        j                          }|r|d)k(  rd }d }|	|	|
d*}d }|c|r!| j                  |t         j"                        }n9t%        t'        j(                               t%        d+      k  rt         j"                  }||||d,}	 t+        j,                  |||||||||-	       |j7                  d|/       y # t        $ r Y Hw xY w# t        $ r Y Dw xY w# t.        $ r;}|j                  d.t1        |      z  t3        j4                                Y d }~sd }~ww xY w)0Nztlsv1.2ztlsv1.1	localhost)defaulti[  int)r   typeT)required0)r   12)r   choicesFbool)r   no_logpathca_certs)r   r   aliasescertfilekeyfile)serverporttopicpayload	client_idqosretainusernamepasswordca_certclient_cert
client_keytls_version)argument_specsupports_check_modez	paho-mqtt)msg	exceptionr   r   r   r   r    r    r   r!   r"   r#   r$   r%   r&   r'   _None)r"   r#   z3.5.2)r   r   r   r'   )r    r!   r   hostnamer   authtlsz#unable to publish to MQTT broker %s)changedr   )sslPROTOCOL_TLSv1_2AttributeErrorPROTOCOL_TLSv1_1r   dictHAS_PAHOMQTT	fail_jsonr   PAHOMQTT_IMP_ERRparamsgetr   socketgetfqdnosgetpidPROTOCOL_SSLv23r   platformpython_versionmqttsingle	Exceptionr	   	traceback
format_exc	exit_json)tls_mapmoduler   r   r   r   r   r    r!   r"   r#   r   r   r   r'   r0   r1   es                     j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/mqtt.pymainrN      s\   G 11	 11	 ,d/%$'4(S/:F3$'$t4FZLITMDv	{KTIy3IJ
 !!F& 1+>JZ[]]x5F==VT*DMMg&Emm	*G!!+r2I
fmmq)
*C]]x(F}}  T2H}}  T2H}}  D1H}}  5Hmmd3G--##M48K%~~/=	7f$D$(;
C!++k33F3FGKH3356,w:OO "11 ! &	


	
" U%0{  
  d  
5	!D**, 	 	
 	

s:   M M# !M3 	M M #	M0/M03	N7<1N22N7__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr?   r3   rG   rB   Bansible_collections.community.general.plugins.module_utils.versionr   r8   r:   r=   paho.mqtt.publishrD   publishImportErrorrH   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr	   rN   __name__     rM   <module>r]      s    A @]~	 
 
   [ $$
 K Ab1J zF c  +y++-Ls   A A32A3