Rename active_schedule to override_schedule and add EmgauwaNow

This commit is contained in:
Tobias Reisinger 2024-05-26 22:47:56 +02:00
parent bf3192ec65
commit 8244a1d837
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 228 additions and 151 deletions

View file

@ -1,11 +1,11 @@
use std::time::{Duration, Instant};
use std::time::Duration;
use actix::Addr;
use chrono::{Local, Timelike};
use chrono::Timelike;
use emgauwa_common::constants::RELAYS_RETRY_TIMEOUT;
use emgauwa_common::errors::EmgauwaError;
use emgauwa_common::models::Controller;
use emgauwa_common::types::{RelayStates, Weekday};
use emgauwa_common::types::{EmgauwaNow, RelayState, RelayStates, Weekday};
use emgauwa_common::utils::printable_relay_states;
use futures::pin_mut;
use tokio::time;
@ -32,42 +32,44 @@ 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).await?;
calc_relay_states(&mut relay_states, &mut this, app_state, &now).await?;
let mut duration_override = None;
loop {
let now = EmgauwaNow::now();
log::debug!(
"Relay loop at {}: {}",
Local::now().naive_local().time(),
now.time,
printable_relay_states(&this.get_relay_states())
);
let notifier_future = notifier.notified();
pin_mut!(notifier_future);
let mut changed = timeout(
get_next_duration(&this, &mut duration_override),
get_next_duration(&mut this, &mut duration_override, &now),
&mut notifier_future,
)
.await
.is_ok();
check_weekday(app_state, &mut last_weekday, &mut changed).await?;
check_weekday(app_state, &mut last_weekday, &mut changed, &now).await?;
if changed {
log::debug!("Reloading controller in relay loop");
this = utils::app_state_get_this(app_state).await?;
}
let now_pulse = Instant::now();
duration_override = this
.relays
.iter_mut()
.filter_map(|relay| match relay.check_pulsing(&now_pulse) {
.filter_map(|relay| match relay.check_pulsing(&now.instant) {
None => None,
Some(pulse) => {
let dur = pulse - now_pulse;
let dur = pulse - now.instant;
log::debug!(
"Pulsing relay {} for {}s until {:?} ",
relay.r.number,
@ -79,50 +81,57 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
})
.min();
calc_relay_states(&mut relay_states, &mut this, app_state).await?;
calc_relay_states(&mut relay_states, &mut this, app_state, &now).await?;
}
}
fn init_relay_states(relay_states: &mut RelayStates, this: &Controller) {
relay_states.clear();
for _ in 0..this.c.relay_count {
relay_states.push(None);
}
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> {
let now = Local::now().time();
let now_pulse = Instant::now();
this.relays
.iter_mut()
.zip(relay_states.iter_mut())
.for_each(|(relay, state)| {
relay.reload_active_schedule(now.weekday);
relay.is_on = Some(
relay.active_schedule.is_on(&now) || relay.check_pulsing(&now_pulse).is_some(),
relay.active_schedule.is_on(&now.time)
|| relay.check_pulsing(&now.instant).is_some(),
);
*state = relay.is_on;
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
}
fn get_next_duration(this: &Controller, duration_override: &mut Option<Duration>) -> Duration {
fn get_next_duration(
this: &mut Controller,
duration_override: &mut Option<Duration>,
now: &EmgauwaNow,
) -> Duration {
if let Some(duration) = duration_override {
log::debug!("Duration override. Waiting for {}s", duration.as_secs());
return *duration;
}
let now = Local::now().time();
let now_in_s = now.num_seconds_from_midnight();
let next_timestamp = this
.get_next_time(&now)
.check_next_time(now)
.map_or(86400, |t| t.num_seconds_from_midnight());
let duration_to_next = Duration::from_secs((next_timestamp - now_in_s) as u64);
let duration_to_next = Duration::from_secs((next_timestamp - now.time_in_s()) as u64);
log::debug!(
"Next timestamp: {}; Waiting for {}s",
@ -137,8 +146,9 @@ async fn check_weekday(
app_state: &Addr<AppState>,
last_weekday: &mut Weekday,
changed: &mut bool,
now: &EmgauwaNow,
) -> Result<(), EmgauwaError> {
let current_weekday = emgauwa_common::utils::get_weekday();
let current_weekday = now.weekday;
if current_weekday.ne(last_weekday) {
log::debug!("Weekday changed");
*last_weekday = current_weekday;