Search in sources :

Example 6 with TsValue

use of org.thingsboard.server.common.data.query.TsValue in project thingsboard by thingsboard.

the class TbAbstractSubCtx method dynamicValueSubUpdate.

private void dynamicValueSubUpdate(String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, Map<String, DynamicValueKeySub> dynamicValueKeySubMap) {
    Map<String, TsValue> latestUpdate = new HashMap<>();
    subscriptionUpdate.getData().forEach((k, v) -> {
        Object[] data = (Object[]) v.get(0);
        latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
    });
    boolean invalidateFilter = false;
    for (Map.Entry<String, TsValue> entry : latestUpdate.entrySet()) {
        String k = entry.getKey();
        TsValue tsValue = entry.getValue();
        DynamicValueKeySub sub = dynamicValueKeySubMap.get(k);
        if (sub.updateValue(tsValue)) {
            invalidateFilter = true;
            updateDynamicValuesByKey(sub, tsValue);
        }
    }
    if (invalidateFilter) {
        update();
    }
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with TsValue

use of org.thingsboard.server.common.data.query.TsValue in project thingsboard by thingsboard.

the class DefaultTbEntityDataSubscriptionService method handleLatestCmd.

private void handleLatestCmd(TbEntityDataSubCtx ctx, LatestValueCmd latestCmd) {
    log.trace("[{}][{}] Going to process latest command: {}", ctx.getSessionId(), ctx.getCmdId(), latestCmd);
    // Fetch the latest values for telemetry keys (in case they are not copied from NoSQL to SQL DB in hybrid mode.
    if (!tsInSqlDB) {
        log.trace("[{}][{}] Going to fetch missing latest values: {}", ctx.getSessionId(), ctx.getCmdId(), latestCmd);
        List<String> allTsKeys = latestCmd.getKeys().stream().filter(key -> key.getType().equals(EntityKeyType.TIME_SERIES)).map(EntityKey::getKey).collect(Collectors.toList());
        Map<EntityData, ListenableFuture<Map<String, TsValue>>> missingTelemetryFutures = new HashMap<>();
        for (EntityData entityData : ctx.getData().getData()) {
            Map<EntityKeyType, Map<String, TsValue>> latestEntityData = entityData.getLatest();
            Map<String, TsValue> tsEntityData = latestEntityData.get(EntityKeyType.TIME_SERIES);
            Set<String> missingTsKeys = new LinkedHashSet<>(allTsKeys);
            if (tsEntityData != null) {
                missingTsKeys.removeAll(tsEntityData.keySet());
            } else {
                tsEntityData = new HashMap<>();
                latestEntityData.put(EntityKeyType.TIME_SERIES, tsEntityData);
            }
            ListenableFuture<List<TsKvEntry>> missingTsData = tsService.findLatest(ctx.getTenantId(), entityData.getEntityId(), missingTsKeys);
            missingTelemetryFutures.put(entityData, Futures.transform(missingTsData, this::toTsValue, MoreExecutors.directExecutor()));
        }
        Futures.addCallback(Futures.allAsList(missingTelemetryFutures.values()), new FutureCallback<List<Map<String, TsValue>>>() {

            @Override
            public void onSuccess(@Nullable List<Map<String, TsValue>> result) {
                missingTelemetryFutures.forEach((key, value) -> {
                    try {
                        key.getLatest().get(EntityKeyType.TIME_SERIES).putAll(value.get());
                    } catch (InterruptedException | ExecutionException e) {
                        log.warn("[{}][{}] Failed to lookup latest telemetry: {}:{}", ctx.getSessionId(), ctx.getCmdId(), key.getEntityId(), allTsKeys, e);
                    }
                });
                EntityDataUpdate update;
                if (!ctx.isInitialDataSent()) {
                    update = new EntityDataUpdate(ctx.getCmdId(), ctx.getData(), null, ctx.getMaxEntitiesPerDataSubscription());
                    ctx.setInitialDataSent(true);
                } else {
                    update = new EntityDataUpdate(ctx.getCmdId(), null, ctx.getData().getData(), ctx.getMaxEntitiesPerDataSubscription());
                }
                wsService.sendWsMsg(ctx.getSessionId(), update);
                ctx.createLatestValuesSubscriptions(latestCmd.getKeys());
            }

            @Override
            public void onFailure(Throwable t) {
                log.warn("[{}][{}] Failed to process websocket command: {}:{}", ctx.getSessionId(), ctx.getCmdId(), ctx.getQuery(), latestCmd, t);
                wsService.sendWsMsg(ctx.getSessionId(), new EntityDataUpdate(ctx.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR.getCode(), "Failed to process websocket command!"));
            }
        }, wsCallBackExecutor);
    } else {
        if (!ctx.isInitialDataSent()) {
            EntityDataUpdate update = new EntityDataUpdate(ctx.getCmdId(), ctx.getData(), null, ctx.getMaxEntitiesPerDataSubscription());
            wsService.sendWsMsg(ctx.getSessionId(), update);
            ctx.setInitialDataSent(true);
        }
        ctx.createLatestValuesSubscriptions(latestCmd.getKeys());
    }
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) LinkedHashSet(java.util.LinkedHashSet) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) TsValue(org.thingsboard.server.common.data.query.TsValue) Autowired(org.springframework.beans.factory.annotation.Autowired) CloseStatus(org.springframework.web.socket.CloseStatus) TenantId(org.thingsboard.server.common.data.id.TenantId) PreDestroy(javax.annotation.PreDestroy) Map(java.util.Map) EntityService(org.thingsboard.server.dao.entity.EntityService) TbServiceInfoProvider(org.thingsboard.server.queue.discovery.TbServiceInfoProvider) ThreadFactory(java.util.concurrent.ThreadFactory) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Set(java.util.Set) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) Collectors(java.util.stream.Collectors) GetTsCmd(org.thingsboard.server.service.telemetry.cmd.v2.GetTsCmd) Executors(java.util.concurrent.Executors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) EntityHistoryCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityHistoryCmd) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) UnsubscribeCmd(org.thingsboard.server.service.telemetry.cmd.v2.UnsubscribeCmd) PostConstruct(javax.annotation.PostConstruct) Lazy(org.springframework.context.annotation.Lazy) TelemetryWebSocketService(org.thingsboard.server.service.telemetry.TelemetryWebSocketService) CustomerId(org.thingsboard.server.common.data.id.CustomerId) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Getter(lombok.Getter) AlarmDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataCmd) LatestValueCmd(org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd) HashMap(java.util.HashMap) Scheduled(org.springframework.scheduling.annotation.Scheduled) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) LinkedHashMap(java.util.LinkedHashMap) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) Service(org.springframework.stereotype.Service) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TimeSeriesCmd(org.thingsboard.server.service.telemetry.cmd.v2.TimeSeriesCmd) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Nullable(org.checkerframework.checker.nullness.qual.Nullable) LinkedHashSet(java.util.LinkedHashSet) ExecutorService(java.util.concurrent.ExecutorService) EntityData(org.thingsboard.server.common.data.query.EntityData) DbCallbackExecutorService(org.thingsboard.server.service.executors.DbCallbackExecutorService) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) FutureCallback(com.google.common.util.concurrent.FutureCallback) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) AlarmDataQuery(org.thingsboard.server.common.data.query.AlarmDataQuery) SubscriptionErrorCode(org.thingsboard.server.service.telemetry.sub.SubscriptionErrorCode) PageData(org.thingsboard.server.common.data.page.PageData) EntityCountCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountCmd) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with TsValue

use of org.thingsboard.server.common.data.query.TsValue in project thingsboard by thingsboard.

the class DefaultTbEntityDataSubscriptionService method handleGetTsCmd.

private ListenableFuture<TbEntityDataSubCtx> handleGetTsCmd(TbEntityDataSubCtx ctx, GetTsCmd cmd, boolean subscribe) {
    List<String> keys = cmd.getKeys();
    List<ReadTsKvQuery> finalTsKvQueryList;
    List<ReadTsKvQuery> tsKvQueryList = cmd.getKeys().stream().map(key -> new BaseReadTsKvQuery(key, cmd.getStartTs(), cmd.getEndTs(), cmd.getInterval(), getLimit(cmd.getLimit()), cmd.getAgg())).collect(Collectors.toList());
    if (cmd.isFetchLatestPreviousPoint()) {
        finalTsKvQueryList = new ArrayList<>(tsKvQueryList);
        finalTsKvQueryList.addAll(cmd.getKeys().stream().map(key -> new BaseReadTsKvQuery(key, cmd.getStartTs() - TimeUnit.DAYS.toMillis(365), cmd.getStartTs(), cmd.getInterval(), 1, cmd.getAgg())).collect(Collectors.toList()));
    } else {
        finalTsKvQueryList = tsKvQueryList;
    }
    Map<EntityData, ListenableFuture<List<TsKvEntry>>> fetchResultMap = new HashMap<>();
    ctx.getData().getData().forEach(entityData -> fetchResultMap.put(entityData, tsService.findAll(ctx.getTenantId(), entityData.getEntityId(), finalTsKvQueryList)));
    return Futures.transform(Futures.allAsList(fetchResultMap.values()), f -> {
        fetchResultMap.forEach((entityData, future) -> {
            Map<String, List<TsValue>> keyData = new LinkedHashMap<>();
            cmd.getKeys().forEach(key -> keyData.put(key, new ArrayList<>()));
            try {
                List<TsKvEntry> entityTsData = future.get();
                if (entityTsData != null) {
                    entityTsData.forEach(entry -> keyData.get(entry.getKey()).add(new TsValue(entry.getTs(), entry.getValueAsString())));
                }
                keyData.forEach((k, v) -> entityData.getTimeseries().put(k, v.toArray(new TsValue[v.size()])));
                if (cmd.isFetchLatestPreviousPoint()) {
                    entityData.getTimeseries().values().forEach(dataArray -> {
                        Arrays.sort(dataArray, (o1, o2) -> Long.compare(o2.getTs(), o1.getTs()));
                    });
                }
            } catch (InterruptedException | ExecutionException e) {
                log.warn("[{}][{}][{}] Failed to fetch historical data", ctx.getSessionId(), ctx.getCmdId(), entityData.getEntityId(), e);
                wsService.sendWsMsg(ctx.getSessionId(), new EntityDataUpdate(ctx.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR.getCode(), "Failed to fetch historical data!"));
            }
        });
        EntityDataUpdate update;
        if (!ctx.isInitialDataSent()) {
            update = new EntityDataUpdate(ctx.getCmdId(), ctx.getData(), null, ctx.getMaxEntitiesPerDataSubscription());
            ctx.setInitialDataSent(true);
        } else {
            update = new EntityDataUpdate(ctx.getCmdId(), null, ctx.getData().getData(), ctx.getMaxEntitiesPerDataSubscription());
        }
        wsService.sendWsMsg(ctx.getSessionId(), update);
        if (subscribe) {
            ctx.createTimeseriesSubscriptions(keys.stream().map(key -> new EntityKey(EntityKeyType.TIME_SERIES, key)).collect(Collectors.toList()), cmd.getStartTs(), cmd.getEndTs());
        }
        ctx.getData().getData().forEach(ed -> ed.getTimeseries().clear());
        return ctx;
    }, wsCallBackExecutor);
}
Also used : Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) TsValue(org.thingsboard.server.common.data.query.TsValue) Autowired(org.springframework.beans.factory.annotation.Autowired) CloseStatus(org.springframework.web.socket.CloseStatus) TenantId(org.thingsboard.server.common.data.id.TenantId) PreDestroy(javax.annotation.PreDestroy) Map(java.util.Map) EntityService(org.thingsboard.server.dao.entity.EntityService) TbServiceInfoProvider(org.thingsboard.server.queue.discovery.TbServiceInfoProvider) ThreadFactory(java.util.concurrent.ThreadFactory) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Set(java.util.Set) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) Collectors(java.util.stream.Collectors) GetTsCmd(org.thingsboard.server.service.telemetry.cmd.v2.GetTsCmd) Executors(java.util.concurrent.Executors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) EntityHistoryCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityHistoryCmd) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) UnsubscribeCmd(org.thingsboard.server.service.telemetry.cmd.v2.UnsubscribeCmd) PostConstruct(javax.annotation.PostConstruct) Lazy(org.springframework.context.annotation.Lazy) TelemetryWebSocketService(org.thingsboard.server.service.telemetry.TelemetryWebSocketService) CustomerId(org.thingsboard.server.common.data.id.CustomerId) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Getter(lombok.Getter) AlarmDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataCmd) LatestValueCmd(org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd) HashMap(java.util.HashMap) Scheduled(org.springframework.scheduling.annotation.Scheduled) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) LinkedHashMap(java.util.LinkedHashMap) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) Service(org.springframework.stereotype.Service) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TimeSeriesCmd(org.thingsboard.server.service.telemetry.cmd.v2.TimeSeriesCmd) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Nullable(org.checkerframework.checker.nullness.qual.Nullable) LinkedHashSet(java.util.LinkedHashSet) ExecutorService(java.util.concurrent.ExecutorService) EntityData(org.thingsboard.server.common.data.query.EntityData) DbCallbackExecutorService(org.thingsboard.server.service.executors.DbCallbackExecutorService) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) FutureCallback(com.google.common.util.concurrent.FutureCallback) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) AlarmDataQuery(org.thingsboard.server.common.data.query.AlarmDataQuery) SubscriptionErrorCode(org.thingsboard.server.service.telemetry.sub.SubscriptionErrorCode) PageData(org.thingsboard.server.common.data.page.PageData) EntityCountCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountCmd) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) TsValue(org.thingsboard.server.common.data.query.TsValue) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EntityData(org.thingsboard.server.common.data.query.EntityData) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) LinkedHashMap(java.util.LinkedHashMap) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKey(org.thingsboard.server.common.data.query.EntityKey) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with TsValue

use of org.thingsboard.server.common.data.query.TsValue in project thingsboard by thingsboard.

the class TbAlarmDataSubCtx method sendWsMsg.

@Override
void sendWsMsg(String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType, boolean resultToLatestValues) {
    EntityId entityId = subToEntityIdMap.get(subscriptionUpdate.getSubscriptionId());
    if (entityId != null) {
        Map<String, TsValue> latestUpdate = new HashMap<>();
        subscriptionUpdate.getData().forEach((k, v) -> {
            Object[] data = (Object[]) v.get(0);
            latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
        });
        EntityData entityData = entitiesMap.get(entityId);
        entityData.getLatest().computeIfAbsent(keyType, tmp -> new HashMap<>()).putAll(latestUpdate);
        log.trace("[{}][{}][{}][{}] Received subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
        List<AlarmData> update = alarmsMap.values().stream().filter(alarm -> entityId.equals(alarm.getEntityId())).map(alarm -> {
            alarm.getLatest().computeIfAbsent(keyType, tmp -> new HashMap<>()).putAll(latestUpdate);
            return alarm;
        }).collect(Collectors.toList());
        if (!update.isEmpty()) {
            wsService.sendWsMsg(sessionId, new AlarmDataUpdate(cmdId, null, update, maxEntitiesPerAlarmSubscription, data.getTotalElements()));
        }
    } else {
        log.trace("[{}][{}][{}][{}] Received stale subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
    }
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) Setter(lombok.Setter) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) Getter(lombok.Getter) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) TsValue(org.thingsboard.server.common.data.query.TsValue) AlarmSubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.AlarmSubscriptionUpdate) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) Map(java.util.Map) ToString(lombok.ToString) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityService(org.thingsboard.server.dao.entity.EntityService) EntityKey(org.thingsboard.server.common.data.query.EntityKey) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) Collection(java.util.Collection) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) Set(java.util.Set) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) Collectors(java.util.stream.Collectors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AlarmData(org.thingsboard.server.common.data.query.AlarmData) AlarmDataQuery(org.thingsboard.server.common.data.query.AlarmDataQuery) AlarmId(org.thingsboard.server.common.data.id.AlarmId) PageData(org.thingsboard.server.common.data.page.PageData) TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) TelemetryWebSocketService(org.thingsboard.server.service.telemetry.TelemetryWebSocketService) Collections(java.util.Collections) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) EntityData(org.thingsboard.server.common.data.query.EntityData) ToString(lombok.ToString) EntityId(org.thingsboard.server.common.data.id.EntityId) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 10 with TsValue

use of org.thingsboard.server.common.data.query.TsValue in project thingsboard by thingsboard.

the class TbEntityDataSubCtx method sendLatestWsMsg.

private void sendLatestWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) {
    Map<String, TsValue> latestUpdate = new HashMap<>();
    subscriptionUpdate.getData().forEach((k, v) -> {
        Object[] data = (Object[]) v.get(0);
        latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
    });
    EntityData entityData = getDataForEntity(entityId);
    if (entityData != null && entityData.getLatest() != null) {
        Map<String, TsValue> latestCtxValues = entityData.getLatest().get(keyType);
        log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues);
        if (latestCtxValues != null) {
            latestCtxValues.forEach((k, v) -> {
                TsValue update = latestUpdate.get(k);
                if (update != null) {
                    // Ignore notifications about deleted keys
                    if (!(update.getTs() == 0 && (update.getValue() == null || update.getValue().isEmpty()))) {
                        if (update.getTs() < v.getTs()) {
                            log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
                            latestUpdate.remove(k);
                        } else if ((update.getTs() == v.getTs() && update.getValue().equals(v.getValue()))) {
                            log.trace("[{}][{}][{}] Removed duplicate update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
                            latestUpdate.remove(k);
                        }
                    } else {
                        log.trace("[{}][{}][{}] Received deleted notification for: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k);
                    }
                }
            });
            // Setting new values
            latestUpdate.forEach(latestCtxValues::put);
        }
    }
    if (!latestUpdate.isEmpty()) {
        Map<EntityKeyType, Map<String, TsValue>> latestMap = Collections.singletonMap(keyType, latestUpdate);
        entityData = new EntityData(entityId, latestMap, null);
        wsService.sendWsMsg(sessionId, new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData), maxEntitiesPerDataSubscription));
    }
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) HashMap(java.util.HashMap) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TsValue (org.thingsboard.server.common.data.query.TsValue)14 EntityData (org.thingsboard.server.common.data.query.EntityData)12 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)10 EntityDataUpdate (org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate)10 EntityKey (org.thingsboard.server.common.data.query.EntityKey)9 EntityDataCmd (org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd)9 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 LatestValueCmd (org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd)7 Test (org.junit.Test)6 Device (org.thingsboard.server.common.data.Device)6 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)6 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)6 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)6 TelemetryPluginCmdsWrapper (org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 EntityKeyType (org.thingsboard.server.common.data.query.EntityKeyType)5