Gets rid of currentWeather

This commit is contained in:
litoral05
2026-05-29 11:40:37 +01:00
parent 460e57bc3a
commit 8138ecc1d2
5 changed files with 78 additions and 33 deletions
@@ -7,6 +7,7 @@ import com.litoralregas.backend.weather.dto.WeatherLocationUpdateRequest;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@RequestMapping("/api/weather")
public class WeatherController { public class WeatherController {
private final WeatherService weatherService; private final WeatherService weatherService;
@@ -15,19 +16,19 @@ public class WeatherController {
this.weatherService = weatherService; this.weatherService = weatherService;
} }
@GetMapping("/api/weather/forecast") @GetMapping("/forecast")
public WeatherForecastResponse getForecast( public WeatherForecastResponse getForecast(
@RequestParam(defaultValue = "7") int days @RequestParam(defaultValue = "7") int days
) { ) {
return weatherService.getConfiguredForecast(days); return weatherService.getConfiguredForecast(days);
} }
@GetMapping("/api/weather/location") @GetMapping("/location")
public WeatherConfiguredLocationResponse getLocation() { public WeatherConfiguredLocationResponse getLocation() {
return weatherService.getConfiguredLocation(); return weatherService.getConfiguredLocation();
} }
@PutMapping("/api/weather/location") @PutMapping("/location")
public WeatherConfiguredLocationResponse updateLocation( public WeatherConfiguredLocationResponse updateLocation(
@RequestBody WeatherLocationUpdateRequest request @RequestBody WeatherLocationUpdateRequest request
) { ) {
@@ -38,7 +39,7 @@ public class WeatherController {
); );
} }
@GetMapping("/api/weather/search") @GetMapping("/search")
public JsonNode search(@RequestParam String query) { public JsonNode search(@RequestParam String query) {
return weatherService.search(query); return weatherService.search(query);
} }
@@ -112,7 +112,6 @@ public class WeatherService {
private WeatherForecastResponse toForecastResponse(JsonNode payload) { private WeatherForecastResponse toForecastResponse(JsonNode payload) {
JsonNode location = payload.path("location"); JsonNode location = payload.path("location");
JsonNode current = payload.path("current");
JsonNode forecastDays = payload.path("forecast").path("forecastday"); JsonNode forecastDays = payload.path("forecast").path("forecastday");
List<WeatherDailyDto> daily = new ArrayList<>(); List<WeatherDailyDto> daily = new ArrayList<>();
@@ -120,6 +119,9 @@ public class WeatherService {
for (JsonNode dayNode : forecastDays) { for (JsonNode dayNode : forecastDays) {
JsonNode day = dayNode.path("day"); JsonNode day = dayNode.path("day");
JsonNode astro = dayNode.path("astro"); JsonNode astro = dayNode.path("astro");
JsonNode hours = dayNode.path("hour");
WindSummary windSummary = summarizeWind(hours);
daily.add(new WeatherDailyDto( daily.add(new WeatherDailyDto(
textOrNull(dayNode, "date"), textOrNull(dayNode, "date"),
@@ -129,6 +131,11 @@ public class WeatherService {
doubleOrNull(day, "totalprecip_mm"), doubleOrNull(day, "totalprecip_mm"),
intOrNull(day, "daily_chance_of_rain"), intOrNull(day, "daily_chance_of_rain"),
doubleOrNull(day, "maxwind_kph"), doubleOrNull(day, "maxwind_kph"),
windSummary.averageWindKph(),
windSummary.averageWindDegree(),
windSummary.averageWindDirection(),
doubleOrNull(day, "avghumidity"),
doubleOrNull(day, "avgvis_km"),
doubleOrNull(day, "uv"), doubleOrNull(day, "uv"),
textOrNull(astro, "sunrise"), textOrNull(astro, "sunrise"),
textOrNull(astro, "sunset"), textOrNull(astro, "sunset"),
@@ -145,22 +152,9 @@ public class WeatherService {
doubleOrNull(location, "lon"), doubleOrNull(location, "lon"),
textOrNull(location, "localtime") 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 daily
); );
} }
private WeatherConditionDto toCondition(JsonNode condition) { private WeatherConditionDto toCondition(JsonNode condition) {
if (condition == null || condition.isMissingNode() || condition.isNull()) { if (condition == null || condition.isMissingNode() || condition.isNull()) {
return null; return null;
@@ -253,4 +247,60 @@ public class WeatherService {
.orElseThrow(() -> .orElseThrow(() ->
new IllegalStateException("Weather settings not configured.")); 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];
}
} }
@@ -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
) {}
@@ -5,9 +5,18 @@ public record WeatherDailyDto(
Double maxTemperatureC, Double maxTemperatureC,
Double minTemperatureC, Double minTemperatureC,
Double averageTemperatureC, Double averageTemperatureC,
Double totalPrecipitationMm, Double totalPrecipitationMm,
Integer dailyRainChance, Integer dailyRainChance,
Double maxWindKph, Double maxWindKph,
Double averageWindKph,
Double averageWindDegree,
String averageWindDirection,
Double averageHumidity,
Double averageVisibilityKm,
Double uv, Double uv,
String sunrise, String sunrise,
String sunset, String sunset,
@@ -4,6 +4,5 @@ import java.util.List;
public record WeatherForecastResponse( public record WeatherForecastResponse(
WeatherLocationDto location, WeatherLocationDto location,
WeatherCurrentDto current,
List<WeatherDailyDto> daily List<WeatherDailyDto> daily
) {} ) {}