Search in sources :

Example 1 with ToPluginRpcResponseDeviceMsg

use of org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg 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 ToPluginRpcResponseDeviceMsg

use of org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg 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)

Example 3 with ToPluginRpcResponseDeviceMsg

use of org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processTimeout.

public void processTimeout(ActorContext context, TimeoutMsg msg) {
    ToDeviceRpcRequestMetadata requestMd = rpcPendingMap.remove(msg.getId());
    if (requestMd != null) {
        logger.debug("[{}] RPC request [{}] timeout detected!", deviceId, msg.getId());
        ToPluginRpcResponseDeviceMsg responsePluginMsg = toPluginRpcResponseMsg(requestMd.getMsg(), requestMd.isSent() ? RpcError.TIMEOUT : RpcError.NO_ACTIVE_CONNECTION);
        context.parent().tell(responsePluginMsg, ActorRef.noSender());
    }
}
Also used : ToPluginRpcResponseDeviceMsg(org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg)

Aggregations

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