Search in sources :

Example 1 with RpcStatus

use of org.thingsboard.server.common.data.rpc.RpcStatus in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processRpcResponseStatus.

private void processRpcResponseStatus(TbActorCtx context, SessionInfoProto sessionInfo, ToDeviceRpcResponseStatusMsg responseMsg) {
    UUID rpcId = new UUID(responseMsg.getRequestIdMSB(), responseMsg.getRequestIdLSB());
    RpcStatus status = RpcStatus.valueOf(responseMsg.getStatus());
    ToDeviceRpcRequestMetadata md = toDeviceRpcPendingMap.get(responseMsg.getRequestId());
    if (md != null) {
        JsonNode response = null;
        if (status.equals(RpcStatus.DELIVERED)) {
            if (md.getMsg().getMsg().isOneway()) {
                toDeviceRpcPendingMap.remove(responseMsg.getRequestId());
                if (rpcSequential) {
                    systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(rpcId, null, null));
                }
            } else {
                md.setDelivered(true);
            }
        } else if (status.equals(RpcStatus.TIMEOUT)) {
            Integer maxRpcRetries = md.getMsg().getMsg().getRetries();
            maxRpcRetries = maxRpcRetries == null ? systemContext.getMaxRpcRetries() : Math.min(maxRpcRetries, systemContext.getMaxRpcRetries());
            if (maxRpcRetries <= md.getRetries()) {
                toDeviceRpcPendingMap.remove(responseMsg.getRequestId());
                status = RpcStatus.FAILED;
                response = JacksonUtil.newObjectNode().put("error", "There was a Timeout and all retry attempts have been exhausted. Retry attempts set: " + maxRpcRetries);
            } else {
                md.setRetries(md.getRetries() + 1);
            }
        }
        if (md.getMsg().getMsg().isPersisted()) {
            systemContext.getTbRpcService().save(tenantId, new RpcId(rpcId), status, response);
        }
        if (status != RpcStatus.SENT) {
            sendNextPendingRequest(context);
        }
    } else {
        log.info("[{}][{}] Rpc has already removed from pending map.", deviceId, rpcId);
    }
}
Also used : FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) RpcId(org.thingsboard.server.common.data.id.RpcId) JsonNode(com.fasterxml.jackson.databind.JsonNode) UUID(java.util.UUID) RpcStatus(org.thingsboard.server.common.data.rpc.RpcStatus)

Example 2 with RpcStatus

use of org.thingsboard.server.common.data.rpc.RpcStatus in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processRpcResponses.

private void processRpcResponses(TbActorCtx context, SessionInfoProto sessionInfo, ToDeviceRpcResponseMsg responseMsg) {
    UUID sessionId = getSessionId(sessionInfo);
    log.debug("[{}] Processing rpc command response [{}]", deviceId, sessionId);
    ToDeviceRpcRequestMetadata requestMd = toDeviceRpcPendingMap.remove(responseMsg.getRequestId());
    boolean success = requestMd != null;
    if (success) {
        boolean hasError = StringUtils.isNotEmpty(responseMsg.getError());
        try {
            String payload = hasError ? responseMsg.getError() : responseMsg.getPayload();
            systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(requestMd.getMsg().getMsg().getId(), payload, null));
            if (requestMd.getMsg().getMsg().isPersisted()) {
                RpcStatus status = hasError ? RpcStatus.FAILED : RpcStatus.SUCCESSFUL;
                JsonNode response;
                try {
                    response = JacksonUtil.toJsonNode(payload);
                } catch (IllegalArgumentException e) {
                    response = JacksonUtil.newObjectNode().put("error", payload);
                }
                systemContext.getTbRpcService().save(tenantId, new RpcId(requestMd.getMsg().getMsg().getId()), status, response);
            }
        } finally {
            if (hasError && !requestMd.isDelivered()) {
                sendNextPendingRequest(context);
            }
        }
    } else {
        log.debug("[{}] Rpc command response [{}] is stale!", deviceId, responseMsg.getRequestId());
    }
}
Also used : FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) RpcId(org.thingsboard.server.common.data.id.RpcId) JsonNode(com.fasterxml.jackson.databind.JsonNode) UUID(java.util.UUID) RpcStatus(org.thingsboard.server.common.data.rpc.RpcStatus)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 UUID (java.util.UUID)2 RpcId (org.thingsboard.server.common.data.id.RpcId)2 RpcStatus (org.thingsboard.server.common.data.rpc.RpcStatus)2 FromDeviceRpcResponse (org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse)2