mirror of
				https://github.com/Djeeberjr/fw-anwesenheit.git
				synced 2025-11-04 07:34:10 +00:00 
			
		
		
		
	improved readability
This commit is contained in:
		
							parent
							
								
									eaca9d8cec
								
							
						
					
					
						commit
						3e079c905f
					
				@ -5,7 +5,7 @@ use smart_leds::colors::{GREEN, RED};
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
use tokio::{join, time::sleep};
 | 
			
		||||
 | 
			
		||||
use crate::{hardware::{Buzzer, StatusLed}, spi_led};
 | 
			
		||||
use crate::hardware::{Buzzer, StatusLed};
 | 
			
		||||
 | 
			
		||||
#[cfg(not(feature = "mock_pi"))]
 | 
			
		||||
use crate::{gpio_buzzer::GPIOBuzzer, spi_led::SpiLed};
 | 
			
		||||
@ -15,9 +15,23 @@ use crate::mock::{MockBuzzer, MockLed};
 | 
			
		||||
 | 
			
		||||
const LED_BLINK_DURATION: Duration = Duration::from_secs(1);
 | 
			
		||||
 | 
			
		||||
pub static CURRENTSTATUS: spi_led::CurrentStatus = spi_led::CurrentStatus::Ready;
 | 
			
		||||
pub enum DeviceStatus {
 | 
			
		||||
    NotReady,
 | 
			
		||||
    Ready,
 | 
			
		||||
    HotspotEnabled,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl DeviceStatus {
 | 
			
		||||
    pub fn color(&self) -> RGB8 {
 | 
			
		||||
        match self {
 | 
			
		||||
            Self::NotReady => RGB8::new(0, 0, 0),
 | 
			
		||||
            Self::Ready => RGB8::new(0, 50, 0),
 | 
			
		||||
            Self::HotspotEnabled => RGB8::new(0, 0, 50),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
pub struct Feedback<B: Buzzer, L: StatusLed> {
 | 
			
		||||
    device_status: DeviceStatus,
 | 
			
		||||
    buzzer: B,
 | 
			
		||||
    led: L,
 | 
			
		||||
}
 | 
			
		||||
@ -31,6 +45,8 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
 | 
			
		||||
        buzzer_result.unwrap_or_else(|err| {
 | 
			
		||||
            error!("Failed to buzz: {err}");
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        let _ = self.led_to_status();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn failure(&mut self) {
 | 
			
		||||
@ -42,6 +58,8 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
 | 
			
		||||
        buzzer_result.unwrap_or_else(|err| {
 | 
			
		||||
            error!("Failed to buzz: {err}");
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        let _ = self.led_to_status();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn activate_error_state(&mut self) -> Result<()> {
 | 
			
		||||
@ -50,24 +68,44 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // ------------------ LED -------------------------
 | 
			
		||||
    pub async fn startup(&mut self){
 | 
			
		||||
        self.device_status = DeviceStatus::Ready;
 | 
			
		||||
 | 
			
		||||
    /// flash led for amount of time
 | 
			
		||||
    /// # Arguments 
 | 
			
		||||
    /// * `led`- led or mockled
 | 
			
		||||
    /// * `color` - enum color
 | 
			
		||||
    /// * `duration` - duration in ms
 | 
			
		||||
    pub async fn flash_led_for_duration(led: &mut L, color: RGB8, duration: Duration) -> Result<()> {
 | 
			
		||||
        let led_handle = Self::flash_led_for_duration(&mut self.led, GREEN, Duration::from_secs(1));
 | 
			
		||||
        let buzzer_handle = Self::beep_startup(&mut self.buzzer);
 | 
			
		||||
 | 
			
		||||
        let (buzzer_result, led_result) = join!(buzzer_handle, led_handle);
 | 
			
		||||
 | 
			
		||||
        buzzer_result.unwrap_or_else(|err| {
 | 
			
		||||
            error!("Failed to buzz: {err}");
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        led_result.unwrap_or_else(|err| {
 | 
			
		||||
            error!("Failed to blink led: {err}");
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        let _ = self.led_to_status();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn set_device_status(&mut self, status: DeviceStatus){
 | 
			
		||||
        self.device_status = status;
 | 
			
		||||
        let _ = self.led_to_status();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn led_to_status(&mut self) -> Result<()> {
 | 
			
		||||
        self.led.turn_on(self.device_status.color())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async fn flash_led_for_duration(led: &mut L, color: RGB8, duration: Duration) -> Result<()> {
 | 
			
		||||
        led.turn_on(color)?;
 | 
			
		||||
 | 
			
		||||
        sleep(duration).await;
 | 
			
		||||
 | 
			
		||||
        led.turn_off()?;
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // ----------------- BUZZER ------------------------
 | 
			
		||||
 | 
			
		||||
    /// acknowledge beep tone
 | 
			
		||||
    async fn beep_ack(buzzer: &mut B) -> Result<()> {
 | 
			
		||||
        buzzer
 | 
			
		||||
            .modulated_tone(1200.0, Duration::from_millis(100))
 | 
			
		||||
@ -79,7 +117,6 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Not acknowledge beep tone
 | 
			
		||||
    async fn beep_nak(buzzer: &mut B) -> Result<()> {
 | 
			
		||||
        buzzer
 | 
			
		||||
            .modulated_tone(600.0, Duration::from_millis(150))
 | 
			
		||||
@ -91,8 +128,7 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// beep tone for starting the device
 | 
			
		||||
    pub async  fn beep_startup(buzzer: &mut B) -> Result<()> {
 | 
			
		||||
    async fn beep_startup(buzzer: &mut B) -> Result<()> {
 | 
			
		||||
        buzzer
 | 
			
		||||
            .modulated_tone(523.0, Duration::from_millis(150))
 | 
			
		||||
            .await?;
 | 
			
		||||
@ -128,6 +164,7 @@ impl FeedbackImpl {
 | 
			
		||||
        #[cfg(feature = "mock_pi")]
 | 
			
		||||
        {
 | 
			
		||||
            Ok(Feedback {
 | 
			
		||||
                device_status: DeviceStatus::NotReady,
 | 
			
		||||
                buzzer: MockBuzzer {},
 | 
			
		||||
                led: MockLed {},
 | 
			
		||||
            })
 | 
			
		||||
@ -135,6 +172,7 @@ impl FeedbackImpl {
 | 
			
		||||
        #[cfg(not(feature = "mock_pi"))]
 | 
			
		||||
        {
 | 
			
		||||
            Ok(Feedback {
 | 
			
		||||
                device_status: DeviceStatus::NotReady,
 | 
			
		||||
                buzzer: GPIOBuzzer::new_default()?,
 | 
			
		||||
                led: SpiLed::new()?,
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
@ -1,10 +1,9 @@
 | 
			
		||||
use anyhow::{Result, anyhow};
 | 
			
		||||
use log::{trace, warn};
 | 
			
		||||
use smart_leds::colors::GREEN;
 | 
			
		||||
use std::{env, time::Duration};
 | 
			
		||||
use std::env;
 | 
			
		||||
use tokio::process::Command;
 | 
			
		||||
 | 
			
		||||
use crate::{feedback::{self, FeedbackImpl}, hardware::Hotspot, spi_led};
 | 
			
		||||
use crate::hardware::Hotspot;
 | 
			
		||||
 | 
			
		||||
const SSID: &str = "fwa";
 | 
			
		||||
const CON_NAME: &str = "fwa-hotspot";
 | 
			
		||||
@ -127,9 +126,6 @@ impl Hotspot for NMHotspot {
 | 
			
		||||
            return Err(anyhow!("nmcli command had non-zero exit code"));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        feedback::CURRENTSTATUS = Ready;
 | 
			
		||||
        FeedbackImpl::flash_led_for_duration(led, GREEN, Duration::from_secs(1));
 | 
			
		||||
        
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										62
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								src/main.rs
									
									
									
									
									
								
							@ -7,7 +7,6 @@ use hardware::{Hotspot, create_hotspot};
 | 
			
		||||
use id_store::IDStore;
 | 
			
		||||
use log::{error, info, warn};
 | 
			
		||||
use pm3::run_pm3;
 | 
			
		||||
use smart_leds::colors::BLUE;
 | 
			
		||||
use std::{
 | 
			
		||||
    env::{self, args},
 | 
			
		||||
    sync::Arc,
 | 
			
		||||
@ -46,6 +45,7 @@ async fn run_webserver<H>(
 | 
			
		||||
    store: Arc<Mutex<IDStore>>,
 | 
			
		||||
    id_channel: Sender<String>,
 | 
			
		||||
    hotspot: Arc<Mutex<H>>,
 | 
			
		||||
    user_feedback: Arc<Mutex<FeedbackImpl>>,
 | 
			
		||||
) -> Result<()>
 | 
			
		||||
where
 | 
			
		||||
    H: Hotspot + Send + Sync + 'static,
 | 
			
		||||
@ -53,8 +53,13 @@ where
 | 
			
		||||
    let activity_channel = spawn_idle_watcher(Duration::from_secs(60 * 30), move || {
 | 
			
		||||
        info!("No activity on webserver. Disabling hotspot");
 | 
			
		||||
        let cloned_hotspot = hotspot.clone();
 | 
			
		||||
        let cloned_user_feedback = user_feedback.clone();
 | 
			
		||||
        tokio::spawn(async move {
 | 
			
		||||
            let _ = cloned_hotspot.lock().await.disable_hotspot().await;
 | 
			
		||||
            cloned_user_feedback
 | 
			
		||||
                .lock()
 | 
			
		||||
                .await
 | 
			
		||||
                .set_device_status(feedback::DeviceStatus::Ready);
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
@ -64,9 +69,6 @@ where
 | 
			
		||||
 | 
			
		||||
    start_webserver(store, id_channel, notifier).await?;
 | 
			
		||||
 | 
			
		||||
    feedback::CURRENTSTATUS = Hotspot;
 | 
			
		||||
    FeedbackImpl::flash_led_for_duration(led, BLUE, 1000);
 | 
			
		||||
    
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -99,32 +101,38 @@ async fn handle_ids_loop(
 | 
			
		||||
    hotspot_enable_ids: Vec<TallyID>,
 | 
			
		||||
    id_store: Arc<Mutex<IDStore>>,
 | 
			
		||||
    hotspot: Arc<Mutex<impl Hotspot>>,
 | 
			
		||||
    mut user_feedback: FeedbackImpl,
 | 
			
		||||
    user_feedback: Arc<Mutex<FeedbackImpl>>,
 | 
			
		||||
) -> Result<()> {
 | 
			
		||||
    while let Ok(tally_id_string) = id_channel.recv().await {
 | 
			
		||||
        let tally_id = TallyID(tally_id_string);
 | 
			
		||||
 | 
			
		||||
        if hotspot_enable_ids.contains(&tally_id) {
 | 
			
		||||
            info!("Enableing hotspot");
 | 
			
		||||
            hotspot
 | 
			
		||||
                .lock()
 | 
			
		||||
                .await
 | 
			
		||||
                .enable_hotspot()
 | 
			
		||||
                .await
 | 
			
		||||
                .unwrap_or_else(|err| {
 | 
			
		||||
                    error!("Hotspot: {err}");
 | 
			
		||||
                });
 | 
			
		||||
            let hotspot_enable_result = hotspot.lock().await.enable_hotspot().await;
 | 
			
		||||
 | 
			
		||||
            match hotspot_enable_result {
 | 
			
		||||
                Ok(_) => {
 | 
			
		||||
                    user_feedback
 | 
			
		||||
                        .lock()
 | 
			
		||||
                        .await
 | 
			
		||||
                        .set_device_status(feedback::DeviceStatus::HotspotEnabled);
 | 
			
		||||
                }
 | 
			
		||||
                Err(e) => {
 | 
			
		||||
                    error!("Hotspot: {e}");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // TODO: Should the ID be added anyway or ignored ?
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if id_store.lock().await.add_id(tally_id) {
 | 
			
		||||
            info!("Added new id to current day");
 | 
			
		||||
 | 
			
		||||
            user_feedback.success().await;
 | 
			
		||||
            user_feedback.lock().await.success().await;
 | 
			
		||||
 | 
			
		||||
            if let Err(e) = id_store.lock().await.export_json(STORE_PATH).await {
 | 
			
		||||
                error!("Failed to save id store to file: {e}");
 | 
			
		||||
                user_feedback.failure().await;
 | 
			
		||||
                user_feedback.lock().await.failure().await;
 | 
			
		||||
                // TODO: How to handle a failure to save ?
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@ -133,8 +141,8 @@ async fn handle_ids_loop(
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn enter_error_state(mut feedback: FeedbackImpl, hotspot: Arc<Mutex<impl Hotspot>>) {
 | 
			
		||||
    let _ = feedback.activate_error_state().await;
 | 
			
		||||
async fn enter_error_state(feedback: Arc<Mutex<FeedbackImpl>>, hotspot: Arc<Mutex<impl Hotspot>>) {
 | 
			
		||||
    let _ = feedback.lock().await.activate_error_state().await;
 | 
			
		||||
    let _ = hotspot.lock().await.enable_hotspot().await;
 | 
			
		||||
 | 
			
		||||
    let mut sigterm = signal(SignalKind::terminate()).unwrap();
 | 
			
		||||
@ -147,13 +155,13 @@ async fn main() -> Result<()> {
 | 
			
		||||
 | 
			
		||||
    info!("Starting application");
 | 
			
		||||
 | 
			
		||||
    let user_feedback = Feedback::new()?;
 | 
			
		||||
    let user_feedback = Arc::new(Mutex::new(Feedback::new()?));
 | 
			
		||||
    let hotspot = Arc::new(Mutex::new(create_hotspot()?));
 | 
			
		||||
 | 
			
		||||
    let error_flag_set = args().any(|e| e == "--error" || e == "-e");
 | 
			
		||||
    if error_flag_set {
 | 
			
		||||
        error!("Error flag set. Entering error state");
 | 
			
		||||
        enter_error_state(user_feedback, hotspot).await;
 | 
			
		||||
        enter_error_state(user_feedback.clone(), hotspot).await;
 | 
			
		||||
        return Ok(());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -165,22 +173,24 @@ async fn main() -> Result<()> {
 | 
			
		||||
 | 
			
		||||
    let pm3_handle = run_pm3(tx);
 | 
			
		||||
 | 
			
		||||
    user_feedback.lock().await.startup().await;
 | 
			
		||||
 | 
			
		||||
    let loop_handle = handle_ids_loop(
 | 
			
		||||
        rx,
 | 
			
		||||
        hotspot_enable_ids,
 | 
			
		||||
        store.clone(),
 | 
			
		||||
        hotspot.clone(),
 | 
			
		||||
        user_feedback,
 | 
			
		||||
        user_feedback.clone(),
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    let webserver_handle = run_webserver(store.clone(), sse_tx, hotspot.clone());
 | 
			
		||||
 | 
			
		||||
    feedback::CURRENTSTATUS = Ready;
 | 
			
		||||
    FeedbackImpl::beep_startup(buzzer);
 | 
			
		||||
    FeedbackImpl::flash_led_for_duration(led, GREEN, Duration::from_secs(1));
 | 
			
		||||
    let webserver_handle = run_webserver(
 | 
			
		||||
        store.clone(),
 | 
			
		||||
        sse_tx,
 | 
			
		||||
        hotspot.clone(),
 | 
			
		||||
        user_feedback.clone(),
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    let run_result = try_join!(pm3_handle, loop_handle, webserver_handle);
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    if let Err(e) = run_result {
 | 
			
		||||
        error!("Failed to run application: {e}");
 | 
			
		||||
 | 
			
		||||
@ -1,27 +1,12 @@
 | 
			
		||||
use anyhow::Result;
 | 
			
		||||
use rgb::RGB8;
 | 
			
		||||
use rppal::spi::{Bus, Mode, SlaveSelect, Spi};
 | 
			
		||||
use smart_leds::SmartLedsWrite;
 | 
			
		||||
use ws2812_spi::Ws2812;
 | 
			
		||||
 | 
			
		||||
use crate::{feedback, hardware::StatusLed};
 | 
			
		||||
use crate::hardware::StatusLed;
 | 
			
		||||
 | 
			
		||||
const SPI_CLOCK_SPEED: u32 = 3_800_000;
 | 
			
		||||
 | 
			
		||||
pub enum CurrentStatus {
 | 
			
		||||
    Ready,
 | 
			
		||||
    Hotspot,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl CurrentStatus {
 | 
			
		||||
    pub fn color(&self) -> RGB8 {
 | 
			
		||||
        match self {
 | 
			
		||||
            CurrentStatus::Ready => RGB8::new(0, 50, 0),
 | 
			
		||||
            CurrentStatus::Hotspot => RGB8::new(0, 0, 50),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct SpiLed {
 | 
			
		||||
    controller: Ws2812<Spi>,
 | 
			
		||||
}
 | 
			
		||||
@ -37,7 +22,7 @@ impl SpiLed {
 | 
			
		||||
impl StatusLed for SpiLed {
 | 
			
		||||
    fn turn_off(&mut self) -> Result<()> {
 | 
			
		||||
        self.controller
 | 
			
		||||
            .write(vec![feedback::CURRENTSTATUS.color()].into_iter())?;
 | 
			
		||||
            .write(vec![rgb::RGB8::new(0, 0, 0)].into_iter())?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user