Search in sources :

Example 1 with ToDeviceActorMsg

use of org.thingsboard.server.common.msg.device.ToDeviceActorMsg in project thingsboard by thingsboard.

the class TenantActor method process.

private void process(RuleChainDeviceMsg msg) {
    ToDeviceActorMsg toDeviceActorMsg = msg.getToDeviceActorMsg();
    ActorRef deviceActor = getOrCreateDeviceActor(toDeviceActorMsg.getDeviceId());
    RuleActorChain tenantChain = ruleManager.getRuleChain(this.context());
    RuleActorChain chain = new ComplexRuleActorChain(msg.getRuleChain(), tenantChain);
    deviceActor.tell(new RuleChainDeviceMsg(toDeviceActorMsg, chain), context().self());
}
Also used : ComplexRuleActorChain(org.thingsboard.server.actors.rule.ComplexRuleActorChain) RuleActorChain(org.thingsboard.server.actors.rule.RuleActorChain) ToDeviceActorMsg(org.thingsboard.server.common.msg.device.ToDeviceActorMsg) ComplexRuleActorChain(org.thingsboard.server.actors.rule.ComplexRuleActorChain) ActorRef(akka.actor.ActorRef)

Example 2 with ToDeviceActorMsg

use of org.thingsboard.server.common.msg.device.ToDeviceActorMsg in project thingsboard by thingsboard.

the class ASyncMsgProcessor method processToDeviceActorMsg.

@Override
protected void processToDeviceActorMsg(ActorContext ctx, ToDeviceActorSessionMsg msg) {
    updateSessionCtx(msg, SessionType.ASYNC);
    if (firstMsg) {
        toDeviceMsg(new SessionOpenMsg()).ifPresent(m -> forwardToAppActor(ctx, m));
        firstMsg = false;
    }
    ToDeviceActorMsg pendingMsg = toDeviceMsg(msg);
    FromDeviceMsg fromDeviceMsg = pendingMsg.getPayload();
    switch(fromDeviceMsg.getMsgType()) {
        case POST_TELEMETRY_REQUEST:
        case POST_ATTRIBUTES_REQUEST:
            FromDeviceRequestMsg requestMsg = (FromDeviceRequestMsg) fromDeviceMsg;
            if (requestMsg.getRequestId() >= 0) {
                logger.debug("[{}] Pending request {} registered", requestMsg.getRequestId(), requestMsg.getMsgType());
                // TODO: handle duplicates.
                pendingMap.put(requestMsg.getRequestId(), pendingMsg);
            }
            break;
        case SUBSCRIBE_ATTRIBUTES_REQUEST:
            subscribedToAttributeUpdates = true;
            break;
        case UNSUBSCRIBE_ATTRIBUTES_REQUEST:
            subscribedToAttributeUpdates = false;
            break;
        case SUBSCRIBE_RPC_COMMANDS_REQUEST:
            subscribedToRpcCommands = true;
            break;
        case UNSUBSCRIBE_RPC_COMMANDS_REQUEST:
            subscribedToRpcCommands = false;
            break;
        default:
            break;
    }
    currentTargetServer = forwardToAppActor(ctx, pendingMsg);
}
Also used : ToDeviceActorMsg(org.thingsboard.server.common.msg.device.ToDeviceActorMsg)

Example 3 with ToDeviceActorMsg

use of org.thingsboard.server.common.msg.device.ToDeviceActorMsg in project thingsboard by thingsboard.

the class RuleActorMessageProcessor method onRuleProcessingMsg.

protected void onRuleProcessingMsg(ActorContext context, RuleProcessingMsg msg) throws RuleException {
    if (state != ComponentLifecycleState.ACTIVE) {
        pushToNextRule(context, msg.getCtx(), RuleEngineError.NO_ACTIVE_RULES);
        return;
    }
    ChainProcessingContext chainCtx = msg.getCtx();
    ToDeviceActorMsg inMsg = chainCtx.getInMsg();
    ruleCtx.update(inMsg, chainCtx.getDeviceMetaData());
    logger.debug("[{}] Going to filter in msg: {}", entityId, inMsg);
    for (RuleFilter filter : filters) {
        if (!filter.filter(ruleCtx, inMsg)) {
            logger.debug("[{}] In msg is NOT valid for processing by current rule: {}", entityId, inMsg);
            pushToNextRule(context, msg.getCtx(), RuleEngineError.NO_FILTERS_MATCHED);
            return;
        }
    }
    RuleProcessingMetaData inMsgMd;
    if (processor != null) {
        logger.debug("[{}] Going to process in msg: {}", entityId, inMsg);
        inMsgMd = processor.process(ruleCtx, inMsg);
    } else {
        inMsgMd = new RuleProcessingMetaData();
    }
    logger.debug("[{}] Going to convert in msg: {}", entityId, inMsg);
    if (action != null) {
        Optional<RuleToPluginMsg<?>> ruleToPluginMsgOptional = action.convert(ruleCtx, inMsg, inMsgMd);
        if (ruleToPluginMsgOptional.isPresent()) {
            RuleToPluginMsg<?> ruleToPluginMsg = ruleToPluginMsgOptional.get();
            logger.debug("[{}] Device msg is converted to: {}", entityId, ruleToPluginMsg);
            context.parent().tell(new RuleToPluginMsgWrapper(pluginTenantId, pluginId, tenantId, entityId, ruleToPluginMsg), context.self());
            if (action.isOneWayAction()) {
                pushToNextRule(context, msg.getCtx(), RuleEngineError.NO_TWO_WAY_ACTIONS);
                return;
            } else {
                pendingMsgMap.put(ruleToPluginMsg.getUid(), msg);
                scheduleMsgWithDelay(context, new RuleToPluginTimeoutMsg(ruleToPluginMsg.getUid()), systemContext.getPluginProcessingTimeout());
                return;
            }
        }
    }
    logger.debug("[{}] Nothing to send to plugin: {}", entityId, pluginId);
    pushToNextRule(context, msg.getCtx(), RuleEngineError.NO_TWO_WAY_ACTIONS);
}
Also used : RuleToPluginMsgWrapper(org.thingsboard.server.actors.plugin.RuleToPluginMsgWrapper) ToDeviceActorMsg(org.thingsboard.server.common.msg.device.ToDeviceActorMsg) RuleToPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.RuleToPluginMsg)

Example 4 with ToDeviceActorMsg

use of org.thingsboard.server.common.msg.device.ToDeviceActorMsg in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method onRulesProcessedMsg.

void onRulesProcessedMsg(ActorContext context, RulesProcessedMsg msg) {
    ChainProcessingContext ctx = msg.getCtx();
    ToDeviceActorMsg inMsg = ctx.getInMsg();
    SessionId sid = inMsg.getSessionId();
    ToDeviceSessionActorMsg response;
    if (ctx.getResponse() != null) {
        response = new BasicToDeviceSessionActorMsg(ctx.getResponse(), sid);
    } else {
        response = new BasicToDeviceSessionActorMsg(ctx.getError(), sid);
    }
    sendMsgToSessionActor(response, inMsg.getServerAddress());
}
Also used : ToDeviceActorMsg(org.thingsboard.server.common.msg.device.ToDeviceActorMsg) SessionId(org.thingsboard.server.common.data.id.SessionId)

Aggregations

ToDeviceActorMsg (org.thingsboard.server.common.msg.device.ToDeviceActorMsg)4 ActorRef (akka.actor.ActorRef)1 RuleToPluginMsgWrapper (org.thingsboard.server.actors.plugin.RuleToPluginMsgWrapper)1 ComplexRuleActorChain (org.thingsboard.server.actors.rule.ComplexRuleActorChain)1 RuleActorChain (org.thingsboard.server.actors.rule.RuleActorChain)1 SessionId (org.thingsboard.server.common.data.id.SessionId)1 RuleToPluginMsg (org.thingsboard.server.extensions.api.plugins.msg.RuleToPluginMsg)1