-
Notifications
You must be signed in to change notification settings - Fork 21
/
demo_shadow.cpp
98 lines (87 loc) · 2.75 KB
/
demo_shadow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (c) 2020-2021 Arm Limited
* SPDX-License-Identifier: Apache-2.0
*/
#if MBED_CONF_AWS_CLIENT_SHADOW
#include "mbed.h"
#include "mbed-trace/mbed_trace.h"
#include "AWSClient/AWSClient.h"
extern "C" {
#include "core_json.h"
}
#define TRACE_GROUP "Main"
// This should not be called for the Device Shadow demo -
// Device Shadow messages are internally consumed by the client
void on_message_callback(
const char *topic,
uint16_t topic_length,
const void *payload,
size_t payload_length)
{
tr_warning(
"Message received on non-shadow topic: %.*s payload: %.*s",
topic_length,
topic,
(const char *) payload_length,
payload
);
}
void demo()
{
AWSClient &client = AWSClient::getInstance();
// Download shadow document
auto ret = client.downloadShadowDocument();
if (ret == MBED_SUCCESS) {
tr_info("Device Shadow document downloaded");
} else {
tr_error("AWSClient::downloadShadowDocument() failed: %d", ret);
return; // cannot continue without downloadShadowDocument()
}
// Get a desired shadow value
const char shadow_demo_int_key[] = "DemoNumber";
const int shadow_demo_int_value = 100;
char *shadow_value;
size_t shadow_value_length;
ret = client.getShadowDesiredValue(
shadow_demo_int_key,
strlen(shadow_demo_int_key),
&shadow_value,
&shadow_value_length
);
if (ret == MBED_SUCCESS) {
tr_info("Desired value of %s: %.*s", shadow_demo_int_key, shadow_value_length, shadow_value);
} else {
tr_error(
"AWSClient::getShadowDesiredValue() failed: %d, "
"please ensure you have set a desired value for %s (e.g. using the AWS Console)",
ret,
shadow_demo_int_key
);
}
// Report a string shadow value
const char shadow_demo_string_key[] = "DemoName";
const char shadow_demo_string_value[] = "mbed-os-example-for-aws";
ret = client.publishShadowReportedValue(
shadow_demo_string_key,
strlen(shadow_demo_string_key),
shadow_demo_string_value,
strlen(shadow_demo_string_value)
);
if (ret == MBED_SUCCESS) {
tr_info("Device Shadow reported string value published");
} else {
tr_error("AWSClient::publishShadowReportedValue() failed: %d", ret);
}
// Report an integer shadow value
ret = client.publishShadowReportedValue(
shadow_demo_int_key,
strlen(shadow_demo_int_key),
shadow_demo_int_value
);
if (ret == MBED_SUCCESS) {
tr_info("Device Shadow reported integer value published");
} else {
tr_error("AWSClient::publishShadowReportedValue() failed: %d", ret);
}
}
#endif // MBED_CONF_AWS_CLIENT_SHADOW