Compare commits

..

2 commits

4 changed files with 74 additions and 3 deletions

View file

@ -23,6 +23,9 @@ pub use relays::DbRelay;
pub use schedules::{DbPeriods, DbSchedule};
pub use tag::DbTag;
#[cfg(test)]
pub(crate) use model_utils::Period;
use crate::errors::{DatabaseError, EmgauwaError};
static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations"

View file

@ -63,12 +63,13 @@ impl Period {
let start_after_now = self.start.gt(now);
// add check for end time being 00:00 because end being 00:00 would cause end_after_now to always be false
// this will handle end like 24:00 and end_after_now will be true
// same for start_before_end
let end_after_now = self.end.gt(now) || self.end.eq(&NaiveTime::MIN);
let start_before_end = self.start.lt(&self.end);
let start_before_end = self.start.lt(&self.end) || self.end.eq(&NaiveTime::MIN);
match (start_after_now, end_after_now, start_before_end) {
(false, false, true) => false, // both before now; start before end means "normal" period before now
(false, false, false) => true, // both before now; end before start means "inversed" period around now
(false, false, false) => true, // both before now; end before start means "inverse" period around now
(true, false, _) => false, // only start after now
(false, true, _) => true, // only end after now
(true, true, true) => false, // both after now but start first

View file

@ -5,3 +5,68 @@ pub mod models;
pub mod settings;
pub mod types;
pub mod utils;
#[cfg(test)]
mod periods {
use chrono::NaiveTime;
use crate::db::Period;
use crate::types::EmgauwaNow;
const MIDNIGHT: NaiveTime = NaiveTime::MIN;
fn new_time(hour: u32, minute: u32) -> NaiveTime {
NaiveTime::from_hms_opt(hour, minute, 0).expect("Failed to create NaiveTime")
}
fn new_period(start_hour: u32, start_minute: u32, end_hour: u32, end_minute: u32) -> Period {
Period {
start: new_time(start_hour, start_minute),
end: new_time(end_hour, end_minute),
}
}
#[test]
fn always_on() {
let period = Period::new_on();
let now: EmgauwaNow = EmgauwaNow::now(&MIDNIGHT);
assert_eq!(period.is_always_on(), true);
assert_eq!(period.is_on(&MIDNIGHT), true);
assert_eq!(period.is_on(&new_time(12, 00)), true);
assert_eq!(period.is_on(&now.time), true);
}
#[test]
fn simple_period() {
let period = new_period(11, 00, 13, 00);
assert_eq!(period.is_always_on(), false);
assert_eq!(period.is_on(&MIDNIGHT), false);
assert_eq!(period.is_on(&new_time(10, 00)), false);
assert_eq!(period.is_on(&new_time(11, 00)), true);
assert_eq!(period.is_on(&new_time(12, 00)), true);
assert_eq!(period.is_on(&new_time(13, 00)), false);
assert_eq!(period.is_on(&new_time(14, 00)), false);
}
#[test]
fn to_midnight_period() {
let period = new_period(22, 00, 00, 00);
assert_eq!(period.is_always_on(), false);
assert_eq!(period.is_on(&MIDNIGHT), false);
assert_eq!(period.is_on(&new_time(21, 00)), false);
assert_eq!(period.is_on(&new_time(22, 00)), true);
assert_eq!(period.is_on(&new_time(23, 00)), true);
assert_eq!(period.is_on(&new_time(00, 00)), false);
assert_eq!(period.is_on(&new_time(01, 00)), false);
}
#[test]
fn from_midnight_period() {
let period = new_period(00, 00, 02, 00);
assert_eq!(period.is_always_on(), false);
assert_eq!(period.is_on(&MIDNIGHT), true);
assert_eq!(period.is_on(&new_time(23, 00)), false);
assert_eq!(period.is_on(&new_time(00, 00)), true);
assert_eq!(period.is_on(&new_time(01, 00)), true);
assert_eq!(period.is_on(&new_time(02, 00)), false);
assert_eq!(period.is_on(&new_time(03, 00)), false);
}
}

View file

@ -113,7 +113,9 @@ impl Relay {
}
}
self.active_schedule = Some(self.schedules.get(weekday as usize).unwrap().clone())
if let Some(schedule) = self.schedules.get(weekday as usize) {
self.active_schedule = Some(schedule.clone());
}
}
pub fn apply_state(&mut self, state: &RelayState) {