Search in sources :

Example 1 with ToCoreNotificationMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg 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 ToCoreNotificationMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg 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 ToCoreNotificationMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg 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 4 with ToCoreNotificationMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg in project thingsboard by thingsboard.

the class DefaultTbCoreConsumerService method handleNotification.

@Override
protected void handleNotification(UUID id, TbProtoQueueMsg<ToCoreNotificationMsg> msg, TbCallback callback) {
    ToCoreNotificationMsg toCoreNotification = msg.getValue();
    if (toCoreNotification.hasToLocalSubscriptionServiceMsg()) {
        log.trace("[{}] Forwarding message to local subscription service {}", id, toCoreNotification.getToLocalSubscriptionServiceMsg());
        forwardToLocalSubMgrService(toCoreNotification.getToLocalSubscriptionServiceMsg(), callback);
    } else if (toCoreNotification.hasFromDeviceRpcResponse()) {
        log.trace("[{}] Forwarding message to RPC service {}", id, toCoreNotification.getFromDeviceRpcResponse());
        forwardToCoreRpcService(toCoreNotification.getFromDeviceRpcResponse(), callback);
    } else if (toCoreNotification.getComponentLifecycleMsg() != null && !toCoreNotification.getComponentLifecycleMsg().isEmpty()) {
        handleComponentLifecycleMsg(id, toCoreNotification.getComponentLifecycleMsg());
        callback.onSuccess();
    } else if (toCoreNotification.getEdgeEventUpdateMsg() != null && !toCoreNotification.getEdgeEventUpdateMsg().isEmpty()) {
        Optional<TbActorMsg> actorMsg = encodingService.decode(toCoreNotification.getEdgeEventUpdateMsg().toByteArray());
        if (actorMsg.isPresent()) {
            log.trace("[{}] Forwarding message to App Actor {}", id, actorMsg.get());
            actorContext.tellWithHighPriority(actorMsg.get());
        }
        callback.onSuccess();
    }
    if (statsEnabled) {
        stats.log(toCoreNotification);
    }
}
Also used : ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) Optional(java.util.Optional)

Example 5 with ToCoreNotificationMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg in project thingsboard by thingsboard.

the class DefaultTbClusterService method broadcast.

private void broadcast(ComponentLifecycleMsg msg) {
    byte[] msgBytes = encodingService.encode(msg);
    TbQueueProducer<TbProtoQueueMsg<ToRuleEngineNotificationMsg>> toRuleEngineProducer = producerProvider.getRuleEngineNotificationsMsgProducer();
    Set<String> tbRuleEngineServices = new HashSet<>(partitionService.getAllServiceIds(ServiceType.TB_RULE_ENGINE));
    EntityType entityType = msg.getEntityId().getEntityType();
    if (entityType.equals(EntityType.TENANT) || entityType.equals(EntityType.TENANT_PROFILE) || entityType.equals(EntityType.DEVICE_PROFILE) || entityType.equals(EntityType.API_USAGE_STATE) || (entityType.equals(EntityType.DEVICE) && msg.getEvent() == ComponentLifecycleEvent.UPDATED) || entityType.equals(EntityType.EDGE)) {
        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().setComponentLifecycleMsg(ByteString.copyFrom(msgBytes)).build();
            toCoreNfProducer.send(tpi, new TbProtoQueueMsg<>(msg.getEntityId().getId(), toCoreMsg), null);
            toCoreNfs.incrementAndGet();
        }
        // No need to push notifications twice
        tbRuleEngineServices.removeAll(tbCoreServices);
    }
    for (String serviceId : tbRuleEngineServices) {
        TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_RULE_ENGINE, serviceId);
        ToRuleEngineNotificationMsg toRuleEngineMsg = ToRuleEngineNotificationMsg.newBuilder().setComponentLifecycleMsg(ByteString.copyFrom(msgBytes)).build();
        toRuleEngineProducer.send(tpi, new TbProtoQueueMsg<>(msg.getEntityId().getId(), toRuleEngineMsg), null);
        toRuleEngineNfs.incrementAndGet();
    }
}
Also used : EntityType(org.thingsboard.server.common.data.EntityType) ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ToRuleEngineNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineNotificationMsg) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) ByteString(com.google.protobuf.ByteString) HashSet(java.util.HashSet)

Aggregations

ToCoreNotificationMsg (org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg)6 TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)4 TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)3 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 TreeMap (java.util.TreeMap)1 EntityType (org.thingsboard.server.common.data.EntityType)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 EdgeEventUpdateMsg (org.thingsboard.server.common.msg.edge.EdgeEventUpdateMsg)1 FromDeviceRPCResponseProto (org.thingsboard.server.gen.transport.TransportProtos.FromDeviceRPCResponseProto)1 TbAlarmSubscriptionUpdateProto (org.thingsboard.server.gen.transport.TransportProtos.TbAlarmSubscriptionUpdateProto)1 TbSubscriptionUpdateProto (org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateProto)1 TbSubscriptionUpdateTsValue (org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateTsValue)1 TbSubscriptionUpdateValueListProto (org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateValueListProto)1 ToRuleEngineNotificationMsg (org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineNotificationMsg)1