Added additional weather data

This commit is contained in:
2023-05-07 21:36:52 +02:00
parent a57371b2cf
commit 5ae46ed06f
3 changed files with 31 additions and 5 deletions

View File

@@ -3,22 +3,33 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct WeatherResponse { pub struct WeatherResponse {
pub id: usize,
pub weather: Vec<Weather>, pub weather: Vec<Weather>,
pub wind: Wind, pub wind: Wind,
pub main: Main,
pub clouds: Clouds,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Weather { pub struct Weather {
pub id: usize, pub id: u16,
pub main: String, pub main: String,
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct Main {
pub temp: f32,
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Wind { pub struct Wind {
pub speed: f32, pub speed: f32,
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct Clouds {
pub all: u16,
}
fn construct_weather_data_url( fn construct_weather_data_url(
base_url: &str, base_url: &str,
latitude: &str, latitude: &str,
@@ -27,7 +38,7 @@ fn construct_weather_data_url(
current_datetime: &DateTime<Utc>, current_datetime: &DateTime<Utc>,
) -> String { ) -> String {
format!( format!(
"{}?lat={}&lon={}&dt={}&appid={}", "{}?lat={}&lon={}&dt={}&appid={}&exclude=minutely,hourly,daily,alerts",
base_url, base_url,
latitude, latitude,
longitude, longitude,
@@ -44,6 +55,9 @@ pub async fn fetch_weather_data(
current_datetime: &DateTime<Utc>, current_datetime: &DateTime<Utc>,
) -> Result<WeatherResponse, reqwest::Error> { ) -> Result<WeatherResponse, reqwest::Error> {
let url = construct_weather_data_url(base_url, latitude, longitude, api_key, current_datetime); let url = construct_weather_data_url(base_url, latitude, longitude, api_key, current_datetime);
let resp = reqwest::get(url.clone()).await?.text().await;
println!("{:?}", resp);
reqwest::get(url).await?.json().await reqwest::get(url).await?.json().await
} }
@@ -62,7 +76,7 @@ mod test {
&Utc.with_ymd_and_hms(2023, 1, 2, 1, 0, 0).unwrap(), &Utc.with_ymd_and_hms(2023, 1, 2, 1, 0, 0).unwrap(),
); );
assert_eq!( assert_eq!(
"http://test?lat=1.14&lon=1.12&dt=1672621200&appid=abc", "http://test?lat=1.14&lon=1.12&dt=1672621200&appid=abc&exclude=minutely,hourly,daily,alerts",
result result
); );
} }

View File

@@ -33,10 +33,18 @@ async fn read_weather_data(settings: &input::Input) -> Result<()> {
.await?; .await?;
info!("Start writing weather data..."); info!("Start writing weather data...");
let map = write::WeatherReading { let mut map = write::WeatherReading {
wind_speed: result.wind.speed, wind_speed: result.wind.speed,
time: Utc::now(), time: Utc::now(),
temp: result.main.temp,
clouds: result.clouds.all,
main: None,
id: None,
}; };
if let Some(weather_element) = result.weather.first() {
map.main = Some(weather_element.main.clone());
map.id = Some(weather_element.id);
}
write::write_weather_data( write::write_weather_data(
map, map,
&settings.influx_db_base_url, &settings.influx_db_base_url,

View File

@@ -6,6 +6,10 @@ use influxdb::{Client, InfluxDbWriteable};
pub struct WeatherReading { pub struct WeatherReading {
pub time: DateTime<Utc>, pub time: DateTime<Utc>,
pub wind_speed: f32, pub wind_speed: f32,
pub clouds: u16,
pub temp: f32,
pub id: Option<u16>,
pub main: Option<String>,
} }
pub async fn write_weather_data( pub async fn write_weather_data(