Add handler for relay states

This commit is contained in:
Tobias Reisinger 2024-04-25 14:10:32 +02:00
parent 82f2d49dc6
commit 55617dbd7c
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
8 changed files with 53 additions and 20 deletions
emgauwa-controller/src

View file

@ -3,6 +3,7 @@ use std::sync::Arc;
use actix::{Actor, Context, Handler, Message};
use emgauwa_lib::errors::EmgauwaError;
use emgauwa_lib::models::Controller;
use emgauwa_lib::types::RelayStates;
use futures::executor::block_on;
use sqlx::{Pool, Sqlite};
use tokio::sync::Notify;
@ -13,8 +14,8 @@ pub struct Reload {}
#[derive(Message)]
#[rtype(result = "()")]
pub struct UpdateRelaysOn {
pub relays_are_on: Vec<Option<bool>>,
pub struct UpdateRelayStates {
pub relay_states: RelayStates,
}
#[derive(Message)]
@ -74,17 +75,11 @@ impl Handler<Reload> for AppState {
}
}
impl Handler<UpdateRelaysOn> for AppState {
impl Handler<UpdateRelayStates> for AppState {
type Result = ();
fn handle(&mut self, msg: UpdateRelaysOn, _ctx: &mut Self::Context) -> Self::Result {
self.this
.relays
.iter_mut()
.zip(msg.relays_are_on.iter())
.for_each(|(relay, is_on)| {
relay.is_on = *is_on;
});
fn handle(&mut self, msg: UpdateRelayStates, _ctx: &mut Self::Context) -> Self::Result {
self.this.apply_relay_states(&msg.relay_states);
self.notify_relay_change();
}