Add function to update schedules for relays WsAction

This commit is contained in:
Tobias Reisinger 2024-04-24 01:49:02 +02:00
parent dd850766fd
commit 97d9222a39
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE

View file

@ -1,6 +1,6 @@
use actix::Addr;
use emgauwa_lib::constants::WEBSOCKET_RETRY_TIMEOUT;
use emgauwa_lib::db::{DbController, DbRelay, DbSchedule};
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
use emgauwa_lib::models::{Controller, Relay};
use emgauwa_lib::types::{ControllerWsAction, ScheduleUid};
@ -192,13 +192,24 @@ async fn handle_relays(
"Controller UID mismatch during relay update",
)));
}
DbRelay::get_by_controller_and_num(conn, &this.c, relay.r.number)
let db_relay = DbRelay::get_by_controller_and_num(conn, &this.c, relay.r.number)
.await?
.ok_or(DatabaseError::NotFound)?
.update(conn, relay.r.name.as_str())
.await?;
.ok_or(DatabaseError::NotFound)?;
handle_schedules(conn, app_state, relay.schedules).await?;
db_relay.update(conn, relay.r.name.as_str()).await?;
handle_schedules(conn, app_state, relay.schedules.clone()).await?;
let mut schedules = Vec::new(); // We need to get the schedules from the database to have the right IDs
for schedule in relay.schedules {
schedules.push(
DbSchedule::get_by_uid(conn, &schedule.uid)
.await?
.ok_or(DatabaseError::NotFound)?,
);
}
DbJunctionRelaySchedule::set_schedules(conn, &db_relay, schedules.iter().collect()).await?;
}
app_state.send(app_state::Reload {}).await??;