Search in sources :

Example 1 with PluginWebsocketSessionRef

use of org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef in project thingsboard by thingsboard.

the class PluginWebSocketHandler method afterConnectionEstablished.

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    super.afterConnectionEstablished(session);
    try {
        String internalSessionId = session.getId();
        PluginWebsocketSessionRef sessionRef = toRef(session);
        String externalSessionId = sessionRef.getSessionId();
        internalSessionMap.put(internalSessionId, new SessionMetaData(session, sessionRef));
        externalSessionMap.put(externalSessionId, internalSessionId);
        actorService.process(new SessionEventPluginWebSocketMsg(sessionRef, SessionEvent.onEstablished()));
        log.info("[{}][{}] Session is started", externalSessionId, session.getId());
    } catch (InvalidParameterException e) {
        log.warn("[[{}] Failed to start session", session.getId(), e);
        session.close(CloseStatus.BAD_DATA.withReason(e.getMessage()));
    } catch (Exception e) {
        log.warn("[{}] Failed to start session", session.getId(), e);
        session.close(CloseStatus.SERVER_ERROR.withReason(e.getMessage()));
    }
}
Also used : SessionEventPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.SessionEventPluginWebSocketMsg) InvalidParameterException(java.security.InvalidParameterException) PluginWebsocketSessionRef(org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef) BasicPluginWebsocketSessionRef(org.thingsboard.server.extensions.api.plugins.ws.BasicPluginWebsocketSessionRef) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) InvalidParameterException(java.security.InvalidParameterException) IOException(java.io.IOException)

Example 2 with PluginWebsocketSessionRef

use of org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef in project thingsboard by thingsboard.

the class PluginWebSocketHandler method send.

@Override
public void send(PluginWebsocketMsg<?> wsMsg) throws IOException {
    PluginWebsocketSessionRef sessionRef = wsMsg.getSessionRef();
    String externalId = sessionRef.getSessionId();
    log.debug("[{}] Processing {}", externalId, wsMsg);
    String internalId = externalSessionMap.get(externalId);
    if (internalId != null) {
        SessionMetaData sessionMd = internalSessionMap.get(internalId);
        if (sessionMd != null) {
            if (wsMsg instanceof TextPluginWebSocketMsg) {
                String payload = ((TextPluginWebSocketMsg) wsMsg).getPayload();
                sessionMd.session.sendMessage(new TextMessage(payload));
            }
        } else {
            log.warn("[{}][{}] Failed to find session by internal id", externalId, internalId);
        }
    } else {
        log.warn("[{}] Failed to find session by external id", externalId);
    }
}
Also used : PluginWebsocketSessionRef(org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef) BasicPluginWebsocketSessionRef(org.thingsboard.server.extensions.api.plugins.ws.BasicPluginWebsocketSessionRef) TextPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg) TextMessage(org.springframework.web.socket.TextMessage)

Example 3 with PluginWebsocketSessionRef

use of org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef in project thingsboard by thingsboard.

the class TelemetryWebsocketMsgHandler method handleWsHistoryCmd.

private void handleWsHistoryCmd(PluginContext ctx, PluginWebsocketSessionRef sessionRef, GetHistoryCmd cmd) {
    String sessionId = sessionRef.getSessionId();
    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;
    }
    if (cmd.getEntityId() == null || cmd.getEntityId().isEmpty() || cmd.getEntityType() == null || cmd.getEntityType().isEmpty()) {
        SubscriptionUpdate update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.BAD_REQUEST, "Device id is empty!");
        sendWsMsg(ctx, sessionRef, update);
        return;
    }
    if (cmd.getKeys() == null || cmd.getKeys().isEmpty()) {
        SubscriptionUpdate update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.BAD_REQUEST, "Keys are empty!");
        sendWsMsg(ctx, sessionRef, update);
        return;
    }
    EntityId entityId = EntityIdFactory.getByTypeAndId(cmd.getEntityType(), cmd.getEntityId());
    List<String> keys = new ArrayList<>(getKeys(cmd).orElse(Collections.emptySet()));
    List<TsKvQuery> queries = keys.stream().map(key -> new BaseTsKvQuery(key, cmd.getStartTs(), cmd.getEndTs(), cmd.getInterval(), getLimit(cmd.getLimit()), getAggregation(cmd.getAgg()))).collect(Collectors.toList());
    ctx.loadTimeseries(entityId, queries, new PluginCallback<List<TsKvEntry>>() {

        @Override
        public void onSuccess(PluginContext ctx, List<TsKvEntry> data) {
            sendWsMsg(ctx, sessionRef, new SubscriptionUpdate(cmd.getCmdId(), data));
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            SubscriptionUpdate update;
            if (UnauthorizedException.class.isInstance(e)) {
                update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.UNAUTHORIZED, SubscriptionErrorCode.UNAUTHORIZED.getDefaultMsg());
            } else {
                update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR, FAILED_TO_FETCH_DATA);
            }
            sendWsMsg(ctx, sessionRef, update);
        }
    });
}
Also used : java.util(java.util) TextPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg) DefaultWebsocketMsgHandler(org.thingsboard.server.extensions.api.plugins.handlers.DefaultWebsocketMsgHandler) org.thingsboard.server.extensions.core.plugin.telemetry.cmd(org.thingsboard.server.extensions.core.plugin.telemetry.cmd) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) EntityId(org.thingsboard.server.common.data.id.EntityId) DataConstants(org.thingsboard.server.common.data.DataConstants) PluginWebsocketSessionRef(org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef) SubscriptionState(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SubscriptionManager(org.thingsboard.server.extensions.core.plugin.telemetry.SubscriptionManager) IOException(java.io.IOException) PluginWebsocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.PluginWebsocketMsg) Collectors(java.util.stream.Collectors) SubscriptionType(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType) Slf4j(lombok.extern.slf4j.Slf4j) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) BinaryPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.BinaryPluginWebSocketMsg) UnauthorizedException(org.thingsboard.server.extensions.api.exception.UnauthorizedException) org.thingsboard.server.common.data.kv(org.thingsboard.server.common.data.kv) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) SubscriptionErrorCode(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionErrorCode) StringUtils(org.springframework.util.StringUtils) WsSessionMetaData(org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) 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) EntityId(org.thingsboard.server.common.data.id.EntityId) UnauthorizedException(org.thingsboard.server.extensions.api.exception.UnauthorizedException) WsSessionMetaData(org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData)

Example 4 with PluginWebsocketSessionRef

use of org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef in project thingsboard by thingsboard.

the class TelemetryWebsocketMsgHandler method handleWsAttributesSubscriptionByKeys.

private void handleWsAttributesSubscriptionByKeys(PluginContext ctx, PluginWebsocketSessionRef sessionRef, AttributesSubscriptionCmd cmd, String sessionId, EntityId entityId, List<String> keys) {
    PluginCallback<List<AttributeKvEntry>> callback = new PluginCallback<List<AttributeKvEntry>>() {

        @Override
        public void onSuccess(PluginContext ctx, List<AttributeKvEntry> data) {
            List<TsKvEntry> attributesData = data.stream().map(d -> new BasicTsKvEntry(d.getLastUpdateTs(), d)).collect(Collectors.toList());
            sendWsMsg(ctx, sessionRef, new SubscriptionUpdate(cmd.getCmdId(), attributesData));
            Map<String, Long> subState = new HashMap<>(keys.size());
            keys.forEach(key -> subState.put(key, 0L));
            attributesData.forEach(v -> subState.put(v.getKey(), v.getTs()));
            SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.ATTRIBUTES, false, subState, cmd.getScope());
            subscriptionManager.addLocalWsSubscription(ctx, sessionId, entityId, sub);
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            log.error(FAILED_TO_FETCH_ATTRIBUTES, e);
            SubscriptionUpdate update;
            if (UnauthorizedException.class.isInstance(e)) {
                update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.UNAUTHORIZED, SubscriptionErrorCode.UNAUTHORIZED.getDefaultMsg());
            } else {
                update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR, FAILED_TO_FETCH_ATTRIBUTES);
            }
            sendWsMsg(ctx, sessionRef, update);
        }
    };
    if (StringUtils.isEmpty(cmd.getScope())) {
        ctx.loadAttributes(entityId, Arrays.asList(DataConstants.allScopes()), keys, callback);
    } else {
        ctx.loadAttributes(entityId, cmd.getScope(), keys, callback);
    }
}
Also used : java.util(java.util) TextPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg) DefaultWebsocketMsgHandler(org.thingsboard.server.extensions.api.plugins.handlers.DefaultWebsocketMsgHandler) org.thingsboard.server.extensions.core.plugin.telemetry.cmd(org.thingsboard.server.extensions.core.plugin.telemetry.cmd) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) EntityId(org.thingsboard.server.common.data.id.EntityId) DataConstants(org.thingsboard.server.common.data.DataConstants) PluginWebsocketSessionRef(org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef) SubscriptionState(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SubscriptionManager(org.thingsboard.server.extensions.core.plugin.telemetry.SubscriptionManager) IOException(java.io.IOException) PluginWebsocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.PluginWebsocketMsg) Collectors(java.util.stream.Collectors) SubscriptionType(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType) Slf4j(lombok.extern.slf4j.Slf4j) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) BinaryPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.BinaryPluginWebSocketMsg) UnauthorizedException(org.thingsboard.server.extensions.api.exception.UnauthorizedException) org.thingsboard.server.common.data.kv(org.thingsboard.server.common.data.kv) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) SubscriptionErrorCode(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionErrorCode) StringUtils(org.springframework.util.StringUtils) WsSessionMetaData(org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) 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) SubscriptionState(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Example 5 with PluginWebsocketSessionRef

use of org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef in project thingsboard by thingsboard.

the class TelemetryWebsocketMsgHandler method handleWsAttributesSubscription.

private void handleWsAttributesSubscription(PluginContext ctx, PluginWebsocketSessionRef sessionRef, AttributesSubscriptionCmd cmd, String sessionId, EntityId entityId) {
    PluginCallback<List<AttributeKvEntry>> callback = new PluginCallback<List<AttributeKvEntry>>() {

        @Override
        public void onSuccess(PluginContext ctx, List<AttributeKvEntry> data) {
            List<TsKvEntry> attributesData = data.stream().map(d -> new BasicTsKvEntry(d.getLastUpdateTs(), d)).collect(Collectors.toList());
            sendWsMsg(ctx, sessionRef, new SubscriptionUpdate(cmd.getCmdId(), attributesData));
            Map<String, Long> subState = new HashMap<>(attributesData.size());
            attributesData.forEach(v -> subState.put(v.getKey(), v.getTs()));
            SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.ATTRIBUTES, true, subState, cmd.getScope());
            subscriptionManager.addLocalWsSubscription(ctx, sessionId, entityId, sub);
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            log.error(FAILED_TO_FETCH_ATTRIBUTES, e);
            SubscriptionUpdate update = new SubscriptionUpdate(cmd.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR, FAILED_TO_FETCH_ATTRIBUTES);
            sendWsMsg(ctx, sessionRef, update);
        }
    };
    if (StringUtils.isEmpty(cmd.getScope())) {
        ctx.loadAttributes(entityId, Arrays.asList(DataConstants.allScopes()), callback);
    } else {
        ctx.loadAttributes(entityId, cmd.getScope(), callback);
    }
}
Also used : java.util(java.util) TextPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg) DefaultWebsocketMsgHandler(org.thingsboard.server.extensions.api.plugins.handlers.DefaultWebsocketMsgHandler) org.thingsboard.server.extensions.core.plugin.telemetry.cmd(org.thingsboard.server.extensions.core.plugin.telemetry.cmd) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) EntityId(org.thingsboard.server.common.data.id.EntityId) DataConstants(org.thingsboard.server.common.data.DataConstants) PluginWebsocketSessionRef(org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef) SubscriptionState(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SubscriptionManager(org.thingsboard.server.extensions.core.plugin.telemetry.SubscriptionManager) IOException(java.io.IOException) PluginWebsocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.PluginWebsocketMsg) Collectors(java.util.stream.Collectors) SubscriptionType(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType) Slf4j(lombok.extern.slf4j.Slf4j) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) BinaryPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.BinaryPluginWebSocketMsg) UnauthorizedException(org.thingsboard.server.extensions.api.exception.UnauthorizedException) org.thingsboard.server.common.data.kv(org.thingsboard.server.common.data.kv) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) SubscriptionErrorCode(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionErrorCode) StringUtils(org.springframework.util.StringUtils) WsSessionMetaData(org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) 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) SubscriptionState(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Aggregations

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