Commit 8ef7416a authored by Ishankha K.C's avatar Ishankha K.C

update realtime websocket connectivity

parent 20e860a0
...@@ -47,6 +47,7 @@ public class SecurityConfig { ...@@ -47,6 +47,7 @@ public class SecurityConfig {
"/webjars/**", "/webjars/**",
"/swagger-ui.html", "/swagger-ui.html",
"/emotional/video-process", "/emotional/video-process",
"/test/**"
}; };
@Bean @Bean
......
...@@ -2,25 +2,38 @@ package com.kaluwa.enterprises.babycarebackendservice.config; ...@@ -2,25 +2,38 @@ package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction; import com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.socket.BinaryMessage; import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler; import org.springframework.web.socket.handler.TextWebSocketHandler;
import static com.kaluwa.enterprises.babycarebackendservice.config.WebSocketConfig.SendVideoHandler.sendTextMessageToClient; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static com.kaluwa.enterprises.babycarebackendservice.config.WebSocketConfig.VideoFrameHandler.sendTextMessageToClient;
public class WebSocketClient { public class WebSocketClient {
private WebSocketSession session; private WebSocketSession session;
private static final String WEBSOCKET_URL = "ws://localhost:8000/ws/emotion";
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
public WebSocketClient() {
connectToWebSocket();
}
public WebSocketClient() { private void connectToWebSocket() {
try { try {
this.session = new StandardWebSocketClient() this.session = new StandardWebSocketClient()
.doHandshake(new MyWebSocketHandler(), "ws://localhost:8000/ws/emotion") .doHandshake(new MyWebSocketHandler(this), WEBSOCKET_URL)
.get(); .get();
System.out.println("Connected to WebSocket!");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); System.out.println("Failed to connect to WebSocket, scheduling reconnection...");
scheduleReconnection();
} }
} }
...@@ -32,10 +45,29 @@ public class WebSocketClient { ...@@ -32,10 +45,29 @@ public class WebSocketClient {
} }
} }
private void scheduleReconnection() {
scheduler.schedule(this::connectToWebSocket, 2, TimeUnit.MINUTES);
}
@Scheduled(fixedDelay = 120000) // Check every 5 minutes
private void checkConnection() {
if (session == null || !session.isOpen()) {
System.out.println("WebSocket connection is closed. Reconnecting...");
connectToWebSocket();
}
}
static ObjectMapper objectMapper = new ObjectMapper(); static ObjectMapper objectMapper = new ObjectMapper();
public static EmotionPrediction[] predictions; public static EmotionPrediction[] predictions;
static class MyWebSocketHandler extends TextWebSocketHandler { static class MyWebSocketHandler extends TextWebSocketHandler {
private final WebSocketClient webSocketClient;
public MyWebSocketHandler(WebSocketClient webSocketClient) {
this.webSocketClient = webSocketClient;
}
@Override @Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
// This method will be called when the server sends a text message // This method will be called when the server sends a text message
...@@ -48,5 +80,11 @@ public class WebSocketClient { ...@@ -48,5 +80,11 @@ public class WebSocketClient {
sendTextMessageToClient(lastPrediction); sendTextMessageToClient(lastPrediction);
} }
} }
@Override
public void afterConnectionClosed(WebSocketSession session, org.springframework.web.socket.CloseStatus status) {
System.out.println("WebSocket connection closed. Status: " + status);
webSocketClient.scheduleReconnection();
}
} }
} }
...@@ -28,7 +28,6 @@ public class WebSocketConfig implements WebSocketConfigurer { ...@@ -28,7 +28,6 @@ public class WebSocketConfig implements WebSocketConfigurer {
@Override @Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new VideoFrameHandler(), "/emotional/video-process").setAllowedOrigins("*"); registry.addHandler(new VideoFrameHandler(), "/emotional/video-process").setAllowedOrigins("*");
registry.addHandler(new SendVideoHandler(), "/emotional/video-send").setAllowedOrigins("*");
} }
@Bean @Bean
...@@ -39,34 +38,11 @@ public class WebSocketConfig implements WebSocketConfigurer { ...@@ -39,34 +38,11 @@ public class WebSocketConfig implements WebSocketConfigurer {
@Component @Component
class VideoFrameHandler extends BinaryWebSocketHandler { class VideoFrameHandler extends BinaryWebSocketHandler {
WebSocketClient webSocketClient = customWebSocketClient(); private static WebSocketSession currentSession;
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
byte[] binaryData = message.getPayload().array();
// Process the binary data representing the video frame
// log.info("Received binary data from client: {}", binaryData);
// Here you can process the binary data as needed, such as saving it to a file, performing image processing, etc.
webSocketClient.sendBytesToWebSocket(binaryData);
SendVideoHandler.sendBinaryMessageToClient(binaryData);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
super.handleTextMessage(session, message);
}
}
@Component
class SendVideoHandler extends BinaryWebSocketHandler {
WebSocketClient webSocketClient = customWebSocketClient();
private static StandardWebSocketSession session;
@Override @Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception { public void afterConnectionEstablished(WebSocketSession session) throws Exception {
SendVideoHandler.session = (StandardWebSocketSession) session; currentSession = session; // Store the current session
log.info("Connection established with client: {}", session); log.info("Connection established with client: {}", session);
super.afterConnectionEstablished(session); super.afterConnectionEstablished(session);
} }
...@@ -74,13 +50,21 @@ public class WebSocketConfig implements WebSocketConfigurer { ...@@ -74,13 +50,21 @@ public class WebSocketConfig implements WebSocketConfigurer {
@Override @Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception { protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
byte[] binaryData = message.getPayload().array(); byte[] binaryData = message.getPayload().array();
// Process the binary data as needed
// Send binary data back to the client
sendBinaryMessageToClient(binaryData);
// Optionally, send a text message
EmotionPrediction emotionPrediction = new EmotionPrediction(); // Example object
sendTextMessageToClient(emotionPrediction);
} }
@Override @Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
// Handle session closure here if (currentSession == session) {
// For example, cleanup resources or attempt to reconnect currentSession = null; // Reset session variable
SendVideoHandler.session = null; // Reset session variable }
log.info("Connection closed with client: {}", session); log.info("Connection closed with client: {}", session);
super.afterConnectionClosed(session, status); super.afterConnectionClosed(session, status);
} }
...@@ -89,37 +73,37 @@ public class WebSocketConfig implements WebSocketConfigurer { ...@@ -89,37 +73,37 @@ public class WebSocketConfig implements WebSocketConfigurer {
protected void handleTextMessage(WebSocketSession session, TextMessage message) { protected void handleTextMessage(WebSocketSession session, TextMessage message) {
super.handleTextMessage(session, message); super.handleTextMessage(session, message);
try { try {
System.out.println("Received text message from client: "+message.getPayload()); System.out.println("Received text message from client: " + message.getPayload());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
// Method to send a message back to the client // Method to send a binary message back to the client
public static void sendBinaryMessageToClient(byte[] bytes) { public void sendBinaryMessageToClient(byte[] bytes) {
try { try {
if (session != null && session.isOpen()) { if (currentSession != null && currentSession.isOpen()) {
session.sendMessage(new BinaryMessage(bytes)); currentSession.sendMessage(new BinaryMessage(bytes));
System.out.println("Sent message to client: "+bytes.length); System.out.println("Sent binary message to client: " + bytes.length);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
// Method to send a text message back to the client
public static void sendTextMessageToClient(EmotionPrediction emotionPrediction) { public static void sendTextMessageToClient(EmotionPrediction emotionPrediction) {
try { try {
if (session != null && session.isOpen()) { if (currentSession != null && currentSession.isOpen()) {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(emotionPrediction); String json = objectMapper.writeValueAsString(emotionPrediction);
session.sendMessage(new TextMessage(json)); currentSession.sendMessage(new TextMessage(json));
System.out.println("Sent message to client: " + json); System.out.println("Sent text message to client: " + json);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
\ No newline at end of file
package com.kaluwa.enterprises.babycarebackendservice.rest; package com.kaluwa.enterprises.babycarebackendservice.rest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -18,4 +22,52 @@ public class TestController { ...@@ -18,4 +22,52 @@ public class TestController {
return new ResponseEntity<>("Test OK!", HttpStatus.OK); return new ResponseEntity<>("Test OK!", HttpStatus.OK);
} }
String lightStatus = "OFF";
@GetMapping("/light/on")
public ResponseEntity<String> lightOn() {
log.info("Inside lightOn controller");
lightStatus = "ON";
return new ResponseEntity<>("Light is ON", HttpStatus.OK);
}
@GetMapping("/light/off")
public ResponseEntity<String> lightOff() {
log.info("Inside lightOff controller");
lightStatus = "OFF";
return new ResponseEntity<>("Light is OFF", HttpStatus.OK);
}
@GetMapping("/light/status")
public ResponseEntity<String> lightStatus() {
log.info("Inside lightStatus controller");
return new ResponseEntity<>(lightStatus, HttpStatus.OK);
}
@GetMapping("/music/play")
public ResponseEntity<Resource> getMusicFile() {
log.info("Inside getMusicFile controller");
try {
// Load the music.wav file from the resources folder
ClassPathResource resource = new ClassPathResource("music.wav");
// Check if the resource exists
if (!resource.exists()) {
return ResponseEntity.notFound().build();
}
// Set the headers to specify the content type
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "audio/wav");
// Return the file as the response
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
} catch (Exception e) {
log.error("Error serving music file", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment