Search in sources :

Example 1 with RpcError

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

the class RpcRestMsgHandler method reply.

public void reply(PluginContext ctx, ToDeviceRpcRequest rpcRequest, DeferredResult<ResponseEntity> responseWriter, FromDeviceRpcResponse response) {
    Optional<RpcError> rpcError = response.getError();
    if (rpcError.isPresent()) {
        ctx.logRpcRequest(rpcRequest.getSecurityCtx(), rpcRequest.getDeviceId(), rpcRequest.getBody(), rpcRequest.isOneway(), rpcError, null);
        RpcError error = rpcError.get();
        switch(error) {
            case TIMEOUT:
                responseWriter.setResult(new ResponseEntity<>(HttpStatus.REQUEST_TIMEOUT));
                break;
            case NO_ACTIVE_CONNECTION:
                responseWriter.setResult(new ResponseEntity<>(HttpStatus.CONFLICT));
                break;
            default:
                responseWriter.setResult(new ResponseEntity<>(HttpStatus.REQUEST_TIMEOUT));
                break;
        }
    } else {
        Optional<String> responseData = response.getResponse();
        if (responseData.isPresent() && !StringUtils.isEmpty(responseData.get())) {
            String data = responseData.get();
            try {
                ctx.logRpcRequest(rpcRequest.getSecurityCtx(), rpcRequest.getDeviceId(), rpcRequest.getBody(), rpcRequest.isOneway(), rpcError, null);
                responseWriter.setResult(new ResponseEntity<>(jsonMapper.readTree(data), HttpStatus.OK));
            } catch (IOException e) {
                log.debug("Failed to decode device response: {}", data, e);
                ctx.logRpcRequest(rpcRequest.getSecurityCtx(), rpcRequest.getDeviceId(), rpcRequest.getBody(), rpcRequest.isOneway(), rpcError, e);
                responseWriter.setResult(new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE));
            }
        } else {
            ctx.logRpcRequest(rpcRequest.getSecurityCtx(), rpcRequest.getDeviceId(), rpcRequest.getBody(), rpcRequest.isOneway(), rpcError, null);
            responseWriter.setResult(new ResponseEntity<>(HttpStatus.OK));
        }
    }
}
Also used : ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity) ResponseEntity(org.springframework.http.ResponseEntity) RpcError(org.thingsboard.server.extensions.api.plugins.msg.RpcError) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 ToErrorResponseEntity (org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity)1 RpcError (org.thingsboard.server.extensions.api.plugins.msg.RpcError)1