Refactor weather
This commit is contained in:
parent
7c8f0ce2dc
commit
7d73080e30
1 changed files with 19 additions and 16 deletions
35
src/lib.rs
35
src/lib.rs
|
@ -10,46 +10,48 @@ pub mod wmo;
|
||||||
#[component]
|
#[component]
|
||||||
pub fn Weather() -> impl IntoView {
|
pub fn Weather() -> impl IntoView {
|
||||||
let weather_json = LocalResource::new(async move || {
|
let weather_json = LocalResource::new(async move || {
|
||||||
let json_response = reqwest::get("https://api.open-meteo.com/v1/forecast?latitude=60.236610&longitude=25.083630&timezone=auto&forecast_hours=16&wind_speed_unit=kn&hourly=temperature_2m,wind_speed_10m,precipitation,precipitation_probability,weather_code,is_day").await
|
let json_response = reqwest::get("https://api.open-meteo.com/v1/forecast?latitude=60.236610&longitude=25.083630&timezone=auto&forecast_hours=17&wind_speed_unit=kn&hourly=temperature_2m,wind_speed_10m,precipitation,precipitation_probability,weather_code,is_day").await
|
||||||
.and_then(|response| Ok(response.json::<Value>()));
|
.and_then(|response| Ok(response.json::<Value>()));
|
||||||
match json_response {
|
match json_response {
|
||||||
Ok(result) => Some(result.await.ok()),
|
Ok(result) => Some(result.await.ok()),
|
||||||
_ => None
|
_ => None
|
||||||
}.flatten()
|
}.flatten()
|
||||||
});
|
});
|
||||||
|
let get_hourly_slice = move |key: &str, start_index: usize, end_index: usize| {
|
||||||
|
Some(weather_json.get().flatten()?["hourly"][key].as_array()?[start_index..end_index].to_vec().into_iter())
|
||||||
|
};
|
||||||
|
|
||||||
let times = move || {
|
let times = move || {
|
||||||
Some(weather_json.get().flatten()?["hourly"]["time"].as_array()?.into_iter().map(|timestamp| {
|
Some(get_hourly_slice("time", 0, 16)?.map(|timestamp| {
|
||||||
let datetime = NaiveDateTime::parse_from_str(timestamp.as_str().unwrap_or_default(), "%Y-%m-%dT%H:%M");
|
let datetime = NaiveDateTime::parse_from_str(timestamp.as_str().unwrap_or_default(), "%Y-%m-%dT%H:%M");
|
||||||
view! {<td>{format!("{}", datetime.unwrap_or_default().hour())}</td>}
|
view! {<td>{format!("{}", datetime.unwrap_or_default().hour())}</td>}
|
||||||
}).collect::<Vec<_>>())
|
}).collect::<Vec<_>>())
|
||||||
};
|
};
|
||||||
let weather_codes = move || {
|
let weather_codes = move || {
|
||||||
let json = weather_json.get().flatten()?;
|
let is_day = get_hourly_slice("is_day", 0, 16)?;
|
||||||
let is_day = json["hourly"]["is_day"].as_array()?.into_iter();
|
let codes = get_hourly_slice("weather_code", 0, 16)?;
|
||||||
let codes = json["hourly"]["weather_code"].as_array()?.into_iter();
|
|
||||||
Some(is_day.zip(codes).map(move |(is_day, code)| {
|
Some(is_day.zip(codes).map(move |(is_day, code)| {
|
||||||
let wmo_codes = crate::wmo::get_codes();
|
let wmo_codes = crate::wmo::get_codes();
|
||||||
let image = wmo_codes[code.to_string()][if *is_day == Value::Number(1.into()) {"day"} else {"night"}]["image"].as_str().unwrap_or_default();
|
let code_info = wmo_codes[code.to_string()][if is_day == Value::Number(1.into()) {"day"} else {"night"}].clone();
|
||||||
let description = wmo_codes[code.to_string()][if *is_day == Value::Number(1.into()) {"day"} else {"night"}]["description"].as_str().unwrap_or_default();
|
let image = code_info["image"].as_str().unwrap_or_default();
|
||||||
|
let description = code_info["description"].as_str().unwrap_or_default();
|
||||||
view! { <td><img src={image.to_string()} alt={description.to_string()}/></td> }
|
view! { <td><img src={image.to_string()} alt={description.to_string()}/></td> }
|
||||||
}).collect::<Vec<_>>())
|
}).collect::<Vec<_>>())
|
||||||
};
|
};
|
||||||
let temperatures = move || {
|
let temperatures = move || {
|
||||||
Some(weather_json.get().flatten()?["hourly"]["temperature_2m"].as_array()?.into_iter().map(|temperature| {
|
Some(get_hourly_slice("temperature_2m", 0, 16)?.map(|temperature| {
|
||||||
view! { <td>{format!("{}°C", temperature.to_string())}</td> }
|
view! { <td>{format!("{}°C", temperature.as_f64().unwrap_or_default().round().to_string())}</td> }
|
||||||
}).collect::<Vec<_>>())
|
}).collect::<Vec<_>>())
|
||||||
};
|
};
|
||||||
let wind = move || {
|
let wind = move || {
|
||||||
Some(weather_json.get().flatten()?["hourly"]["wind_speed_10m"].as_array()?.into_iter().map(|wind_speed| {
|
Some(get_hourly_slice("wind_speed_10m", 0, 16)?.map(|wind_speed| {
|
||||||
view! { <td>{format!("{}kn", wind_speed.to_string())}</td> }
|
view! { <td>{format!("{}kn", wind_speed.as_f64().unwrap_or_default().round().to_string())}</td> }
|
||||||
}).collect::<Vec<_>>())
|
}).collect::<Vec<_>>())
|
||||||
};
|
};
|
||||||
let precipitation = move || {
|
let precipitation = move || {
|
||||||
let json = weather_json.get().flatten()?;
|
let amounts = get_hourly_slice("precipitation", 1, 17)?;
|
||||||
let amounts = json["hourly"]["precipitation"].as_array()?.into_iter();
|
let probabilities = get_hourly_slice("precipitation_probability", 1, 17)?;
|
||||||
let probabilities = json["hourly"]["precipitation"].as_array()?.into_iter();
|
Some(amounts.zip(probabilities).map(|(amount, probability)| {
|
||||||
Some(amounts.zip(probabilities).map(|(amount, probability_float)| {
|
|
||||||
let probability = (probability_float.as_f64()? * 100.0).round() as i64;
|
|
||||||
Some(view! {
|
Some(view! {
|
||||||
<td>
|
<td>
|
||||||
<span>{format!("{}mm", amount.to_string())}</span>
|
<span>{format!("{}mm", amount.to_string())}</span>
|
||||||
|
@ -59,6 +61,7 @@ pub fn Weather() -> impl IntoView {
|
||||||
})
|
})
|
||||||
}).collect::<Vec<_>>())
|
}).collect::<Vec<_>>())
|
||||||
};
|
};
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<div class="weather">
|
<div class="weather">
|
||||||
<Suspense fallback=move || view! { <p>"Loading..."</p> }>
|
<Suspense fallback=move || view! { <p>"Loading..."</p> }>
|
||||||
|
|
Loading…
Add table
Reference in a new issue