Files
Astronome/backend/src/weather/seventimer.rs
T
2026-04-10 00:14:10 +02:00

49 lines
1.2 KiB
Rust

use anyhow::Context;
use serde::{Deserialize, Serialize};
const SEVENTIMER_URL: &str =
"http://www.7timer.info/bin/api.pl?lon=4.1167&lat=43.8167&product=astro&output=json";
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum GoNogo {
Go,
Marginal,
Nogo,
}
impl GoNogo {
pub fn as_str(&self) -> &'static str {
match self {
GoNogo::Go => "go",
GoNogo::Marginal => "marginal",
GoNogo::Nogo => "nogo",
}
}
}
pub fn go_nogo(cloudcover: u8, seeing: u8, transparency: u8) -> GoNogo {
if cloudcover <= 2 && seeing <= 3 && transparency <= 3 {
GoNogo::Go
} else if cloudcover <= 4 && seeing <= 5 {
GoNogo::Marginal
} else {
GoNogo::Nogo
}
}
pub async fn fetch_seventimer() -> anyhow::Result<serde_json::Value> {
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()?;
let resp = client
.get(SEVENTIMER_URL)
.send()
.await
.context("7timer request failed")?;
let json = resp.json::<serde_json::Value>().await.context("7timer JSON parse failed")?;
Ok(json)
}