diff --git a/emgauwa-controller/src/relay_loop.rs b/emgauwa-controller/src/relay_loop.rs index f3bb8cc..14d3b00 100644 --- a/emgauwa-controller/src/relay_loop.rs +++ b/emgauwa-controller/src/relay_loop.rs @@ -27,16 +27,30 @@ async fn run_relays(app_state: &Addr) -> Result<(), EmgauwaError> { let default_duration = Duration::new(10, 0); let notifier = &*app_state_get_notifier(app_state).await?; + let mut this = utils::app_state_get_this(app_state).await?; + loop { let notifier_future = notifier.notified(); pin_mut!(notifier_future); let timeout_result = timeout(default_duration, &mut notifier_future).await; let changed = timeout_result.is_ok(); + if changed { + log::debug!("Reloading controller in relay loop"); + this = utils::app_state_get_this(app_state).await?; + } + + let mut relay_debug = String::new(); + for relay in this.relays.iter() { + relay_debug.push_str(&format!( + "{}: {} |", + relay.r.name, relay.active_schedule.name + )); + } log::debug!( - "Relay loop at {} (changed: {})", + "Relay loop at {}: {}", Local::now().naive_local().time(), - changed + relay_debug ); } } diff --git a/emgauwa-lib/src/errors/database_error.rs b/emgauwa-lib/src/errors/database_error.rs index 008e69a..cd9f838 100644 --- a/emgauwa-lib/src/errors/database_error.rs +++ b/emgauwa-lib/src/errors/database_error.rs @@ -58,7 +58,7 @@ impl From<&DatabaseError> for String { } DatabaseError::MigrationError(_) => "error on running migrations", DatabaseError::Unknown(_) => "unknown error", - DatabaseError::EmptyDataInsert => "empty data was attempted to be inserted", + DatabaseError::EmptyDataInsert => "tried to insert empty data", }) } } diff --git a/emgauwa-lib/src/models/relay.rs b/emgauwa-lib/src/models/relay.rs index 979d887..f3948ec 100644 --- a/emgauwa-lib/src/models/relay.rs +++ b/emgauwa-lib/src/models/relay.rs @@ -65,6 +65,16 @@ impl FromDbModel for Relay { impl Relay { pub fn reload(&mut self, conn: &mut PoolConnection) -> Result<(), DatabaseError> { self.r = block_on(self.r.reload(conn))?; + self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?; + + let weekday = utils::get_weekday(); + self.active_schedule = block_on(DbJunctionRelaySchedule::get_schedule( + conn, + &self.r, + weekday as Weekday, + ))? + .ok_or(DatabaseError::NotFound)?; + Ok(()) } }