WeatherInfoService.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package nxt.weather.service;
  2. import nxt.weather.controller.dto.*;
  3. import nxt.weather.service.api.WeatherApiService;
  4. import java.util.ArrayList;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import java.util.Map.Entry;
  8. import java.util.stream.DoubleStream;
  9. import nxt.weather.service.api.dto.ForecastDayDto;
  10. import nxt.weather.exception.WeatherCityNotFoundException;
  11. import nxt.weather.service.api.dto.HourlyDataDto;
  12. import nxt.weather.service.api.dto.WeatherDto;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. /**
  16. * Service s'occupant de récupèrer les infos d'une api météo et analyse pour
  17. * répondre au demande d'un controller
  18. * @author Arthur Brandao
  19. */
  20. @Service
  21. public class WeatherInfoService {
  22. @Autowired
  23. private WeatherApiService api;
  24. public ReturnDto<TodayDto> weather(String city) {
  25. WeatherDto weather;
  26. try {
  27. weather = api.getInformations(city);
  28. } catch(WeatherCityNotFoundException ex) {
  29. return new ReturnDto<>(404, ex.getMessage());
  30. }
  31. List<ForecastDto> days = new ArrayList<>();
  32. weather.getForecastDays().forEach(fd -> {
  33. //Création liste données horaire
  34. HourlyDto[] hourly = new HourlyDto[fd.getHourly().size()];
  35. fd.getHourly().forEach((key, value) -> {
  36. hourly[Integer.parseInt(key.split("H")[0])] = new HourlyDto(
  37. value.getHumidity(),
  38. value.getPrecipitation(),
  39. value.getTemperature());
  40. });
  41. days.add(new ForecastDto(
  42. fd.getDate(),
  43. fd.getDay(),
  44. fd.getCondition(),
  45. fd.getTempMin(),
  46. fd.getTempMax(),
  47. fd.getIcon(),
  48. hourly));
  49. });
  50. TodayDto today = new TodayDto(
  51. weather.getCity().getName(),
  52. weather.getCity().getCountry(),
  53. weather.getCity().getSunrise(),
  54. weather.getCity().getSunset(),
  55. weather.getCurrentCondition().getTemp(),
  56. weather.getCurrentCondition().getWindSpeed(),
  57. weather.getCurrentCondition().getWindDir(),
  58. weather.getCurrentCondition().getPressure(),
  59. weather.getCurrentCondition().getHumidity(),
  60. weather.getCurrentCondition().getCondition(),
  61. weather.getCurrentCondition().getIcon(),
  62. days
  63. );
  64. return new ReturnDto<>(today);
  65. }
  66. public ReturnDto<ForecastDto> heat(String city) {
  67. WeatherDto weather;
  68. try {
  69. weather = api.getInformations(city);
  70. } catch(WeatherCityNotFoundException ex) {
  71. return new ReturnDto<>(404, ex.getMessage());
  72. }
  73. ForecastDayDto fd = weather.getForecastDays().stream().max(getComparatorTempMax()).get();
  74. ForecastDto forecast = new ForecastDto(
  75. fd.getDate(),
  76. fd.getDay(),
  77. fd.getCondition(),
  78. fd.getTempMin(),
  79. fd.getTempMax(),
  80. fd.getIcon());
  81. return new ReturnDto<>(forecast);
  82. }
  83. private Comparator<ForecastDayDto> getComparatorTempMax() {
  84. return Comparator.comparing(ForecastDayDto::getTempMax);
  85. }
  86. public ReturnDto<List<ForecastDto>> rain(String city) {
  87. WeatherDto weather;
  88. try {
  89. weather = api.getInformations(city);
  90. } catch(WeatherCityNotFoundException ex) {
  91. return new ReturnDto<>(404, ex.getMessage());
  92. }
  93. List<ForecastDto> days = new ArrayList<>();
  94. weather.getForecastDays().stream().filter((fd) -> (fd.getHourly().entrySet().stream().anyMatch(h -> h.getValue().getPrecipitation() > 0))).forEachOrdered((fd) -> {
  95. days.add(new ForecastDto(
  96. fd.getDate(),
  97. fd.getDay(),
  98. fd.getCondition(),
  99. fd.getTempMin(),
  100. fd.getTempMax(),
  101. fd.getIcon()));
  102. });
  103. return new ReturnDto<>(days);
  104. }
  105. public ReturnDto<HumidityDto> humidity(String city) {
  106. WeatherDto weather;
  107. try {
  108. weather = api.getInformations(city);
  109. } catch(WeatherCityNotFoundException ex) {
  110. return new ReturnDto<>(404, ex.getMessage());
  111. }
  112. int actual = weather.getCurrentCondition().getHumidity();
  113. double avg = getStreamMapToAverageHumidity(weather).average().getAsDouble();
  114. boolean dry = weather.getForecastDays(0).getHourly().entrySet().stream().mapToDouble(this::getHumidityFromMap).average().getAsDouble() <= getStreamMapToAverageHumidity(weather).min().getAsDouble();
  115. return new ReturnDto<>(new HumidityDto(actual, avg, dry));
  116. }
  117. private DoubleStream getStreamMapToAverageHumidity(WeatherDto weather) {
  118. return weather.getForecastDays().stream().mapToDouble(this::getDailyAverageHumidity);
  119. }
  120. private double getDailyAverageHumidity(ForecastDayDto fd) {
  121. return fd.getHourly().entrySet().stream().mapToDouble(this::getHumidityFromMap).average().getAsDouble();
  122. }
  123. private int getHumidityFromMap(Entry<String, HourlyDataDto> mapHourly) {
  124. return mapHourly.getValue().getHumidity();
  125. }
  126. }