use anyhow::Result; use cabytcini::*; use chrono::prelude::*; use std::{ sync::{Arc, Mutex}, thread, time, }; const UPDATE_FREQUENCY: u64 = 15000; fn main() -> Result<()> { env_logger::init(); println!(r#"{{ "version": 1 }}"#); println!("[[]"); let st: State = State::init(); let mtst = Arc::new(Mutex::new(st)); let cfg = config::load()?; // start front thread { let mtst = mtst.clone(); let cfg = cfg.clone(); thread::spawn(move || front::update(mtst, cfg)); } // start weather thread { let mtst = mtst.clone(); let cfg = cfg.clone(); thread::spawn(move || weather::update(mtst, cfg)); } thread::sleep(time::Duration::from_millis(750)); loop { { let data = mtst.lock().unwrap(); let mut blocks: Vec = vec![Block { full_text: data.front.clone(), color: match data.front.as_str() { "Cadey" => Some("#F86AF3".to_string()), "Nicole" => Some("#C79DD7".to_string()), "Jessie" => Some("#97871A".to_string()), "Ashe" => Some("#458588".to_string()), "Sephie" => Some("#D79921".to_string()), "Mai" => Some("#D65D0E".to_string()), _ => None, }, }]; if let Some(datni) = &data.weather { blocks.push(Block { full_text: format!( "{} {} / {} {}", datni.currently.temperature, datni.currently.summary, datni.daily.data[0].temperature_high, datni.daily.data[0].temperature_low ), color: None, }) } blocks.push(Block { full_text: Local::now().format("%H:%M M%m %-d %a").to_string(), color: None, }); println!(",{}", serde_json::to_string(&blocks)?); } let so_often = time::Duration::from_millis(UPDATE_FREQUENCY); thread::sleep(so_often); } }