From 7a6ef49e58f4ecbc03535e63a8a04e39806ccb9f Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Thu, 27 May 2021 16:06:31 -0700 Subject: [PATCH] Fix bugs in shadow sample. - 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. --- samples/shadow.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/samples/shadow.py b/samples/shadow.py index 2d61e8d2..6540ad99 100644 --- a/samples/shadow.py +++ b/samples/shadow.py @@ -245,7 +245,7 @@ def user_input_thread_fn(): http_proxy_options=proxy_options, ca_filepath=args.root_ca, client_id=args.client_id, - clean_session=False, + clean_session=True, keep_alive_secs=6) else: @@ -256,7 +256,7 @@ def user_input_thread_fn(): client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, client_id=args.client_id, - clean_session=False, + clean_session=True, keep_alive_secs=6, http_proxy_options=proxy_options) @@ -279,15 +279,6 @@ def user_input_thread_fn(): # Subscribe to necessary topics. # Note that is **is** important to wait for "accepted/rejected" subscriptions # to succeed before publishing the corresponding "request". - print("Subscribing to Delta events...") - delta_subscribed_future, _ = shadow_client.subscribe_to_shadow_delta_updated_events( - request=iotshadow.ShadowDeltaUpdatedSubscriptionRequest(thing_name=args.thing_name), - qos=mqtt.QoS.AT_LEAST_ONCE, - callback=on_shadow_delta_updated) - - # Wait for subscription to succeed - delta_subscribed_future.result() - print("Subscribing to Update responses...") update_accepted_subscribed_future, _ = shadow_client.subscribe_to_update_shadow_accepted( request=iotshadow.UpdateShadowSubscriptionRequest(thing_name=args.thing_name), @@ -318,6 +309,15 @@ def user_input_thread_fn(): get_accepted_subscribed_future.result() get_rejected_subscribed_future.result() + print("Subscribing to Delta events...") + delta_subscribed_future, _ = shadow_client.subscribe_to_shadow_delta_updated_events( + request=iotshadow.ShadowDeltaUpdatedSubscriptionRequest(thing_name=args.thing_name), + qos=mqtt.QoS.AT_LEAST_ONCE, + callback=on_shadow_delta_updated) + + # Wait for subscription to succeed + delta_subscribed_future.result() + # The rest of the sample runs asyncronously. # Issue request for shadow's current state.