From 8138ecc1d27cdde6b39b8d0b02d455a4cf0a984c Mon Sep 17 00:00:00 2001 From: litoral05 Date: Fri, 29 May 2026 11:40:37 +0100 Subject: [PATCH] Gets rid of currentWeather --- .../backend/weather/WeatherController.java | 9 ++- .../backend/weather/WeatherService.java | 78 +++++++++++++++---- .../weather/dto/WeatherCurrentDto.java | 14 ---- .../backend/weather/dto/WeatherDailyDto.java | 9 +++ .../weather/dto/WeatherForecastResponse.java | 1 - 5 files changed, 78 insertions(+), 33 deletions(-) delete mode 100644 src/main/java/com/litoralregas/backend/weather/dto/WeatherCurrentDto.java diff --git a/src/main/java/com/litoralregas/backend/weather/WeatherController.java b/src/main/java/com/litoralregas/backend/weather/WeatherController.java index ac41671..5fd9dbf 100644 --- a/src/main/java/com/litoralregas/backend/weather/WeatherController.java +++ b/src/main/java/com/litoralregas/backend/weather/WeatherController.java @@ -7,6 +7,7 @@ import com.litoralregas.backend.weather.dto.WeatherLocationUpdateRequest; import org.springframework.web.bind.annotation.*; @RestController +@RequestMapping("/api/weather") public class WeatherController { private final WeatherService weatherService; @@ -15,19 +16,19 @@ public class WeatherController { this.weatherService = weatherService; } - @GetMapping("/api/weather/forecast") + @GetMapping("/forecast") public WeatherForecastResponse getForecast( @RequestParam(defaultValue = "7") int days ) { return weatherService.getConfiguredForecast(days); } - @GetMapping("/api/weather/location") + @GetMapping("/location") public WeatherConfiguredLocationResponse getLocation() { return weatherService.getConfiguredLocation(); } - @PutMapping("/api/weather/location") + @PutMapping("/location") public WeatherConfiguredLocationResponse updateLocation( @RequestBody WeatherLocationUpdateRequest request ) { @@ -38,7 +39,7 @@ public class WeatherController { ); } - @GetMapping("/api/weather/search") + @GetMapping("/search") public JsonNode search(@RequestParam String query) { return weatherService.search(query); } diff --git a/src/main/java/com/litoralregas/backend/weather/WeatherService.java b/src/main/java/com/litoralregas/backend/weather/WeatherService.java index 0d6f640..ff0319f 100644 --- a/src/main/java/com/litoralregas/backend/weather/WeatherService.java +++ b/src/main/java/com/litoralregas/backend/weather/WeatherService.java @@ -112,7 +112,6 @@ public class WeatherService { private WeatherForecastResponse toForecastResponse(JsonNode payload) { JsonNode location = payload.path("location"); - JsonNode current = payload.path("current"); JsonNode forecastDays = payload.path("forecast").path("forecastday"); List daily = new ArrayList<>(); @@ -120,6 +119,9 @@ public class WeatherService { for (JsonNode dayNode : forecastDays) { JsonNode day = dayNode.path("day"); JsonNode astro = dayNode.path("astro"); + JsonNode hours = dayNode.path("hour"); + + WindSummary windSummary = summarizeWind(hours); daily.add(new WeatherDailyDto( textOrNull(dayNode, "date"), @@ -129,6 +131,11 @@ public class WeatherService { doubleOrNull(day, "totalprecip_mm"), intOrNull(day, "daily_chance_of_rain"), doubleOrNull(day, "maxwind_kph"), + windSummary.averageWindKph(), + windSummary.averageWindDegree(), + windSummary.averageWindDirection(), + doubleOrNull(day, "avghumidity"), + doubleOrNull(day, "avgvis_km"), doubleOrNull(day, "uv"), textOrNull(astro, "sunrise"), textOrNull(astro, "sunset"), @@ -145,22 +152,9 @@ public class WeatherService { doubleOrNull(location, "lon"), textOrNull(location, "localtime") ), - new WeatherCurrentDto( - doubleOrNull(current, "temp_c"), - doubleOrNull(current, "feelslike_c"), - intOrNull(current, "humidity"), - doubleOrNull(current, "precip_mm"), - doubleOrNull(current, "wind_kph"), - doubleOrNull(current, "wind_degree"), - textOrNull(current, "wind_dir"), - doubleOrNull(current, "pressure_mb"), - doubleOrNull(current, "uv"), - toCondition(current.path("condition")) - ), daily ); } - private WeatherConditionDto toCondition(JsonNode condition) { if (condition == null || condition.isMissingNode() || condition.isNull()) { return null; @@ -253,4 +247,60 @@ public class WeatherService { .orElseThrow(() -> new IllegalStateException("Weather settings not configured.")); } + + private record WindSummary( + Double averageWindKph, + Double averageWindDegree, + String averageWindDirection + ) {} + + private WindSummary summarizeWind(JsonNode hours) { + double windSum = 0; + double sinSum = 0; + double cosSum = 0; + int windCount = 0; + int degreeCount = 0; + + for (JsonNode hour : hours) { + Double windKph = doubleOrNull(hour, "wind_kph"); + Double windDegree = doubleOrNull(hour, "wind_degree"); + + if (windKph != null) { + windSum += windKph; + windCount++; + } + + if (windDegree != null) { + double radians = Math.toRadians(windDegree); + sinSum += Math.sin(radians); + cosSum += Math.cos(radians); + degreeCount++; + } + } + + Double averageWindKph = windCount > 0 ? windSum / windCount : null; + + if (degreeCount == 0) { + return new WindSummary(averageWindKph, null, null); + } + + double averageRadians = Math.atan2( + sinSum / degreeCount, + cosSum / degreeCount + ); + + double averageDegree = (Math.toDegrees(averageRadians) + 360) % 360; + + return new WindSummary( + averageWindKph, + averageDegree, + directionName(averageDegree) + ); + } + + private String directionName(double degree) { + String[] labels = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"}; + int index = (int) Math.round(degree / 45.0) % 8; + return labels[index]; + } } \ No newline at end of file diff --git a/src/main/java/com/litoralregas/backend/weather/dto/WeatherCurrentDto.java b/src/main/java/com/litoralregas/backend/weather/dto/WeatherCurrentDto.java deleted file mode 100644 index 1a22ab1..0000000 --- a/src/main/java/com/litoralregas/backend/weather/dto/WeatherCurrentDto.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.litoralregas.backend.weather.dto; - -public record WeatherCurrentDto( - Double temperatureC, - Double feelsLikeC, - Integer humidity, - Double precipitationMm, - Double windKph, - Double windDegree, - String windDirection, - Double pressureMb, - Double uv, - WeatherConditionDto condition -) {} \ No newline at end of file diff --git a/src/main/java/com/litoralregas/backend/weather/dto/WeatherDailyDto.java b/src/main/java/com/litoralregas/backend/weather/dto/WeatherDailyDto.java index 4276e98..9e01e2d 100644 --- a/src/main/java/com/litoralregas/backend/weather/dto/WeatherDailyDto.java +++ b/src/main/java/com/litoralregas/backend/weather/dto/WeatherDailyDto.java @@ -5,9 +5,18 @@ public record WeatherDailyDto( Double maxTemperatureC, Double minTemperatureC, Double averageTemperatureC, + Double totalPrecipitationMm, Integer dailyRainChance, + Double maxWindKph, + Double averageWindKph, + Double averageWindDegree, + String averageWindDirection, + + Double averageHumidity, + Double averageVisibilityKm, + Double uv, String sunrise, String sunset, diff --git a/src/main/java/com/litoralregas/backend/weather/dto/WeatherForecastResponse.java b/src/main/java/com/litoralregas/backend/weather/dto/WeatherForecastResponse.java index 7839156..218d231 100644 --- a/src/main/java/com/litoralregas/backend/weather/dto/WeatherForecastResponse.java +++ b/src/main/java/com/litoralregas/backend/weather/dto/WeatherForecastResponse.java @@ -4,6 +4,5 @@ import java.util.List; public record WeatherForecastResponse( WeatherLocationDto location, - WeatherCurrentDto current, List daily ) {} \ No newline at end of file