Add is_on functions
This commit is contained in:
parent
6414083af0
commit
0460e838bc
4 changed files with 32 additions and 2 deletions
emgauwa-lib/src/models
|
@ -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<DbSchedule>,
|
||||
pub active_schedule: DbSchedule,
|
||||
pub is_on: bool,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
|
@ -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<Sqlite>,
|
||||
) -> 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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue