Add feature to import missing schedules
This commit is contained in:
		
							parent
							
								
									6400b7745c
								
							
						
					
					
						commit
						c8f40284ef
					
				
					 6 changed files with 81 additions and 26 deletions
				
			
		| 
						 | 
				
			
			@ -5,9 +5,10 @@ use actix_web_actors::ws;
 | 
			
		|||
use actix_web_actors::ws::ProtocolError;
 | 
			
		||||
use emgauwa_lib::constants::{HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT};
 | 
			
		||||
use emgauwa_lib::db::errors::DatabaseError;
 | 
			
		||||
use emgauwa_lib::db::{DbController, DbRelay};
 | 
			
		||||
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
 | 
			
		||||
use emgauwa_lib::models::{Controller, FromDbModel};
 | 
			
		||||
use emgauwa_lib::types::{ConnectedControllersType, ControllerUid, ControllerWsAction};
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
use ws::Message;
 | 
			
		||||
| 
						 | 
				
			
			@ -28,12 +29,17 @@ impl Actor for ControllerWs {
 | 
			
		|||
 | 
			
		||||
	fn stopped(&mut self, _ctx: &mut Self::Context) {
 | 
			
		||||
		if let Some(controller_uid) = &self.controller_uid {
 | 
			
		||||
			let mut pool_conn = futures::executor::block_on(self.pool.acquire()).unwrap();
 | 
			
		||||
			let mut pool_conn = block_on(self.pool.acquire()).unwrap();
 | 
			
		||||
 | 
			
		||||
			let mut data = self.connected_controllers.lock().unwrap();
 | 
			
		||||
			if let Some(controller) = data.remove(controller_uid) {
 | 
			
		||||
				futures::executor::block_on(controller.c.update_active(&mut pool_conn, false))
 | 
			
		||||
					.unwrap();
 | 
			
		||||
				if let Err(err) = block_on(controller.c.update_active(&mut pool_conn, false)) {
 | 
			
		||||
					log::error!(
 | 
			
		||||
						"Failed to mark controller {} as inactive: {:?}",
 | 
			
		||||
						controller.c.uid,
 | 
			
		||||
						err
 | 
			
		||||
					)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -49,24 +55,47 @@ impl ControllerWs {
 | 
			
		|||
			ControllerWsAction::Register(controller) => {
 | 
			
		||||
				log::info!("Registering controller: {:?}", controller);
 | 
			
		||||
				let c = &controller.c;
 | 
			
		||||
				let controller_db = futures::executor::block_on(
 | 
			
		||||
					DbController::get_by_uid_or_create(conn, &c.uid, &c.name, c.relay_count),
 | 
			
		||||
				)?;
 | 
			
		||||
				futures::executor::block_on(controller_db.update_active(conn, true))?;
 | 
			
		||||
				let controller_db = block_on(DbController::get_by_uid_or_create(
 | 
			
		||||
					conn,
 | 
			
		||||
					&c.uid,
 | 
			
		||||
					&c.name,
 | 
			
		||||
					c.relay_count,
 | 
			
		||||
				))?;
 | 
			
		||||
				block_on(controller_db.update_active(conn, true))?;
 | 
			
		||||
 | 
			
		||||
				for relay in &controller.relays {
 | 
			
		||||
					let r = &relay.r;
 | 
			
		||||
					futures::executor::block_on(DbRelay::get_by_controller_and_num_or_create(
 | 
			
		||||
						conn,
 | 
			
		||||
						&controller_db,
 | 
			
		||||
						r.number,
 | 
			
		||||
						&r.name,
 | 
			
		||||
					))?;
 | 
			
		||||
					let (new_relay, created) =
 | 
			
		||||
						block_on(DbRelay::get_by_controller_and_num_or_create(
 | 
			
		||||
							conn,
 | 
			
		||||
							&controller_db,
 | 
			
		||||
							relay.r.number,
 | 
			
		||||
							&relay.r.name,
 | 
			
		||||
						))?;
 | 
			
		||||
					if created {
 | 
			
		||||
						let mut relay_schedules = Vec::new();
 | 
			
		||||
						for schedule in &relay.schedules {
 | 
			
		||||
							let (new_schedule, _) = block_on(DbSchedule::get_by_uid_or_create(
 | 
			
		||||
								conn,
 | 
			
		||||
								schedule.uid.clone(),
 | 
			
		||||
								&schedule.name,
 | 
			
		||||
								&schedule.periods,
 | 
			
		||||
							))?;
 | 
			
		||||
							relay_schedules.push(new_schedule);
 | 
			
		||||
						}
 | 
			
		||||
 | 
			
		||||
						block_on(DbJunctionRelaySchedule::set_schedules(
 | 
			
		||||
							conn,
 | 
			
		||||
							&new_relay,
 | 
			
		||||
							relay_schedules.iter().collect(),
 | 
			
		||||
						))?;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				let controller_uid = &controller.c.uid;
 | 
			
		||||
				let controller_db = block_on(DbController::get_by_uid(conn, controller_uid))?
 | 
			
		||||
					.ok_or(DatabaseError::InsertGetError)?;
 | 
			
		||||
				let controller = Controller::from_db_model(conn, controller_db)?;
 | 
			
		||||
 | 
			
		||||
				let controller_uid = &controller.c.uid;
 | 
			
		||||
				self.controller_uid = Some(controller_uid.clone());
 | 
			
		||||
 | 
			
		||||
				let mut data = self.connected_controllers.lock().unwrap();
 | 
			
		||||
| 
						 | 
				
			
			@ -97,7 +126,7 @@ impl ControllerWs {
 | 
			
		|||
 | 
			
		||||
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
 | 
			
		||||
	fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
 | 
			
		||||
		let mut pool_conn = futures::executor::block_on(self.pool.acquire()).unwrap();
 | 
			
		||||
		let mut pool_conn = block_on(self.pool.acquire()).unwrap();
 | 
			
		||||
 | 
			
		||||
		let msg = match msg {
 | 
			
		||||
			Err(_) => {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue