Search in sources :

Example 1 with ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress in project thingsboard by thingsboard.

the class PluginActorMessageProcessor method onClusterEventMsg.

@Override
public void onClusterEventMsg(ClusterEventMsg msg) {
    if (state == ComponentLifecycleState.ACTIVE) {
        ServerAddress address = msg.getServerAddress();
        if (msg.isAdded()) {
            logger.debug("[{}] Going to process server add msg: {}", entityId, address);
            pluginImpl.onServerAdded(trustedCtx, address);
        } else {
            logger.debug("[{}] Going to process server remove msg: {}", entityId, address);
            pluginImpl.onServerRemoved(trustedCtx, address);
        }
    }
}
Also used : ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress)

Example 2 with ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress 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 3 with ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress 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 ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress 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 ServerAddress

use of org.thingsboard.server.common.msg.cluster.ServerAddress 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)

Aggregations

ServerAddress (org.thingsboard.server.common.msg.cluster.ServerAddress)11 Subscription (org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)4 Channel (io.grpc.Channel)1 java.util (java.util)1 TimeoutException (java.util.concurrent.TimeoutException)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 EntityId (org.thingsboard.server.common.data.id.EntityId)1 SessionId (org.thingsboard.server.common.data.id.SessionId)1 org.thingsboard.server.common.data.kv (org.thingsboard.server.common.data.kv)1 FromDeviceMsg (org.thingsboard.server.common.msg.session.FromDeviceMsg)1 DeviceCredentialsUpdateNotificationMsg (org.thingsboard.server.extensions.api.device.DeviceCredentialsUpdateNotificationMsg)1 DeviceNameOrTypeUpdateMsg (org.thingsboard.server.extensions.api.device.DeviceNameOrTypeUpdateMsg)1 PluginCallback (org.thingsboard.server.extensions.api.plugins.PluginCallback)1 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)1