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)]
#[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,

View file

@ -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:?}")