Improve handling of override_schedule

This commit is contained in:
Tobias Reisinger 2024-05-28 21:17:21 +02:00
parent 473832f58a
commit 929985c64a
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 44 additions and 34 deletions

View file

@ -57,20 +57,11 @@ impl Controller {
self.relays self.relays
.iter_mut() .iter_mut()
.zip(relay_states.iter()) .zip(relay_states.iter())
.for_each(|(relay, state)| { .for_each(|(relay, state)| relay.apply_state(state));
relay.active_schedule = state.active_schedule.clone();
relay.is_on = state.is_on;
});
} }
pub fn get_relay_states(&self) -> RelayStates { pub fn get_relay_states(&self) -> RelayStates {
self.relays self.relays.iter().map(RelayState::from).collect()
.iter()
.map(|r| RelayState {
active_schedule: r.active_schedule.clone(),
is_on: r.is_on,
})
.collect()
} }
pub fn check_next_time(&mut self, now: &EmgauwaNow) -> Option<NaiveTime> { pub fn check_next_time(&mut self, now: &EmgauwaNow) -> Option<NaiveTime> {
@ -90,6 +81,8 @@ impl Controller {
.find(|r| r.r.number == relay_num) .find(|r| r.r.number == relay_num)
.ok_or(EmgauwaError::Other(String::from("Relay not found")))?; .ok_or(EmgauwaError::Other(String::from("Relay not found")))?;
log::debug!("Pulsing relay {} until {:?}", relay_num, until);
relay.pulsing = Some(until); relay.pulsing = Some(until);
Ok(()) Ok(())
} }

View file

@ -9,7 +9,7 @@ use sqlx::Sqlite;
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::FromDbModel; use crate::models::FromDbModel;
use crate::types::{EmgauwaUid, Weekday}; use crate::types::{EmgauwaUid, RelayState, Weekday};
use crate::utils; use crate::utils;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
@ -20,12 +20,7 @@ pub struct Relay {
pub controller_id: EmgauwaUid, pub controller_id: EmgauwaUid,
pub schedules: Vec<DbSchedule>, pub schedules: Vec<DbSchedule>,
pub active_schedule: DbSchedule, pub active_schedule: DbSchedule,
#[serde( pub override_schedule: Option<DbSchedule>,
default, // <- important for deserialization
skip_serializing_if = "Option::is_none", // <- important for serialization
with = "::serde_with::rust::double_option",
)]
pub override_schedule: Option<Option<DbSchedule>>,
pub is_on: Option<bool>, pub is_on: Option<bool>,
pub tags: Vec<String>, pub tags: Vec<String>,
@ -112,12 +107,12 @@ impl Relay {
} }
pub fn reload_active_schedule(&mut self, weekday: Weekday) { pub fn reload_active_schedule(&mut self, weekday: Weekday) {
if let Some((Some(schedule), schedule_weekday)) = self.unwrap_override_schedule() { if let Some(schedule) = &self.override_schedule {
if schedule_weekday == weekday { if self.override_schedule_weekday == weekday {
self.active_schedule = schedule.clone(); self.active_schedule = schedule.clone();
return; return;
} }
if schedule_weekday != weekday { if self.override_schedule_weekday != weekday {
self.override_schedule = None; self.override_schedule = None;
} }
} }
@ -125,10 +120,16 @@ impl Relay {
self.active_schedule = self.schedules.get(weekday as usize).unwrap().clone() self.active_schedule = self.schedules.get(weekday as usize).unwrap().clone()
} }
pub fn unwrap_override_schedule(&self) -> Option<(&Option<DbSchedule>, Weekday)> { pub fn apply_state(&mut self, state: &RelayState) {
if let Some(schedule) = &self.override_schedule { self.active_schedule = state.active_schedule.clone();
return Some((schedule, self.override_schedule_weekday)); self.override_schedule.clone_from(&state.override_schedule);
} self.is_on = state.is_on;
None
} }
pub fn find_and_apply_state(&mut self, stated_relays: &[Relay]) {
if let Some(stated_relay) = stated_relays.iter().find(|r| r.r.id == self.r.id) {
self.apply_state(&stated_relay.into());
}
}
} }

View file

@ -2,10 +2,12 @@ mod emgauwa_now;
mod emgauwa_uid; mod emgauwa_uid;
mod request; mod request;
mod schedule_uid; mod schedule_uid;
mod relay_state;
use actix::Message; use actix::Message;
pub use emgauwa_now::EmgauwaNow; pub use emgauwa_now::EmgauwaNow;
pub use emgauwa_uid::EmgauwaUid; pub use emgauwa_uid::EmgauwaUid;
pub use relay_state::{RelayState, RelayStates};
pub use request::*; pub use request::*;
pub use schedule_uid::ScheduleUid; pub use schedule_uid::ScheduleUid;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -16,14 +18,6 @@ use crate::models::{Controller, Relay};
pub type Weekday = i64; pub type Weekday = i64;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelayState {
pub active_schedule: DbSchedule,
pub is_on: Option<bool>
}
pub type RelayStates = Vec<RelayState>;
#[derive(Debug, Serialize, Deserialize, Message)] #[derive(Debug, Serialize, Deserialize, Message)]
#[rtype(result = "Result<(), EmgauwaError>")] #[rtype(result = "Result<(), EmgauwaError>")]
pub enum ControllerWsAction { pub enum ControllerWsAction {

22
src/types/relay_state.rs Normal file
View file

@ -0,0 +1,22 @@
use serde_derive::{Deserialize, Serialize};
use crate::db::DbSchedule;
use crate::models::Relay;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelayState {
pub active_schedule: DbSchedule,
pub override_schedule: Option<DbSchedule>,
pub is_on: Option<bool>
}
pub type RelayStates = Vec<RelayState>;
impl From<&Relay> for RelayState {
fn from(relay: &Relay) -> Self {
RelayState {
active_schedule: relay.active_schedule.clone(),
override_schedule: relay.override_schedule.clone(),
is_on: relay.is_on
}
}
}