Search in sources :

Example 1 with Subscription

use of org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription in project thingsboard by thingsboard.

the class SubscriptionManager method checkSubsciptionsNewAddress.

private void checkSubsciptionsNewAddress(PluginContext ctx, Optional<ServerAddress> newAddressOptional, Set<Subscription> subscriptions) {
    if (newAddressOptional.isPresent()) {
        ServerAddress newAddress = newAddressOptional.get();
        Iterator<Subscription> subscriptionIterator = subscriptions.iterator();
        while (subscriptionIterator.hasNext()) {
            Subscription s = subscriptionIterator.next();
            if (s.isLocal()) {
                if (!newAddress.equals(s.getServer())) {
                    log.trace("[{}] Local subscription is now handled on new server [{}]", s.getWsSessionId(), newAddress);
                    s.setServer(newAddress);
                    rpcHandler.onNewSubscription(ctx, newAddress, s.getWsSessionId(), s);
                }
            } else {
                log.trace("[{}] Remote subscription is now handled on new server address: [{}]", s.getWsSessionId(), newAddress);
                subscriptionIterator.remove();
            // TODO: onUpdate state of subscription by WsSessionId and other maps.
            }
        }
    }
}
Also used : ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Example 2 with Subscription

use of org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription in project thingsboard by thingsboard.

the class SubscriptionManager method onLocalSubscriptionUpdate.

public void onLocalSubscriptionUpdate(PluginContext ctx, EntityId entityId, Predicate<Subscription> filter, Function<Subscription, List<TsKvEntry>> f) {
    Set<Subscription> deviceSubscriptions = subscriptionsByEntityId.get(entityId);
    if (deviceSubscriptions != null) {
        deviceSubscriptions.stream().filter(filter).forEach(s -> {
            String sessionId = s.getWsSessionId();
            List<TsKvEntry> subscriptionUpdate = f.apply(s);
            if (!subscriptionUpdate.isEmpty()) {
                SubscriptionUpdate update = new SubscriptionUpdate(s.getSubscriptionId(), subscriptionUpdate);
                if (s.isLocal()) {
                    updateSubscriptionState(sessionId, s, update);
                    websocketHandler.sendWsMsg(ctx, sessionId, update);
                } else {
                    rpcHandler.onSubscriptionUpdate(ctx, s.getServer(), sessionId, update);
                }
            }
        });
    } else {
        log.debug("[{}] No device subscriptions to process!", entityId);
    }
}
Also used : SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Example 3 with Subscription

use of org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription in project thingsboard by thingsboard.

the class SubscriptionManager method notifyWsSubscriptionClosed.

private void notifyWsSubscriptionClosed(PluginContext ctx, String sessionId, Map<Integer, Subscription> sessionSubscriptions) {
    Set<ServerAddress> affectedServers = new HashSet<>();
    for (Subscription subscription : sessionSubscriptions.values()) {
        if (subscription.getServer() != null) {
            affectedServers.add(subscription.getServer());
        }
    }
    for (ServerAddress address : affectedServers) {
        log.debug("[{}] Going to onSubscriptionUpdate [{}] server about session close event", sessionId, address);
        rpcHandler.onSessionClose(ctx, address, sessionId);
    }
}
Also used : ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Example 4 with Subscription

use of org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription in project thingsboard by thingsboard.

the class SubscriptionManager method addRemoteWsSubscription.

public void addRemoteWsSubscription(PluginContext ctx, ServerAddress address, String sessionId, Subscription subscription) {
    EntityId entityId = subscription.getEntityId();
    log.trace("[{}] Registering remote subscription [{}] for device [{}] to [{}]", sessionId, subscription.getSubscriptionId(), entityId, address);
    registerSubscription(sessionId, entityId, subscription);
    if (subscription.getType() == SubscriptionType.ATTRIBUTES) {
        final Map<String, Long> keyStates = subscription.getKeyStates();
        ctx.loadAttributes(entityId, DataConstants.CLIENT_SCOPE, keyStates.keySet(), new PluginCallback<List<AttributeKvEntry>>() {

            @Override
            public void onSuccess(PluginContext ctx, List<AttributeKvEntry> values) {
                List<TsKvEntry> missedUpdates = new ArrayList<>();
                values.forEach(latestEntry -> {
                    if (latestEntry.getLastUpdateTs() > keyStates.get(latestEntry.getKey())) {
                        missedUpdates.add(new BasicTsKvEntry(latestEntry.getLastUpdateTs(), latestEntry));
                    }
                });
                if (!missedUpdates.isEmpty()) {
                    rpcHandler.onSubscriptionUpdate(ctx, address, sessionId, new SubscriptionUpdate(subscription.getSubscriptionId(), missedUpdates));
                }
            }

            @Override
            public void onFailure(PluginContext ctx, Exception e) {
                log.error("Failed to fetch missed updates.", e);
            }
        });
    } else if (subscription.getType() == SubscriptionType.TIMESERIES) {
        long curTs = System.currentTimeMillis();
        List<TsKvQuery> queries = new ArrayList<>();
        subscription.getKeyStates().entrySet().forEach(e -> {
            queries.add(new BaseTsKvQuery(e.getKey(), e.getValue() + 1L, curTs));
        });
        ctx.loadTimeseries(entityId, queries, new PluginCallback<List<TsKvEntry>>() {

            @Override
            public void onSuccess(PluginContext ctx, List<TsKvEntry> missedUpdates) {
                if (!missedUpdates.isEmpty()) {
                    rpcHandler.onSubscriptionUpdate(ctx, address, sessionId, new SubscriptionUpdate(subscription.getSubscriptionId(), missedUpdates));
                }
            }

            @Override
            public void onFailure(PluginContext ctx, Exception e) {
                log.error("Failed to fetch missed updates.", e);
            }
        });
    }
}
Also used : Setter(lombok.Setter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) java.util(java.util) DataConstants(org.thingsboard.server.common.data.DataConstants) Predicate(java.util.function.Predicate) SubscriptionState(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState) Function(java.util.function.Function) SubscriptionType(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType) Slf4j(lombok.extern.slf4j.Slf4j) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) EntityId(org.thingsboard.server.common.data.id.EntityId) org.thingsboard.server.common.data.kv(org.thingsboard.server.common.data.kv) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback) TelemetryWebsocketMsgHandler(org.thingsboard.server.extensions.core.plugin.telemetry.handlers.TelemetryWebsocketMsgHandler) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) TelemetryRpcMsgHandler(org.thingsboard.server.extensions.core.plugin.telemetry.handlers.TelemetryRpcMsgHandler) StringUtils(org.springframework.util.StringUtils) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) EntityId(org.thingsboard.server.common.data.id.EntityId) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Example 5 with Subscription

use of org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription in project thingsboard by thingsboard.

the class SubscriptionManager method removeSubscription.

public void removeSubscription(PluginContext ctx, String sessionId, Integer subscriptionId) {
    log.debug("[{}][{}] Going to remove subscription.", sessionId, subscriptionId);
    Map<Integer, Subscription> sessionSubscriptions = subscriptionsByWsSessionId.get(sessionId);
    if (sessionSubscriptions != null) {
        Subscription subscription = sessionSubscriptions.remove(subscriptionId);
        if (subscription != null) {
            processSubscriptionRemoval(ctx, sessionId, sessionSubscriptions, subscription);
        } else {
            log.debug("[{}][{}] Subscription not found!", sessionId, subscriptionId);
        }
    } else {
        log.debug("[{}] No session subscriptions found!", sessionId);
    }
}
Also used : Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Aggregations

Subscription (org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)10 ServerAddress (org.thingsboard.server.common.msg.cluster.ServerAddress)4 EntityId (org.thingsboard.server.common.data.id.EntityId)3 SubscriptionUpdate (org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate)2 java.util (java.util)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Setter (lombok.Setter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.springframework.util.StringUtils)1 DataConstants (org.thingsboard.server.common.data.DataConstants)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 org.thingsboard.server.common.data.kv (org.thingsboard.server.common.data.kv)1 PluginCallback (org.thingsboard.server.extensions.api.plugins.PluginCallback)1 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)1 TelemetryRpcMsgHandler (org.thingsboard.server.extensions.core.plugin.telemetry.handlers.TelemetryRpcMsgHandler)1 TelemetryWebsocketMsgHandler (org.thingsboard.server.extensions.core.plugin.telemetry.handlers.TelemetryWebsocketMsgHandler)1 SubscriptionState (org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState)1 SubscriptionType (org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType)1