Gets rid of currentWeather
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<WeatherDailyDto> 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];
|
||||
}
|
||||
}
|
||||
@@ -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 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,
|
||||
|
||||
@@ -4,6 +4,5 @@ import java.util.List;
|
||||
|
||||
public record WeatherForecastResponse(
|
||||
WeatherLocationDto location,
|
||||
WeatherCurrentDto current,
|
||||
List<WeatherDailyDto> daily
|
||||
) {}
|
||||
Reference in New Issue
Block a user