66 lines
1.7 KiB
Rust
66 lines
1.7 KiB
Rust
use crate::{config::Config, MTState};
|
|
use reqwest::blocking::*;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{thread, time};
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Root {
|
|
pub currently: Currently,
|
|
pub daily: Daily,
|
|
}
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Currently {
|
|
pub time: i64,
|
|
pub summary: String,
|
|
pub temperature: f64,
|
|
pub apparent_temperature: f64,
|
|
pub humidity: f64,
|
|
pub icon: String,
|
|
}
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Daily {
|
|
pub summary: String,
|
|
pub icon: String,
|
|
pub data: Vec<Daum>,
|
|
}
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Daum {
|
|
pub time: i64,
|
|
pub icon: String,
|
|
pub temperature_high: f64,
|
|
pub temperature_low: f64,
|
|
}
|
|
|
|
const UPDATE_FREQUENCY: u64 = 15 * 60; // 15 minutes
|
|
|
|
fn get(cfg: &Config) -> reqwest::Result<Root> {
|
|
let client = Client::builder().user_agent(crate::APP_USER_AGENT).build()?;
|
|
let now: Root = client.get(&cfg.weather_url).send()?.json()?;
|
|
|
|
Ok(now)
|
|
}
|
|
|
|
pub(crate) fn update(st: MTState, cfg: Config) {
|
|
loop {
|
|
match get(&cfg) {
|
|
Ok(datni) => {
|
|
let mut data = st.lock().unwrap();
|
|
data.weather = Some(datni);
|
|
}
|
|
Err(why) => {
|
|
log::error!("error getting weather from {}: {:?}", &cfg.weather_url, why);
|
|
}
|
|
}
|
|
|
|
let amt = time::Duration::new(UPDATE_FREQUENCY, 0);
|
|
thread::sleep(amt);
|
|
}
|
|
}
|