Compare commits
No commits in common. "e923ecb9d88061d2ef6f1342613315f1677551c7" and "41cc9e0622b69868eb13db843b1323337cb7a544" have entirely different histories.
e923ecb9d8
...
41cc9e0622
4 changed files with 3 additions and 74 deletions
|
@ -23,9 +23,6 @@ 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"
|
||||
|
|
|
@ -63,13 +63,12 @@ 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) || self.end.eq(&NaiveTime::MIN);
|
||||
let start_before_end = self.start.lt(&self.end);
|
||||
|
||||
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 "inverse" period around now
|
||||
(false, false, false) => true, // both before now; end before start means "inversed" 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
|
||||
|
|
65
src/lib.rs
65
src/lib.rs
|
@ -5,68 +5,3 @@ 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);
|
||||
}
|
||||
}
|
|
@ -113,9 +113,7 @@ impl Relay {
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(schedule) = self.schedules.get(weekday as usize) {
|
||||
self.active_schedule = Some(schedule.clone());
|
||||
}
|
||||
self.active_schedule = Some(self.schedules.get(weekday as usize).unwrap().clone())
|
||||
}
|
||||
|
||||
pub fn apply_state(&mut self, state: &RelayState) {
|
||||
|
|
Loading…
Reference in a new issue