Add handler for relay states
This commit is contained in:
		
							parent
							
								
									82f2d49dc6
								
							
						
					
					
						commit
						55617dbd7c
					
				
					 8 changed files with 53 additions and 20 deletions
				
			
		| 
						 | 
				
			
			@ -7,6 +7,7 @@ use sqlx::Sqlite;
 | 
			
		|||
use crate::db::DbController;
 | 
			
		||||
use crate::errors::{DatabaseError, EmgauwaError};
 | 
			
		||||
use crate::models::{convert_db_list_cache, FromDbModel, Relay};
 | 
			
		||||
use crate::types::RelayStates;
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone, MessageResponse)]
 | 
			
		||||
pub struct Controller {
 | 
			
		||||
| 
						 | 
				
			
			@ -48,4 +49,17 @@ impl Controller {
 | 
			
		|||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn apply_relay_states(&mut self, relay_states: &RelayStates) {
 | 
			
		||||
		self.relays
 | 
			
		||||
			.iter_mut()
 | 
			
		||||
			.zip(relay_states.iter())
 | 
			
		||||
			.for_each(|(relay, is_on)| {
 | 
			
		||||
				relay.is_on = *is_on;
 | 
			
		||||
			});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn get_relay_states(&self) -> RelayStates {
 | 
			
		||||
		self.relays.iter().map(|r| r.is_on).collect()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,6 +14,8 @@ use crate::models::{Controller, Relay};
 | 
			
		|||
 | 
			
		||||
pub type Weekday = i64;
 | 
			
		||||
 | 
			
		||||
pub type RelayStates = Vec<Option<bool>>;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize, Message)]
 | 
			
		||||
#[rtype(result = "Result<(), EmgauwaError>")]
 | 
			
		||||
pub enum ControllerWsAction {
 | 
			
		||||
| 
						 | 
				
			
			@ -22,4 +24,5 @@ pub enum ControllerWsAction {
 | 
			
		|||
	Schedules(Vec<DbSchedule>),
 | 
			
		||||
	Relays(Vec<Relay>),
 | 
			
		||||
	Controller(Controller),
 | 
			
		||||
	RelayStates((ControllerUid, RelayStates)),
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue