Skip to content

Commit 8635538

Browse files
authored
Minor fixes and polish (#31)
Fixes: - on_connection_resumed callbacks had misnamed argument - GG Discovery had python-2 incompatible str->bytes conversion Polish: - add kwargs to appropriate callbacks - misc
1 parent 6208533 commit 8635538

File tree

7 files changed

+102
-51
lines changed

7 files changed

+102
-51
lines changed

awsiot/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def on_suback(suback_future):
142142
except Exception as e:
143143
future.set_exception(e)
144144

145-
def callback_wrapper(topic, payload):
145+
def callback_wrapper(topic, payload, **kwargs):
146146
try:
147147
payload_obj = json.loads(payload.decode())
148148
event = payload_to_class_fn(payload_obj)

awsiot/greengrass_discovery.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class DiscoveryClient(object):
2121
__slots__ = ['_bootstrap', '_tls_context', '_socket_options', '_region', '_tls_connection_options', '_gg_server_name', 'gg_url', 'port']
2222

2323
def __init__(self, bootstrap, socket_options, tls_context, region):
24-
assert bootstrap is not None and isinstance(bootstrap, ClientBootstrap)
25-
assert socket_options is not None and isinstance(socket_options, SocketOptions)
26-
assert tls_context is not None and isinstance(tls_context, ClientTlsContext)
27-
assert region is not None and isinstance(region, str)
24+
assert isinstance(bootstrap, ClientBootstrap)
25+
assert isinstance(socket_options, SocketOptions)
26+
assert isinstance(tls_context, ClientTlsContext)
27+
assert isinstance(region, str)
2828

2929
self._bootstrap = bootstrap
3030
self._socket_options = socket_options
@@ -44,7 +44,7 @@ def discover(self, thing_name):
4444
future=Future(),
4545
response_body=bytearray())
4646

47-
def on_incoming_body(http_stream, chunk):
47+
def on_incoming_body(http_stream, chunk, **kwargs):
4848
discovery['response_body'].extend(chunk)
4949

5050
def on_request_complete(completion_future):

samples/basic_discovery.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@
7575
exit(0)
7676

7777

78-
def on_connection_interupted(connection, error_code):
79-
print('connection interupted with error {}'.format(error_code))
78+
def on_connection_interupted(connection, error, **kwargs):
79+
print('connection interrupted with error {}'.format(error))
8080

8181

82-
def on_connection_resumed(connection, error_code, session_present):
83-
print('connection resumed with error {}, session present {}'.format(error_code, session_present))
82+
def on_connection_resumed(connection, return_code, session_present, **kwargs):
83+
print('connection resumed with return code {}, session present {}'.format(return_code, session_present))
8484

8585

8686
# Try IoT endpoints until we find one that works
@@ -90,12 +90,19 @@ def try_iot_endpoints():
9090
for connectivity_info in gg_core.connectivity:
9191
try:
9292
print('Trying core {} at host {} port {}'.format(gg_core.thing_arn, connectivity_info.host_address, connectivity_info.port))
93-
mqtt_connection = mqtt_connection_builder.mtls_from_path(endpoint=connectivity_info.host_address, port=connectivity_info.port,
94-
cert_filepath=args.certificate_path, pri_key_filepath=args.private_key_path, client_bootstrap=client_bootstrap,
95-
ca_bytes=bytes(gg_group.certificate_authorities[0], encoding='utf-8'),
96-
on_connection_interrupted=on_connection_interupted, on_connection_resumed=on_connection_resumed,
97-
client_id=args.thing_name, clean_session=False, keep_alive_secs=6)
98-
93+
mqtt_connection = mqtt_connection_builder.mtls_from_path(
94+
endpoint=connectivity_info.host_address,
95+
port=connectivity_info.port,
96+
cert_filepath=args.certificate_path,
97+
pri_key_filepath=args.private_key_path,
98+
client_bootstrap=client_bootstrap,
99+
ca_bytes=gg_group.certificate_authorities[0].encode('utf-8'),
100+
on_connection_interrupted=on_connection_interupted,
101+
on_connection_resumed=on_connection_resumed,
102+
client_id=args.thing_name,
103+
clean_session=False,
104+
keep_alive_secs=6)
105+
99106
connect_future = mqtt_connection.connect()
100107
connect_future.result()
101108
print('Connected!')
@@ -111,7 +118,7 @@ def try_iot_endpoints():
111118

112119
if args.mode == 'both' or args.mode == 'subscribe':
113120

114-
def on_publish(topic, payload):
121+
def on_publish(topic, payload, **kwargs):
115122
print('Publish received on topic {}'.format(topic))
116123
print(payload)
117124

@@ -125,8 +132,8 @@ def on_publish(topic, payload):
125132
message['message'] = args.message
126133
message['sequence'] = loop_count
127134
messageJson = json.dumps(message)
128-
pub_future = mqtt_connection.publish(args.topic, messageJson, QoS.AT_MOST_ONCE)
129-
pub_future[0].result()
135+
pub_future, _ = mqtt_connection.publish(args.topic, messageJson, QoS.AT_MOST_ONCE)
136+
pub_future.result()
130137
print('Published topic {}: {}\n'.format(args.topic, messageJson))
131138

132139
loop_count += 1

samples/jobs.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
6262
"is the region that will be used for computing the Sigv4 signature")
6363
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
64-
"you will likely need to set --root-ca to the ca for your proxy.")
64+
"you will likely need to set --root-ca to the ca for your proxy.")
6565
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
6666

6767
# Using globals to simplify sample code
@@ -244,18 +244,31 @@ def on_update_job_execution_rejected(rejected):
244244
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
245245

246246
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
247-
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(endpoint=args.endpoint,
248-
client_bootstrap=client_bootstrap, region=args.signing_region, credentials_provider=credentials_provider, websocket_proxy_options=proxy_options,
249-
ca_filepath=args.root_ca, client_id=args.client_id, clean_session=False, keep_alive_secs=6)
247+
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
248+
endpoint=args.endpoint,
249+
client_bootstrap=client_bootstrap,
250+
region=args.signing_region,
251+
credentials_provider=credentials_provider,
252+
websocket_proxy_options=proxy_options,
253+
ca_filepath=args.root_ca,
254+
client_id=args.client_id,
255+
clean_session=False,
256+
keep_alive_secs=6)
250257

251258
else:
252-
mqtt_connection = mqtt_connection_builder.mtls_from_path(endpoint=args.endpoint, cert_filepath=args.cert, pri_key_filepath=args.key,
253-
client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, client_id=args.client_id,
254-
clean_session=False, keep_alive_secs=6)
259+
mqtt_connection = mqtt_connection_builder.mtls_from_path(
260+
endpoint=args.endpoint,
261+
cert_filepath=args.cert,
262+
pri_key_filepath=args.key,
263+
client_bootstrap=client_bootstrap,
264+
ca_filepath=args.root_ca,
265+
client_id=args.client_id,
266+
clean_session=False,
267+
keep_alive_secs=6)
255268

256269
print("Connecting to {} with client ID '{}'...".format(
257270
args.endpoint, args.client_id))
258-
271+
259272
connected_future = mqtt_connection.connect()
260273

261274
jobs_client = iotjobs.IotJobsClient(mqtt_connection)

samples/pubsub.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import argparse
1717
from awscrt import io, mqtt, auth, http
1818
from awsiot import mqtt_connection_builder
19+
import sys
1920
import threading
2021
import time
2122

@@ -45,8 +46,8 @@
4546
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
4647
"is the region that will be used for computing the Sigv4 signature")
4748
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
48-
"you will likely need to set --root-ca to the ca for your proxy.")
49-
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
49+
"you will likely need to set --root-ca to the ca for your proxy.")
50+
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
5051

5152
# Using globals to simplify sample code
5253
args = parser.parse_args()
@@ -55,15 +56,15 @@
5556
received_all_event = threading.Event()
5657

5758
# Callback when connection is accidentally lost.
58-
def on_connection_interrupted(connection, error):
59-
print("Connection interrupted. error_code: {}".format(error))
59+
def on_connection_interrupted(connection, error, **kwargs):
60+
print("Connection interrupted. error: {}".format(error))
6061

6162

6263
# Callback when an interrupted connection is re-established.
63-
def on_connection_resumed(connection, error, session_present):
64-
print("Connection resumed. error_code: {} session_present: {}".format(error, session_present))
64+
def on_connection_resumed(connection, return_code, session_present, **kwargs):
65+
print("Connection resumed. return_code: {} session_present: {}".format(return_code, session_present))
6566

66-
if not error and not session_present:
67+
if return_code == mqtt.ConnectReturnCode.ACCEPTED and not session_present:
6768
print("Session did not persist. Resubscribing to existing topics...")
6869
resubscribe_future, _ = connection.resubscribe_existing_topics()
6970

@@ -78,11 +79,11 @@ def on_resubscribe_complete(resubscribe_future):
7879

7980
for topic, qos in resubscribe_results['topics']:
8081
if qos is None:
81-
exit("Server rejected resubscribe to topic: {}".format(topic))
82+
sys.exit("Server rejected resubscribe to topic: {}".format(topic))
8283

8384

8485
# Callback when the subscribed topic receives a message
85-
def on_message_received(topic, payload):
86+
def on_message_received(topic, payload, **kwargs):
8687
print("Received message from topic '{}': {}".format(topic, payload))
8788
global received_count
8889
received_count += 1
@@ -101,15 +102,31 @@ def on_message_received(topic, payload):
101102
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
102103

103104
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
104-
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(endpoint=args.endpoint,
105-
client_bootstrap=client_bootstrap, region=args.signing_region, credentials_provider=credentials_provider, websocket_proxy_options=proxy_options,
106-
ca_filepath=args.root_ca, on_connection_interrupted=on_connection_interrupted, on_connection_resumed=on_connection_resumed,
107-
client_id=args.client_id, clean_session=False, keep_alive_secs=6)
105+
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
106+
endpoint=args.endpoint,
107+
client_bootstrap=client_bootstrap,
108+
region=args.signing_region,
109+
credentials_provider=credentials_provider,
110+
websocket_proxy_options=proxy_options,
111+
ca_filepath=args.root_ca,
112+
on_connection_interrupted=on_connection_interrupted,
113+
on_connection_resumed=on_connection_resumed,
114+
client_id=args.client_id,
115+
clean_session=False,
116+
keep_alive_secs=6)
108117

109118
else:
110-
mqtt_connection = mqtt_connection_builder.mtls_from_path(endpoint=args.endpoint, cert_filepath=args.cert, pri_key_filepath=args.key,
111-
client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, on_connection_interrupted=on_connection_interrupted, on_connection_resumed=on_connection_resumed,
112-
client_id=args.client_id, clean_session=False, keep_alive_secs=6)
119+
mqtt_connection = mqtt_connection_builder.mtls_from_path(
120+
endpoint=args.endpoint,
121+
cert_filepath=args.cert,
122+
pri_key_filepath=args.key,
123+
client_bootstrap=client_bootstrap,
124+
ca_filepath=args.root_ca,
125+
on_connection_interrupted=on_connection_interrupted,
126+
on_connection_resumed=on_connection_resumed,
127+
client_id=args.client_id,
128+
clean_session=False,
129+
keep_alive_secs=6)
113130

114131
print("Connecting to {} with client ID '{}'...".format(
115132
args.endpoint, args.client_id))

samples/shadow.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
5959
"is the region that will be used for computing the Sigv4 signature")
6060
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
61-
"you will likely need to set --root-ca to the ca for your proxy.")
61+
"you will likely need to set --root-ca to the ca for your proxy.")
6262
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
6363

6464
# Using globals to simplify sample code
@@ -249,13 +249,27 @@ def user_input_thread_fn():
249249
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
250250

251251
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
252-
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(endpoint=args.endpoint,
253-
client_bootstrap=client_bootstrap, region=args.signing_region, credentials_provider=credentials_provider, websocket_proxy_options=proxy_options,
254-
ca_filepath=args.root_ca, client_id=args.client_id, clean_session=False, keep_alive_secs=6)
252+
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
253+
endpoint=args.endpoint,
254+
client_bootstrap=client_bootstrap,
255+
region=args.signing_region,
256+
credentials_provider=credentials_provider,
257+
websocket_proxy_options=proxy_options,
258+
ca_filepath=args.root_ca,
259+
client_id=args.client_id,
260+
clean_session=False,
261+
keep_alive_secs=6)
255262

256263
else:
257-
mqtt_connection = mqtt_connection_builder.mtls_from_path(endpoint=args.endpoint, cert_filepath=args.cert, pri_key_filepath=args.key,
258-
client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, client_id=args.client_id, clean_session=False, keep_alive_secs=6)
264+
mqtt_connection = mqtt_connection_builder.mtls_from_path(
265+
endpoint=args.endpoint,
266+
cert_filepath=args.cert,
267+
pri_key_filepath=args.key,
268+
client_bootstrap=client_bootstrap,
269+
ca_filepath=args.root_ca,
270+
client_id=args.client_id,
271+
clean_session=False,
272+
keep_alive_secs=6)
259273

260274
print("Connecting to {} with client ID '{}'...".format(
261275
args.endpoint, args.client_id))

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
setup(
1919
name='awsiotsdk',
20-
version='1.0.2',
20+
version='1.0.3',
2121
description='AWS IoT SDK based on the AWS Common Runtime',
2222
author='AWS SDK Common Runtime Team',
2323
url='https://github.com/aws/aws-iot-device-sdk-python-v2',
2424
packages = ['awsiot'],
2525
install_requires=[
26-
'awscrt==0.5.2',
26+
'awscrt==0.5.5',
2727
'futures;python_version<"3.2"',
2828
'typing;python_version<"3.5"',
2929
],

0 commit comments

Comments
 (0)