Search in sources :

Example 1 with WsSessionMetaData

use of org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData 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 WsSessionMetaData

use of org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData 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 3 with WsSessionMetaData

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

the class DefaultWebsocketMsgHandler method handleWebSocketSessionEvent.

protected void handleWebSocketSessionEvent(PluginContext ctx, PluginWebsocketSessionRef sessionRef, SessionEventPluginWebSocketMsg wsMsg) {
    String sessionId = sessionRef.getSessionId();
    SessionEvent event = wsMsg.getPayload();
    log.debug(PROCESSING_MSG, sessionId, event);
    switch(event.getEventType()) {
        case ESTABLISHED:
            wsSessionsMap.put(sessionId, new WsSessionMetaData(sessionRef));
            break;
        case ERROR:
            log.debug("[{}] Unknown websocket session error: {}. ", sessionId, event.getError().orElse(null));
            break;
        case CLOSED:
            wsSessionsMap.remove(sessionId);
            cleanupWebSocketSession(ctx, sessionId);
            break;
    }
}
Also used : SessionEvent(org.thingsboard.server.extensions.api.plugins.ws.SessionEvent) WsSessionMetaData(org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData)

Example 4 with WsSessionMetaData

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

the class DefaultWebsocketMsgHandler method handleWebSocketPongEvent.

protected void handleWebSocketPongEvent(PluginContext ctx, PluginWebsocketSessionRef sessionRef) {
    String sessionId = sessionRef.getSessionId();
    WsSessionMetaData sessionMD = wsSessionsMap.get(sessionId);
    if (sessionMD != null) {
        log.debug("[{}] Updating session metadata: {}", sessionId, sessionRef);
        sessionMD.setSessionRef(sessionRef);
        sessionMD.setLastActivityTime(System.currentTimeMillis());
    }
}
Also used : WsSessionMetaData(org.thingsboard.server.extensions.api.plugins.ws.WsSessionMetaData)

Aggregations

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