Search in sources :

Example 1 with TimeseriesSubscriptionCmd

use of org.thingsboard.server.service.telemetry.cmd.v1.TimeseriesSubscriptionCmd in project thingsboard by thingsboard.

the class DefaultTelemetryWebSocketService method handleWsTimeseriesSubscription.

private void handleWsTimeseriesSubscription(TelemetryWebSocketSessionRef sessionRef, TimeseriesSubscriptionCmd cmd, String sessionId, EntityId entityId) {
    FutureCallback<List<TsKvEntry>> callback = new FutureCallback<List<TsKvEntry>>() {

        @Override
        public void onSuccess(List<TsKvEntry> data) {
            sendWsMsg(sessionRef, new TelemetrySubscriptionUpdate(cmd.getCmdId(), data));
            Map<String, Long> subState = new HashMap<>(data.size());
            data.forEach(v -> subState.put(v.getKey(), v.getTs()));
            TbTimeseriesSubscription sub = TbTimeseriesSubscription.builder().serviceId(serviceId).sessionId(sessionId).subscriptionId(cmd.getCmdId()).tenantId(sessionRef.getSecurityCtx().getTenantId()).entityId(entityId).updateConsumer(DefaultTelemetryWebSocketService.this::sendWsMsg).allKeys(true).keyStates(subState).build();
            oldSubService.addSubscription(sub);
        }

        @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.findAllLatest(sessionRef.getSecurityCtx().getTenantId(), entityId), 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) TbTimeseriesSubscription(org.thingsboard.server.service.subscription.TbTimeseriesSubscription) TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Function (com.google.common.base.Function)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1