Improve handling of override_schedule

This commit is contained in:
Tobias Reisinger 2024-05-28 21:17:25 +02:00
parent 8244a1d837
commit 2eb38ec11e
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 19 additions and 39 deletions

View file

@ -5,7 +5,7 @@ use chrono::Timelike;
use emgauwa_common::constants::RELAYS_RETRY_TIMEOUT;
use emgauwa_common::errors::EmgauwaError;
use emgauwa_common::models::Controller;
use emgauwa_common::types::{EmgauwaNow, RelayState, RelayStates, Weekday};
use emgauwa_common::types::{EmgauwaNow, Weekday};
use emgauwa_common::utils::printable_relay_states;
use futures::pin_mut;
use tokio::time;
@ -31,11 +31,6 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
let mut last_weekday = emgauwa_common::utils::get_weekday();
let mut this = utils::app_state_get_this(app_state).await?;
let mut relay_states: RelayStates = Vec::new();
let now = EmgauwaNow::now();
init_relay_states(&mut relay_states, &this);
calc_relay_states(&mut relay_states, &mut this, app_state, &now).await?;
let mut duration_override = None;
@ -81,40 +76,26 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
})
.min();
calc_relay_states(&mut relay_states, &mut this, app_state, &now).await?;
calc_relay_states(&mut this, app_state, &now).await?;
}
}
fn init_relay_states(relay_states: &mut RelayStates, this: &Controller) {
relay_states.clear();
this.relays.iter().for_each(|r| {
relay_states.push(RelayState {
active_schedule: r.active_schedule.clone(),
is_on: None
});
});
}
async fn calc_relay_states(
relay_states: &mut RelayStates,
this: &mut Controller,
app_state: &Addr<AppState>,
now: &EmgauwaNow,
) -> Result<(), EmgauwaError> {
this.relays
.iter_mut()
.zip(relay_states.iter_mut())
.for_each(|(relay, state)| {
.for_each(|relay| {
relay.reload_active_schedule(now.weekday);
relay.is_on = Some(
relay.active_schedule.is_on(&now.time)
|| relay.check_pulsing(&now.instant).is_some(),
);
state.is_on = relay.is_on;
state.active_schedule = relay.active_schedule.clone();
});
utils::app_state_update_relays_on(app_state, relay_states.clone()).await
let relay_states = this.get_relay_states();
utils::app_state_update_relay_states(app_state, relay_states.clone()).await
}
fn get_next_duration(