Commit 529440bf authored by Chamod Ishankha's avatar Chamod Ishankha

make web socket to scream live feeds and emotional data to android application

parent 948ec935
......@@ -147,7 +147,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -2,14 +2,18 @@ package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
@Configuration
@EnableWebSocket
public class AppConfig {
@Bean
......@@ -20,6 +24,7 @@ public class AppConfig {
}
@Bean
@Qualifier("primaryWebsocketClient")
public WebSocketClient webSocketClient() {
return new StandardWebSocketClient();
}
......@@ -29,4 +34,12 @@ public class AppConfig {
return builder.build();
}
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(500000);
container.setMaxBinaryMessageBufferSize(500000);
return container;
}
}
......@@ -46,7 +46,8 @@ public class SecurityConfig {
"/swagger-ui/**",
"/webjars/**",
"/swagger-ui.html",
"/emotional/video-process"
"/emotional/video-process",
"/emotional/video-send"
};
@Bean
......
......@@ -8,9 +8,9 @@ import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.sql.Blob;
import static com.kaluwa.enterprises.babycarebackendservice.config.WebSocketConfig.SendVideoHandler.sendTextMessageToClient;
class WebSocketClient {
public class WebSocketClient {
private WebSocketSession session;
......@@ -33,22 +33,20 @@ class WebSocketClient {
}
static ObjectMapper objectMapper = new ObjectMapper();
public static EmotionPrediction[] predictions;
static class MyWebSocketHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
// This method will be called when the server sends a text message
// Deserialize JSON array of objects into an array of Java objects
EmotionPrediction[] predictions = objectMapper.readValue(message.getPayload(), EmotionPrediction[].class);
// Print each element of the array
for (EmotionPrediction prediction : predictions) {
System.out.println("Emotion: " + prediction.getEmotion());
System.out.println("Bounding Box:");
System.out.println(" X: " + prediction.getBounding_box().getX());
System.out.println(" Y: " + prediction.getBounding_box().getY());
System.out.println(" Width: " + prediction.getBounding_box().getWidth());
System.out.println(" Height: " + prediction.getBounding_box().getHeight());
predictions = objectMapper.readValue(message.getPayload(), EmotionPrediction[].class);
if (predictions != null && predictions.length > 0) {
EmotionPrediction lastPrediction = predictions[predictions.length - 1];
// Now you can work with the last prediction
System.out.println("Last emotion prediction: " + lastPrediction);
sendTextMessageToClient(lastPrediction);
}
}
}
}
\ No newline at end of file
}
package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.configurationprocessor.json.JSONStringer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.adapter.standard.StandardWebSocketSession;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.BinaryWebSocketHandler;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import springfox.documentation.spring.web.json.Json;
import javax.sql.rowset.serial.SerialBlob;
import java.sql.Blob;
import java.util.Map;
import java.io.IOException;
@Configuration
@EnableWebSocket
@Slf4j
public class WebSocketConfig implements WebSocketConfigurer {
WebSocketClient webSocketClient = new WebSocketClient();
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(500000);
container.setMaxBinaryMessageBufferSize(500000);
return container;
}
@Override
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
@Qualifier("customWebsocketClient")
public WebSocketClient customWebSocketClient() {
return new WebSocketClient();
}
@Component
class VideoFrameHandler extends BinaryWebSocketHandler {
WebSocketClient webSocketClient = customWebSocketClient();
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
byte[] binaryData = message.getPayload().array();
......@@ -47,12 +49,77 @@ public class WebSocketConfig implements WebSocketConfigurer {
// 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
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
SendVideoHandler.session = (StandardWebSocketSession) session;
log.info("Connection established with client: {}", session);
super.afterConnectionEstablished(session);
}
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
byte[] binaryData = message.getPayload().array();
}
@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
log.info("Connection closed with client: {}", session);
super.afterConnectionClosed(session, status);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
super.handleTextMessage(session, message);
try {
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) {
try {
if (session != null && session.isOpen()) {
session.sendMessage(new BinaryMessage(bytes));
System.out.println("Sent message to client: "+bytes.length);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sendTextMessageToClient(EmotionPrediction emotionPrediction) {
try {
if (session != null && session.isOpen()) {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(emotionPrediction);
session.sendMessage(new TextMessage(json));
System.out.println("Sent message to client: " + json);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
//package com.kaluwa.enterprises.babycarebackendservice.socketHandlers;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.BinaryMessage;
//import org.springframework.web.socket.TextMessage;
//import org.springframework.web.socket.WebSocketSession;
//import org.springframework.web.socket.handler.BinaryWebSocketHandler;
//
//@Component
//@Slf4j
//public
//package com.kaluwa.enterprises.babycarebackendservice.socketHandlers;
//
//// Program to eastablish the socket connection
//
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//
//import com.kaluwa.enterprises.babycarebackendservice.config.WebSocketClient;
//import org.springframework.web.socket.*;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.BinaryMessage;
//import org.springframework.web.socket.TextMessage;
//import org.springframework.web.socket.WebSocketSession;
//import org.springframework.web.socket.handler.BinaryWebSocketHandler;
//import org.springframework.web.socket.handler.TextWebSocketHandler;
//
//// Socket-Connection Configuration class
//public class SocketConnectionHandlers {
//
//
//
//}
//@Component
//@Slf4j
//public
//
......@@ -2,8 +2,10 @@ package com.kaluwa.enterprises.babycarebackendservice;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.web.WebAppConfiguration;
@SpringBootTest
@WebAppConfiguration
class BabyCareBackendServiceApplicationTests {
@Test
......
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