Skip to content

Commit ac73022

Browse files
committed
remove utils from basic_discovery
1 parent 0750ba1 commit ac73022

File tree

1 file changed

+65
-25
lines changed

1 file changed

+65
-25
lines changed

samples/basic_discovery.py

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,81 @@
88
from awsiot.greengrass_discovery import DiscoveryClient
99
from awsiot import mqtt_connection_builder
1010

11-
from utils.command_line_utils import CommandLineUtils
11+
# from utils.command_line_utils import CommandLineUtils
1212

1313
allowed_actions = ['both', 'publish', 'subscribe']
1414

1515
# cmdData is the arguments/input from the command line placed into a single struct for
1616
# use in this sample. This handles all of the command line parsing, validating, etc.
1717
# 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').\nModes:{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)
2366
tls_context = io.ClientTlsContext(tls_options)
2467

2568
socket_options = io.SocketOptions()
2669

2770
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)
3073

3174
print('Performing greengrass discovery...')
3275
discovery_client = DiscoveryClient(
3376
io.ClientBootstrap.get_or_create_static_default(),
3477
socket_options,
3578
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)
3881
discover_response = resp_future.result()
3982

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.")
4484

45-
if (cmdData.input_print_discovery_resp_only):
85+
if (args.input_print_discovery_resp_only):
4686
exit(0)
4787

4888

@@ -65,12 +105,12 @@ def try_iot_endpoints():
65105
mqtt_connection = mqtt_connection_builder.mtls_from_path(
66106
endpoint=connectivity_info.host_address,
67107
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,
70110
ca_bytes=gg_group.certificate_authorities[0].encode('utf-8'),
71111
on_connection_interrupted=on_connection_interupted,
72112
on_connection_resumed=on_connection_resumed,
73-
client_id=cmdData.input_thing_name,
113+
client_id=args.input_thing_name,
74114
clean_session=False,
75115
keep_alive_secs=30)
76116

@@ -88,23 +128,23 @@ def try_iot_endpoints():
88128

89129
mqtt_connection = try_iot_endpoints()
90130

91-
if cmdData.input_mode == 'both' or cmdData.input_mode == 'subscribe':
131+
if args.input_mode == 'both' or args.input_mode == 'subscribe':
92132
def on_publish(topic, payload, dup, qos, retain, **kwargs):
93133
print('Publish received on topic {}'.format(topic))
94134
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)
96136
subscribe_result = subscribe_future.result()
97137

98138
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':
101141
message = {}
102-
message['message'] = cmdData.input_message
142+
message['message'] = args.input_message
103143
message['sequence'] = loop_count
104144
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)
106146
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))
108148

109149
loop_count += 1
110150
time.sleep(1)

0 commit comments

Comments
 (0)