Remove guessing of active_schedule

This commit is contained in:
Tobias Reisinger 2024-05-29 15:51:53 +02:00
parent 929985c64a
commit ce7a79d1de
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 17 additions and 22 deletions

View file

@ -163,14 +163,4 @@ impl DbRelay {
.await?
.ok_or(DatabaseError::NotFound)
}
pub async fn get_active_schedule(
&self,
conn: &mut PoolConnection<Sqlite>,
) -> Result<DbSchedule, DatabaseError> {
let weekday = utils::get_weekday();
DbJunctionRelaySchedule::get_schedule(conn, self, weekday as Weekday)
.await?
.ok_or(DatabaseError::NotFound)
}
}

View file

@ -69,7 +69,7 @@ impl Controller {
.iter_mut()
.filter_map(|r| {
r.reload_active_schedule(now.weekday);
r.active_schedule.get_next_time(&now.time)
r.get_next_time(&now.time)
})
.min()
}

View file

@ -19,7 +19,7 @@ pub struct Relay {
pub controller: DbController,
pub controller_id: EmgauwaUid,
pub schedules: Vec<DbSchedule>,
pub active_schedule: DbSchedule,
pub active_schedule: Option<DbSchedule>,
pub override_schedule: Option<DbSchedule>,
pub is_on: Option<bool>,
pub tags: Vec<String>,
@ -55,7 +55,6 @@ impl FromDbModel for Relay {
let controller_id = cache.uid.clone();
let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
let active_schedule = block_on(db_model.get_active_schedule(conn))?;
let is_on = None;
@ -64,7 +63,7 @@ impl FromDbModel for Relay {
controller: cache,
controller_id,
schedules,
active_schedule,
active_schedule: None,
override_schedule: None,
is_on,
tags,
@ -79,17 +78,23 @@ impl Relay {
self.r = block_on(self.r.reload(conn))?;
self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?;
self.reload_active_schedule(utils::get_weekday());
Ok(())
}
pub fn is_on(&self, now: &NaiveTime) -> bool {
self.active_schedule.is_on(now)
if let Some(active_schedule) = &self.active_schedule {
active_schedule.is_on(now)
} else {
false
}
}
pub fn get_next_time(&self, now: &NaiveTime) -> Option<NaiveTime> {
self.active_schedule.get_next_time(now)
if let Some(active_schedule) = &self.active_schedule {
active_schedule.get_next_time(now)
} else {
None
}
}
pub fn check_pulsing(&mut self, now: &Instant) -> Option<Instant> {
@ -109,7 +114,7 @@ impl Relay {
pub fn reload_active_schedule(&mut self, weekday: Weekday) {
if let Some(schedule) = &self.override_schedule {
if self.override_schedule_weekday == weekday {
self.active_schedule = schedule.clone();
self.active_schedule = Some(schedule.clone());
return;
}
if self.override_schedule_weekday != weekday {
@ -117,11 +122,11 @@ impl Relay {
}
}
self.active_schedule = self.schedules.get(weekday as usize).unwrap().clone()
self.active_schedule = Some(self.schedules.get(weekday as usize).unwrap().clone())
}
pub fn apply_state(&mut self, state: &RelayState) {
self.active_schedule = state.active_schedule.clone();
self.active_schedule.clone_from(&state.active_schedule);
self.override_schedule.clone_from(&state.override_schedule);
self.is_on = state.is_on;
}

View file

@ -4,7 +4,7 @@ use crate::models::Relay;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelayState {
pub active_schedule: DbSchedule,
pub active_schedule: Option<DbSchedule>,
pub override_schedule: Option<DbSchedule>,
pub is_on: Option<bool>
}