Search in sources :

Example 1 with TextPluginWebSocketMsg

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

the class TelemetryWebsocketMsgHandler method sendWsMsg.

private void sendWsMsg(PluginContext ctx, PluginWebsocketSessionRef sessionRef, SubscriptionUpdate update) {
    TextPluginWebSocketMsg reply;
    try {
        reply = new TextPluginWebSocketMsg(sessionRef, jsonMapper.writeValueAsString(update));
        ctx.send(reply);
    } catch (JsonProcessingException e) {
        log.warn("[{}] Failed to encode reply: {}", sessionRef.getSessionId(), update, e);
    } catch (IOException e) {
        log.warn("[{}] Failed to send reply: {}", sessionRef.getSessionId(), update, e);
    }
}
Also used : TextPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with TextPluginWebSocketMsg

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

the class PluginWebSocketHandler method handleTextMessage.

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
    try {
        log.info("[{}] Processing {}", session.getId(), message);
        SessionMetaData sessionMd = internalSessionMap.get(session.getId());
        if (sessionMd != null) {
            actorService.process(new TextPluginWebSocketMsg(sessionMd.sessionRef, message.getPayload()));
        } else {
            log.warn("[{}] Failed to find session", session.getId());
            session.close(CloseStatus.SERVER_ERROR.withReason("Session not found!"));
        }
    } catch (IOException e) {
        log.warn("IO error", e);
    }
}
Also used : TextPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg) IOException(java.io.IOException)

Example 3 with TextPluginWebSocketMsg

use of org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg 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 4 with TextPluginWebSocketMsg

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

the class TelemetryWebsocketMsgHandler method handleWebSocketMsg.

@Override
protected void handleWebSocketMsg(PluginContext ctx, PluginWebsocketSessionRef sessionRef, PluginWebsocketMsg<?> wsMsg) {
    try {
        TelemetryPluginCmdsWrapper cmdsWrapper = null;
        if (wsMsg instanceof TextPluginWebSocketMsg) {
            TextPluginWebSocketMsg textMsg = (TextPluginWebSocketMsg) wsMsg;
            cmdsWrapper = jsonMapper.readValue(textMsg.getPayload(), TelemetryPluginCmdsWrapper.class);
        } else if (wsMsg instanceof BinaryPluginWebSocketMsg) {
            throw new IllegalStateException("Not Implemented!");
        // TODO: add support of BSON here based on
        // https://github.com/michel-kraemer/bson4jackson
        }
        if (cmdsWrapper != null) {
            if (cmdsWrapper.getAttrSubCmds() != null) {
                cmdsWrapper.getAttrSubCmds().forEach(cmd -> handleWsAttributesSubscriptionCmd(ctx, sessionRef, cmd));
            }
            if (cmdsWrapper.getTsSubCmds() != null) {
                cmdsWrapper.getTsSubCmds().forEach(cmd -> handleWsTimeseriesSubscriptionCmd(ctx, sessionRef, cmd));
            }
            if (cmdsWrapper.getHistoryCmds() != null) {
                cmdsWrapper.getHistoryCmds().forEach(cmd -> handleWsHistoryCmd(ctx, sessionRef, cmd));
            }
        }
    } catch (IOException e) {
        log.warn("Failed to decode subscription cmd: {}", e.getMessage(), e);
        SubscriptionUpdate update = new SubscriptionUpdate(UNKNOWN_SUBSCRIPTION_ID, SubscriptionErrorCode.INTERNAL_ERROR, SESSION_META_DATA_NOT_FOUND);
        sendWsMsg(ctx, sessionRef, update);
    }
}
Also used : TextPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg) BinaryPluginWebSocketMsg(org.thingsboard.server.extensions.api.plugins.ws.msg.BinaryPluginWebSocketMsg) IOException(java.io.IOException) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate)

Aggregations

TextPluginWebSocketMsg (org.thingsboard.server.extensions.api.plugins.ws.msg.TextPluginWebSocketMsg)4 IOException (java.io.IOException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TextMessage (org.springframework.web.socket.TextMessage)1 BasicPluginWebsocketSessionRef (org.thingsboard.server.extensions.api.plugins.ws.BasicPluginWebsocketSessionRef)1 PluginWebsocketSessionRef (org.thingsboard.server.extensions.api.plugins.ws.PluginWebsocketSessionRef)1 BinaryPluginWebSocketMsg (org.thingsboard.server.extensions.api.plugins.ws.msg.BinaryPluginWebSocketMsg)1 SubscriptionUpdate (org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate)1