-
Notifications
You must be signed in to change notification settings - Fork 70
/
SampleShadowFeature.cpp
435 lines (368 loc) · 15.5 KB
/
SampleShadowFeature.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include "SampleShadowFeature.h"
#include "../logging/LoggerFactory.h"
#include <aws/common/byte_buf.h>
#include <aws/crt/UUID.h>
#include <aws/iotdevicecommon/IotDevice.h>
#include <chrono>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <thread>
#include <unistd.h>
#include <utility>
#include <aws/iotshadow/ErrorResponse.h>
#include <aws/iotshadow/IotShadowClient.h>
#include <aws/iotshadow/NamedShadowDeltaUpdatedSubscriptionRequest.h>
#include <aws/iotshadow/NamedShadowUpdatedSubscriptionRequest.h>
#include <aws/iotshadow/ShadowDeltaUpdatedEvent.h>
#include <aws/iotshadow/ShadowUpdatedEvent.h>
#include <aws/iotshadow/UpdateNamedShadowRequest.h>
#include <aws/iotshadow/UpdateNamedShadowSubscriptionRequest.h>
#include <aws/iotshadow/UpdateShadowRequest.h>
#include <aws/iotshadow/UpdateShadowResponse.h>
#include <sys/inotify.h>
using namespace std;
using namespace Aws;
using namespace Aws::Iot;
using namespace Aws::Crt;
using namespace Aws::Iotshadow;
using namespace Aws::Iot::DeviceClient::Shadow;
using namespace Aws::Iot::DeviceClient::Util;
using namespace Aws::Iot::DeviceClient::Logging;
constexpr char SampleShadowFeature::TAG[];
constexpr char SampleShadowFeature::NAME[];
constexpr char SampleShadowFeature::DEFAULT_SAMPLE_SHADOW_DOCUMENT_FILE[];
constexpr int SampleShadowFeature::DEFAULT_WAIT_TIME_SECONDS;
constexpr int MAX_EVENTS = 1000; /* Maximum number of events to process*/
constexpr int LEN_NAME = 16; /* Assuming that the length of the filename won't exceed 16 bytes*/
#define EVENT_SIZE (sizeof(struct inotify_event)) /*size of one event*/
#define EVENT_BUFSIZE (MAX_EVENTS * (EVENT_SIZE + LEN_NAME)) /*size of buffer used to store the data of events*/
string SampleShadowFeature::getName()
{
return NAME;
}
int SampleShadowFeature::init(
shared_ptr<SharedCrtResourceManager> manager,
shared_ptr<ClientBaseNotifier> notifier,
const PlainConfig &config)
{
resourceManager = manager;
baseNotifier = notifier;
thingName = *config.thingName;
shadowName = config.sampleShadow.shadowName.value();
if (config.sampleShadow.shadowInputFile.has_value())
{
inputFile = config.sampleShadow.shadowInputFile.value();
}
if (config.sampleShadow.shadowOutputFile.has_value())
{
outputFile = config.sampleShadow.shadowOutputFile.value();
}
return AWS_OP_SUCCESS;
}
void SampleShadowFeature::updateNamedShadowAcceptedHandler(Iotshadow::UpdateShadowResponse *response, int ioError) const
{
if (ioError)
{
LOGM_ERROR(TAG, "Encountered ioError %d within updateNamedShadowAcceptedHandler", ioError);
}
}
void SampleShadowFeature::updateNamedShadowRejectedHandler(Iotshadow::ErrorResponse *errorResponse, int ioError) const
{
if (ioError)
{
LOGM_ERROR(TAG, "Encountered ioError %d within updateNamedShadowRejectedHandler", ioError);
return;
}
if (errorResponse->Message.has_value())
{
LOGM_ERROR(TAG, "UpdateNamedShadowRequest gets rejected: %s", errorResponse->Message->c_str());
}
}
void SampleShadowFeature::updateNamedShadowEventHandler(Iotshadow::ShadowUpdatedEvent *shadowUpdatedEvent, int ioError)
const
{
if (ioError)
{
LOGM_ERROR(TAG, "Encountered ioError %d within updateNamedShadowEventHandler", ioError);
return;
}
// write the response to output file
Crt::JsonObject object;
shadowUpdatedEvent->SerializeToObject(object);
if (FileUtils::StoreValueInFile(object.View().WriteReadable(true).c_str(), outputFile))
{
LOGM_INFO(TAG, "Stored the latest %s shadow document to local successfully", shadowName.c_str());
}
else
{
LOGM_ERROR(TAG, "Failed to store latest %s shadow document to local", shadowName.c_str());
}
}
void SampleShadowFeature::updateNamedShadowDeltaHandler(
Iotshadow::ShadowDeltaUpdatedEvent *shadowDeltaUpdatedEvent,
int ioError)
{
if (ioError)
{
LOGM_ERROR(TAG, "Encountered ioError %d within updateNamedShadowDeltaHandler", ioError);
return;
}
// do the shadow sync
UpdateNamedShadowRequest updateNamedShadowRequest;
updateNamedShadowRequest.ThingName = thingName.c_str();
updateNamedShadowRequest.ShadowName = shadowName.c_str();
ShadowState state;
state.Reported = shadowDeltaUpdatedEvent->State.value();
updateNamedShadowRequest.State = state;
Aws::Crt::UUID uuid;
updateNamedShadowRequest.ClientToken = uuid.ToString();
shadowClient->PublishUpdateNamedShadow(
updateNamedShadowRequest,
AWS_MQTT_QOS_AT_LEAST_ONCE,
std::bind(&SampleShadowFeature::ackUpdateNamedShadowStatus, this, std::placeholders::_1));
}
void SampleShadowFeature::ackUpdateNamedShadowStatus(int ioError) const
{
LOGM_DEBUG(TAG, "Ack received for updateNamedShadowStatus with code {%d}", ioError);
}
void SampleShadowFeature::ackSubscribeToUpdateNamedShadowAccepted(int ioError)
{
LOGM_DEBUG(TAG, "Ack received for SubscribeToUpdateNamedShadowAccepted with code {%d}", ioError);
if (ioError)
{
string errorMessage = "Encountered an ioError while attempting to subscribe to UpdateNamedShadowAccepted";
LOG_ERROR(TAG, errorMessage.c_str());
baseNotifier->onError(this, ClientBaseErrorNotification::SUBSCRIPTION_FAILED, errorMessage);
}
subscribeShadowUpdateAcceptedPromise.set_value(ioError == AWS_OP_SUCCESS);
}
void SampleShadowFeature::ackSubscribeToUpdateNamedShadowRejected(int ioError)
{
LOGM_DEBUG(TAG, "Ack received for SubscribeToUpdateNamedShadowRejected with code {%d}", ioError);
if (ioError)
{
string errorMessage = "Encountered an ioError while attempting to subscribe to UpdateNamedShadowRejected";
LOG_ERROR(TAG, errorMessage.c_str());
baseNotifier->onError(this, ClientBaseErrorNotification::SUBSCRIPTION_FAILED, errorMessage);
}
subscribeShadowUpdateRejectedPromise.set_value(ioError == AWS_OP_SUCCESS);
}
void SampleShadowFeature::ackSubscribeToUpdateEvent(int ioError)
{
LOGM_DEBUG(TAG, "Ack received for SubscribeToUpdateNamedShadowEvent with code {%d}", ioError);
if (ioError)
{
string errorMessage = "Encountered an ioError while attempting to subscribe to UpdateNamedShadowEvent";
LOG_ERROR(TAG, errorMessage.c_str());
baseNotifier->onError(this, ClientBaseErrorNotification::SUBSCRIPTION_FAILED, errorMessage);
}
subscribeShadowUpdateEventPromise.set_value(ioError == AWS_OP_SUCCESS);
}
void SampleShadowFeature::ackSubscribeToUpdateDelta(int ioError)
{
LOGM_DEBUG(TAG, "Ack received for SubscribeToUpdateNamedShadowDelta with code {%d}", ioError);
if (ioError)
{
string errorMessage = "Encountered an ioError while attempting to subscribe to UpdateNamedShadowDelta";
LOG_ERROR(TAG, errorMessage.c_str());
baseNotifier->onError(this, ClientBaseErrorNotification::SUBSCRIPTION_FAILED, errorMessage);
}
subscribeShadowUpdateDeltaPromise.set_value(ioError == AWS_OP_SUCCESS);
}
void SampleShadowFeature::runFileMonitor()
{
ssize_t len = 0;
string fileDir = FileUtils::ExtractParentDirectory(inputFile.c_str());
string fileName = inputFile.substr(fileDir.length());
char buf[EVENT_BUFSIZE];
int fd = 0;
int dir_wd = 0;
int file_wd = 0;
fd = inotify_init();
if (fd == -1)
{
LOGM_ERROR(TAG, "Encounter error %d while initializing the inode notify system return s%", fd);
return;
}
dir_wd = inotify_add_watch(fd, fileDir.c_str(), IN_CREATE);
if (dir_wd == -1)
{
LOGM_ERROR(TAG, "Encounter error %d while adding the watch for input file's parent directory", fd);
goto exit;
}
file_wd = inotify_add_watch(fd, inputFile.c_str(), IN_CLOSE_WRITE);
if (file_wd == -1)
{
LOGM_ERROR(TAG, "Encounter error %d while adding the watch for target file", fd);
goto exit;
}
while (!needStop.load())
{
len = read(fd, buf, EVENT_BUFSIZE);
if (len <= 0)
{
LOG_WARN(TAG, "Couldn't monitor any more target file modify events as it reaches max read buffer size");
goto exit;
}
for (int i = 0; i < len;)
{
auto *e = (struct inotify_event *)&buf[i];
if (e->mask & IN_CREATE)
{
if (strcmp(e->name, fileName.c_str()) != 0)
goto next;
if (e->mask & IN_ISDIR)
goto next;
LOG_DEBUG(TAG, "New file is created with the same name of target file, start updating the shadow");
readAndUpdateShadowFromFile();
file_wd = inotify_add_watch(fd, inputFile.c_str(), IN_CLOSE_WRITE | IN_DELETE_SELF);
}
if (e->mask & IN_CLOSE_WRITE)
{
LOG_DEBUG(TAG, "The target file is modified, start updating the shadow");
readAndUpdateShadowFromFile();
}
if (e->mask & IN_DELETE_SELF)
{
if (e->mask & IN_ISDIR)
goto next;
LOG_DEBUG(TAG, "The target file is deleted by itself, removing the watch");
inotify_rm_watch(fd, file_wd);
}
next:
i += EVENT_SIZE + e->len;
}
this_thread::sleep_for(std::chrono::milliseconds(500));
}
exit:
inotify_rm_watch(fd, file_wd);
inotify_rm_watch(fd, dir_wd);
close(fd);
}
bool SampleShadowFeature::subscribeToPertinentShadowTopics()
{
UpdateNamedShadowSubscriptionRequest updateNamedShadowSubscriptionRequest;
updateNamedShadowSubscriptionRequest.ThingName = thingName.c_str();
updateNamedShadowSubscriptionRequest.ShadowName = shadowName.c_str();
shadowClient->SubscribeToUpdateNamedShadowAccepted(
updateNamedShadowSubscriptionRequest,
AWS_MQTT_QOS_AT_LEAST_ONCE,
std::bind(
&SampleShadowFeature::updateNamedShadowAcceptedHandler, this, std::placeholders::_1, std::placeholders::_2),
std::bind(&SampleShadowFeature::ackSubscribeToUpdateNamedShadowAccepted, this, std::placeholders::_1));
shadowClient->SubscribeToUpdateNamedShadowRejected(
updateNamedShadowSubscriptionRequest,
AWS_MQTT_QOS_AT_LEAST_ONCE,
std::bind(
&SampleShadowFeature::updateNamedShadowRejectedHandler, this, std::placeholders::_1, std::placeholders::_2),
std::bind(&SampleShadowFeature::ackSubscribeToUpdateNamedShadowRejected, this, std::placeholders::_1));
NamedShadowUpdatedSubscriptionRequest namedShadowUpdatedSubscriptionRequest;
namedShadowUpdatedSubscriptionRequest.ThingName = thingName.c_str();
namedShadowUpdatedSubscriptionRequest.ShadowName = shadowName.c_str();
shadowClient->SubscribeToNamedShadowUpdatedEvents(
namedShadowUpdatedSubscriptionRequest,
AWS_MQTT_QOS_AT_LEAST_ONCE,
std::bind(
&SampleShadowFeature::updateNamedShadowEventHandler, this, std::placeholders::_1, std::placeholders::_2),
std::bind(&SampleShadowFeature::ackSubscribeToUpdateEvent, this, std::placeholders::_1));
NamedShadowDeltaUpdatedSubscriptionRequest namedShadowDeltaUpdatedSubscriptionRequest;
namedShadowDeltaUpdatedSubscriptionRequest.ThingName = thingName.c_str();
namedShadowDeltaUpdatedSubscriptionRequest.ShadowName = shadowName.c_str();
shadowClient->SubscribeToNamedShadowDeltaUpdatedEvents(
namedShadowDeltaUpdatedSubscriptionRequest,
AWS_MQTT_QOS_AT_LEAST_ONCE,
std::bind(
&SampleShadowFeature::updateNamedShadowDeltaHandler, this, std::placeholders::_1, std::placeholders::_2),
std::bind(&SampleShadowFeature::ackSubscribeToUpdateDelta, this, std::placeholders::_1));
// check if we have subscribed all update topic successfully before making the call
auto futureSubscribeShadowUpdateDeltaPromise = subscribeShadowUpdateDeltaPromise.get_future();
auto futureSubscribeShadowUpdateEventPromise = subscribeShadowUpdateEventPromise.get_future();
auto futureSubscribeShadowUpdateRejectedPromise = subscribeShadowUpdateRejectedPromise.get_future();
auto futureSubscribeShadowUpdateAcceptedPromise = subscribeShadowUpdateAcceptedPromise.get_future();
if (futureSubscribeShadowUpdateAcceptedPromise.wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) ==
future_status::timeout ||
futureSubscribeShadowUpdateRejectedPromise.wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) ==
future_status::timeout ||
futureSubscribeShadowUpdateDeltaPromise.wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) ==
future_status::timeout ||
futureSubscribeShadowUpdateEventPromise.wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) ==
future_status::timeout)
{
LOGM_ERROR(TAG, "Subscribing to pertinent %s shadowUpdate topics timed out", shadowName.c_str());
return false;
}
if (!futureSubscribeShadowUpdateAcceptedPromise.get() || !futureSubscribeShadowUpdateRejectedPromise.get() ||
!futureSubscribeShadowUpdateDeltaPromise.get() || !futureSubscribeShadowUpdateEventPromise.get())
{
return false;
}
return true;
}
void SampleShadowFeature::readAndUpdateShadowFromFile()
{
Crt::JsonObject jsonObj;
if (inputFile.empty())
{
jsonObj.WithString("welcome", "aws-iot");
}
else
{
string expandedPath = inputFile;
ifstream setting(expandedPath.c_str());
if (!setting.is_open())
{
LOGM_ERROR(TAG, "Unable to open file: '%s'", Sanitize(expandedPath).c_str());
return;
}
std::string contents((std::istreambuf_iterator<char>(setting)), std::istreambuf_iterator<char>());
jsonObj = Aws::Crt::JsonObject(contents.c_str());
if (!jsonObj.WasParseSuccessful())
{
LOGM_ERROR(
TAG,
"Couldn't parse JSON shadow data file. GetErrorMessage returns: %s",
jsonObj.GetErrorMessage().c_str());
return;
}
setting.close();
}
UpdateNamedShadowRequest updateNamedShadowRequest;
updateNamedShadowRequest.ThingName = thingName.c_str();
updateNamedShadowRequest.ShadowName = shadowName.c_str();
ShadowState state;
state.Reported = jsonObj;
updateNamedShadowRequest.State = state;
Aws::Crt::UUID uuid;
updateNamedShadowRequest.ClientToken = uuid.ToString();
shadowClient->PublishUpdateNamedShadow(
updateNamedShadowRequest,
AWS_MQTT_QOS_AT_LEAST_ONCE,
std::bind(&SampleShadowFeature::ackUpdateNamedShadowStatus, this, std::placeholders::_1));
}
int SampleShadowFeature::start()
{
LOGM_INFO(TAG, "Starting %s", getName().c_str());
shadowClient = unique_ptr<IotShadowClient>(new IotShadowClient(resourceManager.get()->getConnection()));
if (!subscribeToPertinentShadowTopics())
{
LOGM_ERROR(TAG, "Failed to subscribe to related %s shadow topics", shadowName.c_str());
this->stop();
}
readAndUpdateShadowFromFile();
if (!inputFile.empty())
{
thread file_monitor_thread(&SampleShadowFeature::runFileMonitor, this);
file_monitor_thread.detach();
}
baseNotifier->onEvent(static_cast<Feature *>(this), ClientBaseEventNotification::FEATURE_STARTED);
return AWS_OP_SUCCESS;
}
int SampleShadowFeature::stop()
{
needStop.store(true);
baseNotifier->onEvent(static_cast<Feature *>(this), ClientBaseEventNotification::FEATURE_STOPPED);
return AWS_OP_SUCCESS;
}