From bfa871764e27f1251f0dbaf0c985dc222063a670 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Mon, 8 Jul 2024 21:26:40 +0900 Subject: [PATCH 1/6] =?UTF-8?q?=E2=9C=A8=20=20feat:=20=EC=B2=B4=EA=B0=90?= =?UTF-8?q?=EC=98=A8=EB=8F=84=20=EA=B3=84=EC=82=B0=EC=8B=9D=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=B0=8F=20Kafka=20=EC=97=B0=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WeatherTestController.java | 8 -------- .../weatherservice/service/WeatherService.java | 11 +++++++++++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherTestController.java b/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherTestController.java index 4a92c8ef..c708bc8f 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherTestController.java +++ b/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherTestController.java @@ -51,12 +51,4 @@ public void airKoreaTest(@RequestBody AirTestRequest request) throws URISyntaxEx public void accuweatherTest(@RequestBody AccuweatherTestRequest request) throws URISyntaxException, IOException { weatherService.convertLocation(request.latitude(), request.longitude()); } - - @GetMapping("/converㅅ") - public LocalDateTime convertTest() { - - LocalDateTime specificDateTime = LocalDateTime.of(2024, 7, 5, 23, 0); - log.info("Changed : {}", specificDateTime); - return weatherService.convertLocalDateTimeToDailyWeatherTime(specificDateTime); - } } diff --git a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java index 1252f9e5..3d156180 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java +++ b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java @@ -123,6 +123,10 @@ public void createDailyWeather( KafkaMessage kafkaMessage = KafkaMessage.of(regionName, dailyWeather.getWindDegree()); producer.produceMessage("alarm-wind", kafkaMessage); + double windChill = calculateWindChill(Double.valueOf(tmp), Double.valueOf(wsd)); + KafkaMessage kafkaMessageWindChill = KafkaMessage.of(regionName, String.valueOf(windChill)); + producer.produceMessage("alarm-temp", kafkaMessageWindChill); + DailyWeather save = dailyWeatherRepository.save(dailyWeather); log.info("[*] 하루 온도 : {}", save); @@ -242,4 +246,11 @@ public List getRegionList() { public String convertGpsToRegionName(double latitude, double longitude) { return regionRepository.findRegionByLatAndLong(latitude, longitude).get(0).getRegionName(); } + + public double calculateWindChill(double temp, double wind) { + if (temp > 10 || wind < 4.8) { + return temp; + } + return 13.12 + 0.6215 * temp - 11.37 * Math.pow(wind, 0.16) + 0.3965 * temp * Math.pow(wind, 0.16); + } } From c4e7372f618f7c9c27971304c9afb71607b85586 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Tue, 9 Jul 2024 17:52:43 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../batch/DailyWeatherTasklet.java | 4 +- .../batch/ExpectedWeatherTasklet.java | 4 +- .../{OpenApiUtil.java => OpenApiUtils.java} | 7 +-- .../service/WeatherService.java | 46 +++-------------- .../weatherservice/utills/DateTimeUtils.java | 51 +++++++++++++++++++ .../GpsTransferUtils.java} | 7 ++- .../{gps => utills}/LatXLngY.java | 2 +- .../weatherservice/GpsTransferTest.java | 6 +-- 8 files changed, 72 insertions(+), 55 deletions(-) rename weather-service/src/main/java/com/waither/weatherservice/openapi/{OpenApiUtil.java => OpenApiUtils.java} (97%) create mode 100644 weather-service/src/main/java/com/waither/weatherservice/utills/DateTimeUtils.java rename weather-service/src/main/java/com/waither/weatherservice/{gps/GpsTransfer.java => utills/GpsTransferUtils.java} (96%) rename weather-service/src/main/java/com/waither/weatherservice/{gps => utills}/LatXLngY.java (72%) diff --git a/weather-service/src/main/java/com/waither/weatherservice/batch/DailyWeatherTasklet.java b/weather-service/src/main/java/com/waither/weatherservice/batch/DailyWeatherTasklet.java index 5cf7145f..2a2062bc 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/batch/DailyWeatherTasklet.java +++ b/weather-service/src/main/java/com/waither/weatherservice/batch/DailyWeatherTasklet.java @@ -1,5 +1,7 @@ package com.waither.weatherservice.batch; +import static com.waither.weatherservice.utills.DateTimeUtils.*; + import java.net.URISyntaxException; import java.time.LocalDateTime; import java.util.List; @@ -24,7 +26,7 @@ public class DailyWeatherTasklet implements Tasklet { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) { LocalDateTime baseTime = LocalDateTime.now(); - String[] dateTime = weatherService.convertLocalDateTimeToString(baseTime).split("_"); + String[] dateTime = convertLocalDateTimeToString(baseTime).split("_"); List regionList = weatherService.getRegionList(); regionList.stream() .forEach(region -> { diff --git a/weather-service/src/main/java/com/waither/weatherservice/batch/ExpectedWeatherTasklet.java b/weather-service/src/main/java/com/waither/weatherservice/batch/ExpectedWeatherTasklet.java index d901dc87..0a76400f 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/batch/ExpectedWeatherTasklet.java +++ b/weather-service/src/main/java/com/waither/weatherservice/batch/ExpectedWeatherTasklet.java @@ -1,5 +1,7 @@ package com.waither.weatherservice.batch; +import static com.waither.weatherservice.utills.DateTimeUtils.*; + import java.net.URISyntaxException; import java.time.LocalDateTime; import java.util.List; @@ -24,7 +26,7 @@ public class ExpectedWeatherTasklet implements Tasklet { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) { LocalDateTime baseTime = LocalDateTime.now().minusHours(1); - String[] dateTime = weatherService.convertLocalDateTimeToString(baseTime).split("_"); + String[] dateTime = convertLocalDateTimeToString(baseTime).split("_"); List regionList = weatherService.getRegionList(); regionList.stream() .forEach(region -> { diff --git a/weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtil.java b/weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtils.java similarity index 97% rename from weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtil.java rename to weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtils.java index ffc66ad6..ff6ca14d 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtil.java +++ b/weather-service/src/main/java/com/waither/weatherservice/openapi/OpenApiUtils.java @@ -28,7 +28,7 @@ @Slf4j @Component @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class OpenApiUtil { +public class OpenApiUtils { @Value("${openapi.forecast.key}") private String forecastKey; @@ -145,11 +145,6 @@ public List callAdvisoryApi(String location, String tod } } - public String convertLocalDateToString(LocalDate localDate) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); - return localDate.format(formatter); - } - public List callAirKorea(String searchDate) throws URISyntaxException { int pageNo = 1; int numOfRows = 10; diff --git a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java index 3d156180..de0fa21d 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java +++ b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java @@ -1,11 +1,11 @@ package com.waither.weatherservice.service; +import static com.waither.weatherservice.utills.DateTimeUtils.*; + import java.io.IOException; import java.net.URISyntaxException; import java.time.LocalDate; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Arrays; import java.util.List; import org.springframework.stereotype.Service; @@ -18,12 +18,12 @@ import com.waither.weatherservice.entity.Region; import com.waither.weatherservice.entity.WeatherAdvisory; import com.waither.weatherservice.exception.WeatherExceptionHandler; -import com.waither.weatherservice.gps.GpsTransfer; +import com.waither.weatherservice.utills.GpsTransferUtils; import com.waither.weatherservice.kafka.KafkaMessage; import com.waither.weatherservice.kafka.Producer; import com.waither.weatherservice.openapi.ForeCastOpenApiResponse; import com.waither.weatherservice.openapi.MsgOpenApiResponse; -import com.waither.weatherservice.openapi.OpenApiUtil; +import com.waither.weatherservice.openapi.OpenApiUtils; import com.waither.weatherservice.repository.DailyWeatherRepository; import com.waither.weatherservice.repository.ExpectedWeatherRepository; import com.waither.weatherservice.repository.RegionRepository; @@ -39,7 +39,7 @@ @RequiredArgsConstructor public class WeatherService { - private final OpenApiUtil openApiUtil; + private final OpenApiUtils openApiUtil; private final DailyWeatherRepository dailyWeatherRepository; private final ExpectedWeatherRepository expectedWeatherRepository; private final WeatherAdvisoryRepository weatherAdvisoryRepository; @@ -134,9 +134,9 @@ public void createDailyWeather( public void createWeatherAdvisory(double latitude, double longitude) throws URISyntaxException, IOException { LocalDate now = LocalDate.now(); - String today = openApiUtil.convertLocalDateToString(now); + String today = convertLocalDateToString(now); - String location = GpsTransfer.convertGpsToRegionCode(latitude, longitude); + String location = GpsTransferUtils.convertGpsToRegionCode(latitude, longitude); List items = openApiUtil.callAdvisoryApi(location, today); @@ -207,38 +207,6 @@ public MainWeatherResponse getMainWeather(double latitude, double longitude) { return weatherMainResponse; } - public String convertLocalDateTimeToString(LocalDateTime time) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - String formattedDateTime = time.format(formatter); - - String[] lst = formattedDateTime.split(" "); - String baseDate = lst[0].replace("-", ""); - - String[] temp = lst[1].split(":"); - String baseTime = temp[0] + "00"; - - return baseDate + "_" + baseTime; - } - - public LocalDateTime convertLocalDateTimeToDailyWeatherTime(LocalDateTime time) { - - // DailyWeather 정보는 3시간마다 - List scheduledHours = Arrays.asList(0, 3, 6, 9, 12, 15, 18, 21); - - int currentHour = time.getHour(); - int adjustedHour = scheduledHours.stream() - .filter(hour -> hour <= currentHour) - .reduce((first, second) -> second) - .orElse(scheduledHours.get(scheduledHours.size() - 1)); // 이전 날의 마지막 스케줄 시간(21시) 반환 - - // 현재 시간이 첫 스케줄 시간(0시)보다 작을 경우, 전날의 마지막 스케줄 시간으로 설정 - if (currentHour < scheduledHours.get(0)) { - time = time.minusDays(1); - } - - return time.withHour(adjustedHour).withMinute(0).withSecond(0).withNano(0); - } - public List getRegionList() { return regionRepository.findAll(); } diff --git a/weather-service/src/main/java/com/waither/weatherservice/utills/DateTimeUtils.java b/weather-service/src/main/java/com/waither/weatherservice/utills/DateTimeUtils.java new file mode 100644 index 00000000..9e3c7f30 --- /dev/null +++ b/weather-service/src/main/java/com/waither/weatherservice/utills/DateTimeUtils.java @@ -0,0 +1,51 @@ +package com.waither.weatherservice.utills; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.List; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class DateTimeUtils { + + public static LocalDateTime convertLocalDateTimeToDailyWeatherTime(LocalDateTime time) { + + // DailyWeather 정보는 3시간마다 + List scheduledHours = Arrays.asList(0, 3, 6, 9, 12, 15, 18, 21); + + int currentHour = time.getHour(); + int adjustedHour = scheduledHours.stream() + .filter(hour -> hour <= currentHour) + .reduce((first, second) -> second) + .orElse(scheduledHours.get(scheduledHours.size() - 1)); // 이전 날의 마지막 스케줄 시간(21시) 반환 + + // 현재 시간이 첫 스케줄 시간(0시)보다 작을 경우, 전날의 마지막 스케줄 시간으로 설정 + if (currentHour < scheduledHours.get(0)) { + time = time.minusDays(1); + } + + return time.withHour(adjustedHour).withMinute(0).withSecond(0).withNano(0); + } + + public static String convertLocalDateTimeToString(LocalDateTime time) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + String formattedDateTime = time.format(formatter); + + String[] lst = formattedDateTime.split(" "); + String baseDate = lst[0].replace("-", ""); + + String[] temp = lst[1].split(":"); + String baseTime = temp[0] + "00"; + + return baseDate + "_" + baseTime; + } + + public static String convertLocalDateToString(LocalDate localDate) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); + return localDate.format(formatter); + } +} diff --git a/weather-service/src/main/java/com/waither/weatherservice/gps/GpsTransfer.java b/weather-service/src/main/java/com/waither/weatherservice/utills/GpsTransferUtils.java similarity index 96% rename from weather-service/src/main/java/com/waither/weatherservice/gps/GpsTransfer.java rename to weather-service/src/main/java/com/waither/weatherservice/utills/GpsTransferUtils.java index 4b3d2251..ae78b7e7 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/gps/GpsTransfer.java +++ b/weather-service/src/main/java/com/waither/weatherservice/utills/GpsTransferUtils.java @@ -1,4 +1,4 @@ -package com.waither.weatherservice.gps; +package com.waither.weatherservice.utills; import java.io.IOException; import java.io.InputStream; @@ -15,8 +15,7 @@ import lombok.NoArgsConstructor; @NoArgsConstructor(access = AccessLevel.PRIVATE) -// @Component -public class GpsTransfer { +public class GpsTransferUtils { // 격자 간격 (단위: km) private static final double GRID_INTERVAL = 5.0; @@ -110,7 +109,7 @@ public static LatXLngY convertGridToGps(double x, double y) { public static String convertGpsToRegionCode(double lat, double lon) { String regionCode = null; try { - InputStream inputStream = GpsTransfer.class.getResourceAsStream("/api/Region.xlsx"); + InputStream inputStream = GpsTransferUtils.class.getResourceAsStream("/api/Region.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); // 시트 인덱스, 첫 번째 시트를 가져옴 diff --git a/weather-service/src/main/java/com/waither/weatherservice/gps/LatXLngY.java b/weather-service/src/main/java/com/waither/weatherservice/utills/LatXLngY.java similarity index 72% rename from weather-service/src/main/java/com/waither/weatherservice/gps/LatXLngY.java rename to weather-service/src/main/java/com/waither/weatherservice/utills/LatXLngY.java index 3e4444d6..b80463c8 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/gps/LatXLngY.java +++ b/weather-service/src/main/java/com/waither/weatherservice/utills/LatXLngY.java @@ -1,4 +1,4 @@ -package com.waither.weatherservice.gps; +package com.waither.weatherservice.utills; import lombok.Builder; diff --git a/weather-service/src/test/java/com/waither/weatherservice/GpsTransferTest.java b/weather-service/src/test/java/com/waither/weatherservice/GpsTransferTest.java index 1dd3b3e9..438c96a2 100644 --- a/weather-service/src/test/java/com/waither/weatherservice/GpsTransferTest.java +++ b/weather-service/src/test/java/com/waither/weatherservice/GpsTransferTest.java @@ -7,14 +7,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import com.waither.weatherservice.gps.GpsTransfer; -import com.waither.weatherservice.gps.LatXLngY; +import com.waither.weatherservice.utills.GpsTransferUtils; +import com.waither.weatherservice.utills.LatXLngY; @SpringBootTest class GpsTransferTest { @Autowired - GpsTransfer gpsTransfer; + GpsTransferUtils gpsTransfer; LatXLngY expected; From eea895d6fdd1f98a80dae9de73870fa2e4b8f752 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Tue, 9 Jul 2024 19:20:15 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20Redis=20?= =?UTF-8?q?=EC=BA=90=EC=8B=B1=20=EC=8B=9C=EA=B0=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/waither/weatherservice/entity/DailyWeather.java | 2 +- .../java/com/waither/weatherservice/entity/ExpectedWeather.java | 2 +- .../java/com/waither/weatherservice/entity/WeatherAdvisory.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/weather-service/src/main/java/com/waither/weatherservice/entity/DailyWeather.java b/weather-service/src/main/java/com/waither/weatherservice/entity/DailyWeather.java index e3022d07..ca5ce270 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/entity/DailyWeather.java +++ b/weather-service/src/main/java/com/waither/weatherservice/entity/DailyWeather.java @@ -13,7 +13,7 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor(access = AccessLevel.PRIVATE) -@RedisHash(value = "DailyWeather", timeToLive = 28800L) // 유효시간: 8시간 +@RedisHash(value = "DailyWeather", timeToLive = 172800L) // 유효시간: 48시간 public class DailyWeather { @Id diff --git a/weather-service/src/main/java/com/waither/weatherservice/entity/ExpectedWeather.java b/weather-service/src/main/java/com/waither/weatherservice/entity/ExpectedWeather.java index 0ef6563d..fa5001b0 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/entity/ExpectedWeather.java +++ b/weather-service/src/main/java/com/waither/weatherservice/entity/ExpectedWeather.java @@ -16,7 +16,7 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor(access = AccessLevel.PRIVATE) -@RedisHash(value = "ExpectedWeather", timeToLive = 21600L) // 유효시간: 6시간 +@RedisHash(value = "ExpectedWeather", timeToLive = 172800L) // 유효시간: 48시간 public class ExpectedWeather { @Id diff --git a/weather-service/src/main/java/com/waither/weatherservice/entity/WeatherAdvisory.java b/weather-service/src/main/java/com/waither/weatherservice/entity/WeatherAdvisory.java index 03b6d3d5..594a4bdd 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/entity/WeatherAdvisory.java +++ b/weather-service/src/main/java/com/waither/weatherservice/entity/WeatherAdvisory.java @@ -13,7 +13,7 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor(access = AccessLevel.PRIVATE) -@RedisHash(value = "WeatherAdvisory", timeToLive = 86400L) // 유효시간: 24시간 +@RedisHash(value = "WeatherAdvisory", timeToLive = 172800L) // 유효시간: 48시간 public class WeatherAdvisory { @Id From 5489638ed89a527048bed6e8e14ee6a915ddcb4b Mon Sep 17 00:00:00 2001 From: seheonnn Date: Tue, 9 Jul 2024 19:41:08 +0900 Subject: [PATCH 4/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20=EC=B2=B4?= =?UTF-8?q?=EA=B0=90=EC=98=A8=EB=8F=84=20=EC=A1=B0=ED=9A=8C=20api=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WeatherController.java | 18 ++++++++++++++ .../dto/request/GetWindChillRequest.java | 21 ++++++++++++++++ .../service/WeatherService.java | 24 +++++++++++++++---- 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 weather-service/src/main/java/com/waither/weatherservice/dto/request/GetWindChillRequest.java diff --git a/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherController.java b/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherController.java index cdc3163b..dca997a6 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherController.java +++ b/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherController.java @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.RestController; import com.waither.weatherservice.dto.request.GetWeatherRequest; +import com.waither.weatherservice.dto.request.GetWindChillRequest; import com.waither.weatherservice.dto.response.MainWeatherResponse; import com.waither.weatherservice.response.ApiResponse; import com.waither.weatherservice.service.WeatherService; @@ -42,4 +43,21 @@ public ApiResponse convertGpsToRegionName(@ModelAttribute @Valid GetWeat return ApiResponse.onSuccess( weatherService.convertGpsToRegionName(getWeatherRequest.latitude(), getWeatherRequest.longitude())); } + + @Operation(summary = "체감온도 가져오기 (전 날까지만) - user-service 사용", + description = "{" + + "\"latitude\": 37.41," + + "\"longitude\": 126.73," + + "\"baseTime\": \"2024-07-09T12:34:56\" " + + "}") + @GetMapping("/region") + public ApiResponse getWindChill(@ModelAttribute @Valid GetWindChillRequest getWindChillRequest) { + return ApiResponse.onSuccess( + weatherService.getWindChill( + getWindChillRequest.latitude(), + getWindChillRequest.longitude(), + getWindChillRequest.baseTime() + ) + ); + } } diff --git a/weather-service/src/main/java/com/waither/weatherservice/dto/request/GetWindChillRequest.java b/weather-service/src/main/java/com/waither/weatherservice/dto/request/GetWindChillRequest.java new file mode 100644 index 00000000..b179c71f --- /dev/null +++ b/weather-service/src/main/java/com/waither/weatherservice/dto/request/GetWindChillRequest.java @@ -0,0 +1,21 @@ +package com.waither.weatherservice.dto.request; + +import java.time.LocalDateTime; + +import jakarta.validation.constraints.DecimalMax; +import jakarta.validation.constraints.DecimalMin; +import jakarta.validation.constraints.NotNull; + +public record GetWindChillRequest( + @NotNull(message = "[ERROR] 위도 입력은 필수 입니다.") + @DecimalMin(value = "33.0", message = "[ERROR] 위도는 33.0도에서 43.0도 사이여야 합니다.") + @DecimalMax(value = "43.0", message = "[ERROR] 위도는 33.0도에서 43.0도 사이여야 합니다.") + Double latitude, + @NotNull(message = "[ERROR] 경도 입력은 필수 입니다.") + @DecimalMin(value = "124.0", message = "[ERROR] 경도는 124.0도에서 132.0도 사이여야 합니다.") + @DecimalMax(value = "132.0", message = "[ERROR] 경도는 124.0도에서 132.0도 사이여야 합니다.") + Double longitude, + @NotNull(message = "[ERROR] 날짜 입력은 필수 입니다.") + LocalDateTime baseTime +) +{} diff --git a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java index de0fa21d..69ccc0ae 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java +++ b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java @@ -123,10 +123,6 @@ public void createDailyWeather( KafkaMessage kafkaMessage = KafkaMessage.of(regionName, dailyWeather.getWindDegree()); producer.produceMessage("alarm-wind", kafkaMessage); - double windChill = calculateWindChill(Double.valueOf(tmp), Double.valueOf(wsd)); - KafkaMessage kafkaMessageWindChill = KafkaMessage.of(regionName, String.valueOf(windChill)); - producer.produceMessage("alarm-temp", kafkaMessageWindChill); - DailyWeather save = dailyWeatherRepository.save(dailyWeather); log.info("[*] 하루 온도 : {}", save); @@ -221,4 +217,24 @@ public double calculateWindChill(double temp, double wind) { } return 13.12 + 0.6215 * temp - 11.37 * Math.pow(wind, 0.16) + 0.3965 * temp * Math.pow(wind, 0.16); } + + public double getWindChill(double latitude, double longitude, LocalDateTime baseTime) { + + List region = regionRepository.findRegionByLatAndLong(latitude, longitude); + + if (region.isEmpty()) + throw new WeatherExceptionHandler(WeatherErrorCode.REGION_NOT_FOUND); + + String regionName = region.get(0).getRegionName(); + + + LocalDateTime dailyWeatherBaseTime = convertLocalDateTimeToDailyWeatherTime(baseTime.minusHours(1)); + + String dailyWeatherKey = regionName + "_" + convertLocalDateTimeToString(dailyWeatherBaseTime); + + DailyWeather dailyWeather = dailyWeatherRepository.findById(dailyWeatherKey) + .orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.DAILY_NOT_FOUND)); + + return calculateWindChill(Double.valueOf(dailyWeather.getTmp()), Double.valueOf(dailyWeather.getWindDegree())); + } } From 066ad70e106c997579524945939a9cfe64aa1e08 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Tue, 9 Jul 2024 19:41:50 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20=EC=B2=B4?= =?UTF-8?q?=EA=B0=90=EC=98=A8=EB=8F=84=20=EC=A1=B0=ED=9A=8C=20api=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../waither/weatherservice/controller/WeatherController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherController.java b/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherController.java index dca997a6..07be1432 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherController.java +++ b/weather-service/src/main/java/com/waither/weatherservice/controller/WeatherController.java @@ -50,7 +50,7 @@ public ApiResponse convertGpsToRegionName(@ModelAttribute @Valid GetWeat + "\"longitude\": 126.73," + "\"baseTime\": \"2024-07-09T12:34:56\" " + "}") - @GetMapping("/region") + @GetMapping("/wind-chill") public ApiResponse getWindChill(@ModelAttribute @Valid GetWindChillRequest getWindChillRequest) { return ApiResponse.onSuccess( weatherService.getWindChill( From 33c5054d82430c8eb2b24652e6eea7dc3aab9f7e Mon Sep 17 00:00:00 2001 From: seheonnn Date: Tue, 9 Jul 2024 19:47:15 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20=EC=B2=B4?= =?UTF-8?q?=EA=B0=90=EC=98=A8=EB=8F=84=20=EC=A1=B0=ED=9A=8C=20api=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/waither/weatherservice/service/WeatherService.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java index 69ccc0ae..e5438b1a 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java +++ b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java @@ -221,15 +221,11 @@ public double calculateWindChill(double temp, double wind) { public double getWindChill(double latitude, double longitude, LocalDateTime baseTime) { List region = regionRepository.findRegionByLatAndLong(latitude, longitude); - if (region.isEmpty()) throw new WeatherExceptionHandler(WeatherErrorCode.REGION_NOT_FOUND); String regionName = region.get(0).getRegionName(); - - LocalDateTime dailyWeatherBaseTime = convertLocalDateTimeToDailyWeatherTime(baseTime.minusHours(1)); - String dailyWeatherKey = regionName + "_" + convertLocalDateTimeToString(dailyWeatherBaseTime); DailyWeather dailyWeather = dailyWeatherRepository.findById(dailyWeatherKey)