Search in sources :

Example 1 with QualityOfService

use of software.amazon.awssdk.crt.mqtt.QualityOfService in project aws-iot-device-sdk-java-v2 by aws.

the class IotShadowClient method SubscribeToUpdateNamedShadowAccepted.

/**
 * Subscribes to the accepted topic for the UpdateNamedShadow operation
 *
 * Once subscribed, `handler` is invoked each time a message matching
 * the `topic` is received. It is possible for such messages to arrive before
 * the SUBACK is received.
 *
 * AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-mqtt.html#update-accepted-pub-sub-topic
 *
 * @param request Subscription request configuration
 * @param qos Maximum requested QoS that server may use when sending messages to the client.
 *            The server may grant a lower QoS in the SUBACK
 * @param handler callback function to invoke with messages received on the subscription topic
 * @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
 *
 * @return a future containing the MQTT packet id used to perform the subscribe operation
 */
public CompletableFuture<Integer> SubscribeToUpdateNamedShadowAccepted(UpdateNamedShadowSubscriptionRequest request, QualityOfService qos, Consumer<UpdateShadowResponse> handler, Consumer<Exception> exceptionHandler) {
    String topic = "$aws/things/{thingName}/shadow/name/{shadowName}/update/accepted";
    if (request.thingName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("UpdateNamedShadowSubscriptionRequest must have a non-null thingName"));
        return result;
    }
    topic = topic.replace("{thingName}", request.thingName);
    if (request.shadowName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("UpdateNamedShadowSubscriptionRequest must have a non-null shadowName"));
        return result;
    }
    topic = topic.replace("{shadowName}", request.shadowName);
    Consumer<MqttMessage> messageHandler = (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            UpdateShadowResponse response = gson.fromJson(payload, UpdateShadowResponse.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    };
    return connection.subscribe(topic, qos, messageHandler);
}
Also used : UpdateShadowResponse(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowResponse) NamedShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowUpdatedSubscriptionRequest) ShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSubscriptionRequest) DeleteShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowSubscriptionRequest) UpdateShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowSubscriptionRequest) ShadowUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedEvent) GetNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowRequest) HashMap(java.util.HashMap) EnumSerializer(software.amazon.awssdk.iot.EnumSerializer) CompletableFuture(java.util.concurrent.CompletableFuture) DeleteNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowSubscriptionRequest) GsonBuilder(com.google.gson.GsonBuilder) ByteBuffer(java.nio.ByteBuffer) NamedShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowDeltaUpdatedSubscriptionRequest) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) ShadowUpdatedSnapshot(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSnapshot) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) ShadowMetadata(software.amazon.awssdk.iot.iotshadow.model.ShadowMetadata) Gson(com.google.gson.Gson) GetShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowSubscriptionRequest) GetShadowResponse(software.amazon.awssdk.iot.iotshadow.model.GetShadowResponse) DeleteShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowRequest) ShadowStateWithDelta(software.amazon.awssdk.iot.iotshadow.model.ShadowStateWithDelta) DeleteNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ShadowDeltaUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedEvent) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) ShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest) StandardCharsets(java.nio.charset.StandardCharsets) GetShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowRequest) Consumer(java.util.function.Consumer) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) Timestamp(software.amazon.awssdk.iot.Timestamp) ErrorResponse(software.amazon.awssdk.iot.iotshadow.model.ErrorResponse) ShadowStateFactory(software.amazon.awssdk.iot.ShadowStateFactory) DeleteShadowResponse(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowResponse) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UpdateShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) UpdateShadowResponse(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowResponse) CompletableFuture(java.util.concurrent.CompletableFuture) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with QualityOfService

use of software.amazon.awssdk.crt.mqtt.QualityOfService in project aws-iot-device-sdk-java-v2 by aws.

the class IotShadowClient method SubscribeToGetNamedShadowAccepted.

/**
 * Subscribes to the accepted topic for the GetNamedShadow operation.
 *
 * Once subscribed, `handler` is invoked each time a message matching
 * the `topic` is received. It is possible for such messages to arrive before
 * the SUBACK is received.
 *
 * AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-mqtt.html#get-accepted-pub-sub-topic
 *
 * @param request Subscription request configuration
 * @param qos Maximum requested QoS that server may use when sending messages to the client.
 *            The server may grant a lower QoS in the SUBACK
 * @param handler callback function to invoke with messages received on the subscription topic
 * @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
 *
 * @return a future containing the MQTT packet id used to perform the subscribe operation
 */
public CompletableFuture<Integer> SubscribeToGetNamedShadowAccepted(GetNamedShadowSubscriptionRequest request, QualityOfService qos, Consumer<GetShadowResponse> handler, Consumer<Exception> exceptionHandler) {
    String topic = "$aws/things/{thingName}/shadow/name/{shadowName}/get/accepted";
    if (request.thingName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("GetNamedShadowSubscriptionRequest must have a non-null thingName"));
        return result;
    }
    topic = topic.replace("{thingName}", request.thingName);
    if (request.shadowName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("GetNamedShadowSubscriptionRequest must have a non-null shadowName"));
        return result;
    }
    topic = topic.replace("{shadowName}", request.shadowName);
    Consumer<MqttMessage> messageHandler = (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            GetShadowResponse response = gson.fromJson(payload, GetShadowResponse.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    };
    return connection.subscribe(topic, qos, messageHandler);
}
Also used : UpdateShadowResponse(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowResponse) NamedShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowUpdatedSubscriptionRequest) ShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSubscriptionRequest) DeleteShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowSubscriptionRequest) UpdateShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowSubscriptionRequest) ShadowUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedEvent) GetNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowRequest) HashMap(java.util.HashMap) EnumSerializer(software.amazon.awssdk.iot.EnumSerializer) CompletableFuture(java.util.concurrent.CompletableFuture) DeleteNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowSubscriptionRequest) GsonBuilder(com.google.gson.GsonBuilder) ByteBuffer(java.nio.ByteBuffer) NamedShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowDeltaUpdatedSubscriptionRequest) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) ShadowUpdatedSnapshot(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSnapshot) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) ShadowMetadata(software.amazon.awssdk.iot.iotshadow.model.ShadowMetadata) Gson(com.google.gson.Gson) GetShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowSubscriptionRequest) GetShadowResponse(software.amazon.awssdk.iot.iotshadow.model.GetShadowResponse) DeleteShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowRequest) ShadowStateWithDelta(software.amazon.awssdk.iot.iotshadow.model.ShadowStateWithDelta) DeleteNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ShadowDeltaUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedEvent) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) ShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest) StandardCharsets(java.nio.charset.StandardCharsets) GetShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowRequest) Consumer(java.util.function.Consumer) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) Timestamp(software.amazon.awssdk.iot.Timestamp) ErrorResponse(software.amazon.awssdk.iot.iotshadow.model.ErrorResponse) ShadowStateFactory(software.amazon.awssdk.iot.ShadowStateFactory) DeleteShadowResponse(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowResponse) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UpdateShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) GetShadowResponse(software.amazon.awssdk.iot.iotshadow.model.GetShadowResponse) CompletableFuture(java.util.concurrent.CompletableFuture) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with QualityOfService

use of software.amazon.awssdk.crt.mqtt.QualityOfService in project aws-iot-device-sdk-java-v2 by aws.

the class IotShadowClient method SubscribeToGetShadowAccepted.

/**
 * Subscribes to the accepted topic for the GetShadow operation.
 *
 * Once subscribed, `handler` is invoked each time a message matching
 * the `topic` is received. It is possible for such messages to arrive before
 * the SUBACK is received.
 *
 * AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-mqtt.html#get-accepted-pub-sub-topic
 *
 * @param request Subscription request configuration
 * @param qos Maximum requested QoS that server may use when sending messages to the client.
 *            The server may grant a lower QoS in the SUBACK
 * @param handler callback function to invoke with messages received on the subscription topic
 * @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
 *
 * @return a future containing the MQTT packet id used to perform the subscribe operation
 */
public CompletableFuture<Integer> SubscribeToGetShadowAccepted(GetShadowSubscriptionRequest request, QualityOfService qos, Consumer<GetShadowResponse> handler, Consumer<Exception> exceptionHandler) {
    String topic = "$aws/things/{thingName}/shadow/get/accepted";
    if (request.thingName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("GetShadowSubscriptionRequest must have a non-null thingName"));
        return result;
    }
    topic = topic.replace("{thingName}", request.thingName);
    Consumer<MqttMessage> messageHandler = (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            GetShadowResponse response = gson.fromJson(payload, GetShadowResponse.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    };
    return connection.subscribe(topic, qos, messageHandler);
}
Also used : UpdateShadowResponse(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowResponse) NamedShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowUpdatedSubscriptionRequest) ShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSubscriptionRequest) DeleteShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowSubscriptionRequest) UpdateShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowSubscriptionRequest) ShadowUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedEvent) GetNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowRequest) HashMap(java.util.HashMap) EnumSerializer(software.amazon.awssdk.iot.EnumSerializer) CompletableFuture(java.util.concurrent.CompletableFuture) DeleteNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowSubscriptionRequest) GsonBuilder(com.google.gson.GsonBuilder) ByteBuffer(java.nio.ByteBuffer) NamedShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowDeltaUpdatedSubscriptionRequest) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) ShadowUpdatedSnapshot(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSnapshot) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) ShadowMetadata(software.amazon.awssdk.iot.iotshadow.model.ShadowMetadata) Gson(com.google.gson.Gson) GetShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowSubscriptionRequest) GetShadowResponse(software.amazon.awssdk.iot.iotshadow.model.GetShadowResponse) DeleteShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowRequest) ShadowStateWithDelta(software.amazon.awssdk.iot.iotshadow.model.ShadowStateWithDelta) DeleteNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ShadowDeltaUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedEvent) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) ShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest) StandardCharsets(java.nio.charset.StandardCharsets) GetShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowRequest) Consumer(java.util.function.Consumer) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) Timestamp(software.amazon.awssdk.iot.Timestamp) ErrorResponse(software.amazon.awssdk.iot.iotshadow.model.ErrorResponse) ShadowStateFactory(software.amazon.awssdk.iot.ShadowStateFactory) DeleteShadowResponse(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowResponse) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UpdateShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) GetShadowResponse(software.amazon.awssdk.iot.iotshadow.model.GetShadowResponse) CompletableFuture(java.util.concurrent.CompletableFuture) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with QualityOfService

use of software.amazon.awssdk.crt.mqtt.QualityOfService in project aws-iot-device-sdk-java-v2 by aws.

the class IotShadowClient method SubscribeToGetNamedShadowRejected.

/**
 * Subscribes to the rejected topic for the GetNamedShadow operation.
 *
 * Once subscribed, `handler` is invoked each time a message matching
 * the `topic` is received. It is possible for such messages to arrive before
 * the SUBACK is received.
 *
 * AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-mqtt.html#get-rejected-pub-sub-topic
 *
 * @param request Subscription request configuration
 * @param qos Maximum requested QoS that server may use when sending messages to the client.
 *            The server may grant a lower QoS in the SUBACK
 * @param handler callback function to invoke with messages received on the subscription topic
 * @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
 *
 * @return a future containing the MQTT packet id used to perform the subscribe operation
 */
public CompletableFuture<Integer> SubscribeToGetNamedShadowRejected(GetNamedShadowSubscriptionRequest request, QualityOfService qos, Consumer<ErrorResponse> handler, Consumer<Exception> exceptionHandler) {
    String topic = "$aws/things/{thingName}/shadow/name/{shadowName}/get/rejected";
    if (request.thingName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("GetNamedShadowSubscriptionRequest must have a non-null thingName"));
        return result;
    }
    topic = topic.replace("{thingName}", request.thingName);
    if (request.shadowName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("GetNamedShadowSubscriptionRequest must have a non-null shadowName"));
        return result;
    }
    topic = topic.replace("{shadowName}", request.shadowName);
    Consumer<MqttMessage> messageHandler = (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            ErrorResponse response = gson.fromJson(payload, ErrorResponse.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    };
    return connection.subscribe(topic, qos, messageHandler);
}
Also used : UpdateShadowResponse(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowResponse) NamedShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowUpdatedSubscriptionRequest) ShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSubscriptionRequest) DeleteShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowSubscriptionRequest) UpdateShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowSubscriptionRequest) ShadowUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedEvent) GetNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowRequest) HashMap(java.util.HashMap) EnumSerializer(software.amazon.awssdk.iot.EnumSerializer) CompletableFuture(java.util.concurrent.CompletableFuture) DeleteNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowSubscriptionRequest) GsonBuilder(com.google.gson.GsonBuilder) ByteBuffer(java.nio.ByteBuffer) NamedShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowDeltaUpdatedSubscriptionRequest) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) ShadowUpdatedSnapshot(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSnapshot) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) ShadowMetadata(software.amazon.awssdk.iot.iotshadow.model.ShadowMetadata) Gson(com.google.gson.Gson) GetShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowSubscriptionRequest) GetShadowResponse(software.amazon.awssdk.iot.iotshadow.model.GetShadowResponse) DeleteShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowRequest) ShadowStateWithDelta(software.amazon.awssdk.iot.iotshadow.model.ShadowStateWithDelta) DeleteNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ShadowDeltaUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedEvent) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) ShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest) StandardCharsets(java.nio.charset.StandardCharsets) GetShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowRequest) Consumer(java.util.function.Consumer) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) Timestamp(software.amazon.awssdk.iot.Timestamp) ErrorResponse(software.amazon.awssdk.iot.iotshadow.model.ErrorResponse) ShadowStateFactory(software.amazon.awssdk.iot.ShadowStateFactory) DeleteShadowResponse(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowResponse) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UpdateShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) CompletableFuture(java.util.concurrent.CompletableFuture) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ErrorResponse(software.amazon.awssdk.iot.iotshadow.model.ErrorResponse)

Example 5 with QualityOfService

use of software.amazon.awssdk.crt.mqtt.QualityOfService in project aws-iot-device-sdk-java-v2 by aws.

the class IotShadowClient method SubscribeToDeleteShadowRejected.

/**
 * Subscribes to the rejected topic for the DeleteShadow operation
 *
 * Once subscribed, `handler` is invoked each time a message matching
 * the `topic` is received. It is possible for such messages to arrive before
 * the SUBACK is received.
 *
 * AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-mqtt.html#delete-rejected-pub-sub-topic
 *
 * @param request Subscription request configuration
 * @param qos Maximum requested QoS that server may use when sending messages to the client.
 *            The server may grant a lower QoS in the SUBACK
 * @param handler callback function to invoke with messages received on the subscription topic
 * @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
 *
 * @return a future containing the MQTT packet id used to perform the subscribe operation
 */
public CompletableFuture<Integer> SubscribeToDeleteShadowRejected(DeleteShadowSubscriptionRequest request, QualityOfService qos, Consumer<ErrorResponse> handler, Consumer<Exception> exceptionHandler) {
    String topic = "$aws/things/{thingName}/shadow/delete/rejected";
    if (request.thingName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("DeleteShadowSubscriptionRequest must have a non-null thingName"));
        return result;
    }
    topic = topic.replace("{thingName}", request.thingName);
    Consumer<MqttMessage> messageHandler = (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            ErrorResponse response = gson.fromJson(payload, ErrorResponse.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    };
    return connection.subscribe(topic, qos, messageHandler);
}
Also used : UpdateShadowResponse(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowResponse) NamedShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowUpdatedSubscriptionRequest) ShadowUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSubscriptionRequest) DeleteShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowSubscriptionRequest) UpdateShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowSubscriptionRequest) ShadowUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedEvent) GetNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowRequest) HashMap(java.util.HashMap) EnumSerializer(software.amazon.awssdk.iot.EnumSerializer) CompletableFuture(java.util.concurrent.CompletableFuture) DeleteNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowSubscriptionRequest) GsonBuilder(com.google.gson.GsonBuilder) ByteBuffer(java.nio.ByteBuffer) NamedShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.NamedShadowDeltaUpdatedSubscriptionRequest) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) ShadowUpdatedSnapshot(software.amazon.awssdk.iot.iotshadow.model.ShadowUpdatedSnapshot) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) ShadowMetadata(software.amazon.awssdk.iot.iotshadow.model.ShadowMetadata) Gson(com.google.gson.Gson) GetShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowSubscriptionRequest) GetShadowResponse(software.amazon.awssdk.iot.iotshadow.model.GetShadowResponse) DeleteShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowRequest) ShadowStateWithDelta(software.amazon.awssdk.iot.iotshadow.model.ShadowStateWithDelta) DeleteNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ShadowDeltaUpdatedEvent(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedEvent) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) ShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest) StandardCharsets(java.nio.charset.StandardCharsets) GetShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowRequest) Consumer(java.util.function.Consumer) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) Timestamp(software.amazon.awssdk.iot.Timestamp) ErrorResponse(software.amazon.awssdk.iot.iotshadow.model.ErrorResponse) ShadowStateFactory(software.amazon.awssdk.iot.ShadowStateFactory) DeleteShadowResponse(software.amazon.awssdk.iot.iotshadow.model.DeleteShadowResponse) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UpdateShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowRequest) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) CompletableFuture(java.util.concurrent.CompletableFuture) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ErrorResponse(software.amazon.awssdk.iot.iotshadow.model.ErrorResponse)

Aggregations

Gson (com.google.gson.Gson)28 GsonBuilder (com.google.gson.GsonBuilder)28 UnsupportedEncodingException (java.io.UnsupportedEncodingException)28 ByteBuffer (java.nio.ByteBuffer)28 StandardCharsets (java.nio.charset.StandardCharsets)28 CompletableFuture (java.util.concurrent.CompletableFuture)28 Consumer (java.util.function.Consumer)28 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)28 MqttException (software.amazon.awssdk.crt.mqtt.MqttException)28 MqttMessage (software.amazon.awssdk.crt.mqtt.MqttMessage)28 QualityOfService (software.amazon.awssdk.crt.mqtt.QualityOfService)28 EnumSerializer (software.amazon.awssdk.iot.EnumSerializer)28 Timestamp (software.amazon.awssdk.iot.Timestamp)28 HashMap (java.util.HashMap)26 ShadowStateFactory (software.amazon.awssdk.iot.ShadowStateFactory)16 DeleteNamedShadowRequest (software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowRequest)16 DeleteNamedShadowSubscriptionRequest (software.amazon.awssdk.iot.iotshadow.model.DeleteNamedShadowSubscriptionRequest)16 DeleteShadowRequest (software.amazon.awssdk.iot.iotshadow.model.DeleteShadowRequest)16 DeleteShadowResponse (software.amazon.awssdk.iot.iotshadow.model.DeleteShadowResponse)16 DeleteShadowSubscriptionRequest (software.amazon.awssdk.iot.iotshadow.model.DeleteShadowSubscriptionRequest)16