Add basic exchange of relay states
This commit is contained in:
parent
0460e838bc
commit
8a83602d6a
10 changed files with 153 additions and 41 deletions
emgauwa-controller/src
|
@ -4,10 +4,11 @@ use actix::Addr;
|
|||
use chrono::Local;
|
||||
use emgauwa_lib::constants::RELAYS_RETRY_TIMEOUT;
|
||||
use emgauwa_lib::errors::EmgauwaError;
|
||||
use emgauwa_lib::models::Controller;
|
||||
use futures::pin_mut;
|
||||
use tokio::time;
|
||||
use tokio::time::timeout;
|
||||
use utils::app_state_get_notifier;
|
||||
use utils::app_state_get_controller_notifier;
|
||||
|
||||
use crate::app_state::AppState;
|
||||
use crate::utils;
|
||||
|
@ -25,10 +26,12 @@ pub async fn run_relays_loop(app_state: Addr<AppState>) {
|
|||
|
||||
async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
|
||||
let default_duration = Duration::new(10, 0);
|
||||
let notifier = &*app_state_get_notifier(app_state).await?;
|
||||
let notifier = &*app_state_get_controller_notifier(app_state).await?;
|
||||
|
||||
let mut last_weekday = emgauwa_lib::utils::get_weekday();
|
||||
let mut this = utils::app_state_get_this(app_state).await?;
|
||||
let mut relay_states: Vec<Option<bool>> = Vec::new();
|
||||
init_relay_states(&mut relay_states, &this);
|
||||
|
||||
loop {
|
||||
let notifier_future = notifier.notified();
|
||||
|
@ -51,22 +54,35 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
|
|||
|
||||
let mut relay_debug = String::new();
|
||||
let now = Local::now().time();
|
||||
for relay in this.relays.iter() {
|
||||
relay_debug.push_str(&format!(
|
||||
"{}{}: {} ; ",
|
||||
if relay.active_schedule.is_on(&now) {
|
||||
"+"
|
||||
} else {
|
||||
"-"
|
||||
},
|
||||
relay.r.name,
|
||||
relay.active_schedule.name
|
||||
));
|
||||
}
|
||||
this.relays
|
||||
.iter()
|
||||
.zip(relay_states.iter_mut())
|
||||
.for_each(|(relay, state)| {
|
||||
*state = Some(relay.active_schedule.is_on(&now));
|
||||
|
||||
relay_debug.push_str(&format!(
|
||||
"{}{}: {} ; ",
|
||||
if relay.active_schedule.is_on(&now) {
|
||||
"+"
|
||||
} else {
|
||||
"-"
|
||||
},
|
||||
relay.r.name,
|
||||
relay.active_schedule.name
|
||||
));
|
||||
});
|
||||
log::debug!(
|
||||
"Relay loop at {}: {}",
|
||||
Local::now().naive_local().time(),
|
||||
relay_debug
|
||||
);
|
||||
utils::app_state_update_relays_on(app_state, relay_states.clone()).await?;
|
||||
}
|
||||
}
|
||||
|
||||
fn init_relay_states(relay_states: &mut Vec<Option<bool>>, this: &Controller) {
|
||||
relay_states.clear();
|
||||
for _ in 0..this.c.relay_count {
|
||||
relay_states.push(None);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue