Search in sources :

Example 1 with ActorContext

use of akka.actor.ActorContext in project thingsboard by thingsboard.

the class RuleManager method update.

public Optional<ActorRef> update(ActorContext context, RuleId ruleId, ComponentLifecycleEvent event) {
    if (ruleMap == null) {
        doInit(context);
    }
    RuleMetaData rule;
    if (event != ComponentLifecycleEvent.DELETED) {
        rule = systemContext.getRuleService().findRuleById(ruleId);
    } else {
        rule = ruleMap.keySet().stream().filter(r -> r.getId().equals(ruleId)).peek(r -> r.setState(ComponentLifecycleState.SUSPENDED)).findFirst().orElse(null);
        if (rule != null) {
            ruleMap.remove(rule);
            ruleActors.remove(ruleId);
        }
    }
    if (rule != null) {
        RuleActorMetaData actorMd = ruleMap.get(rule);
        if (actorMd == null) {
            ActorRef ref = getOrCreateRuleActor(context, rule.getId());
            actorMd = RuleActorMetaData.systemRule(rule.getId(), rule.getWeight(), ref);
            ruleMap.put(rule, actorMd);
        }
        refreshRuleChain();
        return Optional.of(actorMd.getActorRef());
    } else {
        log.warn("[{}] Can't process unknown rule!", ruleId);
        return Optional.empty();
    }
}
Also used : RuleId(org.thingsboard.server.common.data.id.RuleId) RuleService(org.thingsboard.server.dao.rule.RuleService) java.util(java.util) ActorContext(akka.actor.ActorContext) SimpleRuleActorChain(org.thingsboard.server.actors.rule.SimpleRuleActorChain) ContextAwareActor(org.thingsboard.server.actors.service.ContextAwareActor) FetchFunction(org.thingsboard.server.common.data.page.PageDataIterable.FetchFunction) TenantId(org.thingsboard.server.common.data.id.TenantId) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) Slf4j(lombok.extern.slf4j.Slf4j) RuleActor(org.thingsboard.server.actors.rule.RuleActor) RuleActorChain(org.thingsboard.server.actors.rule.RuleActorChain) PageDataIterable(org.thingsboard.server.common.data.page.PageDataIterable) ComponentLifecycleState(org.thingsboard.server.common.data.plugin.ComponentLifecycleState) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) ActorRef(akka.actor.ActorRef) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) Props(akka.actor.Props) RuleActorMetaData(org.thingsboard.server.actors.rule.RuleActorMetaData) RuleActorMetaData(org.thingsboard.server.actors.rule.RuleActorMetaData) ActorRef(akka.actor.ActorRef) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Example 2 with ActorContext

use of akka.actor.ActorContext in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processAttributesUpdate.

void processAttributesUpdate(ActorContext context, DeviceAttributesEventNotificationMsg msg) {
    refreshAttributes(msg);
    if (attributeSubscriptions.size() > 0) {
        ToDeviceMsg notification = null;
        if (msg.isDeleted()) {
            List<AttributeKey> sharedKeys = msg.getDeletedKeys().stream().filter(key -> DataConstants.SHARED_SCOPE.equals(key.getScope())).collect(Collectors.toList());
            notification = new AttributesUpdateNotification(BasicAttributeKVMsg.fromDeleted(sharedKeys));
        } else {
            if (DataConstants.SHARED_SCOPE.equals(msg.getScope())) {
                List<AttributeKvEntry> attributes = new ArrayList<>(msg.getValues());
                if (attributes.size() > 0) {
                    notification = new AttributesUpdateNotification(BasicAttributeKVMsg.fromShared(attributes));
                } else {
                    logger.debug("[{}] No public server side attributes changed!", deviceId);
                }
            }
        }
        if (notification != null) {
            ToDeviceMsg finalNotification = notification;
            attributeSubscriptions.entrySet().forEach(sub -> {
                ToDeviceSessionActorMsg response = new BasicToDeviceSessionActorMsg(finalNotification, sub.getKey());
                sendMsgToSessionActor(response, sub.getValue().getServer());
            });
        }
    } else {
        logger.debug("[{}] No registered attributes subscriptions to process!", deviceId);
    }
}
Also used : AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) java.util(java.util) Device(org.thingsboard.server.common.data.Device) TimeoutException(java.util.concurrent.TimeoutException) MsgType(org.thingsboard.server.common.msg.session.MsgType) TimeoutMsg(org.thingsboard.server.extensions.api.plugins.msg.TimeoutMsg) BasicAttributeKVMsg(org.thingsboard.server.common.msg.kv.BasicAttributeKVMsg) FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) ToDeviceRpcRequestBody(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) FromDeviceRpcResponse(org.thingsboard.server.extensions.api.plugins.msg.FromDeviceRpcResponse) ActorRef(akka.actor.ActorRef) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) AbstractContextAwareMsgProcessor(org.thingsboard.server.actors.shared.AbstractContextAwareMsgProcessor) RuleChainDeviceMsg(org.thingsboard.server.actors.tenant.RuleChainDeviceMsg) ClusterEventMsg(org.thingsboard.server.common.msg.cluster.ClusterEventMsg) ToPluginRpcResponseDeviceMsg(org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg) ToDeviceMsg(org.thingsboard.server.common.msg.session.ToDeviceMsg) AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) DeviceId(org.thingsboard.server.common.data.id.DeviceId) TimeoutIntMsg(org.thingsboard.server.extensions.api.plugins.msg.TimeoutIntMsg) DataConstants(org.thingsboard.server.common.data.DataConstants) ActorContext(akka.actor.ActorContext) ToDeviceRpcRequestPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestPluginMsg) SessionId(org.thingsboard.server.common.data.id.SessionId) org.thingsboard.server.common.msg.core(org.thingsboard.server.common.msg.core) Predicate(java.util.function.Predicate) LoggingAdapter(akka.event.LoggingAdapter) RpcError(org.thingsboard.server.extensions.api.plugins.msg.RpcError) org.thingsboard.server.actors.rule(org.thingsboard.server.actors.rule) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) org.thingsboard.server.extensions.api.device(org.thingsboard.server.extensions.api.device) ToDeviceActorMsg(org.thingsboard.server.common.msg.device.ToDeviceActorMsg) SessionType(org.thingsboard.server.common.msg.session.SessionType) ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ToDeviceMsg(org.thingsboard.server.common.msg.session.ToDeviceMsg)

Aggregations

ActorContext (akka.actor.ActorContext)2 ActorRef (akka.actor.ActorRef)2 java.util (java.util)2 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)2 Props (akka.actor.Props)1 LoggingAdapter (akka.event.LoggingAdapter)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 org.thingsboard.server.actors.rule (org.thingsboard.server.actors.rule)1 RuleActor (org.thingsboard.server.actors.rule.RuleActor)1 RuleActorChain (org.thingsboard.server.actors.rule.RuleActorChain)1 RuleActorMetaData (org.thingsboard.server.actors.rule.RuleActorMetaData)1 SimpleRuleActorChain (org.thingsboard.server.actors.rule.SimpleRuleActorChain)1 ContextAwareActor (org.thingsboard.server.actors.service.ContextAwareActor)1 AbstractContextAwareMsgProcessor (org.thingsboard.server.actors.shared.AbstractContextAwareMsgProcessor)1 RuleChainDeviceMsg (org.thingsboard.server.actors.tenant.RuleChainDeviceMsg)1