Search in sources :

Example 11 with TelemetrySubscriptionUpdate

use of org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate in project thingsboard by thingsboard.

the class DefaultTelemetryWebSocketService method handleWsHistoryCmd.

private void handleWsHistoryCmd(TelemetryWebSocketSessionRef sessionRef, GetHistoryCmd cmd) {
    String sessionId = sessionRef.getSessionId();
    WsSessionMetaData sessionMD = wsSessionsMap.get(sessionId);
    if (sessionMD == null) {
        log.warn("[{}] Session meta data not found. ", sessionId);
        TelemetrySubscriptionUpdate update = new TelemetrySubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR, SESSION_META_DATA_NOT_FOUND);
        sendWsMsg(sessionRef, update);
        return;
    }
    if (cmd.getEntityId() == null || cmd.getEntityId().isEmpty() || cmd.getEntityType() == null || cmd.getEntityType().isEmpty()) {
        TelemetrySubscriptionUpdate update = new TelemetrySubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.BAD_REQUEST, "Device id is empty!");
        sendWsMsg(sessionRef, update);
        return;
    }
    if (cmd.getKeys() == null || cmd.getKeys().isEmpty()) {
        TelemetrySubscriptionUpdate update = new TelemetrySubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.BAD_REQUEST, "Keys are empty!");
        sendWsMsg(sessionRef, update);
        return;
    }
    EntityId entityId = EntityIdFactory.getByTypeAndId(cmd.getEntityType(), cmd.getEntityId());
    List<String> keys = new ArrayList<>(getKeys(cmd).orElse(Collections.emptySet()));
    List<ReadTsKvQuery> queries = keys.stream().map(key -> new BaseReadTsKvQuery(key, cmd.getStartTs(), cmd.getEndTs(), cmd.getInterval(), getLimit(cmd.getLimit()), getAggregation(cmd.getAgg()))).collect(Collectors.toList());
    FutureCallback<List<TsKvEntry>> callback = new FutureCallback<List<TsKvEntry>>() {

        @Override
        public void onSuccess(List<TsKvEntry> data) {
            sendWsMsg(sessionRef, new TelemetrySubscriptionUpdate(cmd.getCmdId(), data));
        }

        @Override
        public void onFailure(Throwable e) {
            TelemetrySubscriptionUpdate update;
            if (UnauthorizedException.class.isInstance(e)) {
                update = new TelemetrySubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.UNAUTHORIZED, SubscriptionErrorCode.UNAUTHORIZED.getDefaultMsg());
            } else {
                update = new TelemetrySubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR, FAILED_TO_FETCH_DATA);
            }
            sendWsMsg(sessionRef, update);
        }
    };
    accessValidator.validate(sessionRef.getSecurityCtx(), Operation.READ_TELEMETRY, entityId, on(r -> Futures.addCallback(tsService.findAll(sessionRef.getSecurityCtx().getTenantId(), entityId, queries), callback, executor), callback::onFailure));
}
Also used : GetHistoryCmd(org.thingsboard.server.service.telemetry.cmd.v1.GetHistoryCmd) Autowired(org.springframework.beans.factory.annotation.Autowired) AttributesSubscriptionCmd(org.thingsboard.server.service.telemetry.cmd.v1.AttributesSubscriptionCmd) CloseStatus(org.springframework.web.socket.CloseStatus) TenantId(org.thingsboard.server.common.data.id.TenantId) UnauthorizedException(org.thingsboard.server.service.telemetry.exception.UnauthorizedException) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) PreDestroy(javax.annotation.PreDestroy) CmdUpdate(org.thingsboard.server.service.telemetry.cmd.v2.CmdUpdate) Map(java.util.Map) TbServiceInfoProvider(org.thingsboard.server.queue.discovery.TbServiceInfoProvider) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) TbAttributeSubscriptionScope(org.thingsboard.server.service.subscription.TbAttributeSubscriptionScope) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) Function(com.google.common.base.Function) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Set(java.util.Set) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) ValidationResult(org.thingsboard.server.service.security.ValidationResult) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TenantRateLimitException(org.thingsboard.server.dao.util.TenantRateLimitException) UnsubscribeCmd(org.thingsboard.server.service.telemetry.cmd.v2.UnsubscribeCmd) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Operation(org.thingsboard.server.service.security.permission.Operation) ValidationCallback(org.thingsboard.server.service.security.ValidationCallback) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TelemetryPluginCmd(org.thingsboard.server.service.telemetry.cmd.v1.TelemetryPluginCmd) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) TimeseriesSubscriptionCmd(org.thingsboard.server.service.telemetry.cmd.v1.TimeseriesSubscriptionCmd) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AlarmDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataCmd) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) Service(org.springframework.stereotype.Service) ValidationResultCode(org.thingsboard.server.service.security.ValidationResultCode) EntityId(org.thingsboard.server.common.data.id.EntityId) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) ThingsBoardExecutors(org.thingsboard.common.util.ThingsBoardExecutors) DataConstants(org.thingsboard.server.common.data.DataConstants) AccessValidator(org.thingsboard.server.service.security.AccessValidator) TbTimeseriesSubscription(org.thingsboard.server.service.subscription.TbTimeseriesSubscription) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TbEntityDataSubscriptionService(org.thingsboard.server.service.subscription.TbEntityDataSubscriptionService) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) FutureCallback(com.google.common.util.concurrent.FutureCallback) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Futures(com.google.common.util.concurrent.Futures) UserId(org.thingsboard.server.common.data.id.UserId) SubscriptionErrorCode(org.thingsboard.server.service.telemetry.sub.SubscriptionErrorCode) TelemetryPluginCmdsWrapper(org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper) TbLocalSubscriptionService(org.thingsboard.server.service.subscription.TbLocalSubscriptionService) EntityCountCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountCmd) TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) TbAttributeSubscription(org.thingsboard.server.service.subscription.TbAttributeSubscription) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) SubscriptionCmd(org.thingsboard.server.service.telemetry.cmd.v1.SubscriptionCmd) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) EntityId(org.thingsboard.server.common.data.id.EntityId) TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) List(java.util.List) ArrayList(java.util.ArrayList) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Example 12 with TelemetrySubscriptionUpdate

use of org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate 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 13 with TelemetrySubscriptionUpdate

use of org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate in project thingsboard by thingsboard.

the class TbSubscriptionUtils method fromProto.

public static TelemetrySubscriptionUpdate fromProto(TbSubscriptionUpdateProto proto) {
    if (proto.getErrorCode() > 0) {
        return new TelemetrySubscriptionUpdate(proto.getSubscriptionId(), SubscriptionErrorCode.forCode(proto.getErrorCode()), proto.getErrorMsg());
    } else {
        Map<String, List<Object>> data = new TreeMap<>();
        proto.getDataList().forEach(v -> {
            List<Object> values = data.computeIfAbsent(v.getKey(), k -> new ArrayList<>());
            for (int i = 0; i < v.getTsValueCount(); i++) {
                Object[] value = new Object[2];
                TbSubscriptionUpdateTsValue tsValue = v.getTsValue(i);
                value[0] = tsValue.getTs();
                value[1] = tsValue.hasValue() ? tsValue.getValue() : null;
                values.add(value);
            }
        });
        return new TelemetrySubscriptionUpdate(proto.getSubscriptionId(), data);
    }
}
Also used : TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) TbSubscriptionUpdateTsValue(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateTsValue)

Example 14 with TelemetrySubscriptionUpdate

use of org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate in project thingsboard by thingsboard.

the class TbEntityDataSubCtx method sendTsWsMsg.

private void sendTsWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) {
    Map<String, List<TsValue>> tsUpdate = new HashMap<>();
    subscriptionUpdate.getData().forEach((k, v) -> {
        Object[] data = (Object[]) v.get(0);
        tsUpdate.computeIfAbsent(k, key -> new ArrayList<>()).add(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) -> {
                List<TsValue> updateList = tsUpdate.get(k);
                if (updateList != null) {
                    for (TsValue update : new ArrayList<>(updateList)) {
                        if (update.getTs() < v.getTs()) {
                            log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
                        // Looks like this is redundant feature and our UI is ready to merge the updates.
                        // updateList.remove(update);
                        } 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());
                            updateList.remove(update);
                        }
                        if (updateList.isEmpty()) {
                            tsUpdate.remove(k);
                        }
                    }
                }
            });
            // Setting new values
            tsUpdate.forEach((k, v) -> {
                Optional<TsValue> maxValue = v.stream().max(Comparator.comparingLong(TsValue::getTs));
                maxValue.ifPresent(max -> latestCtxValues.put(k, max));
            });
        }
    }
    if (!tsUpdate.isEmpty()) {
        Map<String, TsValue[]> tsMap = new HashMap<>();
        tsUpdate.forEach((key, tsValue) -> tsMap.put(key, tsValue.toArray(new TsValue[tsValue.size()])));
        entityData = new EntityData(entityId, null, tsMap);
        wsService.sendWsMsg(sessionId, new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData), maxEntitiesPerDataSubscription));
    }
}
Also used : Setter(lombok.Setter) Getter(lombok.Getter) TsValue(org.thingsboard.server.common.data.query.TsValue) LatestValueCmd(org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityService(org.thingsboard.server.dao.entity.EntityService) TimeSeriesCmd(org.thingsboard.server.service.telemetry.cmd.v2.TimeSeriesCmd) EntityKey(org.thingsboard.server.common.data.query.EntityKey) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) Set(java.util.Set) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) Collectors(java.util.stream.Collectors) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Optional(java.util.Optional) TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) Comparator(java.util.Comparator) TelemetryWebSocketService(org.thingsboard.server.service.telemetry.TelemetryWebSocketService) Collections(java.util.Collections) TsValue(org.thingsboard.server.common.data.query.TsValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TelemetrySubscriptionUpdate (org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate)14 ArrayList (java.util.ArrayList)8 List (java.util.List)8 HashMap (java.util.HashMap)7 Collections (java.util.Collections)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 Slf4j (lombok.extern.slf4j.Slf4j)6 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)6 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)6 FutureCallback (com.google.common.util.concurrent.FutureCallback)5 Optional (java.util.Optional)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Function (com.google.common.base.Function)4 Futures (com.google.common.util.concurrent.Futures)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4