Skip to content

Commit

Permalink
Merge pull request #269 from IoTeaTime/feature/268-push-msg
Browse files Browse the repository at this point in the history
feat: 객체에 따라 객체 푸시 알림 내용 작성 구현
  • Loading branch information
ywonchae1 authored Dec 7, 2024
2 parents 787c911 + f862010 commit 434f967
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.ioteatime.meonghanyangserver.clients.iot;

import java.util.Arrays;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.ioteatime.meonghanyangserver.cctv.domain.CctvEntity;
import org.ioteatime.meonghanyangserver.cctv.repository.CctvRepository;
import org.ioteatime.meonghanyangserver.clients.google.FcmClient;
import org.ioteatime.meonghanyangserver.clients.iot.dto.AIDetectPayload;
import org.ioteatime.meonghanyangserver.clients.iot.dto.AIObject;
import org.ioteatime.meonghanyangserver.common.exception.BadRequestException;
import org.ioteatime.meonghanyangserver.common.type.IoTErrorType;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -40,10 +43,13 @@ private void detectEventHandler(MqttMessage mqttMessage) {
cctvRepository.findByThingId(getThingIdFromTopic(mqttMessage));
if (cctvEntity.isPresent()) {
CctvEntity cctv = cctvEntity.get();
fcmClient.sendPush(
"뽀삐가 나타났어요!",
Arrays.toString(mqttMessage.getPayload()), // Payload 수정 필요
cctv.getGroup().getFcmTopic());

AIObject aiObject = getObjectTypeFromPayload(mqttMessage.getPayload());
String message =
writeMessage(cctv.getGroup().getGroupName(), cctv.getCctvNickname(), aiObject);

fcmClient.sendPush("뽀삐가 나타났어요!", message, cctv.getGroup().getFcmTopic());
log.info("[객체 탐지 알림] {}", "다음과 같은 메시지를 전송했습니다. : " + message);
log.info(
"[객체 탐지 알림] {}",
cctv.getGroup().getGroupName() + " 그룹에 객체 탐지 푸시 알림 전송을 성공하였습니다.");
Expand All @@ -54,4 +60,35 @@ private static String getThingIdFromTopic(MqttMessage mqttMessage) {
String topic = mqttMessage.getTopic();
return topic.substring(topic.lastIndexOf('/') + 1);
}

private AIObject getObjectTypeFromPayload(byte[] payload) {
try {
ObjectMapper objectMapper = new ObjectMapper();
AIDetectPayload detectPayload = objectMapper.readValue(payload, AIDetectPayload.class);
log.info("[객체 탐지 Payload] {}", detectPayload.objectType());
return detectPayload.objectType();
} catch (IOException e) {
log.error(e.getMessage());
}
return AIObject.other;
}

private String writeMessage(String groupName, String cctvName, AIObject aiObject) {
return groupName
+ "의 "
+ cctvName
+ "에서 "
+ aiObject.getValue()
+ getParticle(aiObject)
+ " 나타났어요!";
}

private String getParticle(AIObject aiObject) {
String particle;
switch (aiObject) {
case person -> particle = "이";
default -> particle = "가";
}
return particle;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.ioteatime.meonghanyangserver.clients.iot.dto;

public record AIDetectPayload(
Long trackingId,
Long timestamp,
AIObject objectType,
Double confidence,
Coordinates coordinates) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.ioteatime.meonghanyangserver.clients.iot.dto;

import lombok.Getter;

@Getter
public enum AIObject {
dog("강아지"),
cat("고양이"),
person("사람"),
other("알 수 없는 물체");

private String value;

AIObject(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.ioteatime.meonghanyangserver.clients.iot.dto;

public record Coordinates(
Integer x1,
Integer y1,
Integer x2,
Integer y2,
Integer x3,
Integer y3,
Integer x4,
Integer y4) {}

0 comments on commit 434f967

Please sign in to comment.