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 mut relay_debug = String::new();
 | 
				
			||||||
 | 
							let now = Local::now().time();
 | 
				
			||||||
		for relay in this.relays.iter() {
 | 
							for relay in this.relays.iter() {
 | 
				
			||||||
			relay_debug.push_str(&format!(
 | 
								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!(
 | 
							log::debug!(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,6 +50,10 @@ impl Period {
 | 
				
			||||||
			end: NaiveTime::MIN,
 | 
								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 {
 | 
					impl Type<Sqlite> for DbPeriods {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
use std::borrow::Borrow;
 | 
					use std::borrow::Borrow;
 | 
				
			||||||
use std::ops::DerefMut;
 | 
					use std::ops::DerefMut;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use chrono::NaiveTime;
 | 
				
			||||||
use serde_derive::{Deserialize, Serialize};
 | 
					use serde_derive::{Deserialize, Serialize};
 | 
				
			||||||
use sqlx::pool::PoolConnection;
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
use sqlx::Sqlite;
 | 
					use sqlx::Sqlite;
 | 
				
			||||||
| 
						 | 
					@ -193,4 +194,8 @@ impl DbSchedule {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		Ok(())
 | 
							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 futures::executor::block_on;
 | 
				
			||||||
use serde_derive::{Deserialize, Serialize};
 | 
					use serde_derive::{Deserialize, Serialize};
 | 
				
			||||||
use sqlx::pool::PoolConnection;
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
| 
						 | 
					@ -16,6 +17,7 @@ pub struct Relay {
 | 
				
			||||||
	pub controller_id: ControllerUid,
 | 
						pub controller_id: ControllerUid,
 | 
				
			||||||
	pub schedules: Vec<DbSchedule>,
 | 
						pub schedules: Vec<DbSchedule>,
 | 
				
			||||||
	pub active_schedule: DbSchedule,
 | 
						pub active_schedule: DbSchedule,
 | 
				
			||||||
 | 
						pub is_on: bool,
 | 
				
			||||||
	pub tags: Vec<String>,
 | 
						pub tags: Vec<String>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,12 +45,16 @@ impl FromDbModel for Relay {
 | 
				
			||||||
		let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
 | 
							let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
 | 
				
			||||||
		let active_schedule = block_on(db_model.get_active_schedule(conn))?;
 | 
							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 {
 | 
							Ok(Relay {
 | 
				
			||||||
			r: db_model,
 | 
								r: db_model,
 | 
				
			||||||
			controller: cache,
 | 
								controller: cache,
 | 
				
			||||||
			controller_id,
 | 
								controller_id,
 | 
				
			||||||
			schedules,
 | 
								schedules,
 | 
				
			||||||
			active_schedule,
 | 
								active_schedule,
 | 
				
			||||||
 | 
								is_on,
 | 
				
			||||||
			tags,
 | 
								tags,
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -68,6 +74,14 @@ impl Relay {
 | 
				
			||||||
		conn: &mut PoolConnection<Sqlite>,
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
	) -> Result<(), DatabaseError> {
 | 
						) -> Result<(), DatabaseError> {
 | 
				
			||||||
		self.active_schedule = block_on(self.r.get_active_schedule(conn))?;
 | 
							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(())
 | 
							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