Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
baby-monitoring-spring-backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
R24-145
baby-monitoring-spring-backend
Commits
529440bf
Commit
529440bf
authored
May 17, 2024
by
Chamod Ishankha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make web socket to scream live feeds and emotional data to android application
parent
948ec935
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
143 additions
and
32 deletions
+143
-32
pom.xml
pom.xml
+4
-1
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/AppConfig.java
.../enterprises/babycarebackendservice/config/AppConfig.java
+13
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/SecurityConfig.java
...rprises/babycarebackendservice/config/SecurityConfig.java
+2
-1
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/WebSocketClient.java
...prises/babycarebackendservice/config/WebSocketClient.java
+11
-13
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/WebSocketConfig.java
...prises/babycarebackendservice/config/WebSocketConfig.java
+84
-17
src/main/java/com/kaluwa/enterprises/babycarebackendservice/socketHandlers/SendVideoHandler.java
...bycarebackendservice/socketHandlers/SendVideoHandler.java
+12
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/socketHandlers/VideoFrameHandler.java
...ycarebackendservice/socketHandlers/VideoFrameHandler.java
+15
-0
src/test/java/com/kaluwa/enterprises/babycarebackendservice/BabyCareBackendServiceApplicationTests.java
...ackendservice/BabyCareBackendServiceApplicationTests.java
+2
-0
No files found.
pom.xml
View file @
529440bf
...
...
@@ -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>
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/AppConfig.java
View file @
529440bf
...
...
@@ -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
;
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/SecurityConfig.java
View file @
529440bf
...
...
@@ -46,7 +46,8 @@ public class SecurityConfig {
"/swagger-ui/**"
,
"/webjars/**"
,
"/swagger-ui.html"
,
"/emotional/video-process"
"/emotional/video-process"
,
"/emotional/video-send"
};
@Bean
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/WebSocketClient.java
View file @
529440bf
...
...
@@ -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,21 +33,19 @@ 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
);
}
}
}
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/WebSocketConfig.java
View file @
529440bf
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.ServletServerContainerFactoryBea
n
;
import
springfox.documentation.spring.web.json.Jso
n
;
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,6 +49,8 @@ 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
...
...
@@ -55,4 +59,67 @@ public class WebSocketConfig implements WebSocketConfigurer {
}
}
@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
src/main/java/com/kaluwa/enterprises/babycarebackendservice/socketHandlers/SendVideoHandler.java
0 → 100644
View file @
529440bf
//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
src/main/java/com/kaluwa/enterprises/babycarebackendservice/socketHandlers/
SocketConnectionHandlers
.java
→
src/main/java/com/kaluwa/enterprises/babycarebackendservice/socketHandlers/
VideoFrameHandler
.java
View file @
529440bf
//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
//
src/test/java/com/kaluwa/enterprises/babycarebackendservice/BabyCareBackendServiceApplicationTests.java
View file @
529440bf
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment