changes
This commit is contained in:
@@ -22,7 +22,7 @@ impl IntoSubscription for Service {
|
||||
fn into_subscription(self, inbox: Inbox) -> Subscription<Wire> {
|
||||
match self.0 {
|
||||
key if key.starts_with(datetime::NAMESPACE) => datetime::ClockTicker::run(inbox),
|
||||
key if key.starts_with(weather::NAMESPACE) => datetime::ClockTicker::run(inbox),
|
||||
key if key.starts_with(weather::NAMESPACE) => weather::WeatherService::run(inbox),
|
||||
_ => Subscription::none(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,53 @@
|
||||
pub struct WeatherService {}
|
||||
use std::time::Duration;
|
||||
|
||||
use common::{Inbox, WeatherResponse, Wire};
|
||||
use iced::{futures::channel::mpsc, Subscription};
|
||||
use tokio::time::interval;
|
||||
|
||||
pub const NAMESPACE: &str = "weather.";
|
||||
|
||||
const FORECAST_URL: &str = "https://api.open-meteo.com/v1/forecast\
|
||||
?latitude=51.3714&longitude=-0.2302\
|
||||
&hourly=temperature_2m,rain,showers,precipitation,relative_humidity_2m";
|
||||
|
||||
pub struct WeatherService {
|
||||
client: reqwest::Client,
|
||||
latest: Option<WeatherResponse>,
|
||||
}
|
||||
|
||||
impl WeatherService {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
client: reqwest::Client::default(),
|
||||
latest: None,
|
||||
}
|
||||
}
|
||||
pub fn run(inbox: Inbox) -> Subscription<Wire> {
|
||||
Subscription::run_with(inbox, |inbox| {
|
||||
let _key = inbox.key();
|
||||
let mut _rx = inbox.take().expect("cant take");
|
||||
|
||||
iced::stream::channel(0, move |mut _sender: mpsc::Sender<Wire>| async move {
|
||||
let mut weather = WeatherService::new();
|
||||
let mut interval = interval(Duration::from_mins(30));
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = interval.tick() => {
|
||||
weather.latest = weather.get_weather().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
async fn get_weather(&self) -> Option<WeatherResponse> {
|
||||
self.client
|
||||
.get(FORECAST_URL)
|
||||
.send()
|
||||
.await
|
||||
.ok()?
|
||||
.json::<WeatherResponse>()
|
||||
.await
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user