Search in sources :

Example 1 with ToDeviceRpcRequest

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

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

the class ClusterGrpcService method toProtoMsg.

private static ClusterAPIProtos.ToDeviceRpcRequestRpcMessage toProtoMsg(ToDeviceRpcRequestPluginMsg msg) {
    ClusterAPIProtos.ToDeviceRpcRequestRpcMessage.Builder builder = ClusterAPIProtos.ToDeviceRpcRequestRpcMessage.newBuilder();
    ToDeviceRpcRequest request = msg.getMsg();
    builder.setAddress(ClusterAPIProtos.PluginAddress.newBuilder().setTenantId(toUid(msg.getPluginTenantId().getId())).setPluginId(toUid(msg.getPluginId().getId())).build());
    builder.setDeviceTenantId(toUid(msg.getTenantId()));
    builder.setDeviceId(toUid(msg.getDeviceId()));
    builder.setMsgId(toUid(request.getId()));
    builder.setOneway(request.isOneway());
    builder.setExpTime(request.getExpirationTime());
    builder.setMethod(request.getBody().getMethod());
    builder.setParams(request.getBody().getParams());
    return builder.build();
}
Also used : ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest)

Example 3 with ToDeviceRpcRequest

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

the class RpcRestMsgHandler method handleDeviceRPCRequest.

private boolean handleDeviceRPCRequest(PluginContext ctx, final PluginRestMsg msg, TenantId tenantId, DeviceId deviceId, RpcRequest cmd, boolean oneWay) throws JsonProcessingException {
    long timeout = System.currentTimeMillis() + (cmd.getTimeout() != null ? cmd.getTimeout() : defaultTimeout);
    ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(cmd.getMethodName(), cmd.getRequestData());
    ctx.checkAccess(deviceId, new PluginCallback<Void>() {

        @Override
        public void onSuccess(PluginContext ctx, Void value) {
            ToDeviceRpcRequest rpcRequest = new ToDeviceRpcRequest(UUID.randomUUID(), msg.getSecurityCtx(), tenantId, deviceId, oneWay, timeout, body);
            rpcManager.process(ctx, new LocalRequestMetaData(rpcRequest, msg.getResponseHolder()));
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            ResponseEntity response;
            if (e instanceof ToErrorResponseEntity) {
                response = ((ToErrorResponseEntity) e).toErrorResponseEntity();
            } else {
                response = new ResponseEntity(HttpStatus.UNAUTHORIZED);
            }
            ctx.logRpcRequest(msg.getSecurityCtx(), deviceId, body, oneWay, Optional.empty(), e);
            msg.getResponseHolder().setResult(response);
        }
    });
    return true;
}
Also used : ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity) ResponseEntity(org.springframework.http.ResponseEntity) LocalRequestMetaData(org.thingsboard.server.extensions.core.plugin.rpc.LocalRequestMetaData) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) ToDeviceRpcRequestBody(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody) ServletException(javax.servlet.ServletException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity)

Example 4 with ToDeviceRpcRequest

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

the class RpcRuleMsgHandler method handle.

private void handle(final PluginContext ctx, TenantId tenantId, RuleId ruleId, ServerSideRpcCallActionMsg msg) {
    DeviceId deviceId = new DeviceId(UUID.fromString(msg.getDeviceId()));
    ctx.checkAccess(deviceId, new PluginCallback<Void>() {

        @Override
        public void onSuccess(PluginContext dummy, Void value) {
            try {
                List<EntityId> deviceIds;
                if (StringUtils.isEmpty(msg.getFromDeviceRelation()) && StringUtils.isEmpty(msg.getToDeviceRelation())) {
                    deviceIds = Collections.singletonList(deviceId);
                } else if (!StringUtils.isEmpty(msg.getFromDeviceRelation())) {
                    List<EntityRelation> relations = ctx.findByFromAndType(deviceId, msg.getFromDeviceRelation()).get();
                    deviceIds = relations.stream().map(EntityRelation::getTo).collect(Collectors.toList());
                } else {
                    List<EntityRelation> relations = ctx.findByToAndType(deviceId, msg.getFromDeviceRelation()).get();
                    deviceIds = relations.stream().map(EntityRelation::getFrom).collect(Collectors.toList());
                }
                ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(msg.getRpcCallMethod(), msg.getRpcCallBody());
                long expirationTime = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(msg.getRpcCallTimeoutInSec());
                for (EntityId address : deviceIds) {
                    DeviceId tmpId = new DeviceId(address.getId());
                    ctx.checkAccess(tmpId, new PluginCallback<Void>() {

                        @Override
                        public void onSuccess(PluginContext ctx, Void value) {
                            ctx.sendRpcRequest(new ToDeviceRpcRequest(UUID.randomUUID(), null, tenantId, tmpId, true, expirationTime, body));
                            log.trace("[{}] Sent RPC Call Action msg", tmpId);
                        }

                        @Override
                        public void onFailure(PluginContext ctx, Exception e) {
                            log.info("[{}] Failed to process RPC Call Action msg", tmpId, e);
                        }
                    });
                }
            } catch (Exception e) {
                log.info("Failed to process RPC Call Action msg", e);
            }
        }

        @Override
        public void onFailure(PluginContext dummy, Exception e) {
            log.info("[{}] Failed to process RPC Call Action msg", deviceId, e);
        }
    });
}
Also used : PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) ToDeviceRpcRequestBody(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody) List(java.util.List) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Aggregations

ToDeviceRpcRequest (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest)4 ToDeviceRpcRequestBody (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody)3 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 List (java.util.List)1 ServletException (javax.servlet.ServletException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 EntityId (org.thingsboard.server.common.data.id.EntityId)1 SessionId (org.thingsboard.server.common.data.id.SessionId)1 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)1 ToErrorResponseEntity (org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity)1 PluginCallback (org.thingsboard.server.extensions.api.plugins.PluginCallback)1 ToPluginRpcResponseDeviceMsg (org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg)1 RuleException (org.thingsboard.server.extensions.api.rules.RuleException)1 LocalRequestMetaData (org.thingsboard.server.extensions.core.plugin.rpc.LocalRequestMetaData)1