Remove guessing of active_schedule
This commit is contained in:
parent
929985c64a
commit
ce7a79d1de
4 changed files with 17 additions and 22 deletions
|
@ -163,14 +163,4 @@ impl DbRelay {
|
||||||
.await?
|
.await?
|
||||||
.ok_or(DatabaseError::NotFound)
|
.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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ impl Controller {
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.filter_map(|r| {
|
.filter_map(|r| {
|
||||||
r.reload_active_schedule(now.weekday);
|
r.reload_active_schedule(now.weekday);
|
||||||
r.active_schedule.get_next_time(&now.time)
|
r.get_next_time(&now.time)
|
||||||
})
|
})
|
||||||
.min()
|
.min()
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ pub struct Relay {
|
||||||
pub controller: DbController,
|
pub controller: DbController,
|
||||||
pub controller_id: EmgauwaUid,
|
pub controller_id: EmgauwaUid,
|
||||||
pub schedules: Vec<DbSchedule>,
|
pub schedules: Vec<DbSchedule>,
|
||||||
pub active_schedule: DbSchedule,
|
pub active_schedule: Option<DbSchedule>,
|
||||||
pub override_schedule: Option<DbSchedule>,
|
pub override_schedule: Option<DbSchedule>,
|
||||||
pub is_on: Option<bool>,
|
pub is_on: Option<bool>,
|
||||||
pub tags: Vec<String>,
|
pub tags: Vec<String>,
|
||||||
|
@ -55,7 +55,6 @@ impl FromDbModel for Relay {
|
||||||
let controller_id = cache.uid.clone();
|
let controller_id = cache.uid.clone();
|
||||||
|
|
||||||
let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
|
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;
|
let is_on = None;
|
||||||
|
|
||||||
|
@ -64,7 +63,7 @@ impl FromDbModel for Relay {
|
||||||
controller: cache,
|
controller: cache,
|
||||||
controller_id,
|
controller_id,
|
||||||
schedules,
|
schedules,
|
||||||
active_schedule,
|
active_schedule: None,
|
||||||
override_schedule: None,
|
override_schedule: None,
|
||||||
is_on,
|
is_on,
|
||||||
tags,
|
tags,
|
||||||
|
@ -79,17 +78,23 @@ impl Relay {
|
||||||
self.r = block_on(self.r.reload(conn))?;
|
self.r = block_on(self.r.reload(conn))?;
|
||||||
self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?;
|
self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?;
|
||||||
|
|
||||||
self.reload_active_schedule(utils::get_weekday());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_on(&self, now: &NaiveTime) -> bool {
|
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> {
|
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> {
|
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) {
|
pub fn reload_active_schedule(&mut self, weekday: Weekday) {
|
||||||
if let Some(schedule) = &self.override_schedule {
|
if let Some(schedule) = &self.override_schedule {
|
||||||
if self.override_schedule_weekday == weekday {
|
if self.override_schedule_weekday == weekday {
|
||||||
self.active_schedule = schedule.clone();
|
self.active_schedule = Some(schedule.clone());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if self.override_schedule_weekday != weekday {
|
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) {
|
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.override_schedule.clone_from(&state.override_schedule);
|
||||||
self.is_on = state.is_on;
|
self.is_on = state.is_on;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::models::Relay;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct RelayState {
|
pub struct RelayState {
|
||||||
pub active_schedule: DbSchedule,
|
pub active_schedule: Option<DbSchedule>,
|
||||||
pub override_schedule: Option<DbSchedule>,
|
pub override_schedule: Option<DbSchedule>,
|
||||||
pub is_on: Option<bool>
|
pub is_on: Option<bool>
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue