Add is_on functions
This commit is contained in:
		
							parent
							
								
									6414083af0
								
							
						
					
					
						commit
						0460e838bc
					
				
					 4 changed files with 32 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -50,10 +50,17 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		let mut relay_debug = String::new();
 | 
			
		||||
		let now = Local::now().time();
 | 
			
		||||
		for relay in this.relays.iter() {
 | 
			
		||||
			relay_debug.push_str(&format!(
 | 
			
		||||
				"{}: {} |",
 | 
			
		||||
				relay.r.name, relay.active_schedule.name
 | 
			
		||||
				"{}{}: {} ; ",
 | 
			
		||||
				if relay.active_schedule.is_on(&now) {
 | 
			
		||||
					"+"
 | 
			
		||||
				} else {
 | 
			
		||||
					"-"
 | 
			
		||||
				},
 | 
			
		||||
				relay.r.name,
 | 
			
		||||
				relay.active_schedule.name
 | 
			
		||||
			));
 | 
			
		||||
		}
 | 
			
		||||
		log::debug!(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -50,6 +50,10 @@ impl Period {
 | 
			
		|||
			end: NaiveTime::MIN,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn is_on(&self, now: &NaiveTime) -> bool {
 | 
			
		||||
		self.start.eq(&self.end) || (self.start.le(now) && self.end.gt(now))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Type<Sqlite> for DbPeriods {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
use std::borrow::Borrow;
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use chrono::NaiveTime;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
| 
						 | 
				
			
			@ -193,4 +194,8 @@ impl DbSchedule {
 | 
			
		|||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn is_on(&self, now: &NaiveTime) -> bool {
 | 
			
		||||
		self.periods.0.iter().any(|period| period.is_on(now))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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