Search in sources :

Example 1 with SubscriptionUpdate

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

the class TelemetryWebsocketMsgHandler method validateSessionMetadata.

private boolean validateSessionMetadata(PluginContext ctx, PluginWebsocketSessionRef sessionRef, SubscriptionCmd cmd, String sessionId) {
    WsSessionMetaData sessionMD = wsSessionsMap.get(sessionId);
    if (sessionMD == null) {
        log.warn("[{}] Session meta data not found. ", sessionId);
        SubscriptionUpdate update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR, SESSION_META_DATA_NOT_FOUND);
        sendWsMsg(ctx, sessionRef, update);
        return false;
    } else {
        return true;
    }
}
Also used : SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) WsSessionMetaData(org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData)

Example 2 with SubscriptionUpdate

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

the class TelemetryWebsocketMsgHandler method getSubscriptionCallback.

private PluginCallback<List<TsKvEntry>> getSubscriptionCallback(final PluginWebsocketSessionRef sessionRef, final TimeseriesSubscriptionCmd cmd, final String sessionId, final EntityId entityId, final long startTs, final List<String> keys) {
    return new PluginCallback<List<TsKvEntry>>() {

        @Override
        public void onSuccess(PluginContext ctx, List<TsKvEntry> data) {
            sendWsMsg(ctx, sessionRef, new SubscriptionUpdate(cmd.getCmdId(), data));
            Map<String, Long> subState = new HashMap<>(keys.size());
            keys.forEach(key -> subState.put(key, startTs));
            data.forEach(v -> subState.put(v.getKey(), v.getTs()));
            SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.TIMESERIES, false, subState, cmd.getScope());
            subscriptionManager.addLocalWsSubscription(ctx, sessionId, entityId, sub);
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            log.error(FAILED_TO_FETCH_DATA, e);
            SubscriptionUpdate update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR, FAILED_TO_FETCH_DATA);
            sendWsMsg(ctx, sessionRef, update);
        }
    };
}
Also used : PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) SubscriptionState(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) UnauthorizedException(org.thingsboard.server.extensions.api.exception.UnauthorizedException) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Example 3 with SubscriptionUpdate

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

the class TelemetryWebsocketMsgHandler method validateSubscriptionCmd.

private boolean validateSubscriptionCmd(PluginContext ctx, PluginWebsocketSessionRef sessionRef, SubscriptionCmd cmd) {
    if (cmd.getEntityId() == null || cmd.getEntityId().isEmpty()) {
        SubscriptionUpdate update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.BAD_REQUEST, "Device id is empty!");
        sendWsMsg(ctx, sessionRef, update);
        return false;
    }
    return true;
}
Also used : SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate)

Example 4 with SubscriptionUpdate

use of org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate 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 5 with SubscriptionUpdate

use of org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate 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)

Aggregations

SubscriptionUpdate (org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate)9 IOException (java.io.IOException)5 PluginCallback (org.thingsboard.server.extensions.api.plugins.PluginCallback)5 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)5 SubscriptionState (org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 java.util (java.util)4 Slf4j (lombok.extern.slf4j.Slf4j)4 StringUtils (org.springframework.util.StringUtils)4 DataConstants (org.thingsboard.server.common.data.DataConstants)4 EntityId (org.thingsboard.server.common.data.id.EntityId)4 org.thingsboard.server.common.data.kv (org.thingsboard.server.common.data.kv)4 UnauthorizedException (org.thingsboard.server.extensions.api.exception.UnauthorizedException)4 WsSessionMetaData (org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData)4 BinaryPluginWebSocketMsg (org.thingsboard.server.extensions.api.plugins.ws.msg.BinaryPluginWebSocketMsg)4 TextPluginWebSocketMsg (org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg)4 SubscriptionType (org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType)4 Collectors (java.util.stream.Collectors)3 EntityIdFactory (org.thingsboard.server.common.data.id.EntityIdFactory)3 DefaultWebsocketMsgHandler (org.thingsboard.server.extensions.api.plugins.handlers.DefaultWebsocketMsgHandler)3