Search in sources :

Example 1 with TbActorCtx

use of org.thingsboard.server.actors.TbActorCtx in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processRpcRequest.

void processRpcRequest(TbActorCtx context, ToDeviceRpcRequestActorMsg msg) {
    ToDeviceRpcRequest request = msg.getMsg();
    ToDeviceRpcRequestMsg rpcRequest = creteToDeviceRpcRequestMsg(request);
    long timeout = request.getExpirationTime() - System.currentTimeMillis();
    boolean persisted = request.isPersisted();
    if (timeout <= 0) {
        log.debug("[{}][{}] Ignoring message due to exp time reached, {}", deviceId, request.getId(), request.getExpirationTime());
        if (persisted) {
            createRpc(request, RpcStatus.EXPIRED);
        }
        return;
    } else if (persisted) {
        createRpc(request, RpcStatus.QUEUED);
    }
    boolean sent = false;
    if (systemContext.isEdgesEnabled() && edgeId != null) {
        log.debug("[{}][{}] device is related to edge [{}]. Saving RPC request to edge queue", tenantId, deviceId, edgeId.getId());
        saveRpcRequestToEdgeQueue(request, rpcRequest.getRequestId());
        sent = true;
    } else if (isSendNewRpcAvailable()) {
        sent = rpcSubscriptions.size() > 0;
        Set<UUID> syncSessionSet = new HashSet<>();
        rpcSubscriptions.forEach((key, value) -> {
            sendToTransport(rpcRequest, key, value.getNodeId());
            if (SessionType.SYNC == value.getType()) {
                syncSessionSet.add(key);
            }
        });
        log.trace("Rpc syncSessionSet [{}] subscription after sent [{}]", syncSessionSet, rpcSubscriptions);
        syncSessionSet.forEach(rpcSubscriptions::remove);
    }
    if (persisted) {
        ObjectNode response = JacksonUtil.newObjectNode();
        response.put("rpcId", request.getId().toString());
        systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(msg.getMsg().getId(), JacksonUtil.toString(response), null));
    }
    if (!persisted && request.isOneway() && sent) {
        log.debug("[{}] Rpc command response sent [{}]!", deviceId, request.getId());
        systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(msg.getMsg().getId(), null, null));
    } else {
        registerPendingRpcRequest(context, msg, sent, rpcRequest, timeout);
    }
    if (sent) {
        log.debug("[{}] RPC request {} is sent!", deviceId, request.getId());
    } else {
        log.debug("[{}] RPC request {} is NOT sent!", deviceId, request.getId());
    }
}
Also used : Arrays(java.util.Arrays) RpcStatus(org.thingsboard.server.common.data.rpc.RpcStatus) KeyValueType(org.thingsboard.server.gen.transport.TransportProtos.KeyValueType) ToDeviceRpcRequestActorMsg(org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg) TenantId(org.thingsboard.server.common.data.id.TenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) SessionType(org.thingsboard.server.gen.transport.TransportProtos.SessionType) FromDeviceRpcResponseActorMsg(org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) Rpc(org.thingsboard.server.common.data.rpc.Rpc) AbstractContextAwareMsgProcessor(org.thingsboard.server.actors.shared.AbstractContextAwareMsgProcessor) SubscribeToAttributeUpdatesMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToAttributeUpdatesMsg) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) DeviceAttributesEventNotificationMsg(org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg) DeviceCredentialsUpdateNotificationMsg(org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg) EdgeId(org.thingsboard.server.common.data.id.EdgeId) GetAttributeResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg) TransportToDeviceActorMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ClaimDeviceMsg(org.thingsboard.server.gen.transport.TransportProtos.ClaimDeviceMsg) DeviceId(org.thingsboard.server.common.data.id.DeviceId) PageLink(org.thingsboard.server.common.data.page.PageLink) ToTransportUpdateCredentialsProto(org.thingsboard.server.gen.transport.TransportProtos.ToTransportUpdateCredentialsProto) StringUtils(org.thingsboard.server.common.data.StringUtils) Set(java.util.Set) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) UUID(java.util.UUID) SortOrder(org.thingsboard.server.common.data.page.SortOrder) Collectors(java.util.stream.Collectors) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) ToServerRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SubscribeToRPCMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToRPCMsg) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) DeviceNameOrTypeUpdateMsg(org.thingsboard.rule.engine.api.msg.DeviceNameOrTypeUpdateMsg) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) GetAttributeRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg) Optional(java.util.Optional) ToDeviceRpcRequestBody(org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody) RemoveRpcActorMsg(org.thingsboard.server.service.rpc.RemoveRpcActorMsg) ConcurrentModificationException(java.util.ConcurrentModificationException) AttributeUpdateNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg) DeviceActorServerSideRpcTimeoutMsg(org.thingsboard.server.common.msg.timeout.DeviceActorServerSideRpcTimeoutMsg) ToDeviceRpcRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ToDeviceRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseMsg) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ToDeviceRpcRequest(org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest) Device(org.thingsboard.server.common.data.Device) JacksonUtil(org.thingsboard.common.util.JacksonUtil) HashMap(java.util.HashMap) DeviceEdgeUpdateMsg(org.thingsboard.rule.engine.api.msg.DeviceEdgeUpdateMsg) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SessionSubscriptionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionSubscriptionInfoProto) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMapRemoveEldest(org.thingsboard.common.util.LinkedHashMapRemoveEldest) CollectionUtils(org.apache.commons.collections.CollectionUtils) SessionCloseNotificationProto(org.thingsboard.server.gen.transport.TransportProtos.SessionCloseNotificationProto) KeyValueProto(org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto) SessionEvent(org.thingsboard.server.gen.transport.TransportProtos.SessionEvent) DeviceSessionsCacheEntry(org.thingsboard.server.gen.transport.TransportProtos.DeviceSessionsCacheEntry) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) Nullable(javax.annotation.Nullable) AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) DataConstants(org.thingsboard.server.common.data.DataConstants) RpcId(org.thingsboard.server.common.data.id.RpcId) FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) SessionEventMsg(org.thingsboard.server.gen.transport.TransportProtos.SessionEventMsg) SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) RpcError(org.thingsboard.server.common.data.rpc.RpcError) FutureCallback(com.google.common.util.concurrent.FutureCallback) TsKvProto(org.thingsboard.server.gen.transport.TransportProtos.TsKvProto) Consumer(java.util.function.Consumer) SubscriptionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SubscriptionInfoProto) ToTransportMsg(org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg) Futures(com.google.common.util.concurrent.Futures) PageData(org.thingsboard.server.common.data.page.PageData) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ToDeviceRpcResponseStatusMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseStatusMsg) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ToDeviceRpcRequest(org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest) ToDeviceRpcRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg)

Example 2 with TbActorCtx

use of org.thingsboard.server.actors.TbActorCtx in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method sendPendingRequests.

private void sendPendingRequests(TbActorCtx context, UUID sessionId, String nodeId) {
    SessionType sessionType = getSessionType(sessionId);
    if (!toDeviceRpcPendingMap.isEmpty()) {
        log.debug("[{}] Pushing {} pending RPC messages to new async session [{}]", deviceId, toDeviceRpcPendingMap.size(), sessionId);
        if (sessionType == SessionType.SYNC) {
            log.debug("[{}] Cleanup sync rpc session [{}]", deviceId, sessionId);
            rpcSubscriptions.remove(sessionId);
        }
    } else {
        log.debug("[{}] No pending RPC messages for new async session [{}]", deviceId, sessionId);
    }
    Set<Integer> sentOneWayIds = new HashSet<>();
    if (rpcSequential) {
        getFirstRpc().ifPresent(processPendingRpc(context, sessionId, nodeId, sentOneWayIds));
    } else if (sessionType == SessionType.ASYNC) {
        toDeviceRpcPendingMap.entrySet().forEach(processPendingRpc(context, sessionId, nodeId, sentOneWayIds));
    } else {
        toDeviceRpcPendingMap.entrySet().stream().findFirst().ifPresent(processPendingRpc(context, sessionId, nodeId, sentOneWayIds));
    }
    sentOneWayIds.stream().filter(id -> !toDeviceRpcPendingMap.get(id).getMsg().getMsg().isPersisted()).forEach(toDeviceRpcPendingMap::remove);
}
Also used : Arrays(java.util.Arrays) RpcStatus(org.thingsboard.server.common.data.rpc.RpcStatus) KeyValueType(org.thingsboard.server.gen.transport.TransportProtos.KeyValueType) ToDeviceRpcRequestActorMsg(org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg) TenantId(org.thingsboard.server.common.data.id.TenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) SessionType(org.thingsboard.server.gen.transport.TransportProtos.SessionType) FromDeviceRpcResponseActorMsg(org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) Rpc(org.thingsboard.server.common.data.rpc.Rpc) AbstractContextAwareMsgProcessor(org.thingsboard.server.actors.shared.AbstractContextAwareMsgProcessor) SubscribeToAttributeUpdatesMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToAttributeUpdatesMsg) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) DeviceAttributesEventNotificationMsg(org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg) DeviceCredentialsUpdateNotificationMsg(org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg) EdgeId(org.thingsboard.server.common.data.id.EdgeId) GetAttributeResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg) TransportToDeviceActorMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ClaimDeviceMsg(org.thingsboard.server.gen.transport.TransportProtos.ClaimDeviceMsg) DeviceId(org.thingsboard.server.common.data.id.DeviceId) PageLink(org.thingsboard.server.common.data.page.PageLink) ToTransportUpdateCredentialsProto(org.thingsboard.server.gen.transport.TransportProtos.ToTransportUpdateCredentialsProto) StringUtils(org.thingsboard.server.common.data.StringUtils) Set(java.util.Set) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) UUID(java.util.UUID) SortOrder(org.thingsboard.server.common.data.page.SortOrder) Collectors(java.util.stream.Collectors) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) ToServerRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SubscribeToRPCMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToRPCMsg) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) DeviceNameOrTypeUpdateMsg(org.thingsboard.rule.engine.api.msg.DeviceNameOrTypeUpdateMsg) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) GetAttributeRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg) Optional(java.util.Optional) ToDeviceRpcRequestBody(org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody) RemoveRpcActorMsg(org.thingsboard.server.service.rpc.RemoveRpcActorMsg) ConcurrentModificationException(java.util.ConcurrentModificationException) AttributeUpdateNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg) DeviceActorServerSideRpcTimeoutMsg(org.thingsboard.server.common.msg.timeout.DeviceActorServerSideRpcTimeoutMsg) ToDeviceRpcRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ToDeviceRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseMsg) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ToDeviceRpcRequest(org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest) Device(org.thingsboard.server.common.data.Device) JacksonUtil(org.thingsboard.common.util.JacksonUtil) HashMap(java.util.HashMap) DeviceEdgeUpdateMsg(org.thingsboard.rule.engine.api.msg.DeviceEdgeUpdateMsg) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SessionSubscriptionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionSubscriptionInfoProto) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMapRemoveEldest(org.thingsboard.common.util.LinkedHashMapRemoveEldest) CollectionUtils(org.apache.commons.collections.CollectionUtils) SessionCloseNotificationProto(org.thingsboard.server.gen.transport.TransportProtos.SessionCloseNotificationProto) KeyValueProto(org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto) SessionEvent(org.thingsboard.server.gen.transport.TransportProtos.SessionEvent) DeviceSessionsCacheEntry(org.thingsboard.server.gen.transport.TransportProtos.DeviceSessionsCacheEntry) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) Nullable(javax.annotation.Nullable) AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) DataConstants(org.thingsboard.server.common.data.DataConstants) RpcId(org.thingsboard.server.common.data.id.RpcId) FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) SessionEventMsg(org.thingsboard.server.gen.transport.TransportProtos.SessionEventMsg) SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) RpcError(org.thingsboard.server.common.data.rpc.RpcError) FutureCallback(com.google.common.util.concurrent.FutureCallback) TsKvProto(org.thingsboard.server.gen.transport.TransportProtos.TsKvProto) Consumer(java.util.function.Consumer) SubscriptionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SubscriptionInfoProto) ToTransportMsg(org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg) Futures(com.google.common.util.concurrent.Futures) PageData(org.thingsboard.server.common.data.page.PageData) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ToDeviceRpcResponseStatusMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseStatusMsg) Collections(java.util.Collections) SessionType(org.thingsboard.server.gen.transport.TransportProtos.SessionType) HashSet(java.util.HashSet)

Example 3 with TbActorCtx

use of org.thingsboard.server.actors.TbActorCtx in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method onUpdate.

@Override
public void onUpdate(TbActorCtx context) {
    RuleChain ruleChain = service.findRuleChainById(tenantId, entityId);
    if (ruleChain != null && RuleChainType.CORE.equals(ruleChain.getType())) {
        ruleChainName = ruleChain.getName();
        List<RuleNode> ruleNodeList = service.getRuleChainNodes(tenantId, entityId);
        log.trace("[{}][{}] Updating rule chain with {} nodes", tenantId, entityId, ruleNodeList.size());
        for (RuleNode ruleNode : ruleNodeList) {
            RuleNodeCtx existing = nodeActors.get(ruleNode.getId());
            if (existing == null) {
                log.trace("[{}][{}] Creating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                TbActorRef ruleNodeActor = createRuleNodeActor(context, ruleNode);
                nodeActors.put(ruleNode.getId(), new RuleNodeCtx(tenantId, self, ruleNodeActor, ruleNode));
            } else {
                log.trace("[{}][{}] Updating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                existing.setSelf(ruleNode);
                existing.getSelfActor().tellWithHighPriority(new RuleNodeUpdatedMsg(tenantId, existing.getSelf().getId()));
            }
        }
        Set<RuleNodeId> existingNodes = ruleNodeList.stream().map(RuleNode::getId).collect(Collectors.toSet());
        List<RuleNodeId> removedRules = nodeActors.keySet().stream().filter(node -> !existingNodes.contains(node)).collect(Collectors.toList());
        removedRules.forEach(ruleNodeId -> {
            log.trace("[{}][{}] Removing rule node [{}]", tenantId, entityId, ruleNodeId);
            RuleNodeCtx removed = nodeActors.remove(ruleNodeId);
            removed.getSelfActor().tellWithHighPriority(new ComponentLifecycleMsg(tenantId, removed.getSelf().getId(), ComponentLifecycleEvent.DELETED));
        });
        initRoutes(ruleChain, ruleNodeList);
    }
}
Also used : TbMsg(org.thingsboard.server.common.msg.TbMsg) HashMap(java.util.HashMap) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) ComponentMsgProcessor(org.thingsboard.server.actors.shared.ComponentMsgProcessor) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) DefaultActorService(org.thingsboard.server.actors.service.DefaultActorService) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) ComponentLifecycleState(org.thingsboard.server.common.data.plugin.ComponentLifecycleState) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) TbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.TbQueueTbMsgCallbackWrapper) RuleNodeUpdatedMsg(org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) TbApiUsageClient(org.thingsboard.server.queue.usagestats.TbApiUsageClient) TbEntityActorId(org.thingsboard.server.actors.TbEntityActorId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) Set(java.util.Set) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) TbRelationTypes(org.thingsboard.rule.engine.api.TbRelationTypes) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) MultipleTbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.MultipleTbQueueTbMsgCallbackWrapper) TbClusterService(org.thingsboard.server.cluster.TbClusterService) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Collections(java.util.Collections) TbActorRef(org.thingsboard.server.actors.TbActorRef) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbActorRef(org.thingsboard.server.actors.TbActorRef) RuleNodeUpdatedMsg(org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 4 with TbActorCtx

use of org.thingsboard.server.actors.TbActorCtx in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processUplinkNotificationMsg.

private void processUplinkNotificationMsg(TbActorCtx context, SessionInfoProto sessionInfo, TransportProtos.UplinkNotificationMsg uplinkNotificationMsg) {
    String nodeId = sessionInfo.getNodeId();
    sessions.entrySet().stream().filter(kv -> kv.getValue().getSessionInfo().getNodeId().equals(nodeId) && (kv.getValue().isSubscribedToAttributes() || kv.getValue().isSubscribedToRPC())).forEach(kv -> {
        ToTransportMsg msg = ToTransportMsg.newBuilder().setSessionIdMSB(kv.getKey().getMostSignificantBits()).setSessionIdLSB(kv.getKey().getLeastSignificantBits()).setUplinkNotificationMsg(uplinkNotificationMsg).build();
        systemContext.getTbCoreToTransportService().process(kv.getValue().getSessionInfo().getNodeId(), msg);
    });
}
Also used : Arrays(java.util.Arrays) RpcStatus(org.thingsboard.server.common.data.rpc.RpcStatus) KeyValueType(org.thingsboard.server.gen.transport.TransportProtos.KeyValueType) ToDeviceRpcRequestActorMsg(org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg) TenantId(org.thingsboard.server.common.data.id.TenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) SessionType(org.thingsboard.server.gen.transport.TransportProtos.SessionType) FromDeviceRpcResponseActorMsg(org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) Rpc(org.thingsboard.server.common.data.rpc.Rpc) AbstractContextAwareMsgProcessor(org.thingsboard.server.actors.shared.AbstractContextAwareMsgProcessor) SubscribeToAttributeUpdatesMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToAttributeUpdatesMsg) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) DeviceAttributesEventNotificationMsg(org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg) DeviceCredentialsUpdateNotificationMsg(org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg) EdgeId(org.thingsboard.server.common.data.id.EdgeId) GetAttributeResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg) TransportToDeviceActorMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ClaimDeviceMsg(org.thingsboard.server.gen.transport.TransportProtos.ClaimDeviceMsg) DeviceId(org.thingsboard.server.common.data.id.DeviceId) PageLink(org.thingsboard.server.common.data.page.PageLink) ToTransportUpdateCredentialsProto(org.thingsboard.server.gen.transport.TransportProtos.ToTransportUpdateCredentialsProto) StringUtils(org.thingsboard.server.common.data.StringUtils) Set(java.util.Set) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) UUID(java.util.UUID) SortOrder(org.thingsboard.server.common.data.page.SortOrder) Collectors(java.util.stream.Collectors) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) ToServerRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SubscribeToRPCMsg(org.thingsboard.server.gen.transport.TransportProtos.SubscribeToRPCMsg) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) DeviceNameOrTypeUpdateMsg(org.thingsboard.rule.engine.api.msg.DeviceNameOrTypeUpdateMsg) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) GetAttributeRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg) Optional(java.util.Optional) ToDeviceRpcRequestBody(org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody) RemoveRpcActorMsg(org.thingsboard.server.service.rpc.RemoveRpcActorMsg) ConcurrentModificationException(java.util.ConcurrentModificationException) AttributeUpdateNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg) DeviceActorServerSideRpcTimeoutMsg(org.thingsboard.server.common.msg.timeout.DeviceActorServerSideRpcTimeoutMsg) ToDeviceRpcRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ToDeviceRpcResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseMsg) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ToDeviceRpcRequest(org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest) Device(org.thingsboard.server.common.data.Device) JacksonUtil(org.thingsboard.common.util.JacksonUtil) HashMap(java.util.HashMap) DeviceEdgeUpdateMsg(org.thingsboard.rule.engine.api.msg.DeviceEdgeUpdateMsg) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SessionSubscriptionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionSubscriptionInfoProto) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMapRemoveEldest(org.thingsboard.common.util.LinkedHashMapRemoveEldest) CollectionUtils(org.apache.commons.collections.CollectionUtils) SessionCloseNotificationProto(org.thingsboard.server.gen.transport.TransportProtos.SessionCloseNotificationProto) KeyValueProto(org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto) SessionEvent(org.thingsboard.server.gen.transport.TransportProtos.SessionEvent) DeviceSessionsCacheEntry(org.thingsboard.server.gen.transport.TransportProtos.DeviceSessionsCacheEntry) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) Nullable(javax.annotation.Nullable) AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) DataConstants(org.thingsboard.server.common.data.DataConstants) RpcId(org.thingsboard.server.common.data.id.RpcId) FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) SessionEventMsg(org.thingsboard.server.gen.transport.TransportProtos.SessionEventMsg) SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) RpcError(org.thingsboard.server.common.data.rpc.RpcError) FutureCallback(com.google.common.util.concurrent.FutureCallback) TsKvProto(org.thingsboard.server.gen.transport.TransportProtos.TsKvProto) Consumer(java.util.function.Consumer) SubscriptionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SubscriptionInfoProto) ToTransportMsg(org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg) Futures(com.google.common.util.concurrent.Futures) PageData(org.thingsboard.server.common.data.page.PageData) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ToDeviceRpcResponseStatusMsg(org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseStatusMsg) Collections(java.util.Collections) ToTransportMsg(org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg)

Example 5 with TbActorCtx

use of org.thingsboard.server.actors.TbActorCtx in project thingsboard by thingsboard.

the class TenantActor method init.

@Override
public void init(TbActorCtx ctx) throws TbActorException {
    super.init(ctx);
    log.debug("[{}] Starting tenant actor.", tenantId);
    try {
        Tenant tenant = systemContext.getTenantService().findTenantById(tenantId);
        if (tenant == null) {
            cantFindTenant = true;
            log.info("[{}] Started tenant actor for missing tenant.", tenantId);
        } else {
            // This Service may be started for specific tenant only.
            Optional<TenantId> isolatedTenantId = systemContext.getServiceInfoProvider().getIsolatedTenant();
            TenantProfile tenantProfile = systemContext.getTenantProfileCache().get(tenant.getTenantProfileId());
            isCore = systemContext.getServiceInfoProvider().isService(ServiceType.TB_CORE);
            isRuleEngine = systemContext.getServiceInfoProvider().isService(ServiceType.TB_RULE_ENGINE);
            if (isRuleEngine) {
                try {
                    if (isolatedTenantId.map(id -> id.equals(tenantId)).orElseGet(() -> !tenantProfile.isIsolatedTbRuleEngine())) {
                        if (getApiUsageState().isReExecEnabled()) {
                            log.debug("[{}] Going to init rule chains", tenantId);
                            initRuleChains();
                        } else {
                            log.info("[{}] Skip init of the rule chains due to API limits", tenantId);
                        }
                    } else {
                        isRuleEngine = false;
                    }
                } catch (Exception e) {
                    cantFindTenant = true;
                }
            }
            log.debug("[{}] Tenant actor started.", tenantId);
        }
    } catch (Exception e) {
        log.warn("[{}] Unknown failure", tenantId, e);
    }
}
Also used : TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) Edge(org.thingsboard.server.common.data.edge.Edge) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleChainManagerActor(org.thingsboard.server.actors.ruleChain.RuleChainManagerActor) EdgeEventUpdateMsg(org.thingsboard.server.common.msg.edge.EdgeEventUpdateMsg) Tenant(org.thingsboard.server.common.data.Tenant) TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceActorCreator(org.thingsboard.server.actors.device.DeviceActorCreator) DefaultActorService(org.thingsboard.server.actors.service.DefaultActorService) TbActorId(org.thingsboard.server.actors.TbActorId) TbActorNotRegisteredException(org.thingsboard.server.actors.TbActorNotRegisteredException) MsgType(org.thingsboard.server.common.msg.MsgType) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) EntityId(org.thingsboard.server.common.data.id.EntityId) TbEntityTypeActorIdPredicate(org.thingsboard.server.actors.TbEntityTypeActorIdPredicate) EntityType(org.thingsboard.server.common.data.EntityType) DeviceAwareMsg(org.thingsboard.server.common.msg.aware.DeviceAwareMsg) EdgeId(org.thingsboard.server.common.data.id.EdgeId) TbEntityActorId(org.thingsboard.server.actors.TbEntityActorId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) EdgeRpcService(org.thingsboard.server.service.edge.rpc.EdgeRpcService) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) Slf4j(lombok.extern.slf4j.Slf4j) TbActorException(org.thingsboard.server.actors.TbActorException) List(java.util.List) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Optional(java.util.Optional) ContextBasedCreator(org.thingsboard.server.actors.service.ContextBasedCreator) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleChainAwareMsg(org.thingsboard.server.common.msg.aware.RuleChainAwareMsg) TbActor(org.thingsboard.server.actors.TbActor) TbActorRef(org.thingsboard.server.actors.TbActorRef) TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) TenantProfile(org.thingsboard.server.common.data.TenantProfile) TbActorNotRegisteredException(org.thingsboard.server.actors.TbActorNotRegisteredException) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) TbActorException(org.thingsboard.server.actors.TbActorException)

Aggregations

List (java.util.List)5 Slf4j (lombok.extern.slf4j.Slf4j)5 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)5 TbActorCtx (org.thingsboard.server.actors.TbActorCtx)5 TenantId (org.thingsboard.server.common.data.id.TenantId)5 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Set (java.util.Set)4 UUID (java.util.UUID)4 Collectors (java.util.stream.Collectors)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 FutureCallback (com.google.common.util.concurrent.FutureCallback)3 Futures (com.google.common.util.concurrent.Futures)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3