Search in sources :

Example 1 with LocalRequestMetaData

use of org.thingsboard.server.extensions.core.plugin.rpc.LocalRequestMetaData 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)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 ToErrorResponseEntity (org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity)1 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)1 ToDeviceRpcRequest (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest)1 ToDeviceRpcRequestBody (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody)1 LocalRequestMetaData (org.thingsboard.server.extensions.core.plugin.rpc.LocalRequestMetaData)1