Search in sources :

Example 86 with TbMsg

use of org.thingsboard.server.common.msg.TbMsg in project thingsboard by thingsboard.

the class TbRabbitMqNode method onMsg.

@Override
public void onMsg(TbContext ctx, TbMsg msg) {
    withCallback(publishMessageAsync(ctx, msg), ctx::tellSuccess, t -> {
        TbMsg next = processException(ctx, msg, t);
        ctx.tellFailure(next, t);
    });
}
Also used : TbMsg(org.thingsboard.server.common.msg.TbMsg)

Example 87 with TbMsg

use of org.thingsboard.server.common.msg.TbMsg in project thingsboard by thingsboard.

the class TbHttpClient method processMessage.

public void processMessage(TbContext ctx, TbMsg msg) {
    String endpointUrl = TbNodeUtils.processPattern(config.getRestEndpointUrlPattern(), msg);
    HttpHeaders headers = prepareHeaders(msg);
    HttpMethod method = HttpMethod.valueOf(config.getRequestMethod());
    HttpEntity<String> entity;
    if (HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method) || HttpMethod.OPTIONS.equals(method) || HttpMethod.TRACE.equals(method) || config.isIgnoreRequestBody()) {
        entity = new HttpEntity<>(headers);
    } else {
        entity = new HttpEntity<>(msg.getData(), headers);
    }
    ListenableFuture<ResponseEntity<String>> future = httpClient.exchange(endpointUrl, method, entity, String.class);
    future.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {

        @Override
        public void onFailure(Throwable throwable) {
            TbMsg next = processException(ctx, msg, throwable);
            ctx.tellFailure(next, throwable);
        }

        @Override
        public void onSuccess(ResponseEntity<String> responseEntity) {
            if (responseEntity.getStatusCode().is2xxSuccessful()) {
                TbMsg next = processResponse(ctx, msg, responseEntity);
                ctx.tellSuccess(next);
            } else {
                TbMsg next = processFailureResponse(ctx, msg, responseEntity);
                ctx.tellNext(next, TbRelationTypes.FAILURE);
            }
        }
    });
    if (pendingFutures != null) {
        processParallelRequests(future);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpMethod(org.springframework.http.HttpMethod) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Example 88 with TbMsg

use of org.thingsboard.server.common.msg.TbMsg in project thingsboard by thingsboard.

the class TbSendRPCRequestNode method onMsg.

@Override
public void onMsg(TbContext ctx, TbMsg msg) {
    JsonObject json = jsonParser.parse(msg.getData()).getAsJsonObject();
    String tmp;
    if (msg.getOriginator().getEntityType() != EntityType.DEVICE) {
        ctx.tellFailure(msg, new RuntimeException("Message originator is not a device entity!"));
    } else if (!json.has("method")) {
        ctx.tellFailure(msg, new RuntimeException("Method is not present in the message!"));
    } else if (!json.has("params")) {
        ctx.tellFailure(msg, new RuntimeException("Params are not present in the message!"));
    } else {
        int requestId = json.has("requestId") ? json.get("requestId").getAsInt() : random.nextInt();
        boolean restApiCall = msg.getType().equals(DataConstants.RPC_CALL_FROM_SERVER_TO_DEVICE);
        tmp = msg.getMetaData().getValue("oneway");
        boolean oneway = !StringUtils.isEmpty(tmp) && Boolean.parseBoolean(tmp);
        tmp = msg.getMetaData().getValue(DataConstants.PERSISTENT);
        boolean persisted = !StringUtils.isEmpty(tmp) && Boolean.parseBoolean(tmp);
        tmp = msg.getMetaData().getValue("requestUUID");
        UUID requestUUID = !StringUtils.isEmpty(tmp) ? UUID.fromString(tmp) : Uuids.timeBased();
        tmp = msg.getMetaData().getValue("originServiceId");
        String originServiceId = !StringUtils.isEmpty(tmp) ? tmp : null;
        tmp = msg.getMetaData().getValue(DataConstants.EXPIRATION_TIME);
        long expirationTime = !StringUtils.isEmpty(tmp) ? Long.parseLong(tmp) : (System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(config.getTimeoutInSeconds()));
        tmp = msg.getMetaData().getValue(DataConstants.RETRIES);
        Integer retries = !StringUtils.isEmpty(tmp) ? Integer.parseInt(tmp) : null;
        String params = parseJsonData(json.get("params"));
        String additionalInfo = parseJsonData(json.get(DataConstants.ADDITIONAL_INFO));
        RuleEngineDeviceRpcRequest request = RuleEngineDeviceRpcRequest.builder().oneway(oneway).method(json.get("method").getAsString()).body(params).tenantId(ctx.getTenantId()).deviceId(new DeviceId(msg.getOriginator().getId())).requestId(requestId).requestUUID(requestUUID).originServiceId(originServiceId).expirationTime(expirationTime).retries(retries).restApiCall(restApiCall).persisted(persisted).additionalInfo(additionalInfo).build();
        ctx.getRpcService().sendRpcRequestToDevice(request, ruleEngineDeviceRpcResponse -> {
            if (ruleEngineDeviceRpcResponse.getError().isEmpty()) {
                TbMsg next = ctx.newMsg(msg.getQueueName(), msg.getType(), msg.getOriginator(), msg.getCustomerId(), msg.getMetaData(), ruleEngineDeviceRpcResponse.getResponse().orElse("{}"));
                ctx.enqueueForTellNext(next, TbRelationTypes.SUCCESS);
            } else {
                TbMsg next = ctx.newMsg(msg.getQueueName(), msg.getType(), msg.getOriginator(), msg.getCustomerId(), msg.getMetaData(), wrap("error", ruleEngineDeviceRpcResponse.getError().get().name()));
                ctx.enqueueForTellFailure(next, ruleEngineDeviceRpcResponse.getError().get().name());
            }
        });
        ctx.ack(msg);
    }
}
Also used : RuleEngineDeviceRpcRequest(org.thingsboard.rule.engine.api.RuleEngineDeviceRpcRequest) DeviceId(org.thingsboard.server.common.data.id.DeviceId) JsonObject(com.google.gson.JsonObject) UUID(java.util.UUID) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Aggregations

TbMsg (org.thingsboard.server.common.msg.TbMsg)88 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)57 Test (org.junit.Test)46 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)29 List (java.util.List)19 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)18 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)17 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)16 EntityId (org.thingsboard.server.common.data.id.EntityId)16 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)16 Device (org.thingsboard.server.common.data.Device)15 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 AlarmCondition (org.thingsboard.server.common.data.device.profile.AlarmCondition)14 AlarmConditionFilter (org.thingsboard.server.common.data.device.profile.AlarmConditionFilter)14 AlarmConditionFilterKey (org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey)14 AlarmRule (org.thingsboard.server.common.data.device.profile.AlarmRule)14 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)14 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)13 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)13