From 1e93c85f522ca5e2789b885e6aef05a854e77ed5 Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Fri, 6 Oct 2023 12:50:39 -0500 Subject: [PATCH] Use autolocate city for met_no location --- src/blocks/weather.rs | 13 +++++++------ src/blocks/weather/met_no.rs | 6 ++++-- src/blocks/weather/open_weather_map.rs | 3 ++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/blocks/weather.rs b/src/blocks/weather.rs index cf898eee9c..7f6e6ac33e 100644 --- a/src/blocks/weather.rs +++ b/src/blocks/weather.rs @@ -54,7 +54,7 @@ //! `altitude` | Meters above sea level of the ground | No | Approximated by server //! `forecast_hours` | How many hours should be forecast | No | 12 //! -//! Met.no does not support location name. +//! Met.no does not support location name, but if autolocate is enabled then autolocate's city value is used. //! //! # Available Format Keys //! @@ -153,7 +153,7 @@ fn default_interval() -> Seconds { trait WeatherProvider { async fn get_weather( &self, - autolocated_location: Option, + autolocated_location: &Option, need_forecast: bool, ) -> Result; } @@ -324,7 +324,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> { None }; - let fetch = || provider.get_weather(location, need_forecast); + let fetch = || provider.get_weather(&location, need_forecast); let data = fetch.retry(&ExponentialBuilder::default()).await?; let data_values = data.into_values(); @@ -381,10 +381,11 @@ enum UnitSystem { Imperial, } -#[derive(Deserialize, Clone, Copy)] +#[derive(Deserialize, Clone)] struct Coordinates { latitude: f64, longitude: f64, + city: String, } struct AutolocateResult { @@ -399,7 +400,7 @@ async fn find_ip_location(interval: Duration) -> Result { let guard = LAST_AUTOLOCATE.lock().unwrap(); if let Some(cached) = &*guard { if cached.timestamp.elapsed() < interval { - return Ok(cached.location); + return Ok(cached.location.clone()); } } } @@ -448,7 +449,7 @@ async fn find_ip_location(interval: Duration) -> Result { { let mut guard = LAST_AUTOLOCATE.lock().unwrap(); *guard = Some(AutolocateResult { - location, + location: location.clone(), timestamp: Instant::now(), }); } diff --git a/src/blocks/weather/met_no.rs b/src/blocks/weather/met_no.rs index adf683516b..3a82726d16 100644 --- a/src/blocks/weather/met_no.rs +++ b/src/blocks/weather/met_no.rs @@ -150,7 +150,7 @@ fn translate(legend: &LegendsStore, summary: &str, lang: &ApiLanguage) -> String impl WeatherProvider for Service<'_> { async fn get_weather( &self, - location: Option, + location: &Option, need_forecast: bool, ) -> Result { let (lat, lon) = location @@ -284,7 +284,9 @@ impl WeatherProvider for Service<'_> { }; Ok(WeatherResult { - location: "Unknown".to_string(), + location: location + .as_ref() + .map_or("Unknown".to_string(), |c| c.city.clone()), current_weather: self .get_weather_instant(&data.properties.timeseries.first().unwrap().data), forecast, diff --git a/src/blocks/weather/open_weather_map.rs b/src/blocks/weather/open_weather_map.rs index ec9a73acd7..4589a50e44 100644 --- a/src/blocks/weather/open_weather_map.rs +++ b/src/blocks/weather/open_weather_map.rs @@ -202,10 +202,11 @@ struct CityCoord { impl WeatherProvider for Service<'_> { async fn get_weather( &self, - autolocated: Option, + autolocated: &Option, need_forecast: bool, ) -> Result { let location_query = autolocated + .as_ref() .map(|al| format!("lat={}&lon={}", al.latitude, al.longitude)) .or_else(|| self.location_query.clone()) .error("no location was provided")?;