From a4fc60f021c8d0f7cfd8f57e33e25d9d4ca67434 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Wed, 17 Jan 2024 08:47:28 +1100 Subject: [PATCH] control loop update + tesla data update separate --- src/config.rs | 2 ++ src/main.rs | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6f2f2d7..3aff2f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,7 @@ pub fn access_config<'a>() -> &'a Config { #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(default)] pub struct Config { + pub charge_rate_update_interval_seconds: u64, pub tesla_update_interval_seconds: u64, pub tesla_update_interval_while_charging_seconds: u64, pub pl_watch_interval_seconds: u64, @@ -35,6 +36,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { Self { + charge_rate_update_interval_seconds: 30, tesla_update_interval_seconds: 120, tesla_update_interval_while_charging_seconds: 5, pl_watch_interval_seconds: 30, diff --git a/src/main.rs b/src/main.rs index 56b1f75..7f4021b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,29 +124,36 @@ async fn main() { // spawn the api loop tokio::task::spawn(async move { - let mut interval = tokio::time::interval(std::time::Duration::from_secs( - config.tesla_update_interval_seconds, - )); - let mut charge_interval = + let mut normal_data_update_interval = tokio::time::interval( + std::time::Duration::from_secs(config.tesla_update_interval_seconds), + ); + let mut charge_data_update_interval = tokio::time::interval(std::time::Duration::from_secs( config.tesla_update_interval_while_charging_seconds, )); + let mut charge_rate_update_interval = tokio::time::interval( + std::time::Duration::from_secs(config.charge_rate_update_interval_seconds), + ); loop { // await either the next interval OR a message from the other thread tokio::select! { - _ = interval.tick() => { + _ = normal_data_update_interval.tick() => { if !interface.state.read().unwrap().is_charging_at_home() { interface.refresh().await } }, - _ = charge_interval.tick() => { + _ = charge_data_update_interval.tick() => { + if interface.state.read().unwrap().is_charging_at_home() { + interface.refresh().await + } + }, + _ = charge_rate_update_interval.tick() => { if interface.state.read().unwrap().is_charging_at_home() { if let Some(request) = tesla_charge_rate_controller.control_charge_rate() { interface.process_request(request).await; } - interface.refresh().await } - } + }, api_message = api_receiver.recv() => match api_message { Ok(message) => interface.process_request(message).await, Err(e) => panic!("Error on Tesla receive channel: {e:?}")