Search in sources :

Example 1 with TbQueueMsgMetadata

use of org.thingsboard.server.queue.TbQueueMsgMetadata in project thingsboard by thingsboard.

the class DefaultTbQueueRequestTemplate method sendToRequestTemplate.

void sendToRequestTemplate(Request request, UUID requestId, SettableFuture<Response> future, ResponseMetaData<Response> responseMetaData) {
    log.trace("[{}] Sending request, key [{}], expTime [{}], request {}", requestId, request.getKey(), responseMetaData.expTime, request);
    if (messagesStats != null) {
        messagesStats.incrementTotal();
    }
    requestTemplate.send(TopicPartitionInfo.builder().topic(requestTemplate.getDefaultTopic()).build(), request, new TbQueueCallback() {

        @Override
        public void onSuccess(TbQueueMsgMetadata metadata) {
            if (messagesStats != null) {
                messagesStats.incrementSuccessful();
            }
            log.trace("[{}] Request sent: {}, request {}", requestId, metadata, request);
        }

        @Override
        public void onFailure(Throwable t) {
            if (messagesStats != null) {
                messagesStats.incrementFailed();
            }
            pendingRequests.remove(requestId);
            future.setException(t);
        }
    });
}
Also used : TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata)

Example 2 with TbQueueMsgMetadata

use of org.thingsboard.server.queue.TbQueueMsgMetadata in project thingsboard by thingsboard.

the class TelemetryEdgeProcessor method processPostAttributes.

private ListenableFuture<Void> processPostAttributes(TenantId tenantId, CustomerId customerId, EntityId entityId, TransportProtos.PostAttributeMsg msg, TbMsgMetaData metaData) {
    SettableFuture<Void> futureToSet = SettableFuture.create();
    JsonObject json = JsonUtils.getJsonObject(msg.getKvList());
    Pair<String, RuleChainId> defaultQueueAndRuleChain = getDefaultQueueNameAndRuleChainId(tenantId, entityId);
    String queueName = defaultQueueAndRuleChain.getKey();
    RuleChainId ruleChainId = defaultQueueAndRuleChain.getValue();
    TbMsg tbMsg = TbMsg.newMsg(queueName, SessionMsgType.POST_ATTRIBUTES_REQUEST.name(), entityId, customerId, metaData, gson.toJson(json), ruleChainId, null);
    tbClusterService.pushMsgToRuleEngine(tenantId, tbMsg.getOriginator(), tbMsg, new TbQueueCallback() {

        @Override
        public void onSuccess(TbQueueMsgMetadata metadata) {
            futureToSet.set(null);
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("Can't process post attributes [{}]", msg, t);
            futureToSet.setException(t);
        }
    });
    return futureToSet;
}
Also used : TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) JsonObject(com.google.gson.JsonObject) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Example 3 with TbQueueMsgMetadata

use of org.thingsboard.server.queue.TbQueueMsgMetadata in project thingsboard by thingsboard.

the class TelemetryEdgeProcessor method processAttributesUpdate.

private ListenableFuture<Void> processAttributesUpdate(TenantId tenantId, CustomerId customerId, EntityId entityId, TransportProtos.PostAttributeMsg msg, TbMsgMetaData metaData) {
    SettableFuture<Void> futureToSet = SettableFuture.create();
    JsonObject json = JsonUtils.getJsonObject(msg.getKvList());
    Set<AttributeKvEntry> attributes = JsonConverter.convertToAttributes(json);
    ListenableFuture<List<Void>> future = attributesService.save(tenantId, entityId, metaData.getValue("scope"), new ArrayList<>(attributes));
    Futures.addCallback(future, new FutureCallback<List<Void>>() {

        @Override
        public void onSuccess(@Nullable List<Void> voids) {
            Pair<String, RuleChainId> defaultQueueAndRuleChain = getDefaultQueueNameAndRuleChainId(tenantId, entityId);
            String queueName = defaultQueueAndRuleChain.getKey();
            RuleChainId ruleChainId = defaultQueueAndRuleChain.getValue();
            TbMsg tbMsg = TbMsg.newMsg(queueName, DataConstants.ATTRIBUTES_UPDATED, entityId, customerId, metaData, gson.toJson(json), ruleChainId, null);
            tbClusterService.pushMsgToRuleEngine(tenantId, tbMsg.getOriginator(), tbMsg, new TbQueueCallback() {

                @Override
                public void onSuccess(TbQueueMsgMetadata metadata) {
                    futureToSet.set(null);
                }

                @Override
                public void onFailure(Throwable t) {
                    log.error("Can't process attributes update [{}]", msg, t);
                    futureToSet.setException(t);
                }
            });
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("Can't process attributes update [{}]", msg, t);
            futureToSet.setException(t);
        }
    }, dbCallbackExecutorService);
    return futureToSet;
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) JsonObject(com.google.gson.JsonObject) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata) List(java.util.List) ArrayList(java.util.ArrayList) TbMsg(org.thingsboard.server.common.msg.TbMsg) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 4 with TbQueueMsgMetadata

use of org.thingsboard.server.queue.TbQueueMsgMetadata in project thingsboard by thingsboard.

the class TelemetryEdgeProcessor method processAttributeDeleteMsg.

private ListenableFuture<Void> processAttributeDeleteMsg(TenantId tenantId, EntityId entityId, AttributeDeleteMsg attributeDeleteMsg, String entityType) {
    SettableFuture<Void> futureToSet = SettableFuture.create();
    String scope = attributeDeleteMsg.getScope();
    List<String> attributeNames = attributeDeleteMsg.getAttributeNamesList();
    attributesService.removeAll(tenantId, entityId, scope, attributeNames);
    if (EntityType.DEVICE.name().equals(entityType)) {
        Set<AttributeKey> attributeKeys = new HashSet<>();
        for (String attributeName : attributeNames) {
            attributeKeys.add(new AttributeKey(scope, attributeName));
        }
        tbClusterService.pushMsgToCore(DeviceAttributesEventNotificationMsg.onDelete(tenantId, (DeviceId) entityId, attributeKeys), new TbQueueCallback() {

            @Override
            public void onSuccess(TbQueueMsgMetadata metadata) {
                futureToSet.set(null);
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("Can't process attribute delete msg [{}]", attributeDeleteMsg, t);
                futureToSet.setException(t);
            }
        });
    }
    return futureToSet;
}
Also used : AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) DeviceId(org.thingsboard.server.common.data.id.DeviceId) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata) HashSet(java.util.HashSet)

Example 5 with TbQueueMsgMetadata

use of org.thingsboard.server.queue.TbQueueMsgMetadata in project thingsboard by thingsboard.

the class DeviceEdgeProcessor method processDeviceRpcCallResponseFromEdge.

public ListenableFuture<Void> processDeviceRpcCallResponseFromEdge(TenantId tenantId, DeviceRpcCallMsg deviceRpcCallMsg) {
    log.trace("[{}] processDeviceRpcCallResponseMsg [{}]", tenantId, deviceRpcCallMsg);
    SettableFuture<Void> futureToSet = SettableFuture.create();
    UUID requestUuid = new UUID(deviceRpcCallMsg.getRequestUuidMSB(), deviceRpcCallMsg.getRequestUuidLSB());
    DeviceId deviceId = new DeviceId(new UUID(deviceRpcCallMsg.getDeviceIdMSB(), deviceRpcCallMsg.getDeviceIdLSB()));
    FromDeviceRpcResponse response;
    if (!StringUtils.isEmpty(deviceRpcCallMsg.getResponseMsg().getError())) {
        response = new FromDeviceRpcResponse(requestUuid, null, RpcError.valueOf(deviceRpcCallMsg.getResponseMsg().getError()));
    } else {
        response = new FromDeviceRpcResponse(requestUuid, deviceRpcCallMsg.getResponseMsg().getResponse(), null);
    }
    TbQueueCallback callback = new TbQueueCallback() {

        @Override
        public void onSuccess(TbQueueMsgMetadata metadata) {
            futureToSet.set(null);
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("Can't process push notification to core [{}]", deviceRpcCallMsg, t);
            futureToSet.setException(t);
        }
    };
    FromDeviceRpcResponseActorMsg msg = new FromDeviceRpcResponseActorMsg(deviceRpcCallMsg.getRequestId(), tenantId, deviceId, response);
    tbClusterService.pushMsgToCore(msg, callback);
    return futureToSet;
}
Also used : FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) DeviceId(org.thingsboard.server.common.data.id.DeviceId) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata) UUID(java.util.UUID) FromDeviceRpcResponseActorMsg(org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg)

Aggregations

TbQueueCallback (org.thingsboard.server.queue.TbQueueCallback)7 TbQueueMsgMetadata (org.thingsboard.server.queue.TbQueueMsgMetadata)7 TbMsg (org.thingsboard.server.common.msg.TbMsg)4 JsonObject (com.google.gson.JsonObject)3 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 UUID (java.util.UUID)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1 AttributeKey (org.thingsboard.server.common.data.kv.AttributeKey)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 FromDeviceRpcResponse (org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse)1 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)1 FromDeviceRpcResponseActorMsg (org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg)1