Refactor the utils (remove them)

This commit is contained in:
Tobias Reisinger 2024-05-29 00:59:08 +02:00
parent bb982c4444
commit b065c8dd97
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
6 changed files with 53 additions and 110 deletions

View file

@ -1,7 +1,7 @@
use std::sync::Arc;
use std::time::{Duration, Instant};
use actix::{Actor, Context, Handler, Message};
use actix::{Actor, Addr, Context, Handler, Message};
use emgauwa_common::constants;
use emgauwa_common::db::DbSchedule;
use emgauwa_common::errors::EmgauwaError;
@ -84,6 +84,20 @@ impl AppState {
pub fn notify_relay_change(&self) {
self.relay_notifier.notify_one();
}
pub async fn trigger_reload(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
app_state
.send(Reload {})
.await
.map_err(EmgauwaError::from)?
}
pub async fn get_this(app_state: &Addr<AppState>) -> Result<Controller, EmgauwaError> {
app_state
.send(GetThis {})
.await
.map_err(EmgauwaError::from)
}
}
impl Actor for AppState {

View file

@ -18,7 +18,6 @@ mod drivers;
mod errors;
mod relay_loop;
mod settings;
mod utils;
mod ws;
async fn create_this_controller(

View file

@ -10,10 +10,9 @@ use emgauwa_common::utils::printable_relay_states;
use futures::pin_mut;
use tokio::time;
use tokio::time::timeout;
use utils::app_state_get_controller_notifier;
use crate::app_state::AppState;
use crate::utils;
use crate::app_state;
pub async fn run_relays_loop(app_state: Addr<AppState>) {
log::debug!("Spawned relays loop");
@ -27,10 +26,12 @@ pub async fn run_relays_loop(app_state: Addr<AppState>) {
}
async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
let notifier = &*app_state_get_controller_notifier(app_state).await?;
let notifier = &*app_state
.send(app_state::GetControllerNotifier {})
.await?;
let mut last_weekday = emgauwa_common::utils::get_weekday();
let mut this = utils::app_state_get_this(app_state).await?;
let mut this = AppState::get_this(app_state).await?;
let mut duration_override = None;
@ -52,7 +53,7 @@ async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
if changed {
log::debug!("Reloading controller in relay loop");
this = utils::app_state_get_this(app_state).await?;
this = AppState::get_this(app_state).await?;
}
log::debug!(
@ -97,8 +98,13 @@ async fn calc_relay_states(
|| relay.check_pulsing(&now.instant).is_some(),
);
});
let relay_states = this.get_relay_states();
utils::app_state_update_relay_states(app_state, relay_states.clone()).await
app_state
.send(app_state::UpdateRelayStates {
relay_states: this.get_relay_states()
})
.await
.map_err(EmgauwaError::from)
}
fn get_next_duration(
@ -136,7 +142,7 @@ async fn check_weekday(
if current_weekday.ne(last_weekday) {
log::debug!("Weekday changed");
*last_weekday = current_weekday;
utils::app_state_reload(app_state).await?;
AppState::trigger_reload(app_state).await?;
*changed = true;
}

View file

@ -1,83 +0,0 @@
use std::sync::Arc;
use actix::Addr;
use emgauwa_common::db::DbSchedule;
use emgauwa_common::errors::EmgauwaError;
use emgauwa_common::models::Controller;
use emgauwa_common::types::{RelayStates, Weekday};
use tokio::sync::Notify;
use crate::app_state;
use crate::app_state::AppState;
pub async fn app_state_get_this(app_state: &Addr<AppState>) -> Result<Controller, EmgauwaError> {
app_state
.send(app_state::GetThis {})
.await
.map_err(EmgauwaError::from)
}
pub async fn app_state_get_relay_notifier(
app_state: &Addr<AppState>,
) -> Result<Arc<Notify>, EmgauwaError> {
app_state
.send(app_state::GetRelayNotifier {})
.await
.map_err(EmgauwaError::from)
}
pub async fn app_state_get_controller_notifier(
app_state: &Addr<AppState>,
) -> Result<Arc<Notify>, EmgauwaError> {
app_state
.send(app_state::GetControllerNotifier {})
.await
.map_err(EmgauwaError::from)
}
pub async fn app_state_reload(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
app_state
.send(app_state::Reload {})
.await
.map_err(EmgauwaError::from)?
}
pub async fn app_state_update_relay_states(
app_state: &Addr<AppState>,
relay_states: RelayStates,
) -> Result<(), EmgauwaError> {
app_state
.send(app_state::UpdateRelayStates { relay_states })
.await
.map_err(EmgauwaError::from)
}
pub async fn app_state_relay_pulse(
app_state: &Addr<AppState>,
relay_number: i64,
duration: Option<u32>,
) -> Result<(), EmgauwaError> {
app_state
.send(app_state::RelayPulse {
relay_number,
duration,
})
.await
.map_err(EmgauwaError::from)?
}
pub async fn app_state_relay_override_schedule(
app_state: &Addr<AppState>,
relay_number: i64,
schedule: Option<DbSchedule>,
weekday: Weekday,
) -> Result<(), EmgauwaError> {
app_state
.send(app_state::RelayOverrideSchedule {
relay_number,
weekday,
schedule,
})
.await
.map_err(EmgauwaError::from)?
}

View file

@ -10,8 +10,7 @@ use emgauwa_common::models::{Controller, Relay};
use emgauwa_common::types::{ControllerWsAction, ScheduleUid};
use crate::app_state::AppState;
use crate::utils;
use crate::utils::app_state_get_this;
use crate::app_state;
pub async fn handle_message(
pool: Pool<Sqlite>,
@ -53,7 +52,7 @@ pub async fn handle_action(
app_state: &Addr<AppState>,
action: ControllerWsAction,
) -> Result<(), EmgauwaError> {
let this = app_state_get_this(app_state).await?;
let this = AppState::get_this(app_state).await?;
match action {
ControllerWsAction::Controller(controller) => {
@ -67,7 +66,7 @@ pub async fn handle_action(
_ => return Ok(()),
};
utils::app_state_reload(app_state).await
AppState::trigger_reload(app_state).await
}
async fn handle_controller(
@ -154,13 +153,13 @@ async fn handle_relays(
);
}
utils::app_state_relay_override_schedule(
app_state,
relay.r.number,
relay.override_schedule.clone(),
relay.override_schedule_weekday,
)
.await?;
app_state
.send(app_state::RelayOverrideSchedule {
relay_number: relay.r.number,
schedule: relay.override_schedule.clone(),
weekday: relay.override_schedule_weekday,
})
.await??;
DbJunctionRelaySchedule::set_schedules(conn, &db_relay, schedules.iter().collect()).await?;
}
@ -170,8 +169,13 @@ async fn handle_relays(
async fn handle_relay_pulse(
app_state: &Addr<AppState>,
relay_num: i64,
relay_number: i64,
duration: Option<u32>,
) -> Result<(), EmgauwaError> {
utils::app_state_relay_pulse(app_state, relay_num, duration).await
app_state
.send(app_state::RelayPulse {
relay_number,
duration,
})
.await?
}

View file

@ -9,9 +9,9 @@ use sqlx::{Pool, Sqlite};
use tokio::time;
use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::Message;
use crate::app_state;
use crate::app_state::AppState;
use crate::utils::{app_state_get_relay_notifier, app_state_get_this};
use crate::ws::handlers::handle_message;
pub async fn run_ws_loop(pool: Pool<Sqlite>, app_state: Addr<AppState>, url: String) {
@ -43,7 +43,7 @@ async fn run_websocket(
let (mut write, read) = ws_stream.split();
let ws_action = ControllerWsAction::Register(app_state_get_this(app_state).await?);
let ws_action = ControllerWsAction::Register(AppState::get_this(app_state).await?);
let ws_action_json = serde_json::to_string(&ws_action)?;
if let Err(err) = write.send(Message::text(ws_action_json)).await {
@ -73,11 +73,14 @@ async fn read_app_state(
app_state: Addr<AppState>,
tx: futures_channel::mpsc::UnboundedSender<Message>,
) -> Result<(), EmgauwaError> {
let notifier = &*app_state_get_relay_notifier(&app_state).await?;
let notifier = &*app_state
.send(app_state::GetRelayNotifier {})
.await?;
loop {
notifier.notified().await;
log::debug!("Relay change detected");
let this = app_state_get_this(&app_state).await?;
let this = AppState::get_this(&app_state).await?;
let relay_states = this.get_relay_states();
let ws_action = ControllerWsAction::RelayStates((this.c.uid, relay_states));