2023-11-27 14:09:14 +00:00
|
|
|
use std::time::Duration;
|
2023-11-29 13:27:46 +00:00
|
|
|
|
2023-12-07 03:30:33 +00:00
|
|
|
use actix::Addr;
|
2024-04-25 22:00:42 +00:00
|
|
|
use chrono::{Local, Timelike};
|
2023-12-07 03:30:33 +00:00
|
|
|
use emgauwa_lib::constants::RELAYS_RETRY_TIMEOUT;
|
|
|
|
use emgauwa_lib::errors::EmgauwaError;
|
2024-04-24 23:26:53 +00:00
|
|
|
use emgauwa_lib::models::Controller;
|
2024-04-25 22:00:42 +00:00
|
|
|
use emgauwa_lib::types::{RelayStates, Weekday};
|
|
|
|
use emgauwa_lib::utils::printable_relay_states;
|
2023-12-07 03:30:33 +00:00
|
|
|
use futures::pin_mut;
|
2023-11-27 14:09:14 +00:00
|
|
|
use tokio::time;
|
2023-12-07 03:30:33 +00:00
|
|
|
use tokio::time::timeout;
|
2024-04-24 23:26:53 +00:00
|
|
|
use utils::app_state_get_controller_notifier;
|
2023-11-25 23:54:03 +00:00
|
|
|
|
2023-12-07 03:30:33 +00:00
|
|
|
use crate::app_state::AppState;
|
|
|
|
use crate::utils;
|
2023-11-29 13:27:46 +00:00
|
|
|
|
2023-12-07 03:30:33 +00:00
|
|
|
pub async fn run_relays_loop(app_state: Addr<AppState>) {
|
|
|
|
log::debug!("Spawned relays loop");
|
2023-11-27 14:09:14 +00:00
|
|
|
loop {
|
2023-12-07 03:30:33 +00:00
|
|
|
let run_result = run_relays(&app_state).await;
|
|
|
|
if let Err(err) = run_result {
|
|
|
|
log::error!("Error running relays: {}", err);
|
|
|
|
}
|
|
|
|
time::sleep(RELAYS_RETRY_TIMEOUT).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
|
2024-04-24 23:26:53 +00:00
|
|
|
let notifier = &*app_state_get_controller_notifier(app_state).await?;
|
2023-12-07 03:30:33 +00:00
|
|
|
|
2024-04-24 00:29:53 +00:00
|
|
|
let mut last_weekday = emgauwa_lib::utils::get_weekday();
|
2024-04-23 23:29:07 +00:00
|
|
|
let mut this = utils::app_state_get_this(app_state).await?;
|
2024-04-25 12:10:32 +00:00
|
|
|
let mut relay_states: RelayStates = Vec::new();
|
2024-04-24 23:26:53 +00:00
|
|
|
init_relay_states(&mut relay_states, &this);
|
2024-04-25 22:00:42 +00:00
|
|
|
calc_relay_states(&mut relay_states, &mut this, app_state).await?;
|
2024-04-23 23:29:07 +00:00
|
|
|
|
2023-12-07 03:30:33 +00:00
|
|
|
loop {
|
2024-04-25 22:00:42 +00:00
|
|
|
log::debug!(
|
|
|
|
"Relay loop at {}: {}",
|
|
|
|
Local::now().naive_local().time(),
|
|
|
|
printable_relay_states(&this.get_relay_states())
|
|
|
|
);
|
|
|
|
|
2023-12-07 03:30:33 +00:00
|
|
|
let notifier_future = notifier.notified();
|
|
|
|
pin_mut!(notifier_future);
|
2024-04-25 22:00:42 +00:00
|
|
|
let mut changed = timeout(get_next_duration(&this), &mut notifier_future)
|
|
|
|
.await
|
|
|
|
.is_ok();
|
|
|
|
|
|
|
|
check_weekday(app_state, &mut last_weekday, &mut changed).await?;
|
2023-12-07 03:30:33 +00:00
|
|
|
|
2024-04-23 23:29:07 +00:00
|
|
|
if changed {
|
|
|
|
log::debug!("Reloading controller in relay loop");
|
|
|
|
this = utils::app_state_get_this(app_state).await?;
|
|
|
|
}
|
|
|
|
|
2024-04-25 22:00:42 +00:00
|
|
|
calc_relay_states(&mut relay_states, &mut this, app_state).await?;
|
2024-04-24 23:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-25 12:10:32 +00:00
|
|
|
fn init_relay_states(relay_states: &mut RelayStates, this: &Controller) {
|
2024-04-24 23:26:53 +00:00
|
|
|
relay_states.clear();
|
|
|
|
for _ in 0..this.c.relay_count {
|
|
|
|
relay_states.push(None);
|
2023-11-27 14:09:14 +00:00
|
|
|
}
|
2023-11-27 11:49:40 +00:00
|
|
|
}
|
2024-04-25 22:00:42 +00:00
|
|
|
|
|
|
|
async fn calc_relay_states(
|
|
|
|
relay_states: &mut RelayStates,
|
|
|
|
this: &mut Controller,
|
|
|
|
app_state: &Addr<AppState>,
|
|
|
|
) -> Result<(), EmgauwaError> {
|
|
|
|
let now = Local::now().time();
|
|
|
|
|
|
|
|
this.relays
|
|
|
|
.iter_mut()
|
|
|
|
.zip(relay_states.iter_mut())
|
|
|
|
.for_each(|(relay, state)| {
|
|
|
|
relay.is_on = Some(relay.active_schedule.is_on(&now));
|
|
|
|
*state = relay.is_on;
|
|
|
|
});
|
|
|
|
utils::app_state_update_relays_on(app_state, relay_states.clone()).await
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_next_duration(this: &Controller) -> Duration {
|
|
|
|
let now = Local::now().time();
|
|
|
|
let now_in_s = now.num_seconds_from_midnight();
|
|
|
|
let next_timestamp = this
|
|
|
|
.get_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);
|
|
|
|
|
|
|
|
log::debug!(
|
|
|
|
"Next timestamp: {}; Waiting for {}s",
|
|
|
|
next_timestamp,
|
|
|
|
duration_to_next.as_secs()
|
|
|
|
);
|
|
|
|
|
|
|
|
duration_to_next
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn check_weekday(
|
|
|
|
app_state: &Addr<AppState>,
|
|
|
|
last_weekday: &mut Weekday,
|
|
|
|
changed: &mut bool,
|
|
|
|
) -> Result<(), EmgauwaError> {
|
|
|
|
let current_weekday = emgauwa_lib::utils::get_weekday();
|
|
|
|
if current_weekday.ne(last_weekday) {
|
|
|
|
log::debug!("Weekday changed");
|
|
|
|
*last_weekday = current_weekday;
|
|
|
|
utils::app_state_reload(app_state).await?;
|
|
|
|
*changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|