2022-02-13 15:38:17 +01:00
|
|
|
//! The Buienradar data provider.
|
|
|
|
//!
|
|
|
|
//! For more information about Buienradar, see: <https://www.buienradar.nl/overbuienradar/contact>
|
2022-02-13 16:46:02 +01:00
|
|
|
//! and <https://www.buienradar.nl/overbuienradar/gratis-weerdata>.
|
2022-02-13 15:38:17 +01:00
|
|
|
|
2022-02-14 21:06:31 +01:00
|
|
|
use cached::proc_macro::cached;
|
2022-02-13 15:38:17 +01:00
|
|
|
use chrono::offset::TimeZone;
|
|
|
|
use chrono::serde::ts_seconds;
|
2022-02-13 16:46:02 +01:00
|
|
|
use chrono::{DateTime, Local, NaiveTime, ParseError, Utc};
|
2022-02-13 15:38:17 +01:00
|
|
|
use chrono_tz::Europe;
|
2022-02-13 16:46:02 +01:00
|
|
|
use csv::ReaderBuilder;
|
2022-02-13 15:38:17 +01:00
|
|
|
use reqwest::Url;
|
2022-02-13 16:46:02 +01:00
|
|
|
use rocket::serde::{Deserialize, Serialize};
|
2022-02-13 15:38:17 +01:00
|
|
|
|
2022-02-14 21:06:31 +01:00
|
|
|
use crate::{cache_key, Metric};
|
2022-02-13 15:38:17 +01:00
|
|
|
|
|
|
|
/// The base URL for the Buienradar API.
|
|
|
|
const BUIENRADAR_BASE_URL: &str = "https://gpsgadget.buienradar.nl/data/raintext";
|
|
|
|
|
2022-02-13 16:46:02 +01:00
|
|
|
/// A row in the precipitation text output.
|
|
|
|
///
|
|
|
|
/// This is an intermediate type used to represent rows of the output.
|
|
|
|
#[derive(Debug, Deserialize)]
|
2022-02-13 15:38:17 +01:00
|
|
|
#[serde(crate = "rocket::serde")]
|
2022-02-13 16:46:02 +01:00
|
|
|
struct Row {
|
|
|
|
/// The precipitation value in the range `0..=255`.
|
|
|
|
value: u16,
|
|
|
|
|
|
|
|
/// The time in the `HH:MM` format.
|
|
|
|
time: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The Buienradar API precipitation data item.
|
2022-02-14 21:06:31 +01:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2022-02-13 16:46:02 +01:00
|
|
|
#[serde(crate = "rocket::serde", try_from = "Row")]
|
2022-02-13 15:38:17 +01:00
|
|
|
pub(crate) struct Item {
|
|
|
|
/// The time(stamp) of the forecast.
|
|
|
|
#[serde(serialize_with = "ts_seconds::serialize")]
|
|
|
|
time: DateTime<Utc>,
|
|
|
|
|
|
|
|
/// The forecasted value.
|
|
|
|
///
|
2022-02-13 16:46:02 +01:00
|
|
|
/// Its unit is mm/h.
|
2022-02-13 15:38:17 +01:00
|
|
|
value: f32,
|
|
|
|
}
|
|
|
|
|
2022-02-13 16:46:02 +01:00
|
|
|
impl TryFrom<Row> for Item {
|
|
|
|
type Error = ParseError;
|
|
|
|
|
|
|
|
fn try_from(row: Row) -> Result<Self, Self::Error> {
|
|
|
|
let time = parse_time(&row.time)?;
|
|
|
|
let value = convert_value(row.value);
|
|
|
|
|
|
|
|
Ok(Item { time, value })
|
|
|
|
}
|
2022-02-13 15:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a time string to date/time in the UTC time zone.
|
|
|
|
///
|
|
|
|
/// The provided time has the format `HH:MM` and is considered to be in the Europe/Amsterdam
|
|
|
|
/// time zone.
|
2022-02-13 16:46:02 +01:00
|
|
|
fn parse_time(t: &str) -> Result<DateTime<Utc>, ParseError> {
|
|
|
|
// First, get the naive time.
|
|
|
|
let ntime = NaiveTime::parse_from_str(t, "%H:%M")?;
|
|
|
|
// FIXME: This might actually be the day before when started on a machine that
|
|
|
|
// doesn't run in the Europe/Amsterdam time zone.
|
|
|
|
let ndtime = Local::today().naive_local().and_time(ntime);
|
2022-02-13 15:38:17 +01:00
|
|
|
// Then, interpret the naive date/time in the Europe/Amsterdam time zone and convert it to the
|
|
|
|
// UTC time zone.
|
|
|
|
let ldtime = Europe::Amsterdam.from_local_datetime(&ndtime).unwrap();
|
|
|
|
let dtime = ldtime.with_timezone(&Utc);
|
|
|
|
|
2022-02-13 16:46:02 +01:00
|
|
|
Ok(dtime)
|
2022-02-13 15:38:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 16:46:02 +01:00
|
|
|
/// Converts a precipitation value into an precipitation intensity value in mm/h.
|
2022-02-13 15:38:17 +01:00
|
|
|
///
|
|
|
|
/// For the conversion formula, see: <https://www.buienradar.nl/overbuienradar/gratis-weerdata>.
|
2022-02-13 16:46:02 +01:00
|
|
|
fn convert_value(v: u16) -> f32 {
|
2022-02-13 15:38:17 +01:00
|
|
|
let base: f32 = 10.0;
|
2022-02-13 16:46:02 +01:00
|
|
|
let value = base.powf((v as f32 - 109.0) / 32.0);
|
2022-02-13 15:38:17 +01:00
|
|
|
|
2022-02-13 16:46:02 +01:00
|
|
|
(value * 10.0).round() / 10.0
|
2022-02-13 15:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the Buienradar forecasted precipitation items for the provided position.
|
|
|
|
///
|
|
|
|
/// It only supports the following metric:
|
|
|
|
/// * [`Metric::Precipitation`]
|
|
|
|
///
|
|
|
|
/// Returns [`None`] if retrieval or deserialization fails, or if the metric is not supported by
|
|
|
|
/// this provider.
|
2022-02-14 21:06:31 +01:00
|
|
|
#[cached(
|
|
|
|
time = 300,
|
|
|
|
convert = "{ cache_key(lat, lon, metric) }",
|
|
|
|
key = "(i32, i32, Metric)",
|
|
|
|
option = true
|
|
|
|
)]
|
2022-02-13 15:38:17 +01:00
|
|
|
pub(crate) async fn get(lat: f64, lon: f64, metric: Metric) -> Option<Vec<Item>> {
|
|
|
|
if metric != Metric::Precipitation {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut url = Url::parse(BUIENRADAR_BASE_URL).unwrap();
|
|
|
|
url.query_pairs_mut()
|
|
|
|
.append_pair("lat", &format!("{:.02}", lat))
|
|
|
|
.append_pair("lon", &format!("{:.02}", lon));
|
|
|
|
|
|
|
|
println!("▶️ Retrieving Buienradar data from {url}");
|
|
|
|
let response = reqwest::get(url).await.ok()?;
|
|
|
|
let output = match response.error_for_status() {
|
|
|
|
Ok(res) => res.text().await.ok()?,
|
|
|
|
Err(_err) => return None,
|
|
|
|
};
|
|
|
|
|
2022-02-13 16:46:02 +01:00
|
|
|
let mut rdr = ReaderBuilder::new()
|
|
|
|
.has_headers(false)
|
|
|
|
.delimiter(b'|')
|
|
|
|
.from_reader(output.as_bytes());
|
|
|
|
rdr.deserialize().collect::<Result<_, _>>().ok()
|
2022-02-13 15:38:17 +01:00
|
|
|
}
|