Search in sources :

Example 1 with SessionId

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

the class DeviceActorMessageProcessor method processRpcRequest.

void processRpcRequest(ActorContext context, ToDeviceRpcRequestPluginMsg msg) {
    ToDeviceRpcRequest request = msg.getMsg();
    ToDeviceRpcRequestBody body = request.getBody();
    ToDeviceRpcRequestMsg rpcRequest = new ToDeviceRpcRequestMsg(rpcSeq++, body.getMethod(), body.getParams());
    long timeout = request.getExpirationTime() - System.currentTimeMillis();
    if (timeout <= 0) {
        logger.debug("[{}][{}] Ignoring message due to exp time reached", deviceId, request.getId(), request.getExpirationTime());
        return;
    }
    boolean sent = rpcSubscriptions.size() > 0;
    Set<SessionId> syncSessionSet = new HashSet<>();
    rpcSubscriptions.entrySet().forEach(sub -> {
        ToDeviceSessionActorMsg response = new BasicToDeviceSessionActorMsg(rpcRequest, sub.getKey());
        sendMsgToSessionActor(response, sub.getValue().getServer());
        if (SessionType.SYNC == sub.getValue().getType()) {
            syncSessionSet.add(sub.getKey());
        }
    });
    syncSessionSet.forEach(rpcSubscriptions::remove);
    if (request.isOneway() && sent) {
        ToPluginRpcResponseDeviceMsg responsePluginMsg = toPluginRpcResponseMsg(msg, (String) null);
        context.parent().tell(responsePluginMsg, ActorRef.noSender());
        logger.debug("[{}] Rpc command response sent [{}]!", deviceId, request.getId());
    } else {
        registerPendingRpcRequest(context, msg, sent, rpcRequest, timeout);
    }
    if (sent) {
        logger.debug("[{}] RPC request {} is sent!", deviceId, request.getId());
    } else {
        logger.debug("[{}] RPC request {} is NOT sent!", deviceId, request.getId());
    }
}
Also used : ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) ToDeviceRpcRequestBody(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody) SessionId(org.thingsboard.server.common.data.id.SessionId) ToPluginRpcResponseDeviceMsg(org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg)

Example 2 with SessionId

use of org.thingsboard.server.common.data.id.SessionId 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 3 with SessionId

use of org.thingsboard.server.common.data.id.SessionId 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 4 with SessionId

use of org.thingsboard.server.common.data.id.SessionId 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)

Example 5 with SessionId

use of org.thingsboard.server.common.data.id.SessionId 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

SessionId (org.thingsboard.server.common.data.id.SessionId)5 FromDeviceMsg (org.thingsboard.server.common.msg.session.FromDeviceMsg)3 ToPluginRpcResponseDeviceMsg (org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg)2 TimeoutException (java.util.concurrent.TimeoutException)1 ServerAddress (org.thingsboard.server.common.msg.cluster.ServerAddress)1 ToDeviceActorMsg (org.thingsboard.server.common.msg.device.ToDeviceActorMsg)1 SessionType (org.thingsboard.server.common.msg.session.SessionType)1 ToDeviceRpcRequest (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest)1 ToDeviceRpcRequestBody (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody)1