Search in sources :

Example 1 with FromDeviceMsg

use of org.thingsboard.server.common.msg.session.FromDeviceMsg in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processSessionStateMsgs.

private void processSessionStateMsgs(ToDeviceActorMsg msg) {
    SessionId sessionId = msg.getSessionId();
    FromDeviceMsg inMsg = msg.getPayload();
    if (inMsg instanceof SessionOpenMsg) {
        logger.debug("[{}] Processing new session [{}]", deviceId, sessionId);
        sessions.put(sessionId, new SessionInfo(SessionType.ASYNC, msg.getServerAddress()));
    } else if (inMsg instanceof SessionCloseMsg) {
        logger.debug("[{}] Canceling subscriptions for closed session [{}]", deviceId, sessionId);
        sessions.remove(sessionId);
        attributeSubscriptions.remove(sessionId);
        rpcSubscriptions.remove(sessionId);
    }
}
Also used : FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) SessionId(org.thingsboard.server.common.data.id.SessionId)

Example 2 with FromDeviceMsg

use of org.thingsboard.server.common.msg.session.FromDeviceMsg in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processSubscriptionCommands.

private void processSubscriptionCommands(ActorContext context, ToDeviceActorMsg msg) {
    SessionId sessionId = msg.getSessionId();
    SessionType sessionType = msg.getSessionType();
    FromDeviceMsg inMsg = msg.getPayload();
    if (inMsg.getMsgType() == MsgType.SUBSCRIBE_ATTRIBUTES_REQUEST) {
        logger.debug("[{}] Registering attributes subscription for session [{}]", deviceId, sessionId);
        attributeSubscriptions.put(sessionId, new SessionInfo(sessionType, msg.getServerAddress()));
    } else if (inMsg.getMsgType() == MsgType.UNSUBSCRIBE_ATTRIBUTES_REQUEST) {
        logger.debug("[{}] Canceling attributes subscription for session [{}]", deviceId, sessionId);
        attributeSubscriptions.remove(sessionId);
    } else if (inMsg.getMsgType() == MsgType.SUBSCRIBE_RPC_COMMANDS_REQUEST) {
        logger.debug("[{}] Registering rpc subscription for session [{}][{}]", deviceId, sessionId, sessionType);
        rpcSubscriptions.put(sessionId, new SessionInfo(sessionType, msg.getServerAddress()));
        sendPendingRequests(context, sessionId, sessionType, msg.getServerAddress());
    } else if (inMsg.getMsgType() == MsgType.UNSUBSCRIBE_RPC_COMMANDS_REQUEST) {
        logger.debug("[{}] Canceling rpc subscription for session [{}][{}]", deviceId, sessionId, sessionType);
        rpcSubscriptions.remove(sessionId);
    }
}
Also used : FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) SessionType(org.thingsboard.server.common.msg.session.SessionType) SessionId(org.thingsboard.server.common.data.id.SessionId)

Example 3 with FromDeviceMsg

use of org.thingsboard.server.common.msg.session.FromDeviceMsg in project thingsboard by thingsboard.

the class AlarmProcessor method process.

@Override
public RuleProcessingMetaData process(RuleContext ctx, ToDeviceActorMsg wrapper) throws RuleException {
    RuleProcessingMetaData md = new RuleProcessingMetaData();
    FromDeviceMsg msg = wrapper.getPayload();
    Bindings bindings = buildBindings(ctx, msg);
    boolean isActiveAlarm;
    boolean isClearedAlarm;
    VelocityContext context = VelocityUtils.createContext(ctx.getDeviceMetaData(), msg);
    for (Object key : context.getKeys()) {
        md.put(key.toString(), context.get(key.toString()));
    }
    try {
        isActiveAlarm = newAlarmEvaluator.execute(bindings);
        isClearedAlarm = clearAlarmEvaluator.execute(bindings);
    } catch (ScriptException e) {
        log.debug("[{}] Failed to evaluate alarm expressions!", ctx.getRuleId(), e);
        throw new RuleException("Failed to evaluate alarm expressions!", e);
    }
    if (!isActiveAlarm && !isClearedAlarm) {
        log.debug("[{}] Incoming message do not trigger alarm", ctx.getRuleId());
        return md;
    }
    Alarm existing;
    if (isActiveAlarm) {
        existing = processActiveAlarm(ctx, msg, md);
    } else {
        existing = processInactiveAlarm(ctx, md, context);
    }
    if (existing != null) {
        md.put("alarmId", existing.getId().getId());
        md.put("alarmType", existing.getType());
        md.put("alarmSeverity", existing.getSeverity());
        try {
            if (!StringUtils.isEmpty(existing.getDetails())) {
                md.put("alarmDetails", mapper.writeValueAsString(existing.getDetails()));
            } else {
                md.put("alarmDetails", "{}");
            }
        } catch (JsonProcessingException e) {
            throw new RuleException("Failed to serialize alarm details", e);
        }
    }
    return md;
}
Also used : FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) ScriptException(javax.script.ScriptException) VelocityContext(org.apache.velocity.VelocityContext) RuleProcessingMetaData(org.thingsboard.server.extensions.api.rules.RuleProcessingMetaData) Alarm(org.thingsboard.server.common.data.alarm.Alarm) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) Bindings(javax.script.Bindings) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with FromDeviceMsg

use of org.thingsboard.server.common.msg.session.FromDeviceMsg in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processRpcResponses.

void processRpcResponses(ActorContext context, ToDeviceActorMsg msg) {
    SessionId sessionId = msg.getSessionId();
    FromDeviceMsg inMsg = msg.getPayload();
    if (inMsg.getMsgType() == MsgType.TO_DEVICE_RPC_RESPONSE) {
        logger.debug("[{}] Processing rpc command response [{}]", deviceId, sessionId);
        ToDeviceRpcResponseMsg responseMsg = (ToDeviceRpcResponseMsg) inMsg;
        ToDeviceRpcRequestMetadata requestMd = rpcPendingMap.remove(responseMsg.getRequestId());
        boolean success = requestMd != null;
        if (success) {
            ToPluginRpcResponseDeviceMsg responsePluginMsg = toPluginRpcResponseMsg(requestMd.getMsg(), responseMsg.getData());
            Optional<ServerAddress> pluginServerAddress = requestMd.getMsg().getServerAddress();
            if (pluginServerAddress.isPresent()) {
                systemContext.getRpcService().tell(pluginServerAddress.get(), responsePluginMsg);
                logger.debug("[{}] Rpc command response sent to remote plugin actor [{}]!", deviceId, requestMd.getMsg().getMsg().getId());
            } else {
                context.parent().tell(responsePluginMsg, ActorRef.noSender());
                logger.debug("[{}] Rpc command response sent to local plugin actor [{}]!", deviceId, requestMd.getMsg().getMsg().getId());
            }
        } else {
            logger.debug("[{}] Rpc command response [{}] is stale!", deviceId, responseMsg.getRequestId());
        }
        if (msg.getSessionType() == SessionType.SYNC) {
            BasicCommandAckResponse response = success ? BasicCommandAckResponse.onSuccess(MsgType.TO_DEVICE_RPC_REQUEST, responseMsg.getRequestId()) : BasicCommandAckResponse.onError(MsgType.TO_DEVICE_RPC_REQUEST, responseMsg.getRequestId(), new TimeoutException());
            sendMsgToSessionActor(new BasicToDeviceSessionActorMsg(response, msg.getSessionId()), msg.getServerAddress());
        }
    }
}
Also used : FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) SessionId(org.thingsboard.server.common.data.id.SessionId) ToPluginRpcResponseDeviceMsg(org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

FromDeviceMsg (org.thingsboard.server.common.msg.session.FromDeviceMsg)4 SessionId (org.thingsboard.server.common.data.id.SessionId)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Bindings (javax.script.Bindings)1 ScriptException (javax.script.ScriptException)1 VelocityContext (org.apache.velocity.VelocityContext)1 Alarm (org.thingsboard.server.common.data.alarm.Alarm)1 ServerAddress (org.thingsboard.server.common.msg.cluster.ServerAddress)1 SessionType (org.thingsboard.server.common.msg.session.SessionType)1 ToPluginRpcResponseDeviceMsg (org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg)1 RuleException (org.thingsboard.server.extensions.api.rules.RuleException)1 RuleProcessingMetaData (org.thingsboard.server.extensions.api.rules.RuleProcessingMetaData)1