2022-02-12 12:25:05 +01:00
|
|
|
//! Service that provides today's weather forecast for air quality, rain and UV metrics.
|
|
|
|
//!
|
|
|
|
//! This is useful if you want to prepare for going outside and need to know what happens in the
|
|
|
|
//! near future or later today.
|
|
|
|
|
|
|
|
#![warn(
|
|
|
|
clippy::all,
|
|
|
|
missing_debug_implementations,
|
|
|
|
rust_2018_idioms,
|
|
|
|
rustdoc::broken_intra_doc_links
|
|
|
|
)]
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
use std::sync::{Arc, Mutex};
|
2022-02-12 21:35:58 +01:00
|
|
|
|
2022-02-12 15:58:56 +01:00
|
|
|
use color_eyre::Result;
|
2022-02-12 12:25:05 +01:00
|
|
|
use geocoding::{Forward, Openstreetmap, Point};
|
|
|
|
use rocket::serde::json::Json;
|
|
|
|
use rocket::serde::Serialize;
|
2022-02-12 15:58:56 +01:00
|
|
|
use rocket::tokio::{self, select};
|
2022-02-12 21:35:58 +01:00
|
|
|
use rocket::{get, routes, FromFormField, State};
|
2022-02-12 15:58:56 +01:00
|
|
|
|
2022-02-13 11:16:19 +01:00
|
|
|
use self::maps::{Maps, MapsHandle};
|
2022-02-13 15:39:09 +01:00
|
|
|
use self::providers::buienradar::Item as BuienradarItem;
|
2022-02-13 12:45:27 +01:00
|
|
|
use self::providers::luchtmeetnet::Item as LuchtmeetnetItem;
|
2022-02-12 15:58:56 +01:00
|
|
|
|
2022-02-13 12:45:27 +01:00
|
|
|
pub(crate) mod maps;
|
|
|
|
pub(crate) mod providers;
|
2022-02-12 12:25:05 +01:00
|
|
|
|
|
|
|
/// The current for a specific location.
|
|
|
|
///
|
|
|
|
/// Only the metrics asked for are included as well as the position and current time.
|
|
|
|
///
|
|
|
|
/// TODO: Fill the metrics with actual data!
|
2022-02-13 12:46:42 +01:00
|
|
|
#[derive(Debug, Default, Serialize)]
|
2022-02-12 12:25:05 +01:00
|
|
|
#[serde(crate = "rocket::serde")]
|
|
|
|
struct Forecast {
|
|
|
|
/// The latitude of the position.
|
|
|
|
lat: f64,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
2022-02-12 12:25:05 +01:00
|
|
|
/// The longitude of the position.
|
|
|
|
lon: f64,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
2022-02-12 12:25:05 +01:00
|
|
|
/// The current time (in seconds since the UNIX epoch).
|
|
|
|
time: i64,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
|
|
|
/// The air quality index (when asked for).
|
|
|
|
#[serde(rename = "AQI", skip_serializing_if = "Option::is_none")]
|
2022-02-13 12:46:42 +01:00
|
|
|
aqi: Option<Vec<LuchtmeetnetItem>>,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
|
|
|
/// The NO₂ concentration (when asked for).
|
|
|
|
#[serde(rename = "NO2", skip_serializing_if = "Option::is_none")]
|
2022-02-13 12:46:42 +01:00
|
|
|
no2: Option<Vec<LuchtmeetnetItem>>,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
|
|
|
/// The O₃ concentration (when asked for).
|
|
|
|
#[serde(rename = "O3", skip_serializing_if = "Option::is_none")]
|
2022-02-13 12:46:42 +01:00
|
|
|
o3: Option<Vec<LuchtmeetnetItem>>,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
2022-02-12 16:18:30 +01:00
|
|
|
/// The combination of pollen + air quality index (when asked for).
|
2022-02-12 15:01:02 +01:00
|
|
|
#[serde(rename = "PAQI", skip_serializing_if = "Option::is_none")]
|
2022-02-13 12:46:42 +01:00
|
|
|
paqi: Option<()>,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
|
|
|
/// The particulate matter in the air (when asked for).
|
|
|
|
#[serde(rename = "PM10", skip_serializing_if = "Option::is_none")]
|
2022-02-13 12:46:42 +01:00
|
|
|
pm10: Option<Vec<LuchtmeetnetItem>>,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
|
|
|
/// The pollen in the air (when asked for).
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-02-13 12:46:42 +01:00
|
|
|
pollen: Option<()>,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
|
|
|
/// The precipitation (when asked for).
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-02-13 15:39:09 +01:00
|
|
|
precipitation: Option<Vec<BuienradarItem>>,
|
2022-02-12 15:01:02 +01:00
|
|
|
|
|
|
|
/// The UV index (when asked for).
|
|
|
|
#[serde(rename = "UVI", skip_serializing_if = "Option::is_none")]
|
2022-02-13 12:46:42 +01:00
|
|
|
uvi: Option<()>,
|
2022-02-12 12:25:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Forecast {
|
|
|
|
fn new(lat: f64, lon: f64) -> Self {
|
|
|
|
let time = chrono::Utc::now().timestamp();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
lat,
|
|
|
|
lon,
|
|
|
|
time,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The supported metrics.
|
|
|
|
///
|
|
|
|
/// This is used for selecting which metrics should be calculated & returned.
|
|
|
|
#[allow(clippy::upper_case_acronyms)]
|
2022-02-12 15:02:24 +01:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, FromFormField)]
|
2022-02-12 12:25:05 +01:00
|
|
|
enum Metric {
|
|
|
|
/// All metrics.
|
2022-02-12 15:02:24 +01:00
|
|
|
#[field(value = "all")]
|
2022-02-12 12:25:05 +01:00
|
|
|
All,
|
|
|
|
/// The air quality index.
|
|
|
|
AQI,
|
|
|
|
/// The NO₂ concentration.
|
|
|
|
NO2,
|
|
|
|
/// The O₃ concentration.
|
|
|
|
O3,
|
2022-02-12 16:18:30 +01:00
|
|
|
/// The combination of pollen + air quality index.
|
2022-02-12 12:25:05 +01:00
|
|
|
PAQI,
|
|
|
|
/// The particulate matter in the air.
|
|
|
|
PM10,
|
|
|
|
/// The pollen in the air.
|
|
|
|
Pollen,
|
|
|
|
/// The precipitation.
|
|
|
|
Precipitation,
|
|
|
|
/// The UV index.
|
|
|
|
UVI,
|
|
|
|
}
|
|
|
|
|
2022-02-12 15:02:24 +01:00
|
|
|
impl Metric {
|
|
|
|
/// Returns all supported metrics.
|
|
|
|
fn all() -> Vec<Metric> {
|
|
|
|
use Metric::*;
|
|
|
|
|
|
|
|
Vec::from([AQI, NO2, O3, PAQI, PM10, Pollen, Precipitation, UVI])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 12:25:05 +01:00
|
|
|
/// Calculates and returns the forecast.
|
|
|
|
///
|
|
|
|
/// The provided list `metrics` determines what will be included in the forecast.
|
2022-02-12 21:35:58 +01:00
|
|
|
async fn forecast(
|
|
|
|
lat: f64,
|
|
|
|
lon: f64,
|
|
|
|
metrics: Vec<Metric>,
|
|
|
|
_maps_handle: &State<MapsHandle>,
|
|
|
|
) -> Forecast {
|
2022-02-12 12:25:05 +01:00
|
|
|
let mut forecast = Forecast::new(lat, lon);
|
|
|
|
|
2022-02-12 15:02:24 +01:00
|
|
|
// Expand the `All` metric if present, deduplicate otherwise.
|
|
|
|
let mut metrics = metrics;
|
|
|
|
if metrics.contains(&Metric::All) {
|
|
|
|
metrics = Metric::all();
|
|
|
|
} else {
|
|
|
|
metrics.dedup()
|
|
|
|
}
|
|
|
|
|
2022-02-12 12:25:05 +01:00
|
|
|
for metric in metrics {
|
|
|
|
match metric {
|
2022-02-12 15:02:24 +01:00
|
|
|
// This should have been expanded to all the metrics matched below.
|
2022-02-13 12:46:42 +01:00
|
|
|
Metric::All => unreachable!("The all metric should have been expanded"),
|
|
|
|
Metric::AQI => forecast.aqi = providers::luchtmeetnet::get(lat, lon, metric).await,
|
|
|
|
Metric::NO2 => forecast.no2 = providers::luchtmeetnet::get(lat, lon, metric).await,
|
|
|
|
Metric::O3 => forecast.o3 = providers::luchtmeetnet::get(lat, lon, metric).await,
|
|
|
|
Metric::PAQI => forecast.paqi = Some(()),
|
|
|
|
Metric::PM10 => forecast.pm10 = providers::luchtmeetnet::get(lat, lon, metric).await,
|
|
|
|
Metric::Pollen => forecast.pollen = Some(()),
|
2022-02-13 15:39:09 +01:00
|
|
|
Metric::Precipitation => {
|
|
|
|
forecast.precipitation = providers::buienradar::get(lat, lon, metric).await
|
|
|
|
}
|
2022-02-13 12:46:42 +01:00
|
|
|
Metric::UVI => forecast.uvi = Some(()),
|
2022-02-12 12:25:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
forecast
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the geocoded position for the given address.
|
2022-02-12 15:57:48 +01:00
|
|
|
async fn address_position(address: String) -> Option<(f64, f64)> {
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
let osm = Openstreetmap::new();
|
|
|
|
let points: Vec<Point<f64>> = osm.forward(&address).ok()?;
|
|
|
|
|
|
|
|
points.get(0).map(|point| (point.x(), point.y()))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
.flatten()
|
2022-02-12 12:25:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for retrieving the forecast for an address.
|
|
|
|
#[get("/forecast?<address>&<metrics>")]
|
2022-02-12 21:35:58 +01:00
|
|
|
async fn forecast_address(
|
|
|
|
address: String,
|
|
|
|
metrics: Vec<Metric>,
|
|
|
|
maps_handle: &State<MapsHandle>,
|
|
|
|
) -> Option<Json<Forecast>> {
|
2022-02-12 15:57:48 +01:00
|
|
|
let (lat, lon) = address_position(address).await?;
|
2022-02-12 21:35:58 +01:00
|
|
|
let forecast = forecast(lat, lon, metrics, maps_handle).await;
|
2022-02-12 12:25:05 +01:00
|
|
|
|
|
|
|
Some(Json(forecast))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for retrieving the forecast for a geocoded position.
|
|
|
|
#[get("/forecast?<lat>&<lon>&<metrics>", rank = 2)]
|
2022-02-12 21:35:58 +01:00
|
|
|
async fn forecast_geo(
|
|
|
|
lat: f64,
|
|
|
|
lon: f64,
|
|
|
|
metrics: Vec<Metric>,
|
|
|
|
maps_handle: &State<MapsHandle>,
|
|
|
|
) -> Json<Forecast> {
|
|
|
|
let forecast = forecast(lat, lon, metrics, maps_handle).await;
|
2022-02-12 12:25:05 +01:00
|
|
|
|
|
|
|
Json(forecast)
|
|
|
|
}
|
|
|
|
|
2022-02-12 15:58:56 +01:00
|
|
|
/// Starts the main maps refresh loop and sets up and launches Rocket.
|
2022-02-13 11:22:22 +01:00
|
|
|
///
|
|
|
|
/// See [`maps::run`] for the maps refresh loop.
|
2022-02-12 15:58:56 +01:00
|
|
|
#[rocket::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
color_eyre::install()?;
|
|
|
|
|
2022-02-12 21:35:58 +01:00
|
|
|
let maps = Maps::new();
|
|
|
|
let maps_handle = Arc::new(Mutex::new(maps));
|
|
|
|
let maps_updater = tokio::spawn(maps::run(Arc::clone(&maps_handle)));
|
|
|
|
|
2022-02-12 15:58:56 +01:00
|
|
|
let rocket = rocket::build()
|
2022-02-12 21:35:58 +01:00
|
|
|
.manage(maps_handle)
|
2022-02-12 15:58:56 +01:00
|
|
|
.mount("/", routes![forecast_address, forecast_geo])
|
|
|
|
.ignite()
|
|
|
|
.await?;
|
|
|
|
let shutdown = rocket.shutdown();
|
|
|
|
|
|
|
|
select! {
|
|
|
|
result = rocket.launch() => {
|
|
|
|
result?
|
|
|
|
}
|
|
|
|
result = maps_updater => {
|
|
|
|
shutdown.notify();
|
|
|
|
result?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-02-12 12:25:05 +01:00
|
|
|
}
|