forked from aws/aws-iot-device-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShadowDelta.cpp
373 lines (324 loc) · 14.4 KB
/
ShadowDelta.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/**
* @file ShadowDelta.cpp
* @brief Sample demonstrating Shadow operations
*
*/
#include <chrono>
#include <cstring>
#ifdef USE_WEBSOCKETS
#include "WebSocketConnection.hpp"
#elif defined USE_MBEDTLS
#include "MbedTLSConnection.hpp"
#else
#include "OpenSSLConnection.hpp"
#endif
#include "util/logging/Logging.hpp"
#include "util/logging/LogMacros.hpp"
#include "util/logging/ConsoleLogSystem.hpp"
#include "ConfigCommon.hpp"
#include "ShadowDelta.hpp"
#define LOG_TAG_SHADOW_DELTA "[Sample - ShadowDelta]"
#define SDK_SAMPLE_TOPIC "Pub_Sub_Sample_Topic"
#define MESSAGE_COUNT 10
#define SHADOW_DOCUMENT_STATE_KEY "state"
#define SHADOW_DOCUMENT_REPORTED_KEY "reported"
#define SHADOW_DOCUMENT_DESIRED_KEY "desired"
#define SHADOW_DOCUMENT_VERSION_KEY "version"
#define SHADOW_DOCUMENT_TIMESTAMP_KEY "timestamp"
#define MSG_COUNT_KEY "cur_msg_count"
#define SHADOW_TOPIC_PREFIX "$aws/things/"
#define SHADOW_TOPIC_MIDDLE "/shadow/"
#define SHADOW_REQUEST_TYPE_UPDATE_STRING "update"
#define SHADOW_DOCUMENT_EMPTY_STRING "{" \
" \"state\" : {" \
" \"desired\" : {" \
" \"cur_msg_count\" : 0" \
" }," \
" \"reported\" : {" \
" \"cur_msg_count\" : 0" \
" }" \
" }" \
"}"
namespace awsiotsdk {
namespace samples {
ResponseCode ShadowDelta::InitializeTLS() {
ResponseCode rc = ResponseCode::SUCCESS;
#ifdef USE_WEBSOCKETS
p_network_connection_ = std::shared_ptr<NetworkConnection>(
new network::WebSocketConnection(ConfigCommon::endpoint_, ConfigCommon::endpoint_https_port_,
ConfigCommon::root_ca_path_, ConfigCommon::aws_region_,
ConfigCommon::aws_access_key_id_,
ConfigCommon::aws_secret_access_key_,
ConfigCommon::aws_session_token_,
ConfigCommon::tls_handshake_timeout_,
ConfigCommon::tls_read_timeout_,
ConfigCommon::tls_write_timeout_, true));
if(nullptr == p_network_connection_) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Failed to initialize Network Connection with rc : %d", static_cast<int>(rc));
rc = ResponseCode::FAILURE;
}
#elif defined USE_MBEDTLS
p_network_connection_ = std::make_shared<network::MbedTLSConnection>(ConfigCommon::endpoint_,
ConfigCommon::endpoint_port_,
ConfigCommon::root_ca_path_,
ConfigCommon::client_cert_path_,
ConfigCommon::client_key_path_,
ConfigCommon::tls_handshake_timeout_,
ConfigCommon::tls_read_timeout_,
ConfigCommon::tls_write_timeout_, true);
if(nullptr == p_network_connection_) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Failed to initialize Network Connection with rc : %d", static_cast<int>(rc));
rc = ResponseCode::FAILURE;
}
#else
std::shared_ptr<network::OpenSSLConnection> p_network_connection =
std::make_shared<network::OpenSSLConnection>(ConfigCommon::endpoint_, ConfigCommon::endpoint_port_,
ConfigCommon::root_ca_path_,
ConfigCommon::client_cert_path_,
ConfigCommon::client_key_path_,
ConfigCommon::tls_handshake_timeout_,
ConfigCommon::tls_read_timeout_,
ConfigCommon::tls_write_timeout_, true);
rc = p_network_connection->Initialize();
if(ResponseCode::SUCCESS != rc) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Failed to initialize Network Connection with rc : %d", static_cast<int>(rc));
rc = ResponseCode::FAILURE;
} else {
p_network_connection_ = std::dynamic_pointer_cast<NetworkConnection>(p_network_connection);
}
#endif
return rc;
}
ResponseCode ShadowDelta::ActionResponseHandler(util::String thing_name, ShadowRequestType request_type,
ShadowResponseType response_type, util::JsonDocument &payload) {
switch(response_type) {
case ShadowResponseType::Accepted:
sync_action_response_ = ResponseCode::SHADOW_REQUEST_ACCEPTED;
break;
case ShadowResponseType::Rejected:
sync_action_response_ = ResponseCode::SHADOW_REQUEST_REJECTED;
break;
case ShadowResponseType::Delta:
sync_action_response_ = ResponseCode::SHADOW_RECEIVED_DELTA;
break;
}
sync_action_response_wait_.notify_all();
return sync_action_response_;
}
ResponseCode ShadowDelta::RunSample() {
total_published_messages_ = 0;
cur_pending_messages_ = 0;
ResponseCode rc = InitializeTLS();
if(ResponseCode::SUCCESS != rc) {
return rc;
}
p_iot_client_ = std::shared_ptr<MqttClient>(MqttClient::Create(p_network_connection_, ConfigCommon::mqtt_command_timeout_));
if(nullptr == p_iot_client_) {
return ResponseCode::FAILURE;
}
util::String client_id_tagged = ConfigCommon::base_client_id_;
client_id_tagged.append("_shadow_delta_tester_");
client_id_tagged.append(std::to_string(rand()));
std::unique_ptr<Utf8String> client_id = Utf8String::Create(client_id_tagged);
rc = p_iot_client_->Connect(ConfigCommon::mqtt_command_timeout_, ConfigCommon::is_clean_session_,
mqtt::Version::MQTT_3_1_1, ConfigCommon::keep_alive_timeout_secs_,
std::move(client_id), nullptr, nullptr, nullptr);
if(ResponseCode::MQTT_CONNACK_CONNECTION_ACCEPTED != rc) {
return rc;
}
// Get lock on shadow response handler execution
std::unique_lock<std::mutex> block_handler_lock(sync_action_response_lock_);
{
// Using mqtt command timeout as shadow action timeout
// Using Thing name as client token prefix
std::chrono::milliseconds shadow_action_timeout = ConfigCommon::mqtt_command_timeout_;
Shadow my_shadow(p_iot_client_, ConfigCommon::mqtt_command_timeout_, ConfigCommon::thing_name_,
ConfigCommon::thing_name_);
// Subscribe to shadow actions
Shadow::RequestHandlerPtr p_action_handler =
std::bind(&ShadowDelta::ActionResponseHandler, this, std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4);
util::Map<ShadowRequestType, Shadow::RequestHandlerPtr> request_mapping;
request_mapping.insert(std::make_pair(ShadowRequestType::Get, p_action_handler));
request_mapping.insert(std::make_pair(ShadowRequestType::Update, p_action_handler));
request_mapping.insert(std::make_pair(ShadowRequestType::Delete, p_action_handler));
request_mapping.insert(std::make_pair(ShadowRequestType::Delta, p_action_handler));
my_shadow.AddShadowSubscription(request_mapping);
// Start from a no shadow state
// Attempt to get the current shadow
rc = my_shadow.PerformGetAsync();
if(ResponseCode::SUCCESS == rc) {
sync_action_response_wait_.wait_for(block_handler_lock, shadow_action_timeout);
rc = sync_action_response_;
if(ResponseCode::SHADOW_REQUEST_ACCEPTED == rc) {
// Shadow exists, delete it
rc = my_shadow.PerformDeleteAsync();
sync_action_response_wait_.wait_for(block_handler_lock, shadow_action_timeout);
rc = sync_action_response_;
if(ResponseCode::SHADOW_REQUEST_ACCEPTED != rc) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Shadow Delete request failed!!");
rc = p_iot_client_->Disconnect(ConfigCommon::mqtt_command_timeout_);
std::cout << "Exiting Sample!!!!" << std::endl;
return rc;
}
}
}
// Shadow delete, update with sample shadow
util::JsonDocument doc;
unsigned int request_itr = 0;
util::String update_topic = SHADOW_TOPIC_PREFIX;
update_topic.append(ConfigCommon::thing_name_);
update_topic.append(SHADOW_TOPIC_MIDDLE);
update_topic.append(SHADOW_REQUEST_TYPE_UPDATE_STRING);
// Rapidjson uses move semantics, doc needs to be initialized again
rc = util::JsonParser::InitializeFromJsonString(doc, SHADOW_DOCUMENT_EMPTY_STRING);
if(ResponseCode::SUCCESS != rc) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Json Parse for sample failed with return code : %d",
static_cast<int>(rc));
rc = p_iot_client_->Disconnect(ConfigCommon::mqtt_command_timeout_);
std::cout << "Exiting Sample!!!!" << std::endl;
return rc;
}
do {
if(0 != request_itr) {
doc = my_shadow.GetServerDocument();
}
// Checking if a member exists
if(doc[SHADOW_DOCUMENT_STATE_KEY][SHADOW_DOCUMENT_DESIRED_KEY].HasMember(MSG_COUNT_KEY)) {
// Erasing a member
doc[SHADOW_DOCUMENT_STATE_KEY][SHADOW_DOCUMENT_DESIRED_KEY].EraseMember(MSG_COUNT_KEY);
}
// Creating JsonValue for key
util::JsonValue key(MSG_COUNT_KEY, doc.GetAllocator());
// Creating JsonValue for the value of the above key
util::JsonValue val(request_itr + 1);
// Adding a member
doc[SHADOW_DOCUMENT_STATE_KEY][SHADOW_DOCUMENT_DESIRED_KEY].AddMember(key.Move(), val.Move(),
doc.GetAllocator());
// Update current device shadow using the above doc
my_shadow.UpdateDeviceShadow(doc);
// Perform an Update operation
// This will generate a diff between the last received server state and the current device state
// and perform a shadow update operation
rc = my_shadow.PerformUpdateAsync();
sync_action_response_wait_.wait_for(block_handler_lock, shadow_action_timeout);
rc = sync_action_response_;
if(ResponseCode::SHADOW_REQUEST_REJECTED == rc) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Shadow update failed with return code : %d",
static_cast<int>(rc));
break;
}
//Sleep for 1 second and wait for all messages to be received
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if(my_shadow.IsInSync()) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Expected shadow to be out of sync!!");
} else {
AWS_LOG_INFO(LOG_TAG_SHADOW_DELTA, "Shadow out of sync!!");
}
// Get the current server document
doc = my_shadow.GetServerDocument();
util::String device = util::JsonParser::ToString(doc);
std::cout << std::endl << "Server Shadow State ------- " << std::endl << device << std::endl
<< std::endl;
std::cout << "--------------------------- " << std::endl << std::endl;
// Checking if a member exists
if(doc[SHADOW_DOCUMENT_STATE_KEY][SHADOW_DOCUMENT_REPORTED_KEY].HasMember(MSG_COUNT_KEY)) {
// Erasing a member
doc[SHADOW_DOCUMENT_STATE_KEY][SHADOW_DOCUMENT_REPORTED_KEY].EraseMember(MSG_COUNT_KEY);
}
// Creating JsonValue for key
key = util::JsonValue(MSG_COUNT_KEY, doc.GetAllocator());
// Creating JsonValue for the value of the above key
val = util::JsonValue(request_itr + 1);
// Adding a member
doc[SHADOW_DOCUMENT_STATE_KEY][SHADOW_DOCUMENT_REPORTED_KEY].AddMember(key.Move(), val.Move(),
doc.GetAllocator());
// Below code demonstrates both performing an update using Shadow API as well as direct publish
if(0 == (request_itr % 2)) {
// Update current device shadow using the above doc
my_shadow.UpdateDeviceShadow(doc);
// Perform an Update operation
rc = my_shadow.PerformUpdateAsync();
sync_action_response_wait_.wait_for(block_handler_lock, shadow_action_timeout);
rc = sync_action_response_;
if(ResponseCode::SHADOW_REQUEST_REJECTED == rc) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "2Shadow update failed with return code : %d",
static_cast<int>(rc));
break;
}
} else {
// Update device shadow using a publish to test Delta topic
util::JsonDocument diff;
util::JsonDocument cur_server_state_doc = my_shadow.GetServerDocument();
util::JsonParser::DiffValues(diff, cur_server_state_doc, doc, diff.GetAllocator());
if(diff.HasMember(SHADOW_DOCUMENT_TIMESTAMP_KEY)) {
diff.EraseMember(SHADOW_DOCUMENT_TIMESTAMP_KEY);
}
if(diff.HasMember(SHADOW_DOCUMENT_VERSION_KEY)) {
diff.EraseMember(SHADOW_DOCUMENT_VERSION_KEY);
}
util::String payload = util::JsonParser::ToString(diff);
rc = p_iot_client_->Publish(Utf8String::Create(update_topic), false, false,
awsiotsdk::mqtt::QoS::QOS1, payload,
ConfigCommon::mqtt_command_timeout_);
if(ResponseCode::SUCCESS != rc) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA,
"Shadow update using publish failed with return code : %d",
static_cast<int>(rc));
break;
}
}
//Sleep for 1 second and wait for all messages to be received
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// Get the current server document
doc = my_shadow.GetServerDocument();
device = util::JsonParser::ToString(doc);
std::cout << std::endl << "Server Shadow State ------- " << std::endl << device << std::endl
<< std::endl;
if(my_shadow.IsInSync()) {
AWS_LOG_INFO(LOG_TAG_SHADOW_DELTA, "Shadow is in sync!!");
} else {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Expected shadow to be in sync!!");
}
request_itr++;
} while(request_itr < MESSAGE_COUNT);
}
rc = p_iot_client_->Disconnect(ConfigCommon::mqtt_command_timeout_);
if(ResponseCode::SUCCESS != rc) {
AWS_LOG_ERROR(LOG_TAG_SHADOW_DELTA, "Disconnect failed with return code : %d", static_cast<int>(rc));
}
std::cout<<"Exiting Sample!!!!"<<std::endl;
return ResponseCode::SUCCESS;
}
}
}
int main(int argc, char **argv) {
std::shared_ptr<awsiotsdk::util::Logging::ConsoleLogSystem> p_log_system = std::make_shared<awsiotsdk::util::Logging::ConsoleLogSystem>(awsiotsdk::util::Logging::LogLevel::Info);
awsiotsdk::util::Logging::InitializeAWSLogging(p_log_system);
std::unique_ptr<awsiotsdk::samples::ShadowDelta> shadow_delta = std::unique_ptr<awsiotsdk::samples::ShadowDelta>(new awsiotsdk::samples::ShadowDelta());
awsiotsdk::ResponseCode rc = awsiotsdk::ConfigCommon::InitializeCommon("config/SampleConfig.json");
if(awsiotsdk::ResponseCode::SUCCESS == rc) {
rc = shadow_delta->RunSample();
}
#ifdef WIN32
std::cout<<"Press any key to continue!!!!"<<std::endl;
getchar();
#endif
awsiotsdk::util::Logging::ShutdownAWSLogging();
return static_cast<int>(rc);
}