Make the application update every now and then

This commit is contained in:
Savanni D'Gerinel 2023-08-10 09:11:05 -04:00
parent 63884a32f6
commit 6a6dc3dc1e
2 changed files with 24 additions and 13 deletions

View File

@ -94,21 +94,22 @@ pub fn main() {
async move { async move {
let soluna_client = SolunaClient::new(); let soluna_client = SolunaClient::new();
let transit = soluna_client
.request(latitude, longitude, Local::now())
.await;
let now = Local::now();
let state = State {
date: IFC::from(now.date_naive().with_year(12023).unwrap()),
next_event: EVENTS.next_event(now.with_timezone(&Utc)).unwrap(),
transit: Some(transit),
};
loop { loop {
let transit = soluna_client
.request(latitude.clone(), longitude.clone(), Local::now())
.await;
let now = Local::now();
let state = State {
date: IFC::from(now.date_naive().with_year(12023).unwrap()),
next_event: EVENTS.next_event(now.with_timezone(&Utc)).unwrap(),
transit: Some(transit),
};
if let Some(ref gtk_tx) = *core.tx.read().unwrap() { if let Some(ref gtk_tx) = *core.tx.read().unwrap() {
let _ = gtk_tx.send(Message::Refresh(state.clone())); let _ = gtk_tx.send(Message::Refresh(state.clone()));
} }
std::thread::sleep(std::time::Duration::from_secs(300)); std::thread::sleep(std::time::Duration::from_secs(60));
} }
} }
}); });

View File

@ -1,7 +1,7 @@
// 41.78, -71.41 // 41.78, -71.41
// https://api.solunar.org/solunar/41.78,-71.41,20211029,-4 // https://api.solunar.org/solunar/41.78,-71.41,20211029,-4
use chrono::{DateTime, Duration, NaiveTime, Offset, TimeZone, Utc}; use chrono::{DateTime, Duration, Local, NaiveTime, Offset, TimeZone, Timelike, Utc};
use geo_types::{Latitude, Longitude}; use geo_types::{Latitude, Longitude};
use memorycache::MemoryCache; use memorycache::MemoryCache;
use reqwest; use reqwest;
@ -111,7 +111,17 @@ impl SolunaClient {
.and_then(|header| header.to_str().ok()) .and_then(|header| header.to_str().ok())
.and_then(|expiration| DateTime::parse_from_rfc2822(expiration).ok()) .and_then(|expiration| DateTime::parse_from_rfc2822(expiration).ok())
.map(|dt_local| DateTime::<Utc>::from(dt_local)) .map(|dt_local| DateTime::<Utc>::from(dt_local))
.unwrap_or(Utc::now() + Duration::seconds(3600)); .unwrap_or(
Local::now()
.with_hour(0)
.and_then(|dt| dt.with_minute(0))
.and_then(|dt| dt.with_second(0))
.and_then(|dt| dt.with_nanosecond(0))
.map(|dt| dt.with_timezone(&Utc))
.unwrap()
+ Duration::days(1),
);
println!("expiration: {:?}", expiration);
let soluna: SunMoonJs = response.json().await.unwrap(); let soluna: SunMoonJs = response.json().await.unwrap();
(expiration, soluna) (expiration, soluna)
}) })