Add relay pulse functionality
This commit is contained in:
parent
e2f3d7b82a
commit
61a3c6093b
14 changed files with 201 additions and 13 deletions
emgauwa-lib/src/models
|
@ -1,3 +1,5 @@
|
|||
use std::time::Instant;
|
||||
|
||||
use chrono::NaiveTime;
|
||||
use futures::executor::block_on;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
|
@ -19,6 +21,10 @@ pub struct Relay {
|
|||
pub active_schedule: DbSchedule,
|
||||
pub is_on: Option<bool>,
|
||||
pub tags: Vec<String>,
|
||||
|
||||
// for internal use only.
|
||||
#[serde(skip)]
|
||||
pub pulsing: Option<Instant>,
|
||||
}
|
||||
|
||||
|
||||
|
@ -55,6 +61,7 @@ impl FromDbModel for Relay {
|
|||
active_schedule,
|
||||
is_on,
|
||||
tags,
|
||||
pulsing: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -83,4 +90,18 @@ impl Relay {
|
|||
pub fn get_next_time(&self, now: &NaiveTime) -> Option<NaiveTime> {
|
||||
self.active_schedule.get_next_time(now)
|
||||
}
|
||||
|
||||
pub fn check_pulsing(&mut self, now: &Instant) -> Option<Instant> {
|
||||
match self.pulsing {
|
||||
Some(dur_instant) => {
|
||||
if dur_instant.lt(now) {
|
||||
self.pulsing = None;
|
||||
None
|
||||
} else {
|
||||
Some(dur_instant)
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue