From ccc30dcdafc16f50cacd172afea242f762a8736f Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Fri, 26 Apr 2024 01:46:55 +0200 Subject: [PATCH] Add function to update reconnecting controllers --- .../src/handlers/v1/ws/controllers/handlers.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs b/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs index 5f763e5..7becb41 100644 --- a/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs +++ b/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs @@ -2,13 +2,13 @@ use actix::{Actor, AsyncContext}; use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::models::{Controller, FromDbModel}; -use emgauwa_lib::types::{ControllerUid, RelayStates}; +use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RelayStates}; use emgauwa_lib::utils; use futures::executor::block_on; use sqlx::pool::PoolConnection; use sqlx::Sqlite; -use crate::app_state::{ConnectController, UpdateRelayStates}; +use crate::app_state::{Action, ConnectController, UpdateRelayStates}; use crate::handlers::v1::ws::controllers::ControllersWs; impl ControllersWs { @@ -31,6 +31,8 @@ impl ControllersWs { c.relay_count, ))?; block_on(controller_db.update_active(conn, true))?; + // update only the relay count + block_on(controller_db.update(conn, &controller_db.name, c.relay_count))?; for relay in &controller.relays { log::debug!( @@ -77,7 +79,16 @@ impl ControllersWs { self.controller_uid = Some(controller_uid.clone()); block_on(self.app_state.send(ConnectController { address: addr.recipient(), - controller, + controller: controller.clone(), + }))??; + + block_on(self.app_state.send(Action { + controller_uid: controller_uid.clone(), + action: ControllerWsAction::Controller(controller.clone()), + }))??; + block_on(self.app_state.send(Action { + controller_uid: controller_uid.clone(), + action: ControllerWsAction::Relays(controller.relays), }))??; log::debug!("Done registering controller");