in dynamodb-api/src/main/java/com/awssamples/iot/dynamodb/api/handlers/HandleIotNotificationEvent.java [33:77]
public String handleRequest(final Map input, final Context context) {
if ((input == null) || (!input.containsKey(UUID_DYNAMO_DB_COLUMN_NAME))) {
throw new RuntimeException("Message cannot be handled. It does not contain a UUID.");
}
String uuid = (String) input.get(UUID_DYNAMO_DB_COLUMN_NAME);
CreateThingRequest createThingRequest = CreateThingRequest.builder()
.thingName(uuid)
.build();
// Try to create the thing but we don't care if it fails, we will just log it
Try.of(() -> IotClient.create().createThing(createThingRequest))
.onFailure(throwable -> log.warn("Create thing failed for [" + uuid + "] [" + throwable.getMessage() + "]"));
CreateThingGroupRequest createThingGroupRequest = CreateThingGroupRequest.builder()
.thingGroupName(THING_GROUP)
.build();
// Try to create the thing group but we don't care if it fails, we will just log it
Try.of(() -> IotClient.create().createThingGroup(createThingGroupRequest))
.onFailure(throwable -> log.warn("Create thing group failed for [" + THING_GROUP + "] [" + throwable.getMessage() + "]"));
UpdateThingGroupsForThingRequest updateThingGroupsForThingRequest = UpdateThingGroupsForThingRequest.builder()
.thingGroupsToAdd(THING_GROUP)
.thingName(uuid)
.build();
// Try to create the thing group but we don't care if it fails, we will just log it
Try.of(() -> IotClient.create().updateThingGroupsForThing(updateThingGroupsForThingRequest))
.onFailure(throwable -> log.warn("Adding thing [" + uuid + "] to group [" + THING_GROUP + "] failed [" + throwable.getMessage() + "]"));
String shadowJson = "{\"state\":{\"reported\":{\"" + LAST_CONTACT + "\":" + Instant.now().toEpochMilli() + "}}}";
UpdateThingShadowRequest updateThingShadowRequest = UpdateThingShadowRequest.builder()
.thingName(uuid)
.payload(SdkBytes.fromUtf8String(shadowJson))
.build();
// Try to update the thing shadow but we don't care if it fails, we will just log it
Try.of(() -> IotDataPlaneClient.create().updateThingShadow(updateThingShadowRequest))
.onFailure(throwable -> log.warn("Update shadow failed for [" + uuid + "] [" + throwable.getMessage() + "]"));
return "done";
}