Skip to content

Commit

Permalink
feat: 객체 탐지 푸시 알림 구현 (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
ywonchae1 committed Dec 6, 2024
1 parent d5455c0 commit 8f06876
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public interface CctvRepository {

void deleteByGroupId(Long groupId);

Optional<CctvEntity> findByCctvId(Long thingId);
Optional<CctvEntity> findByCctvId(Long cctvId);

List<CctvEntity> findByGroupId(Long groupId);

Optional<CctvEntity> findByThingId(String thingId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ioteatime.meonghanyangserver.cctv.repository;

import static org.ioteatime.meonghanyangserver.cctv.domain.QCctvEntity.cctvEntity;
import static org.ioteatime.meonghanyangserver.group.domain.QGroupEntity.groupEntity;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand Down Expand Up @@ -83,4 +84,15 @@ public List<CctvEntity> findByGroupId(Long groupId) {

return result;
}

@Override
public Optional<CctvEntity> findByThingId(String thingId) {
return Optional.ofNullable(
jpaQueryFactory
.selectFrom(cctvEntity)
.join(cctvEntity.group, groupEntity)
.fetchJoin()
.where(cctvEntity.thingId.eq(thingId))
.fetchOne());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.ioteatime.meonghanyangserver.clients.iot;

import java.util.Arrays;
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.common.exception.BadRequestException;
import org.ioteatime.meonghanyangserver.common.type.IoTErrorType;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.crt.mqtt.MqttMessage;

// 객체 탐지 클라이언트
@Slf4j
@Component
public class AIDetectClient {
private final FcmClient fcmClient;
private final IotMqttClient iotMqttClient;
private final CctvRepository cctvRepository;

public AIDetectClient(
FcmClient fcmClient, IotMqttClient iotMqttClient, CctvRepository cctvRepository) {
this.fcmClient = fcmClient;
this.iotMqttClient = iotMqttClient;
this.cctvRepository = cctvRepository;

iotMqttClient.subscribe("/mhn/event/detect/things/#", this::handleDetectEvent);
iotMqttClient.subscribe("/mhn/event/detect/things/+", this::handleDetectEvent);
log.info("[객체 탐지] {}", "탐지 Topic을 구독하였습니다.");
}

private void handleDetectEvent(MqttMessage mqttMessage) {
log.info("[객체 탐지] {}", "탐지 Topic이 발행되었습니다.");
// thingId 기준 GroupId 검색
if (mqttMessage.getTopic() == null || mqttMessage.getTopic().isEmpty()) {
throw new BadRequestException(IoTErrorType.TOPIC_NULL);
}
Optional<CctvEntity> cctvEntity =
cctvRepository.findByThingId(getThingIdFromTopic(mqttMessage));
if (cctvEntity.isPresent()) {
CctvEntity cctv = cctvEntity.get();
fcmClient.sendPush(
"뽀삐가 나타났어요!",
Arrays.toString(mqttMessage.getPayload()),
cctv.getGroup().getFcmTopic());
log.info(
"[객체 탐지 알림] {}",
cctv.getGroup().getGroupName() + " 그룹 객체 탐지 푸시 알림 전송에 성공하였습니다.");
}
}

private static String getThingIdFromTopic(MqttMessage mqttMessage) {
String topic = mqttMessage.getTopic();
return topic.substring(topic.lastIndexOf('/') + 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.ioteatime.meonghanyangserver.common.type;

public enum IoTErrorType implements ErrorTypeCode {
UPDATE_SHADOW("INTERNAL_SERVER", "Shadow 갱신에 실패하였습니다.");
UPDATE_SHADOW("INTERNAL_SERVER", "Shadow 갱신에 실패하였습니다."),
TOPIC_NULL("BAD_REQUEST", "Topic이 Null입니다.");

private final String message;
private final String description;
Expand Down

0 comments on commit 8f06876

Please sign in to comment.