Improve handling of override_schedule
This commit is contained in:
		
							parent
							
								
									8244a1d837
								
							
						
					
					
						commit
						2eb38ec11e
					
				
					 4 changed files with 19 additions and 39 deletions
				
			
		| 
						 | 
					@ -179,7 +179,7 @@ impl Handler<RelayOverrideSchedule> for AppState {
 | 
				
			||||||
			weekday
 | 
								weekday
 | 
				
			||||||
		);
 | 
							);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		relay.override_schedule = Some(schedule);
 | 
							relay.override_schedule = schedule;
 | 
				
			||||||
		relay.override_schedule_weekday = weekday;
 | 
							relay.override_schedule_weekday = weekday;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Ok(())
 | 
							Ok(())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,7 +5,7 @@ use chrono::Timelike;
 | 
				
			||||||
use emgauwa_common::constants::RELAYS_RETRY_TIMEOUT;
 | 
					use emgauwa_common::constants::RELAYS_RETRY_TIMEOUT;
 | 
				
			||||||
use emgauwa_common::errors::EmgauwaError;
 | 
					use emgauwa_common::errors::EmgauwaError;
 | 
				
			||||||
use emgauwa_common::models::Controller;
 | 
					use emgauwa_common::models::Controller;
 | 
				
			||||||
use emgauwa_common::types::{EmgauwaNow, RelayState, RelayStates, Weekday};
 | 
					use emgauwa_common::types::{EmgauwaNow, Weekday};
 | 
				
			||||||
use emgauwa_common::utils::printable_relay_states;
 | 
					use emgauwa_common::utils::printable_relay_states;
 | 
				
			||||||
use futures::pin_mut;
 | 
					use futures::pin_mut;
 | 
				
			||||||
use tokio::time;
 | 
					use tokio::time;
 | 
				
			||||||
| 
						 | 
					@ -31,11 +31,6 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let mut last_weekday = emgauwa_common::utils::get_weekday();
 | 
						let mut last_weekday = emgauwa_common::utils::get_weekday();
 | 
				
			||||||
	let mut this = utils::app_state_get_this(app_state).await?;
 | 
						let mut this = utils::app_state_get_this(app_state).await?;
 | 
				
			||||||
	let mut relay_states: RelayStates = Vec::new();
 | 
					 | 
				
			||||||
	let now = EmgauwaNow::now();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	init_relay_states(&mut relay_states, &this);
 | 
					 | 
				
			||||||
	calc_relay_states(&mut relay_states, &mut this, app_state, &now).await?;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let mut duration_override = None;
 | 
						let mut duration_override = None;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -81,40 +76,26 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
			.min();
 | 
								.min();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		calc_relay_states(&mut relay_states, &mut this, app_state, &now).await?;
 | 
							calc_relay_states(&mut this, app_state, &now).await?;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn init_relay_states(relay_states: &mut RelayStates, this: &Controller) {
 | 
					 | 
				
			||||||
	relay_states.clear();
 | 
					 | 
				
			||||||
	this.relays.iter().for_each(|r| {
 | 
					 | 
				
			||||||
		relay_states.push(RelayState {
 | 
					 | 
				
			||||||
			active_schedule: r.active_schedule.clone(),
 | 
					 | 
				
			||||||
			is_on: None
 | 
					 | 
				
			||||||
		});
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async fn calc_relay_states(
 | 
					async fn calc_relay_states(
 | 
				
			||||||
	relay_states: &mut RelayStates,
 | 
					 | 
				
			||||||
	this: &mut Controller,
 | 
						this: &mut Controller,
 | 
				
			||||||
	app_state: &Addr<AppState>,
 | 
						app_state: &Addr<AppState>,
 | 
				
			||||||
	now: &EmgauwaNow,
 | 
						now: &EmgauwaNow,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
	this.relays
 | 
						this.relays
 | 
				
			||||||
		.iter_mut()
 | 
							.iter_mut()
 | 
				
			||||||
		.zip(relay_states.iter_mut())
 | 
							.for_each(|relay| {
 | 
				
			||||||
		.for_each(|(relay, state)| {
 | 
					 | 
				
			||||||
			relay.reload_active_schedule(now.weekday);
 | 
								relay.reload_active_schedule(now.weekday);
 | 
				
			||||||
			relay.is_on = Some(
 | 
								relay.is_on = Some(
 | 
				
			||||||
				relay.active_schedule.is_on(&now.time)
 | 
									relay.active_schedule.is_on(&now.time)
 | 
				
			||||||
					|| relay.check_pulsing(&now.instant).is_some(),
 | 
										|| relay.check_pulsing(&now.instant).is_some(),
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
 | 
					 | 
				
			||||||
			state.is_on = relay.is_on;
 | 
					 | 
				
			||||||
			state.active_schedule = relay.active_schedule.clone();
 | 
					 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
	utils::app_state_update_relays_on(app_state, relay_states.clone()).await
 | 
						let relay_states = this.get_relay_states();
 | 
				
			||||||
 | 
						utils::app_state_update_relay_states(app_state, relay_states.clone()).await
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn get_next_duration(
 | 
					fn get_next_duration(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -42,7 +42,7 @@ pub async fn app_state_reload(app_state: &Addr<AppState>) -> Result<(), EmgauwaE
 | 
				
			||||||
		.map_err(EmgauwaError::from)?
 | 
							.map_err(EmgauwaError::from)?
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub async fn app_state_update_relays_on(
 | 
					pub async fn app_state_update_relay_states(
 | 
				
			||||||
	app_state: &Addr<AppState>,
 | 
						app_state: &Addr<AppState>,
 | 
				
			||||||
	relay_states: RelayStates,
 | 
						relay_states: RelayStates,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,13 @@
 | 
				
			||||||
use actix::Addr;
 | 
					use actix::Addr;
 | 
				
			||||||
 | 
					use sqlx::{Pool, Sqlite};
 | 
				
			||||||
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
 | 
					use tokio_tungstenite::tungstenite;
 | 
				
			||||||
 | 
					use tokio_tungstenite::tungstenite::Message;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use emgauwa_common::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
 | 
					use emgauwa_common::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
 | 
				
			||||||
use emgauwa_common::errors::{DatabaseError, EmgauwaError};
 | 
					use emgauwa_common::errors::{DatabaseError, EmgauwaError};
 | 
				
			||||||
use emgauwa_common::models::{Controller, Relay};
 | 
					use emgauwa_common::models::{Controller, Relay};
 | 
				
			||||||
use emgauwa_common::types::{ControllerWsAction, ScheduleUid};
 | 
					use emgauwa_common::types::{ControllerWsAction, ScheduleUid};
 | 
				
			||||||
use sqlx::pool::PoolConnection;
 | 
					 | 
				
			||||||
use sqlx::{Pool, Sqlite};
 | 
					 | 
				
			||||||
use tokio_tungstenite::tungstenite;
 | 
					 | 
				
			||||||
use tokio_tungstenite::tungstenite::Message;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::app_state::AppState;
 | 
					use crate::app_state::AppState;
 | 
				
			||||||
use crate::utils;
 | 
					use crate::utils;
 | 
				
			||||||
| 
						 | 
					@ -153,15 +154,13 @@ async fn handle_relays(
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if let Some((override_schedule, weekday)) = relay.unwrap_override_schedule() {
 | 
							utils::app_state_relay_override_schedule(
 | 
				
			||||||
			utils::app_state_relay_override_schedule(
 | 
								app_state,
 | 
				
			||||||
				app_state,
 | 
								relay.r.number,
 | 
				
			||||||
				relay.r.number,
 | 
								relay.override_schedule.clone(),
 | 
				
			||||||
				override_schedule.clone(),
 | 
								relay.override_schedule_weekday,
 | 
				
			||||||
				weekday,
 | 
							)
 | 
				
			||||||
			)
 | 
							.await?;
 | 
				
			||||||
			.await?;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		DbJunctionRelaySchedule::set_schedules(conn, &db_relay, schedules.iter().collect()).await?;
 | 
							DbJunctionRelaySchedule::set_schedules(conn, &db_relay, schedules.iter().collect()).await?;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue