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 {
"/webjars/**",
"/swagger-ui.html",
"/emotional/video-process",
"/test/**"
};
@Bean
......
......@@ -2,25 +2,38 @@ package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
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 {
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 {
this.session = new StandardWebSocketClient()
.doHandshake(new MyWebSocketHandler(), "ws://localhost:8000/ws/emotion")
.doHandshake(new MyWebSocketHandler(this), WEBSOCKET_URL)
.get();
System.out.println("Connected to WebSocket!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Failed to connect to WebSocket, scheduling reconnection...");
scheduleReconnection();
}
}
......@@ -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();
public static EmotionPrediction[] predictions;
static class MyWebSocketHandler extends TextWebSocketHandler {
private final WebSocketClient webSocketClient;
public MyWebSocketHandler(WebSocketClient webSocketClient) {
this.webSocketClient = webSocketClient;
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
// This method will be called when the server sends a text message
......@@ -48,5 +80,11 @@ public class WebSocketClient {
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 {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new VideoFrameHandler(), "/emotional/video-process").setAllowedOrigins("*");
registry.addHandler(new SendVideoHandler(), "/emotional/video-send").setAllowedOrigins("*");
}
@Bean
......@@ -39,34 +38,11 @@ public class WebSocketConfig implements WebSocketConfigurer {
@Component
class VideoFrameHandler extends BinaryWebSocketHandler {
WebSocketClient webSocketClient = customWebSocketClient();
@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;
private static WebSocketSession currentSession;
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
SendVideoHandler.session = (StandardWebSocketSession) session;
currentSession = session; // Store the current session
log.info("Connection established with client: {}", session);
super.afterConnectionEstablished(session);
}
......@@ -74,13 +50,21 @@ public class WebSocketConfig implements WebSocketConfigurer {
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
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
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
// Handle session closure here
// For example, cleanup resources or attempt to reconnect
SendVideoHandler.session = null; // Reset session variable
if (currentSession == session) {
currentSession = null; // Reset session variable
}
log.info("Connection closed with client: {}", session);
super.afterConnectionClosed(session, status);
}
......@@ -89,37 +73,37 @@ public class WebSocketConfig implements WebSocketConfigurer {
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
super.handleTextMessage(session, message);
try {
System.out.println("Received text message from client: "+message.getPayload());
System.out.println("Received text message from client: " + message.getPayload());
} catch (Exception e) {
e.printStackTrace();
}
}
// Method to send a message back to the client
public static void sendBinaryMessageToClient(byte[] bytes) {
// Method to send a binary message back to the client
public void sendBinaryMessageToClient(byte[] bytes) {
try {
if (session != null && session.isOpen()) {
session.sendMessage(new BinaryMessage(bytes));
System.out.println("Sent message to client: "+bytes.length);
if (currentSession != null && currentSession.isOpen()) {
currentSession.sendMessage(new BinaryMessage(bytes));
System.out.println("Sent binary message to client: " + bytes.length);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Method to send a text message back to the client
public static void sendTextMessageToClient(EmotionPrediction emotionPrediction) {
try {
if (session != null && session.isOpen()) {
if (currentSession != null && currentSession.isOpen()) {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(emotionPrediction);
session.sendMessage(new TextMessage(json));
System.out.println("Sent message to client: " + json);
currentSession.sendMessage(new TextMessage(json));
System.out.println("Sent text message to client: " + json);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
package com.kaluwa.enterprises.babycarebackendservice.rest;
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.ResponseEntity;
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.RestController;
......@@ -18,4 +22,52 @@ public class TestController {
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