Format code a bit

This commit is contained in:
Tobias Reisinger 2024-05-23 20:16:44 +02:00
parent c524d089ad
commit bf3192ec65
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
7 changed files with 31 additions and 23 deletions

BIN
Cargo.lock generated

Binary file not shown.

View file

@ -2,6 +2,7 @@ pub use gpio::GpioDriver;
pub use null::NullDriver; pub use null::NullDriver;
pub use piface::PiFaceDriver; pub use piface::PiFaceDriver;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use crate::errors::EmgauwaControllerError; use crate::errors::EmgauwaControllerError;
mod gpio; mod gpio;

View file

@ -11,9 +11,11 @@ pub struct PiFaceDriver {
impl PiFaceDriver { impl PiFaceDriver {
pub fn new(pin: u8, pfd: &Option<PiFaceDigital>) -> Result<Self, EmgauwaControllerError> { pub fn new(pin: u8, pfd: &Option<PiFaceDigital>) -> Result<Self, EmgauwaControllerError> {
let pfd = pfd.as_ref().ok_or(EmgauwaControllerError::Hardware(String::from( let pfd = pfd
"PiFaceDigital not initialized", .as_ref()
)))?; .ok_or(EmgauwaControllerError::Hardware(String::from(
"PiFaceDigital not initialized",
)))?;
let pfd_pin = pfd.get_output_pin(pin)?; let pfd_pin = pfd.get_output_pin(pin)?;
Ok(Self { pfd_pin }) Ok(Self { pfd_pin })
} }

View file

@ -1,10 +1,10 @@
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use emgauwa_common::errors::EmgauwaError;
use rppal::gpio; use rppal::gpio;
use rppal_mcp23s17::Mcp23s17Error; use rppal_mcp23s17::Mcp23s17Error;
use rppal_pfd::PiFaceDigitalError; use rppal_pfd::PiFaceDigitalError;
use emgauwa_common::errors::EmgauwaError;
#[derive(Debug)] #[derive(Debug)]
pub enum EmgauwaControllerError { pub enum EmgauwaControllerError {
Hardware(String), Hardware(String),

View file

@ -15,11 +15,11 @@ use crate::ws::run_ws_loop;
mod app_state; mod app_state;
mod drivers; mod drivers;
mod errors;
mod relay_loop; mod relay_loop;
mod settings; mod settings;
mod utils; mod utils;
mod ws; mod ws;
mod errors;
async fn create_this_controller( async fn create_this_controller(
conn: &mut PoolConnection<Sqlite>, conn: &mut PoolConnection<Sqlite>,
@ -80,14 +80,10 @@ async fn main() -> Result<(), std::io::Error> {
}; };
for relay in &settings.relays { for relay in &settings.relays {
if DbRelay::get_by_controller_and_num( if DbRelay::get_by_controller_and_num(&mut conn, &db_controller, relay.number)
&mut conn, .await
&db_controller, .map_err(EmgauwaError::from)?
relay.number, .is_none()
)
.await
.map_err(EmgauwaError::from)?
.is_none()
{ {
create_this_relay(&mut conn, &db_controller, relay) create_this_relay(&mut conn, &db_controller, relay)
.await .await
@ -103,7 +99,11 @@ async fn main() -> Result<(), std::io::Error> {
let this = Controller::from_db_model(&mut conn, db_controller).map_err(EmgauwaError::from)?; let this = Controller::from_db_model(&mut conn, db_controller).map_err(EmgauwaError::from)?;
let now = chrono::Local::now().time(); let now = chrono::Local::now().time();
let initial_states: Vec<bool> = this.relays.iter().map(|r| r.active_schedule.is_on(&now)).collect(); let initial_states: Vec<bool> = this
.relays
.iter()
.map(|r| r.active_schedule.is_on(&now))
.collect();
let mut pfd: Option<PiFaceDigital> = None; let mut pfd: Option<PiFaceDigital> = None;
let drivers = settings.relays_make_drivers(&mut pfd, initial_states)?; let drivers = settings.relays_make_drivers(&mut pfd, initial_states)?;

View file

@ -2,6 +2,7 @@ use emgauwa_common::errors::EmgauwaError;
use emgauwa_common::settings; use emgauwa_common::settings;
use rppal_pfd::PiFaceDigital; use rppal_pfd::PiFaceDigital;
use serde_derive::Deserialize; use serde_derive::Deserialize;
use crate::drivers; use crate::drivers;
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
@ -69,13 +70,17 @@ impl Settings {
&self, &self,
pfd: &mut Option<PiFaceDigital>, pfd: &mut Option<PiFaceDigital>,
initial_states: Vec<bool>, initial_states: Vec<bool>,
) -> Result<Vec<Box<dyn drivers::RelayDriver>>, EmgauwaError> { ) -> Result<Vec<Box<dyn drivers::RelayDriver>>, EmgauwaError> {
let mut drivers = Vec::new(); let mut drivers = Vec::new();
let result: Result<(), EmgauwaError> = self.relays.iter().zip(initial_states).try_for_each(|(relay, state)| { let result: Result<(), EmgauwaError> =
let driver = relay.make_driver(pfd, state)?; self.relays
drivers.push(driver); .iter()
Ok(()) .zip(initial_states)
}); .try_for_each(|(relay, state)| {
let driver = relay.make_driver(pfd, state)?;
drivers.push(driver);
Ok(())
});
result?; result?;
Ok(drivers) Ok(drivers)
} }

View file

@ -7,8 +7,8 @@ use emgauwa_common::types::ControllerWsAction;
use futures::{future, pin_mut, SinkExt, StreamExt}; use futures::{future, pin_mut, SinkExt, StreamExt};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
use tokio::time; use tokio::time;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::connect_async; use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::Message;
use crate::app_state::AppState; use crate::app_state::AppState;
use crate::utils::{app_state_get_relay_notifier, app_state_get_this}; use crate::utils::{app_state_get_relay_notifier, app_state_get_this};
@ -89,4 +89,4 @@ async fn read_app_state(
)) ))
})?; })?;
} }
} }