Search in sources :

Example 6 with ToDeviceRpcRequest

use of org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method init.

void init(TbActorCtx ctx) {
    PageLink pageLink = new PageLink(1024, 0, null, new SortOrder("createdTime"));
    PageData<Rpc> pageData;
    do {
        pageData = systemContext.getTbRpcService().findAllByDeviceIdAndStatus(tenantId, deviceId, RpcStatus.QUEUED, pageLink);
        pageData.getData().forEach(rpc -> {
            ToDeviceRpcRequest msg = JacksonUtil.convertValue(rpc.getRequest(), ToDeviceRpcRequest.class);
            long timeout = rpc.getExpirationTime() - System.currentTimeMillis();
            if (timeout <= 0) {
                rpc.setStatus(RpcStatus.EXPIRED);
                systemContext.getTbRpcService().save(tenantId, rpc);
            } else {
                registerPendingRpcRequest(ctx, new ToDeviceRpcRequestActorMsg(systemContext.getServiceId(), msg), false, creteToDeviceRpcRequestMsg(msg), timeout);
            }
        });
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
}
Also used : Rpc(org.thingsboard.server.common.data.rpc.Rpc) ToDeviceRpcRequest(org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest) ToDeviceRpcRequestActorMsg(org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg) PageLink(org.thingsboard.server.common.data.page.PageLink) SortOrder(org.thingsboard.server.common.data.page.SortOrder)

Example 7 with ToDeviceRpcRequest

use of org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest in project thingsboard by thingsboard.

the class DefaultGatewayNotificationsService method formDeviceToGatewayRPCRequest.

private ToDeviceRpcRequest formDeviceToGatewayRPCRequest(TenantId tenantId, DeviceId gatewayDeviceId, JsonNode deviceDataNode, String method) {
    ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(method, JacksonUtil.toString(deviceDataNode));
    long expTime = System.currentTimeMillis() + rpcTimeout;
    UUID rpcRequestUUID = UUID.randomUUID();
    return new ToDeviceRpcRequest(rpcRequestUUID, tenantId, gatewayDeviceId, true, expTime, body, true, 3, null);
}
Also used : ToDeviceRpcRequest(org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest) ToDeviceRpcRequestBody(org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody) UUID(java.util.UUID)

Example 8 with ToDeviceRpcRequest

use of org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest in project thingsboard by thingsboard.

the class AbstractRpcController method handleDeviceRPCRequest.

protected DeferredResult<ResponseEntity> handleDeviceRPCRequest(boolean oneWay, DeviceId deviceId, String requestBody, HttpStatus timeoutStatus, HttpStatus noActiveConnectionStatus) throws ThingsboardException {
    try {
        JsonNode rpcRequestBody = JacksonUtil.toJsonNode(requestBody);
        ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(rpcRequestBody.get("method").asText(), JacksonUtil.toString(rpcRequestBody.get("params")));
        SecurityUser currentUser = getCurrentUser();
        TenantId tenantId = currentUser.getTenantId();
        final DeferredResult<ResponseEntity> response = new DeferredResult<>();
        long timeout = rpcRequestBody.has(DataConstants.TIMEOUT) ? rpcRequestBody.get(DataConstants.TIMEOUT).asLong() : defaultTimeout;
        long expTime = rpcRequestBody.has(DataConstants.EXPIRATION_TIME) ? rpcRequestBody.get(DataConstants.EXPIRATION_TIME).asLong() : System.currentTimeMillis() + Math.max(minTimeout, timeout);
        UUID rpcRequestUUID = rpcRequestBody.has("requestUUID") ? UUID.fromString(rpcRequestBody.get("requestUUID").asText()) : UUID.randomUUID();
        boolean persisted = rpcRequestBody.has(DataConstants.PERSISTENT) && rpcRequestBody.get(DataConstants.PERSISTENT).asBoolean();
        String additionalInfo = JacksonUtil.toString(rpcRequestBody.get(DataConstants.ADDITIONAL_INFO));
        Integer retries = rpcRequestBody.has(DataConstants.RETRIES) ? rpcRequestBody.get(DataConstants.RETRIES).asInt() : null;
        accessValidator.validate(currentUser, Operation.RPC_CALL, deviceId, new HttpValidationCallback(response, new FutureCallback<>() {

            @Override
            public void onSuccess(@Nullable DeferredResult<ResponseEntity> result) {
                ToDeviceRpcRequest rpcRequest = new ToDeviceRpcRequest(rpcRequestUUID, tenantId, deviceId, oneWay, expTime, body, persisted, retries, additionalInfo);
                deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> reply(new LocalRequestMetaData(rpcRequest, currentUser, result), fromDeviceRpcResponse, timeoutStatus, noActiveConnectionStatus), currentUser);
            }

            @Override
            public void onFailure(Throwable e) {
                ResponseEntity entity;
                if (e instanceof ToErrorResponseEntity) {
                    entity = ((ToErrorResponseEntity) e).toErrorResponseEntity();
                } else {
                    entity = new ResponseEntity(HttpStatus.UNAUTHORIZED);
                }
                logRpcCall(currentUser, deviceId, body, oneWay, Optional.empty(), e);
                response.setResult(entity);
            }
        }));
        return response;
    } catch (IllegalArgumentException ioe) {
        throw new ThingsboardException("Invalid request body", ioe, ThingsboardErrorCode.BAD_REQUEST_PARAMS);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) TenantId(org.thingsboard.server.common.data.id.TenantId) ToErrorResponseEntity(org.thingsboard.server.service.telemetry.exception.ToErrorResponseEntity) ResponseEntity(org.springframework.http.ResponseEntity) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) LocalRequestMetaData(org.thingsboard.server.service.rpc.LocalRequestMetaData) ToDeviceRpcRequest(org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest) ToDeviceRpcRequestBody(org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) UUID(java.util.UUID) FutureCallback(com.google.common.util.concurrent.FutureCallback) Nullable(javax.annotation.Nullable) ToErrorResponseEntity(org.thingsboard.server.service.telemetry.exception.ToErrorResponseEntity) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Aggregations

ToDeviceRpcRequest (org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest)8 UUID (java.util.UUID)4 ToDeviceRpcRequestBody (org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody)4 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 Nullable (javax.annotation.Nullable)2 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1