From f6aab9f53f5badcdb9393865c2c47becad53cafa Mon Sep 17 00:00:00 2001 From: Topvennie Date: Fri, 29 Mar 2024 14:18:09 +0100 Subject: [PATCH] change WebSocketMessage data type --- .../logic/positioner/PositionSender.java | 19 ++++--------------- .../telraam/websocket/WebSocketMessage.java | 4 ++-- .../websocket/WebSocketMessageSingleton.java | 4 ++-- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/main/java/telraam/logic/positioner/PositionSender.java b/src/main/java/telraam/logic/positioner/PositionSender.java index e6fe68d..5e779dc 100644 --- a/src/main/java/telraam/logic/positioner/PositionSender.java +++ b/src/main/java/telraam/logic/positioner/PositionSender.java @@ -1,31 +1,20 @@ package telraam.logic.positioner; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.NoArgsConstructor; import telraam.websocket.WebSocketMessage; import telraam.websocket.WebSocketMessageSingleton; import java.util.List; -import java.util.logging.Logger; public class PositionSender { - private static final Logger logger = Logger.getLogger(PositionSender.class.getName()); - private final ObjectMapper mapper = new ObjectMapper(); - private final WebSocketMessage message = new WebSocketMessage(); + private final WebSocketMessage> message = new WebSocketMessage<>(); public PositionSender() { this.message.setTopic("position"); } - public void send(List position) { - try { - String json = mapper.writeValueAsString(position); - this.message.setData(json); - WebSocketMessageSingleton.getInstance().sendToAll(this.message); - } catch (JsonProcessingException e) { - logger.severe("Json conversion error for \"%s\"".formatted(position.toString())); - } + public void send(List positions) { + this.message.setData(positions); + WebSocketMessageSingleton.getInstance().sendToAll(this.message); } } diff --git a/src/main/java/telraam/websocket/WebSocketMessage.java b/src/main/java/telraam/websocket/WebSocketMessage.java index 67cf878..0b4200f 100644 --- a/src/main/java/telraam/websocket/WebSocketMessage.java +++ b/src/main/java/telraam/websocket/WebSocketMessage.java @@ -4,7 +4,7 @@ import lombok.Setter; @Getter @Setter -public class WebSocketMessage { +public class WebSocketMessage { private String topic; - private String data; + private T data; } diff --git a/src/main/java/telraam/websocket/WebSocketMessageSingleton.java b/src/main/java/telraam/websocket/WebSocketMessageSingleton.java index 598a901..d073217 100644 --- a/src/main/java/telraam/websocket/WebSocketMessageSingleton.java +++ b/src/main/java/telraam/websocket/WebSocketMessageSingleton.java @@ -18,7 +18,7 @@ public class WebSocketMessageSingleton { @Getter private static final WebSocketMessageSingleton instance = new WebSocketMessageSingleton(); private static final Set registeredConnections = new HashSet<>(); - private ObjectMapper mapper = new ObjectMapper(); + private final ObjectMapper mapper = new ObjectMapper(); public void registerConnection(WebSocketConnection conn) { boolean modified = registeredConnections.add(conn); @@ -39,7 +39,7 @@ public void sendToAll(String s) { registeredConnections.forEach(conn -> conn.send(s)); } - public void sendToAll(WebSocketMessage message) { + public void sendToAll(WebSocketMessage message) { try { String json = mapper.writeValueAsString(message); this.sendToAll(json);