Search in sources :

Example 1 with RpcRequest

use of org.thingsboard.server.extensions.core.plugin.rpc.cmd.RpcRequest in project thingsboard by thingsboard.

the class RpcRestMsgHandler method handleHttpPostRequest.

@Override
public void handleHttpPostRequest(PluginContext ctx, PluginRestMsg msg) throws ServletException {
    boolean valid = false;
    RestRequest request = msg.getRequest();
    try {
        String[] pathParams = request.getPathParams();
        if (pathParams.length == 2) {
            String method = pathParams[0].toUpperCase();
            if (DataConstants.ONEWAY.equals(method) || DataConstants.TWOWAY.equals(method)) {
                final TenantId tenantId = ctx.getSecurityCtx().orElseThrow(() -> new IllegalStateException("Security context is empty!")).getTenantId();
                JsonNode rpcRequestBody = jsonMapper.readTree(request.getRequestBody());
                RpcRequest cmd = new RpcRequest(rpcRequestBody.get("method").asText(), jsonMapper.writeValueAsString(rpcRequestBody.get("params")));
                if (rpcRequestBody.has("timeout")) {
                    cmd.setTimeout(rpcRequestBody.get("timeout").asLong());
                }
                boolean oneWay = DataConstants.ONEWAY.equals(method);
                DeviceId deviceId = DeviceId.fromString(pathParams[1]);
                valid = handleDeviceRPCRequest(ctx, msg, tenantId, deviceId, cmd, oneWay);
            }
        }
    } catch (IOException e) {
        log.debug("Failed to process POST request due to IO exception", e);
    } catch (RuntimeException e) {
        log.debug("Failed to process POST request due to Runtime exception", e);
    }
    if (!valid) {
        msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RestRequest(org.thingsboard.server.extensions.api.plugins.rest.RestRequest) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RpcRequest(org.thingsboard.server.extensions.core.plugin.rpc.cmd.RpcRequest) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 ToDeviceRpcRequest (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest)1 RestRequest (org.thingsboard.server.extensions.api.plugins.rest.RestRequest)1 RpcRequest (org.thingsboard.server.extensions.core.plugin.rpc.cmd.RpcRequest)1