Skip to content

Commit 17da3af

Browse files
authored
Fix bugs in shadow sample. (#204)
- Wait until we've subscribed to all "reponse" topics before subscribing to "delta". This fixes a bug where a "delta" that fires right away, results in a "update-request" being sent, but we're not yet subscrubed to the "update-response". - Connect with clean_session=True. This avoids weird stuff where we get responses to old requests immediately upon subscribing.
1 parent 3139671 commit 17da3af

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

samples/shadow.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def user_input_thread_fn():
245245
http_proxy_options=proxy_options,
246246
ca_filepath=args.root_ca,
247247
client_id=args.client_id,
248-
clean_session=False,
248+
clean_session=True,
249249
keep_alive_secs=6)
250250

251251
else:
@@ -256,7 +256,7 @@ def user_input_thread_fn():
256256
client_bootstrap=client_bootstrap,
257257
ca_filepath=args.root_ca,
258258
client_id=args.client_id,
259-
clean_session=False,
259+
clean_session=True,
260260
keep_alive_secs=6,
261261
http_proxy_options=proxy_options)
262262

@@ -279,15 +279,6 @@ def user_input_thread_fn():
279279
# Subscribe to necessary topics.
280280
# Note that is **is** important to wait for "accepted/rejected" subscriptions
281281
# to succeed before publishing the corresponding "request".
282-
print("Subscribing to Delta events...")
283-
delta_subscribed_future, _ = shadow_client.subscribe_to_shadow_delta_updated_events(
284-
request=iotshadow.ShadowDeltaUpdatedSubscriptionRequest(thing_name=args.thing_name),
285-
qos=mqtt.QoS.AT_LEAST_ONCE,
286-
callback=on_shadow_delta_updated)
287-
288-
# Wait for subscription to succeed
289-
delta_subscribed_future.result()
290-
291282
print("Subscribing to Update responses...")
292283
update_accepted_subscribed_future, _ = shadow_client.subscribe_to_update_shadow_accepted(
293284
request=iotshadow.UpdateShadowSubscriptionRequest(thing_name=args.thing_name),
@@ -318,6 +309,15 @@ def user_input_thread_fn():
318309
get_accepted_subscribed_future.result()
319310
get_rejected_subscribed_future.result()
320311

312+
print("Subscribing to Delta events...")
313+
delta_subscribed_future, _ = shadow_client.subscribe_to_shadow_delta_updated_events(
314+
request=iotshadow.ShadowDeltaUpdatedSubscriptionRequest(thing_name=args.thing_name),
315+
qos=mqtt.QoS.AT_LEAST_ONCE,
316+
callback=on_shadow_delta_updated)
317+
318+
# Wait for subscription to succeed
319+
delta_subscribed_future.result()
320+
321321
# The rest of the sample runs asyncronously.
322322

323323
# Issue request for shadow's current state.

0 commit comments

Comments
 (0)