diff --git a/src/main/kotlin/gdsc/plantory/PlantoryApplication.kt b/src/main/kotlin/gdsc/plantory/PlantoryApplication.kt index 10b9590..df8fb20 100644 --- a/src/main/kotlin/gdsc/plantory/PlantoryApplication.kt +++ b/src/main/kotlin/gdsc/plantory/PlantoryApplication.kt @@ -2,10 +2,19 @@ package gdsc.plantory import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication +import org.springframework.scheduling.annotation.EnableScheduling +import java.util.TimeZone +import javax.annotation.PostConstruct +@EnableScheduling @SpringBootApplication class PlantoryApplication fun main(args: Array) { runApplication(*args) + + @PostConstruct + fun setTimezone() { + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul")) + } } diff --git a/src/main/kotlin/gdsc/plantory/event/notification/WaterCycleEventListener.kt b/src/main/kotlin/gdsc/plantory/event/notification/WaterCycleEventListener.kt index e16fe66..8df0b61 100644 --- a/src/main/kotlin/gdsc/plantory/event/notification/WaterCycleEventListener.kt +++ b/src/main/kotlin/gdsc/plantory/event/notification/WaterCycleEventListener.kt @@ -8,10 +8,9 @@ import com.google.firebase.messaging.Message import com.google.firebase.messaging.Notification import gdsc.plantory.event.FCMChannel import org.slf4j.LoggerFactory +import org.springframework.context.event.EventListener import org.springframework.scheduling.annotation.Async import org.springframework.stereotype.Component -import org.springframework.transaction.event.TransactionPhase -import org.springframework.transaction.event.TransactionalEventListener @Component class WaterCycleEventListener( @@ -22,12 +21,15 @@ class WaterCycleEventListener( private val log = LoggerFactory.getLogger(WaterCycleEventListener::class.java) } - @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) @Async + @EventListener fun sendFcmNotification(events: WaterCycleEvents) { + log.info("send FCM notification by WaterCycleEventListener") val messages: List = createMessages(events.plantsNeedWateredToday, FCMChannel.WATER_ALERT.name) try { + if (messages.isEmpty()) return + firebaseMessaging.sendEach(messages) } catch (e: FirebaseMessagingException) { log.warn("fail send FCM message", e) diff --git a/src/main/kotlin/gdsc/plantory/plant/service/ReminderService.kt b/src/main/kotlin/gdsc/plantory/plant/service/ReminderService.kt index 2288850..4776de8 100644 --- a/src/main/kotlin/gdsc/plantory/plant/service/ReminderService.kt +++ b/src/main/kotlin/gdsc/plantory/plant/service/ReminderService.kt @@ -5,6 +5,7 @@ import gdsc.plantory.event.notification.WaterCycleEvent import gdsc.plantory.event.notification.WaterCycleEvents import gdsc.plantory.plant.domain.CompanionPlantRepository import gdsc.plantory.plant.presentation.dto.CompanionPlantWaterCycleDto +import org.slf4j.LoggerFactory import org.springframework.scheduling.annotation.Scheduled import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -16,8 +17,13 @@ class ReminderService( private val companionPlantRepository: CompanionPlantRepository, ) { - @Scheduled(cron = "0 0 8 * * *") + companion object { + private val log = LoggerFactory.getLogger(this.javaClass) + } + + @Scheduled(cron = "0 */5 * * * *") fun sendWaterNotification() { + log.info("물주기 알림을 전송합니다."); val companionPlants = companionPlantRepository.findAllByNextWaterDate(LocalDate.now()) val events = WaterCycleEvents(buildWaterCycleEvents(companionPlants)) Events.raise(events) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 1a6e4f8..37017cc 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,8 @@ spring: + sql: + init: + data-locations: classpath:sql/data.sql + mode: always jpa: database: mysql database-platform: org.hibernate.dialect.MySQLDialect diff --git a/src/main/resources/sql/data.sql b/src/main/resources/sql/data.sql new file mode 100644 index 0000000..9e5185e --- /dev/null +++ b/src/main/resources/sql/data.sql @@ -0,0 +1,22 @@ +INSERT INTO PLANT_INFORMATION (name, image_url, family_name, smell, poison, manage_level, grow_speed, require_temp, + minimum_temp, require_humidity, posting_place, special_manage_info, water_cycle_spring, + water_cycle_summer, water_cycle_autumn, water_cycle_winter, created_at, last_modified_at) +VALUES ('가울테리아', 'https://nongsaro.go.kr/cms_contents/301/12938_MF_ATTACH_01.jpg', '진달래과', '중간', '정보없음', '경험자', '느림', + '16~20℃', '0℃ 이하', '40 ~ 70%', '거실 창측 (실내깊이 150~300cm),발코니 내측 (실내깊이 50~150cm),발코니 창측 (실내깊이 0~50cm)', '정보없음', 4, + 3, 4, 4, '2024-01-25 10:42:35', '2024-01-25 10:42:35'), + ('개운죽', 'https://nongsaro.go.kr/cms_contents/301/12954_MF_ATTACH_01.jpg', '백합과', '거의 없음', '있음', '초보자', '보통', + '16~20℃', '13℃ 이상', '40 ~ 70%', + '거실 내측 (실내깊이 300~500cm),거실 창측 (실내깊이 150~300cm),발코니 내측 (실내깊이 50~150cm),발코니 창측 (실내깊이 0~50cm)', + '수경은 물주기가 필요 없으나, 화분은 1-2주에 한번씩 충분히 관수한다.', 4, 3, 4, 4, '2024-01-25 10:42:35', '2024-01-25 10:42:35'), + ('골드크레스트 윌마', 'https://nongsaro.go.kr/cms_contents/301/12955_MF_ATTACH_04.jpg', '측백나무과', '강함', '정보없음', '경험자', + '보통', '16~20℃', '10℃', '40 ~ 70%', '거실 창측 (실내깊이 150~300cm),발코니 내측 (실내깊이 50~150cm),발코니 창측 (실내깊이 0~50cm)', + '겨울에는 건조하게 관리한다. 어린 나무일수록 물을 더 자주 준다.', 4, 3, 4, 4, '2024-01-25 10:42:35', '2024-01-25 10:42:35'), + ('공작란', 'https://nongsaro.go.kr/cms_contents/301/12956_MF_ATTACH_01.jpg', '난초과', '강함', '정보없음', '경험자', '보통', + '16~20℃', '13℃ 이상', '40 ~ 70%', '거실 창측 (실내깊이 150~300cm),발코니 내측 (실내깊이 50~150cm),발코니 창측 (실내깊이 0~50cm)', '정보없음', 4, + 3, 4, 4, '2024-01-25 10:42:35', '2024-01-25 10:42:35'), + ('공작야자', 'https://nongsaro.go.kr/cms_contents/301/12957_MF_REPR_ATTACH_01.jpg', '야자과', '정보없음', '정보없음', '경험자', + '보통', '21~25℃', '13℃ 이상', '70% 이상', '거실 창측 (실내깊이 150~300cm),발코니 내측 (실내깊이 50~150cm),발코니 창측 (실내깊이 0~50cm)', + '정보없음', 4, 3, 4, 4, '2024-01-25 10:42:35', '2024-01-25 10:42:35'), + ('관엽베고니아', 'https://nongsaro.go.kr/cms_contents/301/12962_MF_REPR_ATTACH_01.jpg', '베고니아과', '정보없음', '정보없음', '경험자', + '보통', '16~20℃', '13℃ 이상', '40 ~ 70%', '거실 창측 (실내깊이 150~300cm),발코니 내측 (실내깊이 50~150cm),발코니 창측 (실내깊이 0~50cm)', + '정보없음', 4, 3, 4, 4, '2024-01-25 10:42:35', '2024-01-25 10:42:35');