|
| 1 | +package com.semosan.api.common.weather; |
| 2 | + |
| 3 | +import com.semosan.api.common.util.GridConverter; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.stereotype.Service; |
| 8 | +import org.springframework.web.reactive.function.client.WebClient; |
| 9 | + |
| 10 | +import java.time.Duration; |
| 11 | +import java.time.LocalDateTime; |
| 12 | +import java.time.format.DateTimeFormatter; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +@Slf4j |
| 18 | +@Service |
| 19 | +public class WeatherService { |
| 20 | + |
| 21 | + private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| 22 | + private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("HHmm"); |
| 23 | + |
| 24 | + private final WebClient webClient; |
| 25 | + private final String apiKey; |
| 26 | + |
| 27 | + public WeatherService( |
| 28 | + @Qualifier("weatherApiWebClient") WebClient webClient, |
| 29 | + @Value("${weather.api-key}") String apiKey |
| 30 | + ) { |
| 31 | + this.webClient = webClient; |
| 32 | + this.apiKey = apiKey; |
| 33 | + } |
| 34 | + |
| 35 | + public Optional<Double> getTemperature(double latitude, double longitude) { |
| 36 | + try { |
| 37 | + GridConverter.Grid grid = GridConverter.toGrid(latitude, longitude); |
| 38 | + LocalDateTime now = adjustBaseTime(LocalDateTime.now()); |
| 39 | + |
| 40 | + String baseDate = now.format(DATE_FMT); |
| 41 | + String baseTime = now.format(TIME_FMT); |
| 42 | + |
| 43 | + Map<String, Object> response = webClient.get() |
| 44 | + .uri(uriBuilder -> uriBuilder |
| 45 | + .path("/getUltraSrtNcst") |
| 46 | + .queryParam("serviceKey", apiKey) |
| 47 | + .queryParam("pageNo", 1) |
| 48 | + .queryParam("numOfRows", 10) |
| 49 | + .queryParam("dataType", "JSON") |
| 50 | + .queryParam("base_date", baseDate) |
| 51 | + .queryParam("base_time", baseTime) |
| 52 | + .queryParam("nx", grid.nx()) |
| 53 | + .queryParam("ny", grid.ny()) |
| 54 | + .build()) |
| 55 | + .retrieve() |
| 56 | + .bodyToMono(Map.class) |
| 57 | + .block(Duration.ofSeconds(3)); |
| 58 | + |
| 59 | + return extractTemperature(response); |
| 60 | + } catch (Exception e) { |
| 61 | + log.warn("기상청 API 호출 실패: {}", e.getMessage()); |
| 62 | + return Optional.empty(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @SuppressWarnings("unchecked") |
| 67 | + private Optional<Double> extractTemperature(Map<String, Object> response) { |
| 68 | + if (response == null) return Optional.empty(); |
| 69 | + |
| 70 | + Map<String, Object> root = (Map<String, Object>) response.get("response"); |
| 71 | + if (root == null) return Optional.empty(); |
| 72 | + |
| 73 | + Map<String, Object> body = (Map<String, Object>) root.get("body"); |
| 74 | + if (body == null) return Optional.empty(); |
| 75 | + |
| 76 | + Map<String, Object> items = (Map<String, Object>) body.get("items"); |
| 77 | + if (items == null) return Optional.empty(); |
| 78 | + |
| 79 | + List<Map<String, Object>> itemList = (List<Map<String, Object>>) items.get("item"); |
| 80 | + if (itemList == null) return Optional.empty(); |
| 81 | + |
| 82 | + return itemList.stream() |
| 83 | + .filter(item -> "T1H".equals(item.get("category"))) |
| 84 | + .findFirst() |
| 85 | + .map(item -> Double.parseDouble(String.valueOf(item.get("obsrValue")))); |
| 86 | + } |
| 87 | + |
| 88 | + private LocalDateTime adjustBaseTime(LocalDateTime now) { |
| 89 | + if (now.getMinute() < 40) { |
| 90 | + now = now.minusHours(1); |
| 91 | + } |
| 92 | + return now.withMinute(0).withSecond(0).withNano(0); |
| 93 | + } |
| 94 | +} |
0 commit comments