Search in sources :

Example 1 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class DefaultSubscriptionManagerService method toProto.

private TbProtoQueueMsg<ToCoreNotificationMsg> toProto(TbSubscription subscription, Alarm alarm, boolean deleted) {
    TbAlarmSubscriptionUpdateProto.Builder builder = TbAlarmSubscriptionUpdateProto.newBuilder();
    builder.setSessionId(subscription.getSessionId());
    builder.setSubscriptionId(subscription.getSubscriptionId());
    builder.setAlarm(JacksonUtil.toString(alarm));
    builder.setDeleted(deleted);
    ToCoreNotificationMsg toCoreMsg = ToCoreNotificationMsg.newBuilder().setToLocalSubscriptionServiceMsg(LocalSubscriptionServiceMsgProto.newBuilder().setAlarmSubUpdate(builder.build()).build()).build();
    return new TbProtoQueueMsg<>(subscription.getEntityId().getId(), toCoreMsg);
}
Also used : ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) TbAlarmSubscriptionUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbAlarmSubscriptionUpdateProto)

Example 2 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class DefaultSubscriptionManagerService method toProto.

private TbProtoQueueMsg<ToCoreNotificationMsg> toProto(TbSubscription subscription, List<TsKvEntry> updates, boolean ignoreEmptyUpdates) {
    TbSubscriptionUpdateProto.Builder builder = TbSubscriptionUpdateProto.newBuilder();
    builder.setSessionId(subscription.getSessionId());
    builder.setSubscriptionId(subscription.getSubscriptionId());
    Map<String, List<Object>> data = new TreeMap<>();
    for (TsKvEntry tsEntry : updates) {
        List<Object> values = data.computeIfAbsent(tsEntry.getKey(), k -> new ArrayList<>());
        Object[] value = new Object[2];
        value[0] = tsEntry.getTs();
        value[1] = tsEntry.getValueAsString();
        values.add(value);
    }
    data.forEach((key, value) -> {
        TbSubscriptionUpdateValueListProto.Builder dataBuilder = TbSubscriptionUpdateValueListProto.newBuilder();
        dataBuilder.setKey(key);
        boolean hasData = false;
        for (Object v : value) {
            Object[] array = (Object[]) v;
            TbSubscriptionUpdateTsValue.Builder tsValueBuilder = TbSubscriptionUpdateTsValue.newBuilder();
            tsValueBuilder.setTs((long) array[0]);
            String strVal = (String) array[1];
            if (strVal != null) {
                hasData = true;
                tsValueBuilder.setValue(strVal);
            }
            dataBuilder.addTsValue(tsValueBuilder.build());
        }
        if (!ignoreEmptyUpdates || hasData) {
            builder.addData(dataBuilder.build());
        }
    });
    ToCoreNotificationMsg toCoreMsg = ToCoreNotificationMsg.newBuilder().setToLocalSubscriptionServiceMsg(LocalSubscriptionServiceMsgProto.newBuilder().setSubUpdate(builder.build()).build()).build();
    return new TbProtoQueueMsg<>(subscription.getEntityId().getId(), toCoreMsg);
}
Also used : ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) TbSubscriptionUpdateValueListProto(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateValueListProto) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) TreeMap(java.util.TreeMap) TbSubscriptionUpdateTsValue(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateTsValue) TbSubscriptionUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateProto) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class DefaultTbCoreToTransportService method process.

@Override
public void process(String nodeId, ToTransportMsg msg, Runnable onSuccess, Consumer<Throwable> onFailure) {
    if (nodeId == null || nodeId.isEmpty()) {
        log.trace("process: skipping message without nodeId [{}], (ToTransportMsg) msg [{}]", nodeId, msg);
        if (onSuccess != null) {
            onSuccess.run();
        }
        return;
    }
    TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_TRANSPORT, nodeId);
    UUID sessionId = new UUID(msg.getSessionIdMSB(), msg.getSessionIdLSB());
    log.trace("[{}][{}] Pushing session data to topic: {}", tpi.getFullTopicName(), sessionId, msg);
    TbProtoQueueMsg<ToTransportMsg> queueMsg = new TbProtoQueueMsg<>(NULL_UUID, msg);
    tbTransportProducer.send(tpi, queueMsg, new QueueCallbackAdaptor(onSuccess, onFailure));
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) ToTransportMsg(org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) UUID(java.util.UUID)

Example 4 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class DefaultTbClusterService method onEdgeEventUpdate.

@Override
public void onEdgeEventUpdate(TenantId tenantId, EdgeId edgeId) {
    log.trace("[{}] Processing edge {} event update ", tenantId, edgeId);
    EdgeEventUpdateMsg msg = new EdgeEventUpdateMsg(tenantId, edgeId);
    byte[] msgBytes = encodingService.encode(msg);
    TbQueueProducer<TbProtoQueueMsg<ToCoreNotificationMsg>> toCoreNfProducer = producerProvider.getTbCoreNotificationsMsgProducer();
    Set<String> tbCoreServices = partitionService.getAllServiceIds(ServiceType.TB_CORE);
    for (String serviceId : tbCoreServices) {
        TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_CORE, serviceId);
        ToCoreNotificationMsg toCoreMsg = ToCoreNotificationMsg.newBuilder().setEdgeEventUpdateMsg(ByteString.copyFrom(msgBytes)).build();
        toCoreNfProducer.send(tpi, new TbProtoQueueMsg<>(msg.getEdgeId().getId(), toCoreMsg), null);
        toCoreNfs.incrementAndGet();
    }
}
Also used : ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) EdgeEventUpdateMsg(org.thingsboard.server.common.msg.edge.EdgeEventUpdateMsg) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) ByteString(com.google.protobuf.ByteString)

Example 5 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class DefaultTbRuleEngineConsumerService method printFirstOrAll.

private void printFirstOrAll(TbRuleEngineQueueConfiguration configuration, TbMsgPackProcessingContext ctx, Map<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> map, String prefix) {
    boolean printAll = log.isTraceEnabled();
    log.info("{} to process [{}] messages", prefix, map.size());
    for (Map.Entry<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> pending : map.entrySet()) {
        ToRuleEngineMsg tmp = pending.getValue().getValue();
        TbMsg tmpMsg = TbMsg.fromBytes(configuration.getName(), tmp.getTbMsg().toByteArray(), TbMsgCallback.EMPTY);
        RuleNodeInfo ruleNodeInfo = ctx.getLastVisitedRuleNode(pending.getKey());
        if (printAll) {
            log.trace("[{}] {} to process message: {}, Last Rule Node: {}", TenantId.fromUUID(new UUID(tmp.getTenantIdMSB(), tmp.getTenantIdLSB())), prefix, tmpMsg, ruleNodeInfo);
        } else {
            log.info("[{}] {} to process message: {}, Last Rule Node: {}", TenantId.fromUUID(new UUID(tmp.getTenantIdMSB(), tmp.getTenantIdLSB())), prefix, tmpMsg, ruleNodeInfo);
            break;
        }
    }
}
Also used : RuleNodeInfo(org.thingsboard.server.common.msg.queue.RuleNodeInfo) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Aggregations

TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)21 UUID (java.util.UUID)9 ByteString (com.google.protobuf.ByteString)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)6 ToCoreNotificationMsg (org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg)6 ConcurrentMap (java.util.concurrent.ConcurrentMap)5 List (java.util.List)4 Optional (java.util.Optional)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 TimeUnit (java.util.concurrent.TimeUnit)4 PostConstruct (javax.annotation.PostConstruct)4 Slf4j (lombok.extern.slf4j.Slf4j)4 TbQueueConsumer (org.thingsboard.server.queue.TbQueueConsumer)4 ExecutorService (java.util.concurrent.ExecutorService)3 Executors (java.util.concurrent.Executors)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 PreDestroy (javax.annotation.PreDestroy)3 Value (org.springframework.beans.factory.annotation.Value)3