From 880e8f24a79d04a07a76a2bc1bc2a33fb4a812c1 Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 16 Apr 2025 20:04:55 +0200 Subject: [PATCH] workday ending --- src/buzzer.rs | 16 +++++++++++----- src/id_store.rs | 12 ++++++------ src/main.rs | 14 ++++++++++---- src/webserver.rs | 11 ++++++++++- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/buzzer.rs b/src/buzzer.rs index b9aae13..7030749 100644 --- a/src/buzzer.rs +++ b/src/buzzer.rs @@ -1,7 +1,7 @@ use rppal::gpio::Gpio; use std::{thread, time}; -/// Gibt einen Ton auf einem passiven Buzzer aus. +/// Emits a sound on a passive buzzer. pub fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duration_ms: u64) { let gpio = Gpio::new().expect("GPIO konnte nicht initialisiert werden"); let mut pin = gpio.get(pin_num).expect("Pin konnte nicht geöffnet werden").into_output(); @@ -11,7 +11,7 @@ pub fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duration_ms: let total_cycles = duration_ms as f64 / mod_period; for _ in 0..total_cycles as u64 { - // Modulations-Ein: Träger an für mod_period / 2 + // Modulation on: Carrier on for mod_period / 2 let cycles_on = (carrier_hz as f64 * (mod_period / 2.0) / 1000.0) as u64; for _ in 0..cycles_on { pin.set_high(); @@ -20,21 +20,27 @@ pub fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duration_ms: thread::sleep(carrier_period); } - // Modulations-Aus: Träger aus für mod_period / 2 + // Modulation off: Carrier on for mod_period / 2 let pause = time::Duration::from_millis((mod_period / 2.0) as u64); thread::sleep(pause); } } pub fn beep_ack() { - // GPIO 17, Träger = 2300 Hz, Ton = 440 Hz, Dauer = 1 Sekunde + // GPIO 17, carrier = 2300 Hz, sound = 440 Hz, Dauer = 1 sec modulated_tone(4, 2300, 500, 500); modulated_tone(4, 2300, 700, 500); } pub fn beep_nak() { - // GPIO 17, Träger = 2300 Hz, Ton = 440 Hz, Dauer = 1 Sekunde + // GPIO 17, carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec modulated_tone(4, 2300, 700, 500); modulated_tone(4, 2300, 500, 500); } +pub fn beep_unnkown(){ + modulated_tone(4, 2300, 500, 500); + modulated_tone(4, 2300, 500, 500); + modulated_tone(4, 2300, 500, 500); +} + diff --git a/src/id_store.rs b/src/id_store.rs index 8145f29..92d901b 100644 --- a/src/id_store.rs +++ b/src/id_store.rs @@ -6,12 +6,12 @@ use std::{ }; #[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)] -pub struct TellyID(pub String); +pub struct TallyID(pub String); #[derive(Deserialize, Serialize)] pub struct AttendanceDay { date: String, - ids: Vec, + ids: Vec, } #[derive(Deserialize, Serialize)] @@ -31,7 +31,7 @@ impl IDStore { Ok(serde_json::from_str(&readed_string)?) } - pub fn add_id(&mut self, id: TellyID) { + pub fn add_id(&mut self, id: TallyID) { let day = self.get_current_day(); day.add_id(id); @@ -59,7 +59,7 @@ impl IDStore { pub fn export_csv(&self) -> Result> { let seperator = ";"; - let mut user_ids: HashSet = HashSet::new(); + let mut user_ids: HashSet = HashSet::new(); for day in self.days.values() { for id in day.ids.iter() { @@ -67,7 +67,7 @@ impl IDStore { } } - let mut user_ids: Vec = user_ids.into_iter().collect(); + let mut user_ids: Vec = user_ids.into_iter().collect(); user_ids.sort(); let mut days: Vec = self.days.keys().cloned().collect(); @@ -102,7 +102,7 @@ impl AttendanceDay { } } - fn add_id(&mut self, id: TellyID) { + fn add_id(&mut self, id: TallyID) { if self.ids.contains(&id) { return; } diff --git a/src/main.rs b/src/main.rs index 72bf346..0c012e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ +use std::sync::Arc; + +use id_store::IDStore; use pm3::{pm3_mock, run_pm3}; -use tokio::sync::mpsc; +use tokio::sync::{mpsc, Mutex}; use webserver::start_webserver; mod id_store; @@ -12,7 +15,7 @@ async fn main() { let (tx, mut rx) = mpsc::channel::(32); tokio::spawn(async move { - match pm3_mock(tx).await { + match run_pm3(tx).await { Ok(()) => { println!("PM3 exited with an zero error code"); } @@ -22,9 +25,12 @@ async fn main() { } }); + let store:Arc> = Arc::new(Mutex::new(id_store::IDStore::new())); + tokio::spawn(async move { - while let Some(line) = rx.recv().await { - println!("Got from channel: {}", line); + while let Some(tally_id_string) = rx.recv().await { + println!("Got from channel: {}", tally_id_string); + store.lock().await.add_id(id_store::TallyID(tally_id_string)); } }); diff --git a/src/webserver.rs b/src/webserver.rs index 78206a0..e36ec9f 100644 --- a/src/webserver.rs +++ b/src/webserver.rs @@ -1,20 +1,29 @@ +use rocket::Config; use rocket::{get, http::ContentType, response::content::RawHtml, routes}; use rust_embed::Embed; use std::borrow::Cow; use std::ffi::OsStr; + #[derive(Embed)] #[folder = "web/dist"] struct Asset; pub async fn start_webserver() -> Result<(), rocket::Error> { - rocket::build() + let config = Config { + address: "0.0.0.0".parse().unwrap(), // Listen on all interfaces + port: 8000, + ..Config::default() + }; + + rocket::custom(config) .mount("/", routes![static_files,index]) .launch() .await?; Ok(()) } + #[get("/")] fn index() -> Option>> { let asset = Asset::get("index.html")?;