Add relay pulse functionality

This commit is contained in:
Tobias Reisinger 2024-04-26 16:15:24 +02:00
parent e2f3d7b82a
commit 61a3c6093b
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
14 changed files with 201 additions and 13 deletions

View file

@ -6,3 +6,5 @@ pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(15);
pub const WEBSOCKET_RETRY_TIMEOUT: Duration = Duration::from_secs(5);
pub const RELAYS_RETRY_TIMEOUT: Duration = Duration::from_secs(5);
pub const RELAY_PULSE_DURATION: u64 = 3;

View file

@ -65,13 +65,13 @@ impl Period {
let end_after_now = self.end.gt(now);
let start_before_end = self.start.lt(&self.end);
return match (start_after_now, end_after_now, start_before_end) {
match (start_after_now, end_after_now, start_before_end) {
(false, false, _) => None, // both before now
(true, false, _) => Some(self.start), // only start after now
(false, true, _) => Some(self.end), // only end after now
(true, true, true) => Some(self.start), // both after now but start first
(true, true, false) => Some(self.end), // both after now but end first
};
}
}
}

View file

@ -1,3 +1,5 @@
use std::time::Instant;
use actix::MessageResponse;
use chrono::NaiveTime;
use futures::executor::block_on;
@ -70,4 +72,15 @@ impl Controller {
.filter_map(|r| r.active_schedule.get_next_time(now))
.min()
}
pub fn relay_pulse(&mut self, relay_num: i64, until: Instant) -> Result<(), EmgauwaError> {
let relay = self
.relays
.iter_mut()
.find(|r| r.r.number == relay_num)
.ok_or(EmgauwaError::Other(String::from("Relay not found")))?;
relay.pulsing = Some(until);
Ok(())
}
}

View file

@ -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,
}
}
}

View file

@ -25,4 +25,5 @@ pub enum ControllerWsAction {
Relays(Vec<Relay>),
Controller(Controller),
RelayStates((ControllerUid, RelayStates)),
RelayPulse((i64, Option<u32>)),
}

View file

@ -28,6 +28,11 @@ pub struct RequestRelayUpdate {
pub tags: Option<Vec<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RequestRelayPulse {
pub duration: Option<u32>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RequestScheduleId {
pub id: ScheduleUid,