Search in sources :

Example 61 with MqttMessage

use of software.amazon.awssdk.crt.mqtt.MqttMessage in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsClientWrapper method PublishDescribeJobExecution.

@Override
public CompletableFuture<Integer> PublishDescribeJobExecution(DescribeJobExecutionRequest request, QualityOfService qos) {
    if (request.thingName == null || request.jobId == null) {
        CompletableFuture result = new CompletableFuture();
        result.completeExceptionally(new MqttException("DescribeJobExecutionRequest must have a non-null thingName and a non-null jobId"));
        return result;
    }
    String topic = String.format(DESCRIBE_JOB_TOPIC, request.thingName, request.jobId);
    String payloadJson = this.gson.toJson(request);
    MqttMessage message = new MqttMessage(topic, payloadJson.getBytes(StandardCharsets.UTF_8));
    return this.connection.publish(message, qos, false);
}
Also used : MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) CompletableFuture(java.util.concurrent.CompletableFuture) MqttException(software.amazon.awssdk.crt.mqtt.MqttException)

Example 62 with MqttMessage

use of software.amazon.awssdk.crt.mqtt.MqttMessage in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsClientWrapper method SubscribeToDescribeJobExecutionRejected.

@Override
public CompletableFuture<Integer> SubscribeToDescribeJobExecutionRejected(DescribeJobExecutionSubscriptionRequest request, QualityOfService qos, Consumer<RejectedError> handler, Consumer<Exception> exceptionHandler) {
    if (request.jobId == null || request.thingName == null) {
        CompletableFuture result = new CompletableFuture();
        result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null jobId and a non-null thingName"));
        return result;
    }
    String topic = String.format(JOB_DESCRIBE_REJECTED_TOPIC, request.thingName, request.jobId);
    Consumer<MqttMessage> messageHandler = describeJobSubscriptionCbs.computeIfAbsent(new Pair<>(handler, exceptionHandler), (k) -> (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            RejectedError response = this.gson.fromJson(payload, RejectedError.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    });
    return this.connection.subscribe(topic, qos, messageHandler);
}
Also used : 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) RejectedError(software.amazon.awssdk.iot.iotjobs.model.RejectedError)

Aggregations

MqttMessage (software.amazon.awssdk.crt.mqtt.MqttMessage)62 CompletableFuture (java.util.concurrent.CompletableFuture)53 MqttException (software.amazon.awssdk.crt.mqtt.MqttException)46 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)34 QualityOfService (software.amazon.awssdk.crt.mqtt.QualityOfService)33 UnsupportedEncodingException (java.io.UnsupportedEncodingException)31 StandardCharsets (java.nio.charset.StandardCharsets)31 Consumer (java.util.function.Consumer)31 Gson (com.google.gson.Gson)28 GsonBuilder (com.google.gson.GsonBuilder)28 ByteBuffer (java.nio.ByteBuffer)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