Add relay pulse functionality
This commit is contained in:
		
							parent
							
								
									e2f3d7b82a
								
							
						
					
					
						commit
						61a3c6093b
					
				
					 14 changed files with 201 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
		};
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,4 +25,5 @@ pub enum ControllerWsAction {
 | 
			
		|||
	Relays(Vec<Relay>),
 | 
			
		||||
	Controller(Controller),
 | 
			
		||||
	RelayStates((ControllerUid, RelayStates)),
 | 
			
		||||
	RelayPulse((i64, Option<u32>)),
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue