2022-02-12 21:08:13 +01:00
|
|
|
//! Maps retrieval and caching.
|
|
|
|
//!
|
|
|
|
//! This module provides a task that keeps maps up-to-date using a maps-specific refresh interval.
|
|
|
|
//! It stores all the maps as [`DynamicImage`]s in memory.
|
2022-02-12 15:58:56 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
2022-02-12 21:08:13 +01:00
|
|
|
use chrono::DurationRound;
|
|
|
|
use image::DynamicImage;
|
2022-02-13 13:10:12 +01:00
|
|
|
use reqwest::Url;
|
2022-02-12 21:08:13 +01:00
|
|
|
use rocket::tokio::time::{sleep, Duration, Instant};
|
2022-02-12 15:58:56 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
/// A handle to access the in-memory cached maps.
|
|
|
|
pub(crate) type MapsHandle = Arc<Mutex<Maps>>;
|
2022-02-12 17:20:36 +01:00
|
|
|
|
2022-02-12 15:58:56 +01:00
|
|
|
/// The interval between map refreshes (in seconds).
|
2022-02-13 11:16:19 +01:00
|
|
|
const REFRESH_INTERVAL: Duration = Duration::from_secs(60);
|
2022-02-12 21:08:13 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
/// The base URL for retrieving the pollen maps from Buienradar.
|
2022-02-12 21:08:13 +01:00
|
|
|
const POLLEN_BASE_URL: &str =
|
|
|
|
"https://image.buienradar.nl/2.0/image/sprite/WeatherMapPollenRadarHourlyNL\
|
2022-02-13 13:10:12 +01:00
|
|
|
?width=820&height=988&extension=png&renderBackground=False&renderBranding=False\
|
|
|
|
&renderText=False&history=0&forecast=24&skip=0";
|
2022-02-12 21:08:13 +01:00
|
|
|
|
|
|
|
/// The interval for retrieving pollen maps.
|
|
|
|
const POLLEN_INTERVAL: Duration = Duration::from_secs(600);
|
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
/// The base URL for retrieving the precipitation map from Weerplaza.
|
|
|
|
const PRECIPITATION_BASE_URL: &str =
|
|
|
|
"https://cluster.api.meteoplaza.com/v3/nowcast/tiles/radarnl-forecast";
|
|
|
|
|
|
|
|
/// The interval for retrieving precipitation maps.
|
|
|
|
const PRECIPITATION_INTERVAL: Duration = Duration::from_secs(300);
|
|
|
|
|
|
|
|
/// The base URL for retrieving the UV index maps from Buienradar.
|
2022-02-12 21:08:13 +01:00
|
|
|
const UVI_BASE_URL: &str = "https://image.buienradar.nl/2.0/image/sprite/WeatherMapUVIndexNL\
|
2022-02-13 16:55:20 +01:00
|
|
|
?width=820&height=988&extension=png&&renderBackground=False&renderBranding=False\
|
2022-02-13 13:10:12 +01:00
|
|
|
&renderText=False&history=0&forecast=5&skip=0";
|
2022-02-12 21:08:13 +01:00
|
|
|
|
|
|
|
/// The interval for retrieving UV index maps.
|
|
|
|
const UVI_INTERVAL: Duration = Duration::from_secs(3600);
|
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
/// The `MapsRefresh` trait is used to reduce the time a lock needs to be held when updating maps.
|
|
|
|
///
|
|
|
|
/// When refreshing maps, the lock only needs to be held when checking whether a refresh is
|
|
|
|
/// necessary and when the new maps have been retrieved and can be updated.
|
|
|
|
trait MapsRefresh {
|
|
|
|
/// Determines whether the pollen maps need to be refresh.
|
|
|
|
fn needs_pollen_refresh(&self) -> bool;
|
2022-02-12 21:35:58 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
/// Determines whether the precipitation maps need to be refresh.
|
|
|
|
fn needs_precipitation_refresh(&self) -> bool;
|
2022-02-12 21:08:13 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
/// Determines whether the UV index maps need to be refresh.
|
|
|
|
fn needs_uvi_refresh(&self) -> bool;
|
|
|
|
|
|
|
|
/// Updates the pollen maps.
|
|
|
|
fn set_pollen(&self, pollen: Option<DynamicImage>);
|
|
|
|
|
|
|
|
/// Updates the precipitation maps.
|
|
|
|
fn set_precipitation(&self, precipitation: [Option<DynamicImage>; 24]);
|
|
|
|
|
|
|
|
/// Updates the UV index maps.
|
|
|
|
fn set_uvi(&self, uvi: Option<DynamicImage>);
|
2022-02-12 21:08:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Container type for all in-memory cached maps.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct Maps {
|
|
|
|
/// The pollen maps (from Buienradar).
|
|
|
|
pub(crate) pollen: Option<DynamicImage>,
|
|
|
|
|
|
|
|
/// The timestamp the pollen maps were last refreshed.
|
|
|
|
pollen_stamp: Instant,
|
|
|
|
|
|
|
|
/// The precipitation maps (from Weerplaza).
|
|
|
|
// TODO: Make one large image instead of using an array? This is already the case for the
|
|
|
|
// other maps.
|
|
|
|
pub(crate) precipitation: [Option<DynamicImage>; 24],
|
|
|
|
|
|
|
|
/// The timestamp the precipitation maps were last refreshed.
|
|
|
|
precipitation_stamp: Instant,
|
|
|
|
|
|
|
|
/// The UV index maps (from Buienradar).
|
|
|
|
pub(crate) uvi: Option<DynamicImage>,
|
|
|
|
|
|
|
|
/// The timestamp the UV index maps were last refreshed.
|
|
|
|
uvi_stamp: Instant,
|
|
|
|
}
|
2022-02-12 15:58:56 +01:00
|
|
|
|
|
|
|
impl Maps {
|
2022-02-12 21:08:13 +01:00
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
let now = Instant::now();
|
|
|
|
// Because `Option<DynamicImage>` does not implement `Copy`
|
|
|
|
let precipitation = [(); 24].map(|_| None);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
pollen: None,
|
|
|
|
pollen_stamp: now,
|
|
|
|
precipitation,
|
|
|
|
precipitation_stamp: now,
|
|
|
|
uvi: None,
|
|
|
|
uvi_stamp: now,
|
|
|
|
}
|
|
|
|
}
|
2022-02-13 11:16:19 +01:00
|
|
|
}
|
2022-02-12 21:08:13 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
impl MapsRefresh for MapsHandle {
|
|
|
|
fn needs_pollen_refresh(&self) -> bool {
|
2022-02-13 16:55:03 +01:00
|
|
|
let maps = self.lock().expect("Maps handle mutex was poisoned");
|
2022-02-13 11:16:19 +01:00
|
|
|
maps.pollen.is_none() || Instant::now().duration_since(maps.pollen_stamp) > POLLEN_INTERVAL
|
2022-02-12 21:08:13 +01:00
|
|
|
}
|
2022-02-12 17:20:36 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
fn needs_precipitation_refresh(&self) -> bool {
|
2022-02-13 16:55:03 +01:00
|
|
|
let maps = self.lock().expect("Maps handle mutex was poisoned");
|
2022-02-13 11:16:19 +01:00
|
|
|
maps.precipitation.iter().any(|map| map.is_none())
|
|
|
|
|| Instant::now().duration_since(maps.precipitation_stamp) > PRECIPITATION_INTERVAL
|
|
|
|
}
|
2022-02-12 21:08:13 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
fn needs_uvi_refresh(&self) -> bool {
|
2022-02-13 16:55:03 +01:00
|
|
|
let maps = self.lock().expect("Maps handle mutex was poisoned");
|
2022-02-13 11:16:19 +01:00
|
|
|
maps.uvi.is_none() || Instant::now().duration_since(maps.uvi_stamp) > UVI_INTERVAL
|
2022-02-12 21:08:13 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
fn set_pollen(&self, pollen: Option<DynamicImage>) {
|
2022-02-13 16:55:03 +01:00
|
|
|
let mut maps = self.lock().expect("Maps handle mutex was poisoned");
|
2022-02-13 11:16:19 +01:00
|
|
|
maps.pollen = pollen;
|
|
|
|
maps.pollen_stamp = Instant::now();
|
|
|
|
}
|
2022-02-12 21:08:13 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
fn set_precipitation(&self, precipitation: [Option<DynamicImage>; 24]) {
|
2022-02-13 16:55:03 +01:00
|
|
|
let mut maps = self.lock().expect("Maps handle mutex was poisoned");
|
2022-02-13 11:16:19 +01:00
|
|
|
maps.precipitation = precipitation;
|
|
|
|
maps.precipitation_stamp = Instant::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_uvi(&self, uvi: Option<DynamicImage>) {
|
2022-02-13 16:55:03 +01:00
|
|
|
let mut maps = self.lock().expect("Maps handle mutex was poisoned");
|
2022-02-13 11:16:19 +01:00
|
|
|
maps.uvi = uvi;
|
|
|
|
maps.uvi_stamp = Instant::now();
|
2022-02-12 15:58:56 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-12 21:08:13 +01:00
|
|
|
|
|
|
|
/// Retrieves an image from the provided URL.
|
2022-02-13 11:22:22 +01:00
|
|
|
///
|
|
|
|
/// This returns [`None`] if it fails in either performing the request, retrieving the bytes from
|
|
|
|
/// the image or loading and the decoding the data into [`DynamicImage`].
|
2022-02-13 13:10:12 +01:00
|
|
|
async fn retrieve_image(url: Url) -> Option<DynamicImage> {
|
2022-02-12 21:08:13 +01:00
|
|
|
// TODO: Handle or log errors!
|
|
|
|
let response = reqwest::get(url).await.ok()?;
|
|
|
|
let bytes = response.bytes().await.ok()?;
|
|
|
|
|
|
|
|
image::load_from_memory(&bytes).ok()
|
|
|
|
}
|
2022-02-13 11:16:19 +01:00
|
|
|
|
|
|
|
/// Retrieves the pollen maps from Buienradar.
|
|
|
|
///
|
|
|
|
/// See [`POLLEN_BASE_URL`] for the base URL and [`retrieve_image`] for the retrieval function.
|
|
|
|
async fn retrieve_pollen_maps() -> Option<DynamicImage> {
|
2022-02-13 13:10:12 +01:00
|
|
|
let timestamp = format!("{}", chrono::Local::now().format("%y%m%d%H%M"));
|
|
|
|
let mut url = Url::parse(POLLEN_BASE_URL).unwrap();
|
|
|
|
url.query_pairs_mut().append_pair("timestamp", ×tamp);
|
2022-02-13 11:16:19 +01:00
|
|
|
|
|
|
|
println!("🔽 Refreshing pollen maps from: {}", url);
|
2022-02-13 13:10:12 +01:00
|
|
|
retrieve_image(url).await
|
2022-02-13 11:16:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the pollen maps from Weerplaza.
|
|
|
|
///
|
|
|
|
/// See [`PRECIPITATION_BASE_URL`] for the base URL and [`retrieve_image`] for the retrieval
|
|
|
|
/// function.
|
|
|
|
async fn retrieve_precipitation_maps() -> [Option<DynamicImage>; 24] {
|
|
|
|
let just_before = (chrono::Utc::now() - chrono::Duration::minutes(10))
|
|
|
|
// This only fails if timestamps and durations exceed limits!
|
|
|
|
.duration_trunc(chrono::Duration::minutes(5))
|
|
|
|
.unwrap();
|
2022-02-13 13:10:12 +01:00
|
|
|
let timestamp_prefix = just_before.format("%Y%m%d%H%M");
|
|
|
|
let base_url = Url::parse(PRECIPITATION_BASE_URL).unwrap();
|
2022-02-13 11:16:19 +01:00
|
|
|
|
|
|
|
let mut precipitation: [Option<DynamicImage>; 24] = Default::default();
|
|
|
|
for (index, map) in precipitation.iter_mut().enumerate() {
|
2022-02-13 13:10:12 +01:00
|
|
|
let timestamp = format!("{timestamp_prefix}_{:03}", index * 5);
|
|
|
|
let mut url = base_url.clone();
|
|
|
|
url.path_segments_mut().unwrap().push(×tamp);
|
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
println!("🔽 Refreshing precipitation map from: {}", url);
|
2022-02-13 13:10:12 +01:00
|
|
|
*map = retrieve_image(url).await;
|
2022-02-13 11:16:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
precipitation
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the UV index maps from Buienradar.
|
|
|
|
///
|
|
|
|
/// See [`UVI_BASE_URL`] for the base URL and [`retrieve_image`] for the retrieval function.
|
|
|
|
async fn retrieve_uvi_maps() -> Option<DynamicImage> {
|
2022-02-13 13:10:12 +01:00
|
|
|
let timestamp = format!("{}", chrono::Local::now().format("%y%m%d%H%M"));
|
|
|
|
let mut url = Url::parse(UVI_BASE_URL).unwrap();
|
|
|
|
url.query_pairs_mut().append_pair("timestamp", ×tamp);
|
2022-02-13 11:16:19 +01:00
|
|
|
|
|
|
|
println!("🔽 Refreshing UV index maps from: {}", url);
|
2022-02-13 13:10:12 +01:00
|
|
|
retrieve_image(url).await
|
2022-02-13 11:16:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs a loop that keeps refreshing the maps when necessary.
|
|
|
|
///
|
|
|
|
/// Use [`MapsRefresh`] trait methods on `maps_handle` to check whether each maps type needs to be
|
|
|
|
/// refreshed and uses its retrieval function to update it if necessary.
|
|
|
|
pub(crate) async fn run(maps_handle: MapsHandle) -> ! {
|
|
|
|
loop {
|
|
|
|
println!("🕔 Refreshing the maps (if necessary)...");
|
|
|
|
|
|
|
|
if maps_handle.needs_pollen_refresh() {
|
|
|
|
let pollen = retrieve_pollen_maps().await;
|
|
|
|
maps_handle.set_pollen(pollen);
|
|
|
|
}
|
|
|
|
if maps_handle.needs_precipitation_refresh() {
|
|
|
|
let precipitation = retrieve_precipitation_maps().await;
|
|
|
|
maps_handle.set_precipitation(precipitation);
|
|
|
|
}
|
|
|
|
if maps_handle.needs_uvi_refresh() {
|
|
|
|
let uvi = retrieve_uvi_maps().await;
|
|
|
|
maps_handle.set_uvi(uvi);
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(REFRESH_INTERVAL).await;
|
|
|
|
}
|
|
|
|
}
|