Search in sources :

Example 6 with Subscription

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

the class SubscriptionManager method cleanupWsSessionSubscriptions.

private void cleanupWsSessionSubscriptions(PluginContext ctx, String sessionId, boolean localSession) {
    log.debug("[{}] Removing all subscriptions for particular session.", sessionId);
    Map<Integer, Subscription> sessionSubscriptions = subscriptionsByWsSessionId.get(sessionId);
    if (sessionSubscriptions != null) {
        int sessionSubscriptionSize = sessionSubscriptions.size();
        for (Subscription subscription : sessionSubscriptions.values()) {
            EntityId entityId = subscription.getEntityId();
            Set<Subscription> deviceSubscriptions = subscriptionsByEntityId.get(entityId);
            deviceSubscriptions.remove(subscription);
            if (deviceSubscriptions.isEmpty()) {
                subscriptionsByEntityId.remove(entityId);
            }
        }
        subscriptionsByWsSessionId.remove(sessionId);
        log.debug("[{}] Removed {} subscriptions for particular session.", sessionId, sessionSubscriptionSize);
        if (localSession) {
            notifyWsSubscriptionClosed(ctx, sessionId, sessionSubscriptions);
        }
    } else {
        log.debug("[{}] No subscriptions found!", sessionId);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Example 7 with Subscription

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

the class SubscriptionManager method processSubscriptionRemoval.

private void processSubscriptionRemoval(PluginContext ctx, String sessionId, Map<Integer, Subscription> sessionSubscriptions, Subscription subscription) {
    EntityId entityId = subscription.getEntityId();
    if (subscription.isLocal() && subscription.getServer() != null) {
        rpcHandler.onSubscriptionClose(ctx, subscription.getServer(), sessionId, subscription.getSubscriptionId());
    }
    if (sessionSubscriptions.isEmpty()) {
        log.debug("[{}] Removed last subscription for particular session.", sessionId);
        subscriptionsByWsSessionId.remove(sessionId);
    } else {
        log.debug("[{}] Removed session subscription.", sessionId);
    }
    Set<Subscription> deviceSubscriptions = subscriptionsByEntityId.get(entityId);
    if (deviceSubscriptions != null) {
        boolean result = deviceSubscriptions.remove(subscription);
        if (result) {
            if (deviceSubscriptions.size() == 0) {
                log.debug("[{}] Removed last subscription for particular device.", sessionId);
                subscriptionsByEntityId.remove(entityId);
            } else {
                log.debug("[{}] Removed device subscription.", sessionId);
            }
        } else {
            log.debug("[{}] Subscription not found!", sessionId);
        }
    } else {
        log.debug("[{}] No device subscriptions found!", sessionId);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Example 8 with Subscription

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

the class SubscriptionManager method addLocalWsSubscription.

public void addLocalWsSubscription(PluginContext ctx, String sessionId, EntityId entityId, SubscriptionState sub) {
    Optional<ServerAddress> server = ctx.resolve(entityId);
    Subscription subscription;
    if (server.isPresent()) {
        ServerAddress address = server.get();
        log.trace("[{}] Forwarding subscription [{}] for device [{}] to [{}]", sessionId, sub.getSubscriptionId(), entityId, address);
        subscription = new Subscription(sub, true, address);
        rpcHandler.onNewSubscription(ctx, address, sessionId, subscription);
    } else {
        log.trace("[{}] Registering local subscription [{}] for device [{}]", sessionId, sub.getSubscriptionId(), entityId);
        subscription = new Subscription(sub, true);
    }
    registerSubscription(sessionId, entityId, subscription);
}
Also used : ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Example 9 with Subscription

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

the class SubscriptionManager method getSubscription.

private Optional<Subscription> getSubscription(String sessionId, int subscriptionId) {
    Subscription state = null;
    Map<Integer, Subscription> subMap = subscriptionsByWsSessionId.get(sessionId);
    if (subMap != null) {
        state = subMap.get(subscriptionId);
    }
    return Optional.ofNullable(state);
}
Also used : Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Example 10 with Subscription

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

the class SubscriptionManager method checkSubsciptionsPrevAddress.

private void checkSubsciptionsPrevAddress(Set<Subscription> subscriptions) {
    Iterator<Subscription> subscriptionIterator = subscriptions.iterator();
    while (subscriptionIterator.hasNext()) {
        Subscription s = subscriptionIterator.next();
        if (s.isLocal()) {
            if (s.getServer() != null) {
                log.trace("[{}] Local subscription is no longer handled on remote server address [{}]", s.getWsSessionId(), s.getServer());
                s.setServer(null);
            }
        } else {
            log.trace("[{}] Remote subscription is on up to date server address.", s.getWsSessionId());
        }
    }
}
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