Example usage for com.amazonaws.services.iotdata.model GetThingShadowResult getPayload

List of usage examples for com.amazonaws.services.iotdata.model GetThingShadowResult getPayload

Introduction

In this page you can find the example usage for com.amazonaws.services.iotdata.model GetThingShadowResult getPayload.

Prototype


public java.nio.ByteBuffer getPayload() 

Source Link

Document

The state information, in JSON format.

Usage

From source file:io.klerch.alexa.state.handler.AWSIotStateHandler.java

License:Open Source License

private String getState(final AlexaScope scope) throws AlexaStateException {
    final String thingName = getThingName(scope);

    createThingIfNotExisting(scope);/*  w ww. j  a  v  a  2 s  . co m*/

    final GetThingShadowRequest awsRequest = new GetThingShadowRequest().withThingName(thingName);
    try {
        final GetThingShadowResult response = awsDataClient.getThingShadow(awsRequest);
        final ByteBuffer buffer = response.getPayload();

        try {
            return (buffer != null && buffer.hasArray()) ? new String(buffer.array(), "UTF-8") : "{}";
        } catch (UnsupportedEncodingException e) {
            final String error = format("Could not handle received contents of thing-shadow '%1$s'", thingName);
            log.error(error, e);
            throw AlexaStateException.create(error).withCause(e).withHandler(this).build();
        }
    }
    // if a thing does not have a shadow this is a usual exception
    catch (com.amazonaws.services.iotdata.model.ResourceNotFoundException e) {
        log.info(e);
        // we are fine with a thing having no shadow what just means there's nothing to read out for the model
        // return an empty JSON to indicate nothing is in the thing shadow
        return "{}";
    }
}