Search in sources :

Example 1 with UpdateShadowRequest

use of software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest in project aws-iot-device-sdk-java-v2 by aws.

the class ShadowSample method changeShadowValue.

static CompletableFuture<Void> changeShadowValue(String value) {
    if (localValue != null) {
        if (localValue.equals(value)) {
            System.out.println("Local value is already " + value);
            CompletableFuture<Void> result = new CompletableFuture<>();
            result.complete(null);
            return result;
        }
    }
    System.out.println("Changed local value to " + value);
    localValue = value;
    System.out.println("Updating shadow value to " + value);
    // build a request to let the service know our current value and desired value, and that we only want
    // to update if the version matches the version we know about
    UpdateShadowRequest request = new UpdateShadowRequest();
    request.thingName = thingName;
    request.state = new ShadowState();
    if (value.compareToIgnoreCase("clear_shadow") == 0) {
        request.state.desiredIsNullable = true;
        request.state.reportedIsNullable = true;
        request.state.desired = null;
        request.state.reported = null;
    } else if (value.compareToIgnoreCase("null") == 0) {
        // A bit of a hack - we have to set reportedNullIsValid OR desiredNullIsValid
        // so the JSON formatter will allow null , otherwise null will always be
        // be converted to "null"
        // As long as we're passing a Hashmap that is NOT assigned to null, it will not
        // clear the data - so we pass an empty HashMap to avoid clearing data we want to keep
        request.state.desiredIsNullable = true;
        request.state.reportedIsNullable = false;
        // We will only clear desired, so we need to pass an empty HashMap for reported
        request.state.reported = new HashMap<String, Object>() {

            {
            }
        };
        request.state.desired = new HashMap<String, Object>() {

            {
                put(SHADOW_PROPERTY, null);
            }
        };
    } else {
        request.state.reported = new HashMap<String, Object>() {

            {
                put(SHADOW_PROPERTY, value);
            }
        };
        request.state.desired = new HashMap<String, Object>() {

            {
                put(SHADOW_PROPERTY, value);
            }
        };
    }
    // Publish the request
    return shadow.PublishUpdateShadow(request, QualityOfService.AT_LEAST_ONCE).thenRun(() -> {
        System.out.println("Update request published");
    }).exceptionally((ex) -> {
        System.out.println("Update request failed: " + ex.getMessage());
        System.exit(3);
        return null;
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) HashMap(java.util.HashMap) UpdateShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState)

Example 2 with UpdateShadowRequest

use of software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest in project aws-iot-device-sdk-java-v2 by aws.

the class ShadowUpdate method changeShadowValue.

static CompletableFuture<Integer> changeShadowValue() {
    // build a request to let the service know our current value and desired value, and that we only want
    // to update if the version matches the version we know about
    UpdateShadowRequest request = new UpdateShadowRequest();
    request.thingName = DATestUtils.thing_name;
    request.state = new ShadowState();
    request.state.reported = new HashMap<String, Object>() {

        {
            put(DATestUtils.shadowProperty, DATestUtils.shadowValue);
        }
    };
    request.state.desired = new HashMap<String, Object>() {

        {
            put(DATestUtils.shadowProperty, DATestUtils.shadowValue);
        }
    };
    // Publish the request
    return shadow.PublishUpdateShadow(request, QualityOfService.AT_MOST_ONCE);
}
Also used : UpdateShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState)

Aggregations

ShadowState (software.amazon.awssdk.iot.iotshadow.model.ShadowState)2 UpdateShadowRequest (software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest)2 HashMap (java.util.HashMap)1 CompletableFuture (java.util.concurrent.CompletableFuture)1