Skip to content

Commit

Permalink
Use autolocate city for met_no location
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed Oct 6, 2023
1 parent 205bfeb commit 1e93c85
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/blocks/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down Expand Up @@ -153,7 +153,7 @@ fn default_interval() -> Seconds {
trait WeatherProvider {
async fn get_weather(
&self,
autolocated_location: Option<Coordinates>,
autolocated_location: &Option<Coordinates>,
need_forecast: bool,
) -> Result<WeatherResult>;
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -381,10 +381,11 @@ enum UnitSystem {
Imperial,
}

#[derive(Deserialize, Clone, Copy)]
#[derive(Deserialize, Clone)]
struct Coordinates {
latitude: f64,
longitude: f64,
city: String,
}

struct AutolocateResult {
Expand All @@ -399,7 +400,7 @@ async fn find_ip_location(interval: Duration) -> Result<Coordinates> {
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());
}
}
}
Expand Down Expand Up @@ -448,7 +449,7 @@ async fn find_ip_location(interval: Duration) -> Result<Coordinates> {
{
let mut guard = LAST_AUTOLOCATE.lock().unwrap();
*guard = Some(AutolocateResult {
location,
location: location.clone(),
timestamp: Instant::now(),
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/blocks/weather/met_no.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn translate(legend: &LegendsStore, summary: &str, lang: &ApiLanguage) -> String
impl WeatherProvider for Service<'_> {
async fn get_weather(
&self,
location: Option<Coordinates>,
location: &Option<Coordinates>,
need_forecast: bool,
) -> Result<WeatherResult> {
let (lat, lon) = location
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/blocks/weather/open_weather_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ struct CityCoord {
impl WeatherProvider for Service<'_> {
async fn get_weather(
&self,
autolocated: Option<Coordinates>,
autolocated: &Option<Coordinates>,
need_forecast: bool,
) -> Result<WeatherResult> {
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")?;
Expand Down

0 comments on commit 1e93c85

Please sign in to comment.