Skip to content

Updated Shadow sample to allow clearing properties by passing null #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions samples/shadow/shadow_sync/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,25 @@ static void s_changeShadowValue(

ShadowState state;
JsonObject desired;
desired.WithString(shadowProperty, value);
JsonObject reported;
reported.WithString(shadowProperty, value);

if (value == "null")
{
JsonObject nullObject;
nullObject.AsNull();
desired.WithObject(shadowProperty, nullObject);
reported.WithObject(shadowProperty, nullObject);
}
else if (value == "clear_shadow")
{
desired.AsNull();
reported.AsNull();
}
else
{
desired.WithString(shadowProperty, value);
reported.WithString(shadowProperty, value);
}
state.Desired = desired;
state.Reported = reported;

Expand Down Expand Up @@ -288,7 +304,6 @@ int main(int argc, char *argv[])
if (event->State && event->State->View().ValueExists(shadowProperty))
{
JsonView objectView = event->State->View().GetJsonObject(shadowProperty);

if (objectView.IsNull())
{
fprintf(
Expand Down Expand Up @@ -324,10 +339,17 @@ int main(int argc, char *argv[])
auto onUpdateShadowAccepted = [&](UpdateShadowResponse *response, int ioErr) {
if (ioErr == AWS_OP_SUCCESS)
{
fprintf(
stdout,
"Finished updating reported shadow value to %s.\n",
response->State->Reported->View().GetString(shadowProperty).c_str());
if (response->State->Reported)
{
fprintf(
stdout,
"Finished updating reported shadow value to %s.\n",
response->State->Reported->View().GetString(shadowProperty).c_str());
}
else
{
fprintf(stdout, "Finished clearing shadow properties\n");
}
fprintf(stdout, "Enter desired value:\n");
}
else
Expand Down