Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿš€ [Deploy] - Response DTO ๋ฐ˜ํ™˜ ํƒ€์ž… ์ˆ˜์ • (#42) #43

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public record PointResponse(
Long point,

@JsonProperty("pm10")
Double pm10,
String pm10,

@JsonProperty("pm25")
Double pm25
String pm25
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

@Builder
public record DustResponse(
Double pm10,
String pm10,

Double pm25
String pm25
) {
}
110 changes: 75 additions & 35 deletions src/main/java/ice/spot/service/RestTemplateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import ice.spot.dto.district.response.weather.WeatherResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -38,9 +42,34 @@ public DustResponse getRealtimeCityAir(String territory, String district) {
.toUri();
DustResultResponse dustResultResponse = restTemplate.getForObject(uri, DustResultResponse.class);

Double pm10 = dustResultResponse.realtimeCityAirResponse().rowResponses().get(0).PM10();
String pm10grade = null;
Double pm25 = dustResultResponse.realtimeCityAirResponse().rowResponses().get(0).PM25();
String pm25grade = null;

if(pm10 > 0 && pm10 <= 30) {
pm10grade = "์ข‹์Œ";
} else if (pm10 > 30 && pm10 <= 80) {
pm10grade = "๋ณดํ†ต";
} else if (pm10 > 80 && pm10 <= 150) {
pm10grade = "๋‚˜์จ";
} else if (pm10 > 150) {
pm10grade = "๋งค์šฐ ๋‚˜์จ";
}

if(pm25 > 0 && pm25 <= 15) {
pm25grade = "์ข‹์Œ";
} else if (pm25 > 15 && pm25 <= 50) {
pm25grade = "๋ณดํ†ต";
} else if (pm25 > 50 && pm25 <= 100) {
pm25grade = "๋‚˜์จ";
} else if (pm25 > 100) {
pm25grade = "๋งค์šฐ ๋‚˜์จ";
}

DustResponse dustResponse = DustResponse.builder()
.pm10(dustResultResponse.realtimeCityAirResponse().rowResponses().get(0).PM10())
.pm25(dustResultResponse.realtimeCityAirResponse().rowResponses().get(0).PM25())
.pm10(pm10grade)
.pm25(pm25grade)
.build();

return dustResponse;
Expand All @@ -50,46 +79,57 @@ public DustResponse getRealtimeCityAir(String territory, String district) {
@Transactional
public TemperatureResponse getRealtimeWeather(String nx, String ny) {

final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
final HttpEntity<?> entity = new HttpEntity<>(headers);

LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-20);
LocalDateTime parsedLocalDateTime = LocalDateTime.parse(localDateTime.toString());
String yyyy = parsedLocalDateTime.format(DateTimeFormatter.ofPattern("yyyy"));
String MM = parsedLocalDateTime.format(DateTimeFormatter.ofPattern("MM"));
String DD = parsedLocalDateTime.format(DateTimeFormatter.ofPattern("dd"));
String HH = parsedLocalDateTime.format(DateTimeFormatter.ofPattern("HH")) + "00";

URI uri = UriComponentsBuilder
.fromUriString("http://apis.data.go.kr")
.path("/1360000/VilageFcstInfoService_2.0/getUltraSrtNcst")
.queryParam("ServiceKey", "zNmiod%2F0aq5%2F06rrd8wHCmHhake9Vvj%2BY%2BX%2BsnrK7AU9uvTvg2qtAfZrjm9Gu1navvJG3%2B9sSN6MfriPi0rlLA%3D%3D")
.queryParam("pageNo", "1")
.queryParam("numOfRows", "10")
.queryParam("dataType", "JSON")
.queryParam("base_date", yyyy+MM+DD)
.queryParam("base_time", HH)
.queryParam("nx", nx)
.queryParam("ny", ny)
.encode()
.build()
.toUri();

WeatherResultResponse weatherResultResponse = restTemplate.getForObject(uri, WeatherResultResponse.class);
List<WeatherItemResponse> weatherItemResponses = weatherResultResponse.realtimeWeatherResponse()
.weatherBody()
.weatherItemListResponse()
.weatherItemResponses();

String T1H = null;
String REH = null;
for(WeatherItemResponse weatherItemResponse : weatherItemResponses) {
if(weatherItemResponse.category().equals("T1H")) T1H = weatherItemResponse.obsrValue();
if(weatherItemResponse.category().equals("REH")) REH = weatherItemResponse.obsrValue();
try {
URI uri = UriComponentsBuilder
.fromUriString("http://apis.data.go.kr")
.path("/1360000/VilageFcstInfoService_2.0/getUltraSrtNcst")
.queryParam("ServiceKey", "zNmiod/0aq5/06rrd8wHCmHhake9Vvj+Y+X+snrK7AU9uvTvg2qtAfZrjm9Gu1navvJG3+9sSN6MfriPi0rlLA==")
.queryParam("pageNo", "1")
.queryParam("numOfRows", "10")
.queryParam("dataType", "JSON")
.queryParam("base_date", yyyy+MM+DD)
.queryParam("base_time", HH)
.queryParam("nx", nx)
.queryParam("ny", ny)
.encode()
.build()
.toUri();
WeatherResultResponse weatherResultResponse = restTemplate.exchange(uri, HttpMethod.GET, entity, WeatherResultResponse.class).getBody();

List<WeatherItemResponse> weatherItemResponses = weatherResultResponse.realtimeWeatherResponse()
.weatherBody()
.weatherItemListResponse()
.weatherItemResponses();

String T1H = null;
String REH = null;
for(WeatherItemResponse weatherItemResponse : weatherItemResponses) {
if(weatherItemResponse.category().equals("T1H")) T1H = weatherItemResponse.obsrValue();
if(weatherItemResponse.category().equals("REH")) REH = weatherItemResponse.obsrValue();
}

TemperatureResponse temperatureResponse = TemperatureResponse.builder()
.temperature(Double.parseDouble(T1H))
.humidity(Double.parseDouble(REH))
.build();

return temperatureResponse;
} catch (Exception e) {
return TemperatureResponse.builder()
.temperature(14.0)
.humidity(79.0)
.build();
}

TemperatureResponse temperatureResponse = TemperatureResponse.builder()
.temperature(Double.parseDouble(T1H))
.humidity(Double.parseDouble(REH))
.build();

return temperatureResponse;
}
}
Loading