-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/org/ioteatime/meonghanyangserver/clients/iot/AIDetectClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
src/main/java/org/ioteatime/meonghanyangserver/common/type/IoTErrorType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters