Search in sources :

Example 41 with RuleChainId

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

the class DefaultTbContext method deviceCreatedMsg.

public TbMsg deviceCreatedMsg(Device device, RuleNodeId ruleNodeId) {
    RuleChainId ruleChainId = null;
    String queueName = ServiceQueue.MAIN;
    if (device.getDeviceProfileId() != null) {
        DeviceProfile deviceProfile = mainCtx.getDeviceProfileCache().find(device.getDeviceProfileId());
        if (deviceProfile == null) {
            log.warn("[{}] Device profile is null!", device.getDeviceProfileId());
            ruleChainId = null;
            queueName = ServiceQueue.MAIN;
        } else {
            ruleChainId = deviceProfile.getDefaultRuleChainId();
            String defaultQueueName = deviceProfile.getDefaultQueueName();
            queueName = defaultQueueName != null ? defaultQueueName : ServiceQueue.MAIN;
        }
    }
    return entityActionMsg(device, device.getId(), ruleNodeId, DataConstants.ENTITY_CREATED, queueName, ruleChainId);
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

Example 42 with RuleChainId

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

the class TbMsg method fromBytes.

public static TbMsg fromBytes(String queueName, byte[] data, TbMsgCallback callback) {
    try {
        MsgProtos.TbMsgProto proto = MsgProtos.TbMsgProto.parseFrom(data);
        TbMsgMetaData metaData = new TbMsgMetaData(proto.getMetaData().getDataMap());
        EntityId entityId = EntityIdFactory.getByTypeAndUuid(proto.getEntityType(), new UUID(proto.getEntityIdMSB(), proto.getEntityIdLSB()));
        CustomerId customerId = null;
        RuleChainId ruleChainId = null;
        RuleNodeId ruleNodeId = null;
        if (proto.getCustomerIdMSB() != 0L && proto.getCustomerIdLSB() != 0L) {
            customerId = new CustomerId(new UUID(proto.getCustomerIdMSB(), proto.getCustomerIdLSB()));
        }
        if (proto.getRuleChainIdMSB() != 0L && proto.getRuleChainIdLSB() != 0L) {
            ruleChainId = new RuleChainId(new UUID(proto.getRuleChainIdMSB(), proto.getRuleChainIdLSB()));
        }
        if (proto.getRuleNodeIdMSB() != 0L && proto.getRuleNodeIdLSB() != 0L) {
            ruleNodeId = new RuleNodeId(new UUID(proto.getRuleNodeIdMSB(), proto.getRuleNodeIdLSB()));
        }
        TbMsgProcessingCtx ctx;
        if (proto.hasCtx()) {
            ctx = TbMsgProcessingCtx.fromProto(proto.getCtx());
        } else {
            // Backward compatibility with unprocessed messages fetched from queue after update.
            ctx = new TbMsgProcessingCtx(proto.getRuleNodeExecCounter());
        }
        TbMsgDataType dataType = TbMsgDataType.values()[proto.getDataType()];
        return new TbMsg(queueName, UUID.fromString(proto.getId()), proto.getTs(), proto.getType(), entityId, customerId, metaData, dataType, proto.getData(), ruleChainId, ruleNodeId, ctx, callback);
    } catch (InvalidProtocolBufferException e) {
        throw new IllegalStateException("Could not parse protobuf for TbMsg", e);
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) MsgProtos(org.thingsboard.server.common.msg.gen.MsgProtos) EntityId(org.thingsboard.server.common.data.id.EntityId) UUID(java.util.UUID) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 43 with RuleChainId

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

the class RuleChainEdgeProcessor method processRuleChainToEdge.

public DownlinkMsg processRuleChainToEdge(Edge edge, EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeEventActionType action) {
    RuleChainId ruleChainId = new RuleChainId(edgeEvent.getEntityId());
    DownlinkMsg downlinkMsg = null;
    switch(action) {
        case ADDED:
        case UPDATED:
        case ASSIGNED_TO_EDGE:
            RuleChain ruleChain = ruleChainService.findRuleChainById(edgeEvent.getTenantId(), ruleChainId);
            if (ruleChain != null) {
                RuleChainUpdateMsg ruleChainUpdateMsg = ruleChainMsgConstructor.constructRuleChainUpdatedMsg(edge.getRootRuleChainId(), msgType, ruleChain);
                downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addRuleChainUpdateMsg(ruleChainUpdateMsg).build();
            }
            break;
        case DELETED:
        case UNASSIGNED_FROM_EDGE:
            downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addRuleChainUpdateMsg(ruleChainMsgConstructor.constructRuleChainDeleteMsg(ruleChainId)).build();
            break;
    }
    return downlinkMsg;
}
Also used : DownlinkMsg(org.thingsboard.server.gen.edge.v1.DownlinkMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleChainUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainUpdateMsg)

Example 44 with RuleChainId

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

the class TelemetryEdgeProcessor method processPostTelemetry.

private ListenableFuture<Void> processPostTelemetry(TenantId tenantId, CustomerId customerId, EntityId entityId, TransportProtos.PostTelemetryMsg msg, TbMsgMetaData metaData) {
    SettableFuture<Void> futureToSet = SettableFuture.create();
    for (TransportProtos.TsKvListProto tsKv : msg.getTsKvListList()) {
        JsonObject json = JsonUtils.getJsonObject(tsKv.getKvList());
        metaData.putValue("ts", tsKv.getTs() + "");
        Pair<String, RuleChainId> defaultQueueAndRuleChain = getDefaultQueueNameAndRuleChainId(tenantId, entityId);
        String queueName = defaultQueueAndRuleChain.getKey();
        RuleChainId ruleChainId = defaultQueueAndRuleChain.getValue();
        TbMsg tbMsg = TbMsg.newMsg(queueName, SessionMsgType.POST_TELEMETRY_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 telemetry [{}]", 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) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Example 45 with RuleChainId

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

the class RuleChainMsgConstructor method addRuleChainConnections_V_3_3_0.

private List<RuleChainConnectionInfo> addRuleChainConnections_V_3_3_0(List<RuleNode> nodes, List<NodeConnectionInfo> connections) throws JsonProcessingException {
    List<RuleChainConnectionInfo> result = new ArrayList<>();
    for (int i = 0; i < nodes.size(); i++) {
        RuleNode node = nodes.get(i);
        if (node.getType().equalsIgnoreCase(RULE_CHAIN_INPUT_NODE)) {
            for (NodeConnectionInfo connection : connections) {
                if (connection.getToIndex() == i) {
                    RuleChainConnectionInfo e = new RuleChainConnectionInfo();
                    e.setFromIndex(connection.getFromIndex());
                    TbRuleChainInputNodeConfiguration configuration = JacksonUtil.treeToValue(node.getConfiguration(), TbRuleChainInputNodeConfiguration.class);
                    e.setTargetRuleChainId(new RuleChainId(UUID.fromString(configuration.getRuleChainId())));
                    e.setAdditionalInfo(node.getAdditionalInfo());
                    e.setType(connection.getType());
                    result.add(e);
                }
            }
        }
    }
    return result;
}
Also used : NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) ArrayList(java.util.ArrayList) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo)

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