control loop update + tesla data update separate

This commit is contained in:
Alex Janka 2024-01-17 08:47:28 +11:00
parent 29c7dc52b3
commit a4fc60f021
2 changed files with 17 additions and 8 deletions

View file

@ -16,6 +16,7 @@ pub fn access_config<'a>() -> &'a Config {
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(default)] #[serde(default)]
pub struct Config { pub struct Config {
pub charge_rate_update_interval_seconds: u64,
pub tesla_update_interval_seconds: u64, pub tesla_update_interval_seconds: u64,
pub tesla_update_interval_while_charging_seconds: u64, pub tesla_update_interval_while_charging_seconds: u64,
pub pl_watch_interval_seconds: u64, pub pl_watch_interval_seconds: u64,
@ -35,6 +36,7 @@ pub struct Config {
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
charge_rate_update_interval_seconds: 30,
tesla_update_interval_seconds: 120, tesla_update_interval_seconds: 120,
tesla_update_interval_while_charging_seconds: 5, tesla_update_interval_while_charging_seconds: 5,
pl_watch_interval_seconds: 30, pl_watch_interval_seconds: 30,

View file

@ -124,29 +124,36 @@ async fn main() {
// spawn the api loop // spawn the api loop
tokio::task::spawn(async move { tokio::task::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs( let mut normal_data_update_interval = tokio::time::interval(
config.tesla_update_interval_seconds, std::time::Duration::from_secs(config.tesla_update_interval_seconds),
)); );
let mut charge_interval = let mut charge_data_update_interval =
tokio::time::interval(std::time::Duration::from_secs( tokio::time::interval(std::time::Duration::from_secs(
config.tesla_update_interval_while_charging_seconds, 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 { loop {
// await either the next interval OR a message from the other thread // await either the next interval OR a message from the other thread
tokio::select! { tokio::select! {
_ = interval.tick() => { _ = normal_data_update_interval.tick() => {
if !interface.state.read().unwrap().is_charging_at_home() { if !interface.state.read().unwrap().is_charging_at_home() {
interface.refresh().await 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 interface.state.read().unwrap().is_charging_at_home() {
if let Some(request) = tesla_charge_rate_controller.control_charge_rate() { if let Some(request) = tesla_charge_rate_controller.control_charge_rate() {
interface.process_request(request).await; interface.process_request(request).await;
} }
interface.refresh().await
}
} }
},
api_message = api_receiver.recv() => match api_message { api_message = api_receiver.recv() => match api_message {
Ok(message) => interface.process_request(message).await, Ok(message) => interface.process_request(message).await,
Err(e) => panic!("Error on Tesla receive channel: {e:?}") Err(e) => panic!("Error on Tesla receive channel: {e:?}")