Commit 61df0ae6 authored by Ishankha K.C's avatar Ishankha K.C

Merge branch 'feature/chamode_dev' into 'master'

Feature/chamode dev

See merge request !6
parents 045a2635 404837a9
...@@ -47,12 +47,12 @@ ...@@ -47,12 +47,12 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>org.springframework.boot</groupId> <!-- <groupId>org.springframework.boot</groupId>-->
<artifactId>spring-boot-devtools</artifactId> <!-- <artifactId>spring-boot-devtools</artifactId>-->
<scope>runtime</scope> <!-- <scope>runtime</scope>-->
<optional>true</optional> <!-- <optional>true</optional>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId> <artifactId>spring-boot-configuration-processor</artifactId>
......
...@@ -2,6 +2,7 @@ package com.kaluwa.enterprises.babycarebackendservice.config; ...@@ -2,6 +2,7 @@ package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.kaluwa.enterprises.babycarebackendservice.service.ActivityLogService;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
......
package com.kaluwa.enterprises.babycarebackendservice.config; package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper; 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 com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage; import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
...@@ -14,14 +18,17 @@ import java.util.concurrent.ScheduledExecutorService; ...@@ -14,14 +18,17 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.kaluwa.enterprises.babycarebackendservice.config.WebSocketConfig.VideoFrameHandler.sendTextMessageToClient; 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 { public class WebSocketClient {
private WebSocketSession session; private WebSocketSession session;
private static final String WEBSOCKET_URL = "ws://localhost:8000/ws/emotion";
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private ActivityLogService activityLogService;
public WebSocketClient() { public WebSocketClient(ActivityLogService activityLogService) {
this.activityLogService = activityLogService;
connectToWebSocket(); connectToWebSocket();
} }
...@@ -77,6 +84,16 @@ public class WebSocketClient { ...@@ -77,6 +84,16 @@ public class WebSocketClient {
EmotionPrediction lastPrediction = predictions[predictions.length - 1]; EmotionPrediction lastPrediction = predictions[predictions.length - 1];
// Now you can work with the last prediction // Now you can work with the last prediction
System.out.println("Last emotion prediction: " + lastPrediction); 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); sendTextMessageToClient(lastPrediction);
} }
} }
......
package com.kaluwa.enterprises.babycarebackendservice.config; package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper; 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 com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction;
import lombok.extern.slf4j.Slf4j; 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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.configurationprocessor.json.JSONStringer; import org.springframework.boot.configurationprocessor.json.JSONStringer;
...@@ -25,6 +29,12 @@ import java.io.IOException; ...@@ -25,6 +29,12 @@ import java.io.IOException;
@Slf4j @Slf4j
public class WebSocketConfig implements WebSocketConfigurer { public class WebSocketConfig implements WebSocketConfigurer {
private final ActivityLogService activityLogService;
public WebSocketConfig(ActivityLogService activityLogService) {
this.activityLogService = activityLogService;
}
@Override @Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new VideoFrameHandler(), "/emotional/video-process").setAllowedOrigins("*"); registry.addHandler(new VideoFrameHandler(), "/emotional/video-process").setAllowedOrigins("*");
...@@ -33,7 +43,7 @@ public class WebSocketConfig implements WebSocketConfigurer { ...@@ -33,7 +43,7 @@ public class WebSocketConfig implements WebSocketConfigurer {
@Bean @Bean
@Qualifier("customWebsocketClient") @Qualifier("customWebsocketClient")
public WebSocketClient customWebSocketClient() { public WebSocketClient customWebSocketClient() {
return new WebSocketClient(); return new WebSocketClient(activityLogService);
} }
@Component @Component
...@@ -56,7 +66,7 @@ public class WebSocketConfig implements WebSocketConfigurer { ...@@ -56,7 +66,7 @@ public class WebSocketConfig implements WebSocketConfigurer {
sendBinaryMessageToClient(binaryData); sendBinaryMessageToClient(binaryData);
// Optionally, send a text message // Optionally, send a text message
EmotionPrediction emotionPrediction = new EmotionPrediction(); // Example object EmotionPrediction emotionPrediction = new EmotionPrediction(); // Empty object
sendTextMessageToClient(emotionPrediction); 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 { ...@@ -5,5 +5,6 @@ public class TableNames {
public final static String USER_TABLE = "users"; public final static String USER_TABLE = "users";
public final static String BABY_TABLE = "babies"; public final static String BABY_TABLE = "babies";
public final static String DOCUMENT_TABLE = "documents"; 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
...@@ -6,6 +6,7 @@ import lombok.AllArgsConstructor; ...@@ -6,6 +6,7 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.io.InputStream;
import java.time.LocalDate; import java.time.LocalDate;
import static com.kaluwa.enterprises.babycarebackendservice.constants.Status.STATUS_NEW; import static com.kaluwa.enterprises.babycarebackendservice.constants.Status.STATUS_NEW;
...@@ -54,6 +55,7 @@ public class BabyDto { ...@@ -54,6 +55,7 @@ public class BabyDto {
private String notes; private String notes;
private String uniqKey; private String uniqKey;
private UserDto user; private UserDto user;
private byte[] imageData;
@NotNull(message = "Logged user id is required") @NotNull(message = "Logged user id is required")
@NotEmpty(message = "Logged user id is required") @NotEmpty(message = "Logged user id is required")
......
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");
}
}
}
...@@ -9,14 +9,20 @@ import com.kaluwa.enterprises.babycarebackendservice.mappers.BabyMapper; ...@@ -9,14 +9,20 @@ import com.kaluwa.enterprises.babycarebackendservice.mappers.BabyMapper;
import com.kaluwa.enterprises.babycarebackendservice.model.Baby; import com.kaluwa.enterprises.babycarebackendservice.model.Baby;
import com.kaluwa.enterprises.babycarebackendservice.model.Document; import com.kaluwa.enterprises.babycarebackendservice.model.Document;
import com.kaluwa.enterprises.babycarebackendservice.service.BabyService; import com.kaluwa.enterprises.babycarebackendservice.service.BabyService;
import com.kaluwa.enterprises.babycarebackendservice.service.DocumentService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static com.kaluwa.enterprises.babycarebackendservice.constants.DocumentTypes.IMAGE;
import static com.kaluwa.enterprises.babycarebackendservice.constants.TableNames.BABY_TABLE;
import static com.kaluwa.enterprises.babycarebackendservice.utils.Utils.uniqKeyGenerator; import static com.kaluwa.enterprises.babycarebackendservice.utils.Utils.uniqKeyGenerator;
@Service @Service
...@@ -25,12 +31,13 @@ public class BabyServiceImpl implements BabyService { ...@@ -25,12 +31,13 @@ public class BabyServiceImpl implements BabyService {
private final BabyDao babyDao; private final BabyDao babyDao;
private final BabyMapper babyMapper; private final BabyMapper babyMapper;
private final DocumentDao documentDao; private final DocumentDao documentDao;
private final DocumentService documentService;
public BabyServiceImpl(BabyDao babyDao, BabyMapper babyMapper, DocumentDao documentDao, DocumentService documentService) {
public BabyServiceImpl(BabyDao babyDao, BabyMapper babyMapper, DocumentDao documentDao) {
this.babyDao = babyDao; this.babyDao = babyDao;
this.babyMapper = babyMapper; this.babyMapper = babyMapper;
this.documentDao = documentDao; this.documentDao = documentDao;
this.documentService = documentService;
} }
@Override @Override
...@@ -49,7 +56,19 @@ public class BabyServiceImpl implements BabyService { ...@@ -49,7 +56,19 @@ public class BabyServiceImpl implements BabyService {
public List<BabyDto> getAllBabies() { public List<BabyDto> getAllBabies() {
log.info("Inside baby service getAllBabies method"); log.info("Inside baby service getAllBabies method");
try { try {
return babyMapper.toDtoList(babyDao.findAll());
List<BabyDto> babyList = babyMapper.toDtoList(babyDao.findAll());
babyList.forEach(babyDto -> {
Optional<Document> documentOp = documentDao.findByTableNameAndUniqKeyAndDocumentType(BABY_TABLE, babyDto.getUniqKey(), IMAGE);
documentOp.ifPresent(document -> {
byte[] imageData = documentService.getImage(BABY_TABLE, babyDto.getUniqKey()).getBody();
if (imageData != null) {
babyDto.setImageData(imageData);
}
});
});
return babyList;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error"); throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
...@@ -100,10 +119,8 @@ public class BabyServiceImpl implements BabyService { ...@@ -100,10 +119,8 @@ public class BabyServiceImpl implements BabyService {
if (babyOp.isEmpty()) { if (babyOp.isEmpty()) {
throw new BadRequestAlertException("Baby not found", "baby", "baby.error"); throw new BadRequestAlertException("Baby not found", "baby", "baby.error");
} else { } else {
Optional<Document> documentOp = documentDao.findById(babyOp.get().getDocumentId()); Optional<Document> documentOp = documentDao.findByTableNameAndUniqKeyAndDocumentType(BABY_TABLE, babyOp.get().getUniqKey(), IMAGE);
if (documentOp.isPresent()) { documentOp.ifPresent(document -> documentDao.deleteById(document.getDocumentId()));
documentDao.deleteById(babyOp.get().getDocumentId());
}
babyDao.deleteById(babyId); babyDao.deleteById(babyId);
ResponseDto responseDto = new ResponseDto(); ResponseDto responseDto = new ResponseDto();
responseDto.setId(babyId); responseDto.setId(babyId);
......
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