8
8
from awsiot .greengrass_discovery import DiscoveryClient
9
9
from awsiot import mqtt_connection_builder
10
10
11
- from utils .command_line_utils import CommandLineUtils
11
+ # from utils.command_line_utils import CommandLineUtils
12
12
13
13
allowed_actions = ['both' , 'publish' , 'subscribe' ]
14
14
15
15
# cmdData is the arguments/input from the command line placed into a single struct for
16
16
# use in this sample. This handles all of the command line parsing, validating, etc.
17
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 )
18
+ # cmdData = CommandLineUtils.parse_sample_input_basic_discovery()
19
+
20
+ # --------------------------------- ARGUMENT PARSING -----------------------------------------
21
+ import argparse , uuid
22
+
23
+ def parse_sample_input ():
24
+ parser = argparse .ArgumentParser (
25
+ description = "MQTT5 pub/sub sample (mTLS)." ,
26
+ formatter_class = argparse .ArgumentDefaultsHelpFormatter ,
27
+ )
28
+
29
+ parser .add_argument ("--cert" , required = True , dest = "input_cert" ,
30
+ help = "Path to the certificate file to use during mTLS connection establishment" )
31
+ parser .add_argument ("--key" , required = True , dest = "input_key" ,
32
+ help = "Path to the private key file to use during mTLS connection establishment" )
33
+ parser .add_argument ("--ca_file" , dest = "input_ca" , help = "Path to optional CA bundle (PEM)" )
34
+
35
+ # Messaging
36
+ parser .add_argument ("--topic" , default = f"test/topic/{ uuid .uuid4 ().hex [:8 ]} " , dest = "input_topic" , help = "Topic" )
37
+ parser .add_argument ("--message" , default = "Hello World!" , dest = "input_message" , help = "Message payload" )
38
+ parser .add_argument ("--thing_name" , required = True , dest = "input_thing_name" , help = "The name assigned to your IoT Thing." )
39
+ parser .add_argument ("--region" , required = True , dest = "input_signing_region" , help = "The region to connect through." )
40
+ parser .add_argument ("--max_pub_ops" , type = int , default = 10 , dest = "input_max_pub_ops" ,
41
+ help = "The maximum number of publish operations (optional, default='10')." )
42
+ parser .add_argument ("--print_discover_resp_only" , type = bool , default = False , dest = "input_print_discovery_resp_only" ,
43
+ help = "(optional, default='False')." )
44
+ parser .add_argument ("--mode" , default = 'both' , dest = "input_mode" ,
45
+ help = f"The operation mode (optional, default='both').\n Modes:{ allowed_actions } " )
46
+
47
+ # Proxy (optional)
48
+ parser .add_argument ("--proxy-host" , dest = "input_proxy_host" , help = "HTTP proxy host" )
49
+ parser .add_argument ("--proxy-port" , type = int , default = 0 , dest = "input_proxy_port" , help = "HTTP proxy port" )
50
+
51
+ # Misc
52
+ parser .add_argument ("--client-id" , dest = "input_clientId" ,
53
+ default = f"mqtt5-sample-{ uuid .uuid4 ().hex [:8 ]} " , help = "Client ID" )
54
+
55
+ return parser .parse_args ()
56
+
57
+ args = parse_sample_input ()
58
+
59
+ # --------------------------------- ARGUMENT PARSING END -----------------------------------------
60
+
61
+ # [--mode <mode>]
62
+
63
+ tls_options = io .TlsContextOptions .create_client_with_mtls_from_path (args .input_cert , args .input_key )
64
+ if (args .input_ca is not None ):
65
+ tls_options .override_default_trust_store_from_path (None , args .input_ca )
23
66
tls_context = io .ClientTlsContext (tls_options )
24
67
25
68
socket_options = io .SocketOptions ()
26
69
27
70
proxy_options = None
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 )
71
+ if args .input_proxy_host is not None and args .input_proxy_port != 0 :
72
+ proxy_options = http .HttpProxyOptions (args .input_proxy_host , args .input_proxy_port )
30
73
31
74
print ('Performing greengrass discovery...' )
32
75
discovery_client = DiscoveryClient (
33
76
io .ClientBootstrap .get_or_create_static_default (),
34
77
socket_options ,
35
78
tls_context ,
36
- cmdData .input_signing_region , None , proxy_options )
37
- resp_future = discovery_client .discover (cmdData .input_thing_name )
79
+ args .input_signing_region , None , proxy_options )
80
+ resp_future = discovery_client .discover (args .input_thing_name )
38
81
discover_response = resp_future .result ()
39
82
40
- if (cmdData .input_is_ci ):
41
- print ("Received a greengrass discovery result! Not showing result in CI for possible data sensitivity." )
42
- else :
43
- print (discover_response )
83
+ print ("Received a greengrass discovery result! Not showing result for possible data sensitivity." )
44
84
45
- if (cmdData .input_print_discovery_resp_only ):
85
+ if (args .input_print_discovery_resp_only ):
46
86
exit (0 )
47
87
48
88
@@ -65,12 +105,12 @@ def try_iot_endpoints():
65
105
mqtt_connection = mqtt_connection_builder .mtls_from_path (
66
106
endpoint = connectivity_info .host_address ,
67
107
port = connectivity_info .port ,
68
- cert_filepath = cmdData .input_cert ,
69
- pri_key_filepath = cmdData .input_key ,
108
+ cert_filepath = args .input_cert ,
109
+ pri_key_filepath = args .input_key ,
70
110
ca_bytes = gg_group .certificate_authorities [0 ].encode ('utf-8' ),
71
111
on_connection_interrupted = on_connection_interupted ,
72
112
on_connection_resumed = on_connection_resumed ,
73
- client_id = cmdData .input_thing_name ,
113
+ client_id = args .input_thing_name ,
74
114
clean_session = False ,
75
115
keep_alive_secs = 30 )
76
116
@@ -88,23 +128,23 @@ def try_iot_endpoints():
88
128
89
129
mqtt_connection = try_iot_endpoints ()
90
130
91
- if cmdData .input_mode == 'both' or cmdData .input_mode == 'subscribe' :
131
+ if args .input_mode == 'both' or args .input_mode == 'subscribe' :
92
132
def on_publish (topic , payload , dup , qos , retain , ** kwargs ):
93
133
print ('Publish received on topic {}' .format (topic ))
94
134
print (payload )
95
- subscribe_future , _ = mqtt_connection .subscribe (cmdData .input_topic , QoS .AT_MOST_ONCE , on_publish )
135
+ subscribe_future , _ = mqtt_connection .subscribe (args .input_topic , QoS .AT_MOST_ONCE , on_publish )
96
136
subscribe_result = subscribe_future .result ()
97
137
98
138
loop_count = 0
99
- while loop_count < cmdData .input_max_pub_ops :
100
- if cmdData .input_mode == 'both' or cmdData .input_mode == 'publish' :
139
+ while loop_count < args .input_max_pub_ops :
140
+ if args .input_mode == 'both' or args .input_mode == 'publish' :
101
141
message = {}
102
- message ['message' ] = cmdData .input_message
142
+ message ['message' ] = args .input_message
103
143
message ['sequence' ] = loop_count
104
144
messageJson = json .dumps (message )
105
- pub_future , _ = mqtt_connection .publish (cmdData .input_topic , messageJson , QoS .AT_LEAST_ONCE )
145
+ pub_future , _ = mqtt_connection .publish (args .input_topic , messageJson , QoS .AT_LEAST_ONCE )
106
146
publish_completion_data = pub_future .result ()
107
- print ('Successfully published to topic {} with payload `{}`\n ' .format (cmdData .input_topic , messageJson ))
147
+ print ('Successfully published to topic {} with payload `{}`\n ' .format (args .input_topic , messageJson ))
108
148
109
149
loop_count += 1
110
150
time .sleep (1 )
0 commit comments