Refactor the utils (remove them)
This commit is contained in:
parent
45409168d6
commit
e3f3f85ef5
4 changed files with 11 additions and 23 deletions
|
@ -20,7 +20,7 @@ pub async fn load_state_for_relay(relay: &mut Relay, app_state: &Addr<AppState>)
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn load_state_for_relays(relays: &mut Vec<Relay>, app_state: &Addr<AppState>) -> Result<(), EmgauwaError>{
|
pub async fn load_state_for_relays(relays: &mut [Relay], app_state: &Addr<AppState>) -> Result<(), EmgauwaError>{
|
||||||
let stated_relays = get_stated_relays(app_state).await?;
|
let stated_relays = get_stated_relays(app_state).await?;
|
||||||
relays.iter_mut().for_each(|r| r.find_and_apply_state(&stated_relays));
|
relays.iter_mut().for_each(|r| r.find_and_apply_state(&stated_relays));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
mod handlers;
|
|
||||||
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use actix::{Actor, ActorContext, Addr, AsyncContext, Handler, StreamHandler};
|
use actix::{Actor, ActorContext, Addr, AsyncContext, Handler, StreamHandler};
|
||||||
use actix_web_actors::ws;
|
use actix_web_actors::ws;
|
||||||
use actix_web_actors::ws::ProtocolError;
|
use actix_web_actors::ws::ProtocolError;
|
||||||
|
use futures::executor::block_on;
|
||||||
|
use sqlx::{Pool, Sqlite};
|
||||||
|
use sqlx::pool::PoolConnection;
|
||||||
|
use ws::Message;
|
||||||
|
|
||||||
use emgauwa_common::constants::{HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT};
|
use emgauwa_common::constants::{HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT};
|
||||||
use emgauwa_common::errors::EmgauwaError;
|
use emgauwa_common::errors::EmgauwaError;
|
||||||
use emgauwa_common::types::{ControllerWsAction, EmgauwaUid};
|
use emgauwa_common::types::{ControllerWsAction, EmgauwaUid};
|
||||||
use futures::executor::block_on;
|
|
||||||
use sqlx::pool::PoolConnection;
|
|
||||||
use sqlx::{Pool, Sqlite};
|
|
||||||
use ws::Message;
|
|
||||||
|
|
||||||
use crate::app_state::{AppState, DisconnectController};
|
use crate::app_state::{AppState, DisconnectController};
|
||||||
use crate::utils::flatten_result;
|
|
||||||
|
mod handlers;
|
||||||
|
|
||||||
pub struct ControllersWs {
|
pub struct ControllersWs {
|
||||||
pub pool: Pool<Sqlite>,
|
pub pool: Pool<Sqlite>,
|
||||||
|
@ -32,13 +32,9 @@ impl Actor for ControllersWs {
|
||||||
|
|
||||||
fn stopped(&mut self, _ctx: &mut Self::Context) {
|
fn stopped(&mut self, _ctx: &mut Self::Context) {
|
||||||
if let Some(controller_uid) = &self.controller_uid {
|
if let Some(controller_uid) = &self.controller_uid {
|
||||||
let flat_res = flatten_result(
|
if let Err(err) = block_on(self.app_state.send(DisconnectController {
|
||||||
block_on(self.app_state.send(DisconnectController {
|
|
||||||
controller_uid: controller_uid.clone(),
|
controller_uid: controller_uid.clone(),
|
||||||
}))
|
})).unwrap_or_else(|err| Err(EmgauwaError::from(err))) {
|
||||||
.map_err(EmgauwaError::from),
|
|
||||||
);
|
|
||||||
if let Err(err) = flat_res {
|
|
||||||
log::error!("Error disconnecting controller: {:?}", err);
|
log::error!("Error disconnecting controller: {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,6 @@ use crate::app_state::AppState;
|
||||||
mod app_state;
|
mod app_state;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
mod settings;
|
mod settings;
|
||||||
mod utils;
|
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
pub fn flatten_result<T, E>(res: Result<Result<T, E>, E>) -> Result<T, E> {
|
|
||||||
match res {
|
|
||||||
Ok(Ok(t)) => Ok(t),
|
|
||||||
Ok(Err(e)) => Err(e),
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue