1
1
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
# SPDX-License-Identifier: Apache-2.0.
3
3
4
+ import command_line_utils
4
5
import time
5
6
import json
6
7
from concurrent .futures import Future
7
- from awscrt import io
8
+ from awscrt import io , http
8
9
from awscrt .mqtt import QoS
9
10
from awsiot .greengrass_discovery import DiscoveryClient
10
11
from awsiot import mqtt_connection_builder
11
12
12
13
allowed_actions = ['both' , 'publish' , 'subscribe' ]
13
14
14
15
# Parse arguments
15
- import command_line_utils ;
16
16
cmdUtils = command_line_utils .CommandLineUtils ("Basic Discovery - Greengrass discovery example." )
17
17
cmdUtils .add_common_mqtt_commands ()
18
18
cmdUtils .add_common_topic_message_commands ()
21
21
cmdUtils .register_command ("cert" , "<path>" , "Path to your client certificate in PEM format." , True , str )
22
22
cmdUtils .remove_command ("endpoint" )
23
23
cmdUtils .register_command ("thing_name" , "<str>" , "The name assigned to your IoT Thing" , required = True )
24
- cmdUtils .register_command ("mode" , "<mode>" , "The operation mode (optional, default='both').\n Modes:%s" % str (allowed_actions ), default = 'both' )
24
+ cmdUtils .register_command (
25
+ "mode" , "<mode>" ,
26
+ f"The operation mode (optional, default='both').\n Modes:{ allowed_actions } " , default = 'both' )
25
27
cmdUtils .register_command ("region" , "<str>" , "The region to connect through." , required = True )
26
- cmdUtils .register_command ("max_pub_ops" , "<int>" , "The maximum number of publish operations (optional, default='10')." , default = 10 , type = int )
27
- cmdUtils .register_command ("print_discover_resp_only" , "" , "(optional, default='False')." , default = False , type = bool , action = "store_true" )
28
+ cmdUtils .register_command (
29
+ "max_pub_ops" , "<int>" ,
30
+ "The maximum number of publish operations (optional, default='10')." ,
31
+ default = 10 , type = int )
32
+ cmdUtils .register_command (
33
+ "print_discover_resp_only" , "" , "(optional, default='False')." ,
34
+ default = False , type = bool , action = "store_true" )
35
+ cmdUtils .add_common_proxy_commands ()
28
36
# Needs to be called so the command utils parse the commands
29
37
cmdUtils .get_args ()
30
38
31
- tls_options = io .TlsContextOptions .create_client_with_mtls_from_path (cmdUtils .get_command_required ("cert" ), cmdUtils .get_command_required ("key" ))
39
+ tls_options = io .TlsContextOptions .create_client_with_mtls_from_path (
40
+ cmdUtils .get_command_required ("cert" ), cmdUtils .get_command_required ("key" ))
32
41
if cmdUtils .get_command (cmdUtils .m_cmd_ca_file ):
33
42
tls_options .override_default_trust_store_from_path (None , cmdUtils .get_command (cmdUtils .m_cmd_ca_file ))
34
43
tls_context = io .ClientTlsContext (tls_options )
35
44
36
45
socket_options = io .SocketOptions ()
37
46
47
+ proxy_options = None
48
+ if cmdUtils .get_command (cmdUtils .m_cmd_proxy_host ) != None and cmdUtils .get_command (cmdUtils .m_cmd_proxy_port ) != None :
49
+ proxy_options = http .HttpProxyOptions (
50
+ cmdUtils .get_command_required (cmdUtils .m_cmd_proxy_host ),
51
+ cmdUtils .get_command_required (cmdUtils .m_cmd_proxy_port ))
52
+
38
53
print ('Performing greengrass discovery...' )
39
- discovery_client = DiscoveryClient (io .ClientBootstrap .get_or_create_static_default (), socket_options , tls_context , cmdUtils .get_command_required ("region" ))
54
+ discovery_client = DiscoveryClient (
55
+ io .ClientBootstrap .get_or_create_static_default (),
56
+ socket_options ,
57
+ tls_context ,
58
+ cmdUtils .get_command_required ("region" ), None , proxy_options )
40
59
resp_future = discovery_client .discover (cmdUtils .get_command_required ("thing_name" ))
41
60
discover_response = resp_future .result ()
42
61
@@ -59,7 +78,7 @@ def try_iot_endpoints():
59
78
for gg_core in gg_group .cores :
60
79
for connectivity_info in gg_core .connectivity :
61
80
try :
62
- print ( ' Trying core {} at host {} port {}' . format ( gg_core . thing_arn , connectivity_info .host_address , connectivity_info . port ) )
81
+ print ( f" Trying core { gg_core . thing_arn } at host { connectivity_info . host_address } port { connectivity_info .port } " )
63
82
mqtt_connection = mqtt_connection_builder .mtls_from_path (
64
83
endpoint = connectivity_info .host_address ,
65
84
port = connectivity_info .port ,
0 commit comments