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
e3c40e4e
Commit
e3c40e4e
authored
Oct 19, 2024
by
Ishankha K.C
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save activities into database
parent
f8379cc4
Changes
14
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
209 additions
and
10 deletions
+209
-10
pom.xml
pom.xml
+6
-6
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/AppConfig.java
.../enterprises/babycarebackendservice/config/AppConfig.java
+1
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/WebSocketClient.java
...prises/babycarebackendservice/config/WebSocketClient.java
+19
-2
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/WebSocketConfig.java
...prises/babycarebackendservice/config/WebSocketConfig.java
+12
-2
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/Configs.java
...enterprises/babycarebackendservice/constants/Configs.java
+5
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/LogTypes.java
...nterprises/babycarebackendservice/constants/LogTypes.java
+7
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/TableNames.java
...erprises/babycarebackendservice/constants/TableNames.java
+1
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dao/ActivityLogDao.java
...nterprises/babycarebackendservice/dao/ActivityLogDao.java
+12
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/ActivityLogDto.java
...nterprises/babycarebackendservice/dto/ActivityLogDto.java
+17
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/mappers/ActivityLogMapper.java
...ses/babycarebackendservice/mappers/ActivityLogMapper.java
+16
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/ActivityLog.java
...enterprises/babycarebackendservice/model/ActivityLog.java
+21
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/rest/ActivityLogController.java
...es/babycarebackendservice/rest/ActivityLogController.java
+30
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/ActivityLogService.java
...es/babycarebackendservice/service/ActivityLogService.java
+13
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/ActivityLogServiceImpl.java
...rebackendservice/service/impl/ActivityLogServiceImpl.java
+49
-0
No files found.
pom.xml
View file @
e3c40e4e
...
...
@@ -47,12 +47,12 @@
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency
>
<groupId>
org.springframework.boot
</groupId
>
<artifactId>
spring-boot-devtools
</artifactId
>
<scope>
runtime
</scope
>
<optional>
true
</optional
>
</dependency
>
<!-- <dependency>--
>
<!-- <groupId>org.springframework.boot</groupId>--
>
<!-- <artifactId>spring-boot-devtools</artifactId>--
>
<!-- <scope>runtime</scope>--
>
<!-- <optional>true</optional>--
>
<!-- </dependency>--
>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-configuration-processor
</artifactId>
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/AppConfig.java
View file @
e3c40e4e
...
...
@@ -2,6 +2,7 @@ package com.kaluwa.enterprises.babycarebackendservice.config;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
;
import
com.kaluwa.enterprises.babycarebackendservice.service.ActivityLogService
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.boot.web.client.RestTemplateBuilder
;
import
org.springframework.context.annotation.Bean
;
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/WebSocketClient.java
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.config
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ActivityLogDto
;
import
com.kaluwa.enterprises.babycarebackendservice.service.ActivityLogService
;
import
com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.socket.BinaryMessage
;
import
org.springframework.web.socket.TextMessage
;
import
org.springframework.web.socket.WebSocketSession
;
...
...
@@ -14,14 +18,17 @@ import java.util.concurrent.ScheduledExecutorService;
import
java.util.concurrent.TimeUnit
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
config
.
WebSocketConfig
.
VideoFrameHandler
.
sendTextMessageToClient
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
Configs
.
WEBSOCKET_URL
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
LogTypes
.
EMOTION
;
public
class
WebSocketClient
{
private
WebSocketSession
session
;
private
static
final
String
WEBSOCKET_URL
=
"ws://localhost:8000/ws/emotion"
;
private
static
final
ScheduledExecutorService
scheduler
=
Executors
.
newSingleThreadScheduledExecutor
();
private
ActivityLogService
activityLogService
;
public
WebSocketClient
()
{
public
WebSocketClient
(
ActivityLogService
activityLogService
)
{
this
.
activityLogService
=
activityLogService
;
connectToWebSocket
();
}
...
...
@@ -77,6 +84,16 @@ public class WebSocketClient {
EmotionPrediction
lastPrediction
=
predictions
[
predictions
.
length
-
1
];
// Now you can work with the last prediction
System
.
out
.
println
(
"Last emotion prediction: "
+
lastPrediction
);
// Save the last prediction to the database
if
(
lastPrediction
.
getEmotion
()
!=
null
&&
!
lastPrediction
.
getEmotion
().
isEmpty
())
{
ActivityLogDto
activityLogDto
=
new
ActivityLogDto
();
activityLogDto
.
setActivityLogType
(
EMOTION
);
activityLogDto
.
setActivityLogDescription
(
lastPrediction
.
getEmotion
());
webSocketClient
.
activityLogService
.
saveActivityLog
(
activityLogDto
);
}
// Send the last prediction to the client
sendTextMessageToClient
(
lastPrediction
);
}
}
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/config/WebSocketConfig.java
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.config
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.kaluwa.enterprises.babycarebackendservice.dao.ActivityLogDao
;
import
com.kaluwa.enterprises.babycarebackendservice.model.ActivityLog
;
import
com.kaluwa.enterprises.babycarebackendservice.service.ActivityLogService
;
import
com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction
;
import
lombok.extern.slf4j.Slf4j
;
import
org.bytedeco.opencv.opencv_core.Algorithm
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.boot.configurationprocessor.json.JSONStringer
;
...
...
@@ -25,6 +29,12 @@ import java.io.IOException;
@Slf4j
public
class
WebSocketConfig
implements
WebSocketConfigurer
{
private
final
ActivityLogService
activityLogService
;
public
WebSocketConfig
(
ActivityLogService
activityLogService
)
{
this
.
activityLogService
=
activityLogService
;
}
@Override
public
void
registerWebSocketHandlers
(
WebSocketHandlerRegistry
registry
)
{
registry
.
addHandler
(
new
VideoFrameHandler
(),
"/emotional/video-process"
).
setAllowedOrigins
(
"*"
);
...
...
@@ -33,7 +43,7 @@ public class WebSocketConfig implements WebSocketConfigurer {
@Bean
@Qualifier
(
"customWebsocketClient"
)
public
WebSocketClient
customWebSocketClient
()
{
return
new
WebSocketClient
();
return
new
WebSocketClient
(
activityLogService
);
}
@Component
...
...
@@ -56,7 +66,7 @@ public class WebSocketConfig implements WebSocketConfigurer {
sendBinaryMessageToClient
(
binaryData
);
// Optionally, send a text message
EmotionPrediction
emotionPrediction
=
new
EmotionPrediction
();
// E
xample
object
EmotionPrediction
emotionPrediction
=
new
EmotionPrediction
();
// E
mpty
object
sendTextMessageToClient
(
emotionPrediction
);
}
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/Configs.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.constants
;
public
class
Configs
{
public
static
final
String
WEBSOCKET_URL
=
"ws://localhost:8000/ws/emotion"
;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/LogTypes.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.constants
;
public
class
LogTypes
{
public
static
final
String
EMOTION
=
"EMOTION"
;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/TableNames.java
View file @
e3c40e4e
...
...
@@ -5,5 +5,6 @@ public class TableNames {
public
final
static
String
USER_TABLE
=
"users"
;
public
final
static
String
BABY_TABLE
=
"babies"
;
public
final
static
String
DOCUMENT_TABLE
=
"documents"
;
public
final
static
String
ACTIVITY_LOG_TABLE
=
"activity_logs"
;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dao/ActivityLogDao.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.dao
;
import
com.kaluwa.enterprises.babycarebackendservice.model.ActivityLog
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.stereotype.Repository
;
import
java.util.List
;
@Repository
public
interface
ActivityLogDao
extends
JpaRepository
<
ActivityLog
,
Long
>
{
List
<
ActivityLog
>
findAllByOrderByActivityLogIdDesc
();
}
\ No newline at end of file
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/ActivityLogDto.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.dto
;
import
lombok.*
;
import
java.io.Serializable
;
/**
* DTO for {@link com.kaluwa.enterprises.babycarebackendservice.model.ActivityLog}
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public
class
ActivityLogDto
implements
Serializable
{
private
Long
activityLogId
;
private
String
activityLogType
;
private
String
activityLogDescription
;
}
\ No newline at end of file
src/main/java/com/kaluwa/enterprises/babycarebackendservice/mappers/ActivityLogMapper.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.mappers
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ActivityLogDto
;
import
com.kaluwa.enterprises.babycarebackendservice.model.ActivityLog
;
import
org.mapstruct.*
;
import
java.util.List
;
@Mapper
(
componentModel
=
"spring"
)
public
interface
ActivityLogMapper
{
ActivityLog
toEntity
(
ActivityLogDto
activityLogDto
);
ActivityLogDto
toDto
(
ActivityLog
activityLog
);
List
<
ActivityLogDto
>
listToDto
(
List
<
ActivityLog
>
activityLogs
);
}
\ No newline at end of file
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/ActivityLog.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.model
;
import
jakarta.persistence.*
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
TableNames
.
ACTIVITY_LOG_TABLE
;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table
(
name
=
ACTIVITY_LOG_TABLE
)
public
class
ActivityLog
{
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
)
private
Long
activityLogId
;
private
String
activityLogType
;
private
String
activityLogDescription
;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/rest/ActivityLogController.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.rest
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ActivityLogDto
;
import
com.kaluwa.enterprises.babycarebackendservice.service.ActivityLogService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.List
;
@RestController
@RequestMapping
(
"/activity-logs"
)
@Slf4j
public
class
ActivityLogController
{
private
final
ActivityLogService
activityLogService
;
public
ActivityLogController
(
ActivityLogService
activityLogService
)
{
this
.
activityLogService
=
activityLogService
;
}
@GetMapping
public
ResponseEntity
<
List
<
ActivityLogDto
>>
getAllActivityLogs
()
{
log
.
info
(
"Inside activity log controller getAllActivityLogs method"
);
return
activityLogService
.
getAllActivityLogs
();
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/ActivityLogService.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.service
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ActivityLogDto
;
import
org.springframework.http.ResponseEntity
;
import
java.util.List
;
public
interface
ActivityLogService
{
ActivityLogDto
saveActivityLog
(
ActivityLogDto
activityLogDto
);
ResponseEntity
<
List
<
ActivityLogDto
>>
getAllActivityLogs
();
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/ActivityLogServiceImpl.java
0 → 100644
View file @
e3c40e4e
package
com.kaluwa.enterprises.babycarebackendservice.service.impl
;
import
com.kaluwa.enterprises.babycarebackendservice.dao.ActivityLogDao
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ActivityLogDto
;
import
com.kaluwa.enterprises.babycarebackendservice.error.BadRequestAlertException
;
import
com.kaluwa.enterprises.babycarebackendservice.mappers.ActivityLogMapper
;
import
com.kaluwa.enterprises.babycarebackendservice.service.ActivityLogService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
java.util.List
;
@Service
@Slf4j
public
class
ActivityLogServiceImpl
implements
ActivityLogService
{
private
final
ActivityLogDao
activityLogDao
;
private
final
ActivityLogMapper
activityLogMapper
;
public
ActivityLogServiceImpl
(
ActivityLogDao
activityLogDao
,
ActivityLogMapper
activityLogMapper
)
{
this
.
activityLogDao
=
activityLogDao
;
this
.
activityLogMapper
=
activityLogMapper
;
}
@Override
public
ActivityLogDto
saveActivityLog
(
ActivityLogDto
activityLogDto
)
{
log
.
info
(
"Inside saveActivityLog method in ActivityLogServiceImpl"
);
try
{
return
activityLogMapper
.
toDto
(
activityLogDao
.
save
(
activityLogMapper
.
toEntity
(
activityLogDto
)));
}
catch
(
Exception
e
)
{
log
.
error
(
"Error occurred while saving activity log: {}"
,
e
.
getMessage
());
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"ActivityLog"
,
"errorSavingActivityLog"
);
}
}
@Override
public
ResponseEntity
<
List
<
ActivityLogDto
>>
getAllActivityLogs
()
{
log
.
info
(
"Inside getAllActivityLogs method in ActivityLogServiceImpl"
);
try
{
return
ResponseEntity
.
ok
(
activityLogMapper
.
listToDto
(
activityLogDao
.
findAllByOrderByActivityLogIdDesc
()));
}
catch
(
Exception
e
)
{
log
.
error
(
"Error occurred while fetching activity logs: {}"
,
e
.
getMessage
());
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"ActivityLog"
,
"errorFetchingActivityLogs"
);
}
}
}
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