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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations