Commit e3c40e4e authored by Ishankha K.C's avatar Ishankha K.C

save activities into database

parent f8379cc4
......@@ -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>
......
......@@ -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;
......
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);
}
}
......
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(); // Example object
EmotionPrediction emotionPrediction = new EmotionPrediction(); // Empty object
sendTextMessageToClient(emotionPrediction);
}
......
package com.kaluwa.enterprises.babycarebackendservice.constants;
public class Configs {
public static final String WEBSOCKET_URL = "ws://localhost:8000/ws/emotion";
}
package com.kaluwa.enterprises.babycarebackendservice.constants;
public class LogTypes {
public static final String EMOTION = "EMOTION";
}
......@@ -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";
}
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
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
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
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;
}
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();
}
}
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();
}
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");
}
}
}
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