Search in sources :

Example 21 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId 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 22 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class TelemetryEdgeProcessor method getDefaultQueueNameAndRuleChainId.

private Pair<String, RuleChainId> getDefaultQueueNameAndRuleChainId(TenantId tenantId, EntityId entityId) {
    if (EntityType.DEVICE.equals(entityId.getEntityType())) {
        DeviceProfile deviceProfile = deviceProfileCache.get(tenantId, new DeviceId(entityId.getId()));
        RuleChainId ruleChainId;
        String queueName;
        if (deviceProfile == null) {
            log.warn("[{}] Device profile is null!", entityId);
            ruleChainId = null;
            queueName = ServiceQueue.MAIN;
        } else {
            ruleChainId = deviceProfile.getDefaultRuleChainId();
            String defaultQueueName = deviceProfile.getDefaultQueueName();
            queueName = defaultQueueName != null ? defaultQueueName : ServiceQueue.MAIN;
        }
        return new ImmutablePair<>(queueName, ruleChainId);
    } else {
        return new ImmutablePair<>(ServiceQueue.MAIN, null);
    }
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

Example 23 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId 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 24 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class DefaultEdgeRequestsService method processRuleChainMetadataRequestMsg.

@Override
public ListenableFuture<Void> processRuleChainMetadataRequestMsg(TenantId tenantId, Edge edge, RuleChainMetadataRequestMsg ruleChainMetadataRequestMsg) {
    log.trace("[{}] processRuleChainMetadataRequestMsg [{}][{}]", tenantId, edge.getName(), ruleChainMetadataRequestMsg);
    if (ruleChainMetadataRequestMsg.getRuleChainIdMSB() != 0 && ruleChainMetadataRequestMsg.getRuleChainIdLSB() != 0) {
        RuleChainId ruleChainId = new RuleChainId(new UUID(ruleChainMetadataRequestMsg.getRuleChainIdMSB(), ruleChainMetadataRequestMsg.getRuleChainIdLSB()));
        saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.RULE_CHAIN_METADATA, EdgeEventActionType.ADDED, ruleChainId, null);
    }
    return Futures.immediateFuture(null);
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) UUID(java.util.UUID)

Example 25 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class RuleChainEdgeProcessor method processRuleChainMetadataToEdge.

public DownlinkMsg processRuleChainMetadataToEdge(EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeVersion edgeVersion) {
    RuleChainId ruleChainId = new RuleChainId(edgeEvent.getEntityId());
    RuleChain ruleChain = ruleChainService.findRuleChainById(edgeEvent.getTenantId(), ruleChainId);
    DownlinkMsg downlinkMsg = null;
    if (ruleChain != null) {
        RuleChainMetaData ruleChainMetaData = ruleChainService.loadRuleChainMetaData(edgeEvent.getTenantId(), ruleChainId);
        RuleChainMetadataUpdateMsg ruleChainMetadataUpdateMsg = ruleChainMsgConstructor.constructRuleChainMetadataUpdatedMsg(msgType, ruleChainMetaData, edgeVersion);
        if (ruleChainMetadataUpdateMsg != null) {
            downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addRuleChainMetadataUpdateMsg(ruleChainMetadataUpdateMsg).build();
        }
    }
    return downlinkMsg;
}
Also used : DownlinkMsg(org.thingsboard.server.gen.edge.v1.DownlinkMsg) RuleChainMetadataUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

Aggregations

RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)60 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)22 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)20 Test (org.junit.Test)17 TbMsg (org.thingsboard.server.common.msg.TbMsg)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)12 EntityId (org.thingsboard.server.common.data.id.EntityId)12 ApiOperation (io.swagger.annotations.ApiOperation)11 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 TenantId (org.thingsboard.server.common.data.id.TenantId)11 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)11 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)10 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)8 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 IOException (java.io.IOException)7 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6