implement client for meteo weather api
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package SuMoC.backend.dev;
|
||||
|
||||
import SuMoC.backend.meteo.MeteoClient;
|
||||
import SuMoC.backend.scheduling.ScheduledTasks;
|
||||
import SuMoC.backend.trend.services.StatTableUpdateService;
|
||||
import com.google.firebase.messaging.FirebaseMessaging;
|
||||
@@ -25,6 +26,8 @@ public class DevController {
|
||||
|
||||
private final FirebaseMessaging firebaseMessaging;
|
||||
|
||||
private final MeteoClient meteoClient;
|
||||
|
||||
@PostMapping("/trigger-trend-calculation")
|
||||
public void triggerTrendCalculation() {
|
||||
LocalDate dayOfLastWeek = LocalDate.now().minusDays(7);
|
||||
@@ -51,4 +54,26 @@ public class DevController {
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/test-meteo")
|
||||
public ResponseEntity<String> testMeteoClient() {
|
||||
try {
|
||||
// Test with Zurich coordinates
|
||||
double lat = 47.3769;
|
||||
double lon = 8.5417;
|
||||
|
||||
var forecast = meteoClient.getForecastWeatherCodeToday(lat, lon).block();
|
||||
|
||||
if (forecast != null) {
|
||||
return ResponseEntity.ok("✓ Meteo API working! Received forecast for " +
|
||||
forecast.latitude() + ", " + forecast.longitude() +
|
||||
" with " + (forecast.hourly() != null ? forecast.hourly().weatherCode().size() : 0) +
|
||||
" hourly weather codes");
|
||||
} else {
|
||||
return ResponseEntity.status(500).body("✗ No response from Meteo API");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(500).body("✗ Failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package SuMoC.backend.meteo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public record ForecastResponse(
|
||||
double latitude,
|
||||
double longitude,
|
||||
@JsonProperty("generationtime_ms") double generationtimeMs,
|
||||
@JsonProperty("utc_offset_seconds") int utcOffsetSeconds,
|
||||
String timezone,
|
||||
@JsonProperty("timezone_abbreviation") String timezoneAbbreviation,
|
||||
double elevation,
|
||||
@JsonProperty("hourly_units") HourlyUnits hourlyUnits,
|
||||
Hourly hourly
|
||||
) {
|
||||
/**
|
||||
* Gets the weather code for a specific point in time.
|
||||
*
|
||||
* @param dateTime the target date and time
|
||||
* @return the weather code if found, empty otherwise
|
||||
*/
|
||||
public Optional<Integer> getWeatherCodeAt(LocalDateTime dateTime) {
|
||||
if (hourly == null || hourly.time == null || hourly.weatherCode == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
for (int i = 0; i < hourly.time.size(); i++) {
|
||||
if (hourly.time.get(i).equals(dateTime)) {
|
||||
return Optional.of(hourly.weatherCode.get(i));
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public record HourlyUnits(
|
||||
String time,
|
||||
@JsonProperty("weather_code") String weatherCode
|
||||
) {
|
||||
}
|
||||
|
||||
public record Hourly(
|
||||
List<LocalDateTime> time,
|
||||
@JsonProperty("weather_code") List<Integer> weatherCode
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package SuMoC.backend.meteo;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Service
|
||||
public class MeteoClient {
|
||||
|
||||
private final WebClient meteoClient;
|
||||
|
||||
public MeteoClient(@Qualifier("meteoWebClient") WebClient meteoClient) {
|
||||
this.meteoClient = meteoClient;
|
||||
}
|
||||
|
||||
public Mono<ForecastResponse> getForecastWeatherCodeToday(
|
||||
double lat,
|
||||
double lon
|
||||
) {
|
||||
String today = LocalDate.now().toString(); // Uses ISO-8601 format: yyyy-MM-dd
|
||||
|
||||
return meteoClient.get()
|
||||
.uri(uriBuilder -> uriBuilder.path("/forecast")
|
||||
.queryParam("latitude", lat)
|
||||
.queryParam("longitude", lon)
|
||||
.queryParam("hourly", "weather_code")
|
||||
.queryParam("start_date", today)
|
||||
.queryParam("end_date", today)
|
||||
.build())
|
||||
.retrieve()
|
||||
.bodyToMono(ForecastResponse.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package SuMoC.backend.meteo;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(MeteoProperties.class)
|
||||
public class MeteoClientConfig {
|
||||
|
||||
@Bean
|
||||
public WebClient meteoWebClient(MeteoProperties properties) {
|
||||
return WebClient.builder()
|
||||
.baseUrl(properties.baseUrl())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package SuMoC.backend.meteo;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix = "meteo")
|
||||
public record MeteoProperties(
|
||||
String baseUrl
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package SuMoC.backend.meteo;
|
||||
|
||||
public class UnknownWeatherCodeException extends RuntimeException {
|
||||
public UnknownWeatherCodeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package SuMoC.backend.meteo;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Simplified WMO weather codes, taken from https://gist.github.com/stellasphere/9490c195ed2b53c707087c8c2db4ec0c
|
||||
*/
|
||||
public enum WeatherCondition {
|
||||
|
||||
CLEAR(Set.of(0, 1)),
|
||||
PARTLY_CLOUDY(Set.of(2)),
|
||||
CLOUDY(Set.of(3)),
|
||||
|
||||
FOG(Set.of(45, 48)),
|
||||
|
||||
DRIZZLE(Set.of(51, 53, 55, 56, 57)),
|
||||
RAIN(Set.of(61, 63, 65, 66, 67)),
|
||||
|
||||
SNOW(Set.of(71, 73, 75, 77, 85, 86)),
|
||||
|
||||
SHOWERS(Set.of(80, 81, 82)),
|
||||
|
||||
THUNDERSTORM(Set.of(95, 96, 99)),
|
||||
|
||||
UNKNOWN(Set.of());
|
||||
|
||||
private final Set<Integer> codes;
|
||||
|
||||
WeatherCondition(Set<Integer> codes) {
|
||||
this.codes = codes;
|
||||
}
|
||||
|
||||
public static WeatherCondition from(int code) throws Exception {
|
||||
for (WeatherCondition condition : values()) {
|
||||
if (condition.codes.contains(code)) {
|
||||
return condition;
|
||||
}
|
||||
}
|
||||
throw new UnknownWeatherCodeException("Encountered unknown weather code.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user