Improve the dahboard with the weather info
This commit is contained in:
27
services/external/app/external/aemet.py
vendored
27
services/external/app/external/aemet.py
vendored
@@ -522,20 +522,39 @@ class AEMETClient(BaseAPIClient):
|
||||
async def get_current_weather(self, latitude: float, longitude: float) -> Optional[Dict[str, Any]]:
|
||||
"""Get current weather for coordinates"""
|
||||
try:
|
||||
logger.info("🌤️ Getting current weather from AEMET",
|
||||
lat=latitude, lon=longitude, api_key_configured=bool(self.api_key))
|
||||
|
||||
# Check if API key is configured
|
||||
if not self.api_key:
|
||||
logger.error("❌ AEMET API key not configured - falling back to synthetic data")
|
||||
return await self._get_synthetic_current_weather()
|
||||
|
||||
station_id = self.location_service.find_nearest_station(latitude, longitude)
|
||||
if not station_id:
|
||||
logger.warning("No weather station found", lat=latitude, lon=longitude)
|
||||
logger.warning("❌ No weather station found for coordinates",
|
||||
lat=latitude, lon=longitude,
|
||||
madrid_bounds=f"{AEMETConstants.MADRID_BOUNDS.min_lat}-{AEMETConstants.MADRID_BOUNDS.max_lat}")
|
||||
return await self._get_synthetic_current_weather()
|
||||
|
||||
logger.info("✅ Found nearest weather station", station_id=station_id)
|
||||
|
||||
weather_data = await self._fetch_current_weather_data(station_id)
|
||||
if weather_data:
|
||||
return self.parser.parse_current_weather(weather_data)
|
||||
logger.info("✅ Successfully fetched AEMET weather data", station_id=station_id)
|
||||
parsed_data = self.parser.parse_current_weather(weather_data)
|
||||
# Ensure the source is set to AEMET for successful API calls
|
||||
if parsed_data and isinstance(parsed_data, dict):
|
||||
parsed_data["source"] = WeatherSource.AEMET.value
|
||||
return parsed_data
|
||||
|
||||
logger.info("Falling back to synthetic weather data", reason="invalid_weather_data")
|
||||
logger.warning("❌ AEMET API returned no data - falling back to synthetic",
|
||||
station_id=station_id, reason="invalid_weather_data")
|
||||
return await self._get_synthetic_current_weather()
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get current weather", error=str(e))
|
||||
logger.error("❌ AEMET API failed - falling back to synthetic",
|
||||
error=str(e), error_type=type(e).__name__)
|
||||
return await self._get_synthetic_current_weather()
|
||||
|
||||
async def get_forecast(self, latitude: float, longitude: float, days: int = 7) -> List[Dict[str, Any]]:
|
||||
|
||||
Reference in New Issue
Block a user