Search in sources :

Example 1 with GetShadowRequest

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

the class ShadowSample method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("ShadowSample");
    cmdUtils.addCommonMQTTCommands();
    cmdUtils.registerCommand("key", "<path>", "Path to your key in PEM format.");
    cmdUtils.registerCommand("cert", "<path>", "Path to your client certificate in PEM format.");
    cmdUtils.registerCommand("port", "<int>", "Port to use (optional, default='8883').");
    cmdUtils.registerCommand("thing_name", "<str>", "The name of the IoT thing.");
    cmdUtils.registerCommand("client_id", "<int>", "Client id to use (optional, default='test-*')");
    cmdUtils.sendArguments(args);
    thingName = cmdUtils.getCommandRequired("thing_name", "");
    MqttClientConnectionEvents callbacks = new MqttClientConnectionEvents() {

        @Override
        public void onConnectionInterrupted(int errorCode) {
            if (errorCode != 0) {
                System.out.println("Connection interrupted: " + errorCode + ": " + CRT.awsErrorString(errorCode));
            }
        }

        @Override
        public void onConnectionResumed(boolean sessionPresent) {
            System.out.println("Connection resumed: " + (sessionPresent ? "existing session" : "clean session"));
        }
    };
    try {
        MqttClientConnection connection = cmdUtils.buildMQTTConnection(callbacks);
        shadow = new IotShadowClient(connection);
        CompletableFuture<Boolean> connected = connection.connect();
        try {
            boolean sessionPresent = connected.get();
            System.out.println("Connected to " + (!sessionPresent ? "clean" : "existing") + " session!");
        } catch (Exception ex) {
            throw new RuntimeException("Exception occurred during connect", ex);
        }
        System.out.println("Subscribing to shadow delta events...");
        ShadowDeltaUpdatedSubscriptionRequest requestShadowDeltaUpdated = new ShadowDeltaUpdatedSubscriptionRequest();
        requestShadowDeltaUpdated.thingName = thingName;
        CompletableFuture<Integer> subscribedToDeltas = shadow.SubscribeToShadowDeltaUpdatedEvents(requestShadowDeltaUpdated, QualityOfService.AT_LEAST_ONCE, ShadowSample::onShadowDeltaUpdated);
        subscribedToDeltas.get();
        System.out.println("Subscribing to update respones...");
        UpdateShadowSubscriptionRequest requestUpdateShadow = new UpdateShadowSubscriptionRequest();
        requestUpdateShadow.thingName = thingName;
        CompletableFuture<Integer> subscribedToUpdateAccepted = shadow.SubscribeToUpdateShadowAccepted(requestUpdateShadow, QualityOfService.AT_LEAST_ONCE, ShadowSample::onUpdateShadowAccepted);
        CompletableFuture<Integer> subscribedToUpdateRejected = shadow.SubscribeToUpdateShadowRejected(requestUpdateShadow, QualityOfService.AT_LEAST_ONCE, ShadowSample::onUpdateShadowRejected);
        subscribedToUpdateAccepted.get();
        subscribedToUpdateRejected.get();
        System.out.println("Subscribing to get responses...");
        GetShadowSubscriptionRequest requestGetShadow = new GetShadowSubscriptionRequest();
        requestGetShadow.thingName = thingName;
        CompletableFuture<Integer> subscribedToGetShadowAccepted = shadow.SubscribeToGetShadowAccepted(requestGetShadow, QualityOfService.AT_LEAST_ONCE, ShadowSample::onGetShadowAccepted);
        CompletableFuture<Integer> subscribedToGetShadowRejected = shadow.SubscribeToGetShadowRejected(requestGetShadow, QualityOfService.AT_LEAST_ONCE, ShadowSample::onGetShadowRejected);
        subscribedToGetShadowAccepted.get();
        subscribedToGetShadowRejected.get();
        gotResponse = new CompletableFuture<>();
        System.out.println("Requesting current shadow state...");
        GetShadowRequest getShadowRequest = new GetShadowRequest();
        getShadowRequest.thingName = thingName;
        CompletableFuture<Integer> publishedGetShadow = shadow.PublishGetShadow(getShadowRequest, QualityOfService.AT_LEAST_ONCE);
        publishedGetShadow.get();
        gotResponse.get();
        String newValue = "";
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print(SHADOW_PROPERTY + "> ");
            System.out.flush();
            newValue = scanner.next();
            if (newValue.compareToIgnoreCase("quit") == 0) {
                break;
            }
            gotResponse = new CompletableFuture<>();
            changeShadowValue(newValue).get();
            gotResponse.get();
        }
        scanner.close();
        CompletableFuture<Void> disconnected = connection.disconnect();
        disconnected.get();
    } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
        System.out.println("Exception encountered: " + ex.toString());
    }
    System.out.println("Complete!");
    CrtResource.waitForNoResources();
}
Also used : Scanner(java.util.Scanner) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) UpdateShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowSubscriptionRequest) IotShadowClient(software.amazon.awssdk.iot.iotshadow.IotShadowClient) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) GetShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowSubscriptionRequest) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) GetShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowRequest) ExecutionException(java.util.concurrent.ExecutionException) ShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest) ExecutionException(java.util.concurrent.ExecutionException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException)

Aggregations

Scanner (java.util.Scanner)1 ExecutionException (java.util.concurrent.ExecutionException)1 CrtRuntimeException (software.amazon.awssdk.crt.CrtRuntimeException)1 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)1 MqttClientConnectionEvents (software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents)1 IotShadowClient (software.amazon.awssdk.iot.iotshadow.IotShadowClient)1 GetShadowRequest (software.amazon.awssdk.iot.iotshadow.model.GetShadowRequest)1 GetShadowSubscriptionRequest (software.amazon.awssdk.iot.iotshadow.model.GetShadowSubscriptionRequest)1 ShadowDeltaUpdatedSubscriptionRequest (software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest)1 UpdateShadowSubscriptionRequest (software.amazon.awssdk.iot.iotshadow.model.UpdateShadowSubscriptionRequest)1 CommandLineUtils (utils.commandlineutils.CommandLineUtils)1