11# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22# SPDX-License-Identifier: Apache-2.0.
33
4- from uuid import uuid4
4+ from awscrt import http
5+ from awsiot import mqtt_connection_builder
6+ from utils .command_line_utils import CommandLineUtils
57
68# This sample shows how to create a MQTT connection using a certificate file and key file.
79# This sample is intended to be used as a reference for making MQTT connections.
810
9- # Parse arguments
10- import utils .command_line_utils as command_line_utils
11- cmdUtils = command_line_utils .CommandLineUtils ("Basic Connect - Make a MQTT connection." )
12- cmdUtils .add_common_mqtt_commands ()
13- cmdUtils .add_common_proxy_commands ()
14- cmdUtils .add_common_logging_commands ()
15- cmdUtils .register_command ("key" , "<path>" , "Path to your key in PEM format." , True , str )
16- cmdUtils .register_command ("cert" , "<path>" , "Path to your client certificate in PEM format." , True , str )
17- cmdUtils .register_command ("port" , "<int>" ,
18- "Connection port for direct connection. " +
19- "AWS IoT supports 443 and 8883 (optional, default=8883)." ,
20- False , int )
21- cmdUtils .register_command ("client_id" , "<str>" ,
22- "Client ID to use for MQTT connection (optional, default='test-*')." ,
23- default = "test-" + str (uuid4 ()))
24- cmdUtils .register_command ("is_ci" , "<str>" , "If present the sample will run in CI mode (optional, default='None')" )
25- # Needs to be called so the command utils parse the commands
26- cmdUtils .get_args ()
27- is_ci = cmdUtils .get_command ("is_ci" , None ) != None
28-
2911# Callback when connection is accidentally lost.
3012def on_connection_interrupted (connection , error , ** kwargs ):
3113 print ("Connection interrupted. error: {}" .format (error ))
@@ -36,19 +18,39 @@ def on_connection_resumed(connection, return_code, session_present, **kwargs):
3618
3719
3820if __name__ == '__main__' :
39- # Create a connection using a certificate and key.
40- # Note: The data for the connection is gotten from cmdUtils.
41- # (see build_direct_mqtt_connection for implementation)
42- mqtt_connection = cmdUtils .build_direct_mqtt_connection (on_connection_interrupted , on_connection_resumed )
43-
44- if is_ci == False :
45- print ("Connecting to {} with client ID '{}'..." .format (
46- cmdUtils .get_command (cmdUtils .m_cmd_endpoint ), cmdUtils .get_command ("client_id" )))
21+
22+ # cmdData is the arguments/input from the command line placed into a single struct for
23+ # use in this sample. This handles all of the command line parsing, validating, etc.
24+ # See the Utils/CommandLineUtils for more information.
25+ cmdData = CommandLineUtils .parse_sample_input_basic_connect ()
26+
27+ # Create the proxy options if the data is present in cmdData
28+ proxy_options = None
29+ if cmdData .input_proxy_host is not None and cmdData .input_proxy_port != 0 :
30+ proxy_options = http .HttpProxyOptions (
31+ host_name = cmdData .input_proxy_host ,
32+ port = cmdData .input_proxy_port )
33+
34+ # Create a MQTT connection from the command line data
35+ mqtt_connection = mqtt_connection_builder .mtls_from_path (
36+ endpoint = cmdData .input_endpoint ,
37+ port = cmdData .input_port ,
38+ cert_filepath = cmdData .input_cert ,
39+ pri_key_filepath = cmdData .input_key ,
40+ ca_filepath = cmdData .input_ca ,
41+ on_connection_interrupted = on_connection_interrupted ,
42+ on_connection_resumed = on_connection_resumed ,
43+ client_id = cmdData .input_clientId ,
44+ clean_session = False ,
45+ keep_alive_secs = 30 ,
46+ http_proxy_options = proxy_options )
47+
48+ if not cmdData .input_is_ci :
49+ print (f"Connecting to { cmdData .input_endpoint } with client ID '{ cmdData .input_clientId } '..." )
4750 else :
4851 print ("Connecting to endpoint with client ID" )
4952
5053 connect_future = mqtt_connection .connect ()
51-
5254 # Future.result() waits until a result is available
5355 connect_future .result ()
5456 print ("Connected!" )
0 commit comments