diff --git a/dashboard/src/battery.rs b/dashboard/src/battery.rs index d79b483..9efeef4 100644 --- a/dashboard/src/battery.rs +++ b/dashboard/src/battery.rs @@ -1,6 +1,6 @@ -use std::{thread::sleep, time::Duration}; +use std::time::Duration; -use tokio::sync::mpsc::UnboundedSender; +use tokio::{sync::mpsc::UnboundedSender, time::sleep}; use crate::{error::AppError, messages::EventMessage}; @@ -12,14 +12,20 @@ pub async fn battery_listener(s2c_tx: UnboundedSender) -> Result<( .next() .ok_or_else(|| AppError::NoBattery)??; - loop { - println!("huh?"); - battery.refresh()?; + tokio::spawn({ + let s2c_tx = s2c_tx.clone(); + async move { + loop { + s2c_tx + .send(EventMessage::BatteryPercentage(battery.state_of_charge())) + .ok(); - s2c_tx - .send(EventMessage::BatteryPercentage(battery.state_of_charge())) - .ok(); + sleep(Duration::from_secs(30)).await; - sleep(Duration::from_secs(30)); - } + battery.refresh().ok(); + } + } + }); + + Ok(()) } diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index 581b3ac..5b0be0c 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -43,7 +43,12 @@ async fn main() -> Result<(), AppError> { }); // start pulling data - tokio::spawn(home_assistant::hass(s2c_tx, c2s_rx)); + tokio::spawn({ + let s2c_tx = s2c_tx.clone(); + async move { + home_assistant::hass(s2c_tx, c2s_rx).await.unwrap(); + } + }); app.run().map_err(Into::into) }