3
3
4
4
import time
5
5
import json
6
- from concurrent .futures import Future
7
6
from awscrt import io , http
8
7
from awscrt .mqtt import QoS
9
8
from awsiot .greengrass_discovery import DiscoveryClient
10
9
from awsiot import mqtt_connection_builder
11
10
11
+ from utils .command_line_utils import CommandLineUtils
12
+
12
13
allowed_actions = ['both' , 'publish' , 'subscribe' ]
13
14
14
- # Parse arguments
15
- import utils .command_line_utils as command_line_utils
16
- cmdUtils = command_line_utils .CommandLineUtils ("Basic Discovery - Greengrass discovery example." )
17
- cmdUtils .add_common_mqtt_commands ()
18
- cmdUtils .add_common_topic_message_commands ()
19
- cmdUtils .add_common_logging_commands ()
20
- cmdUtils .register_command ("key" , "<path>" , "Path to your key in PEM format." , True , str )
21
- cmdUtils .register_command ("cert" , "<path>" , "Path to your client certificate in PEM format." , True , str )
22
- cmdUtils .remove_command ("endpoint" )
23
- cmdUtils .register_command ("thing_name" , "<str>" , "The name assigned to your IoT Thing" , required = True )
24
- cmdUtils .register_command (
25
- "mode" , "<mode>" ,
26
- f"The operation mode (optional, default='both').\n Modes:{ allowed_actions } " , default = 'both' )
27
- cmdUtils .register_command ("region" , "<str>" , "The region to connect through." , required = 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 ()
36
- # Needs to be called so the command utils parse the commands
37
- cmdUtils .get_args ()
38
-
39
- tls_options = io .TlsContextOptions .create_client_with_mtls_from_path (
40
- cmdUtils .get_command_required ("cert" ), cmdUtils .get_command_required ("key" ))
41
- if cmdUtils .get_command (cmdUtils .m_cmd_ca_file ):
42
- tls_options .override_default_trust_store_from_path (None , cmdUtils .get_command (cmdUtils .m_cmd_ca_file ))
15
+ # cmdData is the arguments/input from the command line placed into a single struct for
16
+ # use in this sample. This handles all of the command line parsing, validating, etc.
17
+ # See the Utils/CommandLineUtils for more information.
18
+ cmdData = CommandLineUtils .parse_sample_input_basic_discovery ()
19
+
20
+ tls_options = io .TlsContextOptions .create_client_with_mtls_from_path (cmdData .input_cert , cmdData .input_key )
21
+ if (cmdData .input_ca is not None ):
22
+ tls_options .override_default_trust_store_from_path (None , cmdData .input_ca )
43
23
tls_context = io .ClientTlsContext (tls_options )
44
24
45
25
socket_options = io .SocketOptions ()
46
26
47
27
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 ))
28
+ if cmdData .input_proxy_host is not None and cmdData .input_proxy_port != 0 :
29
+ proxy_options = http .HttpProxyOptions (cmdData .input_proxy_host , cmdData .input_proxy_port )
52
30
53
31
print ('Performing greengrass discovery...' )
54
32
discovery_client = DiscoveryClient (
55
33
io .ClientBootstrap .get_or_create_static_default (),
56
34
socket_options ,
57
35
tls_context ,
58
- cmdUtils . get_command_required ( "region" ) , None , proxy_options )
59
- resp_future = discovery_client .discover (cmdUtils . get_command_required ( "thing_name" ) )
36
+ cmdData . input_signing_region , None , proxy_options )
37
+ resp_future = discovery_client .discover (cmdData . input_thing_name )
60
38
discover_response = resp_future .result ()
61
39
62
40
print (discover_response )
63
- if cmdUtils . get_command ( "print_discover_resp_only" ):
41
+ if ( cmdData . input_print_discovery_resp_only ):
64
42
exit (0 )
65
43
66
44
@@ -78,16 +56,17 @@ def try_iot_endpoints():
78
56
for gg_core in gg_group .cores :
79
57
for connectivity_info in gg_core .connectivity :
80
58
try :
81
- print (f"Trying core { gg_core .thing_arn } at host { connectivity_info .host_address } port { connectivity_info .port } " )
59
+ print (
60
+ f"Trying core { gg_core .thing_arn } at host { connectivity_info .host_address } port { connectivity_info .port } " )
82
61
mqtt_connection = mqtt_connection_builder .mtls_from_path (
83
62
endpoint = connectivity_info .host_address ,
84
63
port = connectivity_info .port ,
85
- cert_filepath = cmdUtils . get_command_required ( "cert" ) ,
86
- pri_key_filepath = cmdUtils . get_command_required ( "key" ) ,
64
+ cert_filepath = cmdData . input_cert ,
65
+ pri_key_filepath = cmdData . input_key ,
87
66
ca_bytes = gg_group .certificate_authorities [0 ].encode ('utf-8' ),
88
67
on_connection_interrupted = on_connection_interupted ,
89
68
on_connection_resumed = on_connection_resumed ,
90
- client_id = cmdUtils . get_command_required ( "thing_name" ) ,
69
+ client_id = cmdData . input_clientId ,
91
70
clean_session = False ,
92
71
keep_alive_secs = 30 )
93
72
@@ -102,27 +81,26 @@ def try_iot_endpoints():
102
81
103
82
exit ('All connection attempts failed' )
104
83
105
- mqtt_connection = try_iot_endpoints ()
106
84
107
- if cmdUtils . get_command ( "mode" ) == 'both' or cmdUtils . get_command ( "mode" ) == 'subscribe' :
85
+ mqtt_connection = try_iot_endpoints ()
108
86
87
+ if cmdData .input_mode == 'both' or cmdData .input_mode == 'subscribe' :
109
88
def on_publish (topic , payload , dup , qos , retain , ** kwargs ):
110
89
print ('Publish received on topic {}' .format (topic ))
111
90
print (payload )
112
-
113
- subscribe_future , _ = mqtt_connection .subscribe (cmdUtils .get_command ("topic" ), QoS .AT_MOST_ONCE , on_publish )
91
+ subscribe_future , _ = mqtt_connection .subscribe (cmdData .input_topic , QoS .AT_MOST_ONCE , on_publish )
114
92
subscribe_result = subscribe_future .result ()
115
93
116
94
loop_count = 0
117
- while loop_count < cmdUtils . get_command ( "max_pub_ops" ) :
118
- if cmdUtils . get_command ( "mode" ) == 'both' or cmdUtils . get_command ( "mode" ) == 'publish' :
95
+ while loop_count < cmdData . input_max_pub_ops :
96
+ if cmdData . input_mode == 'both' or cmdData . input_mode == 'publish' :
119
97
message = {}
120
- message ['message' ] = cmdUtils . get_command ( "message" )
98
+ message ['message' ] = cmdData . input_message
121
99
message ['sequence' ] = loop_count
122
100
messageJson = json .dumps (message )
123
- pub_future , _ = mqtt_connection .publish (cmdUtils . get_command ( "topic" ) , messageJson , QoS .AT_MOST_ONCE )
101
+ pub_future , _ = mqtt_connection .publish (cmdData . input_topic , messageJson , QoS .AT_MOST_ONCE )
124
102
pub_future .result ()
125
- print ('Published topic {}: {}\n ' .format (cmdUtils . get_command ( "topic" ) , messageJson ))
103
+ print ('Published topic {}: {}\n ' .format (cmdData . input_topic , messageJson ))
126
104
127
105
loop_count += 1
128
106
time .sleep (1 )
0 commit comments