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-lib/src/models

View file

@ -7,6 +7,7 @@ use sqlx::Sqlite;
use crate::db::DbController;
use crate::errors::{DatabaseError, EmgauwaError};
use crate::models::{convert_db_list_cache, FromDbModel, Relay};
use crate::types::RelayStates;
#[derive(Serialize, Deserialize, Debug, Clone, MessageResponse)]
pub struct Controller {
@ -48,4 +49,17 @@ impl Controller {
}
Ok(())
}
pub fn apply_relay_states(&mut self, relay_states: &RelayStates) {
self.relays
.iter_mut()
.zip(relay_states.iter())
.for_each(|(relay, is_on)| {
relay.is_on = *is_on;
});
}
pub fn get_relay_states(&self) -> RelayStates {
self.relays.iter().map(|r| r.is_on).collect()
}
}