From 0460e838bc1155d166513e07efea2d7627cceb75 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Wed, 24 Apr 2024 03:03:21 +0200 Subject: [PATCH] Add is_on functions --- emgauwa-controller/src/relay_loop.rs | 11 +++++++++-- emgauwa-lib/src/db/model_utils.rs | 4 ++++ emgauwa-lib/src/db/schedules.rs | 5 +++++ emgauwa-lib/src/models/relay.rs | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/emgauwa-controller/src/relay_loop.rs b/emgauwa-controller/src/relay_loop.rs index e2e8356..25000fb 100644 --- a/emgauwa-controller/src/relay_loop.rs +++ b/emgauwa-controller/src/relay_loop.rs @@ -50,10 +50,17 @@ async fn run_relays(app_state: &Addr) -> Result<(), EmgauwaError> { } let mut relay_debug = String::new(); + let now = Local::now().time(); for relay in this.relays.iter() { relay_debug.push_str(&format!( - "{}: {} |", - relay.r.name, relay.active_schedule.name + "{}{}: {} ; ", + if relay.active_schedule.is_on(&now) { + "+" + } else { + "-" + }, + relay.r.name, + relay.active_schedule.name )); } log::debug!( diff --git a/emgauwa-lib/src/db/model_utils.rs b/emgauwa-lib/src/db/model_utils.rs index c3639fe..cfd46ad 100644 --- a/emgauwa-lib/src/db/model_utils.rs +++ b/emgauwa-lib/src/db/model_utils.rs @@ -50,6 +50,10 @@ impl Period { end: NaiveTime::MIN, } } + + pub fn is_on(&self, now: &NaiveTime) -> bool { + self.start.eq(&self.end) || (self.start.le(now) && self.end.gt(now)) + } } impl Type for DbPeriods { diff --git a/emgauwa-lib/src/db/schedules.rs b/emgauwa-lib/src/db/schedules.rs index 39bdb8b..db7634d 100644 --- a/emgauwa-lib/src/db/schedules.rs +++ b/emgauwa-lib/src/db/schedules.rs @@ -1,6 +1,7 @@ use std::borrow::Borrow; use std::ops::DerefMut; +use chrono::NaiveTime; use serde_derive::{Deserialize, Serialize}; use sqlx::pool::PoolConnection; use sqlx::Sqlite; @@ -193,4 +194,8 @@ impl DbSchedule { } Ok(()) } + + pub fn is_on(&self, now: &NaiveTime) -> bool { + self.periods.0.iter().any(|period| period.is_on(now)) + } } diff --git a/emgauwa-lib/src/models/relay.rs b/emgauwa-lib/src/models/relay.rs index aa6a11f..69bb099 100644 --- a/emgauwa-lib/src/models/relay.rs +++ b/emgauwa-lib/src/models/relay.rs @@ -1,3 +1,4 @@ +use chrono::{Local, NaiveTime}; use futures::executor::block_on; use serde_derive::{Deserialize, Serialize}; use sqlx::pool::PoolConnection; @@ -16,6 +17,7 @@ pub struct Relay { pub controller_id: ControllerUid, pub schedules: Vec, pub active_schedule: DbSchedule, + pub is_on: bool, pub tags: Vec, } @@ -43,12 +45,16 @@ impl FromDbModel for Relay { let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?; let active_schedule = block_on(db_model.get_active_schedule(conn))?; + let now = Local::now().time(); + let is_on = active_schedule.is_on(&now); + Ok(Relay { r: db_model, controller: cache, controller_id, schedules, active_schedule, + is_on, tags, }) } @@ -68,6 +74,14 @@ impl Relay { conn: &mut PoolConnection, ) -> Result<(), DatabaseError> { self.active_schedule = block_on(self.r.get_active_schedule(conn))?; + + let now = Local::now().time(); + self.is_on = self.active_schedule.is_on(&now); + Ok(()) } + + pub fn is_on(&self, now: &NaiveTime) -> bool { + self.active_schedule.is_on(now) + } }