From 9d3a261d2dc67c72abb80044b86130bc543b0d83 Mon Sep 17 00:00:00 2001 From: Sondre Wold Date: Tue, 9 Apr 2024 08:13:21 +0200 Subject: [PATCH] Add precipation data --- weather_cli/src/main.rs | 49 ++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/weather_cli/src/main.rs b/weather_cli/src/main.rs index 767e260..2c27187 100644 --- a/weather_cli/src/main.rs +++ b/weather_cli/src/main.rs @@ -54,28 +54,51 @@ struct Detail { } #[derive(Debug, Deserialize, Serialize)] -struct Details { +struct DetailPrecipation { + precipitation_amount: f32, +} + +impl Default for DetailPrecipation { + fn default() -> DetailPrecipation { + DetailPrecipation { + precipitation_amount: 0.0, + } + } +} + +#[derive(Debug, Deserialize, Serialize)] +struct InstantObject { details: Detail, } #[derive(Debug, Deserialize, Serialize)] -struct Instant { - instant: Details, +struct Next1HoursObject { + details: DetailPrecipation, +} + +impl Default for Next1HoursObject { + fn default() -> Next1HoursObject { + let detpre: DetailPrecipation = DetailPrecipation { + ..Default::default() + }; + Next1HoursObject { details: detpre } + } } #[derive(Debug, Deserialize, Serialize)] -struct Data { - data: Instant, +struct DataObject { + instant: InstantObject, + next_1_hours: Option, } #[derive(Debug, Deserialize, Serialize)] -struct Timeseries { - timeseries: Vec, +struct TimeseriesObject { + data: DataObject, } #[derive(Debug, Deserialize, Serialize)] struct Properties { - properties: Timeseries, + timeseries: Vec, } #[derive(Debug, Deserialize, Serialize)] @@ -88,8 +111,6 @@ fn main() { let config: Config = Config::new(&args); let hours: usize = config.hours; let location: Location = Location::new(); - //println!("Current latitude: {:?}", location.lat); - //println!("Current longitude: {:?}", location.lon); let req_str = format!( "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat={}&lon={}", location.lat, location.lon @@ -98,11 +119,13 @@ fn main() { Ok(response) => response, Err(error) => panic!("Failed to fetch weather data from YR: {:?}", error), }; - let obj: Properties = match response.json() { + + let obj: YrResponse = match response.json() { Ok(prop) => prop, Err(error) => panic!("Failed to parse json: {:?}", error), }; let mut temperatures: f32 = 0.0; + let mut rain: f32 = 0.0; for hour in 0..hours { temperatures = temperatures + obj.properties.timeseries[hour] @@ -110,10 +133,14 @@ fn main() { .instant .details .air_temperature; + if let Some(x) = obj.properties.timeseries[hour].data.next_1_hours.as_ref() { + rain += x.details.precipitation_amount; + } } let avg = temperatures / hours as f32; println!( "Average temperature for the next {} hour(s) is going to be {}", hours, avg ); + println!("Total precipation: {}", rain); } -- 2.39.5