Compare commits

6 Commits

Author SHA1 Message Date
732411cd50 edit ids that are already mapped 2025-06-26 15:15:44 +02:00
09725c1e04 moved related files into own module 2025-06-26 12:19:40 +02:00
3e079c905f improved readability 2025-06-25 20:09:00 +02:00
Philipp
eaca9d8cec code clean up 2025-06-18 15:35:33 +02:00
Philipp
cd713d5849 added passive status led + startup sound 2025-06-17 18:06:23 +02:00
1514409070 create id mappings without prev ID 2025-06-09 16:49:57 +02:00
20 changed files with 169 additions and 49 deletions

View File

@@ -8,14 +8,30 @@ use tokio::{join, time::sleep};
use crate::hardware::{Buzzer, StatusLed}; use crate::hardware::{Buzzer, StatusLed};
#[cfg(not(feature = "mock_pi"))] #[cfg(not(feature = "mock_pi"))]
use crate::{gpio_buzzer::GPIOBuzzer, spi_led::SpiLed}; use crate::{hardware::GPIOBuzzer, hardware::SpiLed};
#[cfg(feature = "mock_pi")] #[cfg(feature = "mock_pi")]
use crate::mock::{MockBuzzer, MockLed}; use crate::hardware::{MockBuzzer, MockLed};
const LED_BLINK_DURATION: Duration = Duration::from_secs(1); const LED_BLINK_DURATION: Duration = Duration::from_secs(1);
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> { pub struct Feedback<B: Buzzer, L: StatusLed> {
device_status: DeviceStatus,
buzzer: B, buzzer: B,
led: L, led: L,
} }
@@ -23,23 +39,27 @@ pub struct Feedback<B: Buzzer, L: StatusLed> {
impl<B: Buzzer, L: StatusLed> Feedback<B, L> { impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
pub async fn success(&mut self) { pub async fn success(&mut self) {
let buzzer_handle = Self::beep_ack(&mut self.buzzer); let buzzer_handle = Self::beep_ack(&mut self.buzzer);
let led_handle = Self::blink_led_for_duration(&mut self.led, GREEN, LED_BLINK_DURATION); let led_handle = Self::flash_led_for_duration(&mut self.led, GREEN, LED_BLINK_DURATION);
let (buzzer_result, _) = join!(buzzer_handle, led_handle); let (buzzer_result, _) = join!(buzzer_handle, led_handle);
buzzer_result.unwrap_or_else(|err| { buzzer_result.unwrap_or_else(|err| {
error!("Failed to buzz: {err}"); error!("Failed to buzz: {err}");
}); });
let _ = self.led_to_status();
} }
pub async fn failure(&mut self) { pub async fn failure(&mut self) {
let buzzer_handle = Self::beep_nak(&mut self.buzzer); let buzzer_handle = Self::beep_nak(&mut self.buzzer);
let led_handle = Self::blink_led_for_duration(&mut self.led, RED, LED_BLINK_DURATION); let led_handle = Self::flash_led_for_duration(&mut self.led, RED, LED_BLINK_DURATION);
let (buzzer_result, _) = join!(buzzer_handle, led_handle); let (buzzer_result, _) = join!(buzzer_handle, led_handle);
buzzer_result.unwrap_or_else(|err| { buzzer_result.unwrap_or_else(|err| {
error!("Failed to buzz: {err}"); error!("Failed to buzz: {err}");
}); });
let _ = self.led_to_status();
} }
pub async fn activate_error_state(&mut self) -> Result<()> { pub async fn activate_error_state(&mut self) -> Result<()> {
@@ -48,10 +68,41 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
Ok(()) Ok(())
} }
async fn blink_led_for_duration(led: &mut L, color: RGB8, duration: Duration) -> Result<()> { pub async fn startup(&mut self){
self.device_status = DeviceStatus::Ready;
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)?; led.turn_on(color)?;
sleep(duration).await; sleep(duration).await;
led.turn_off()?; led.turn_off()?;
Ok(()) Ok(())
} }
@@ -76,6 +127,31 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
.await?; .await?;
Ok(()) Ok(())
} }
async fn beep_startup(buzzer: &mut B) -> Result<()> {
buzzer
.modulated_tone(523.0, Duration::from_millis(150))
.await?;
buzzer
.modulated_tone(659.0, Duration::from_millis(150))
.await?;
buzzer
.modulated_tone(784.0, Duration::from_millis(150))
.await?;
buzzer
.modulated_tone(1046.0, Duration::from_millis(200))
.await?;
sleep(Duration::from_millis(100)).await;
buzzer
.modulated_tone(784.0, Duration::from_millis(100))
.await?;
buzzer
.modulated_tone(880.0, Duration::from_millis(200))
.await?;
Ok(())
}
} }
#[cfg(feature = "mock_pi")] #[cfg(feature = "mock_pi")]
@@ -88,6 +164,7 @@ impl FeedbackImpl {
#[cfg(feature = "mock_pi")] #[cfg(feature = "mock_pi")]
{ {
Ok(Feedback { Ok(Feedback {
device_status: DeviceStatus::NotReady,
buzzer: MockBuzzer {}, buzzer: MockBuzzer {},
led: MockLed {}, led: MockLed {},
}) })
@@ -95,6 +172,7 @@ impl FeedbackImpl {
#[cfg(not(feature = "mock_pi"))] #[cfg(not(feature = "mock_pi"))]
{ {
Ok(Feedback { Ok(Feedback {
device_status: DeviceStatus::NotReady,
buzzer: GPIOBuzzer::new_default()?, buzzer: GPIOBuzzer::new_default()?,
led: SpiLed::new()?, led: SpiLed::new()?,
}) })

View File

@@ -1,11 +1,14 @@
use anyhow::Result; use anyhow::Result;
use std::time::Duration; use std::time::Duration;
#[cfg(feature = "mock_pi")] mod gpio_buzzer;
use crate::mock::MockHotspot; mod hotspot;
mod mock;
mod spi_led;
#[cfg(not(feature = "mock_pi"))] pub use gpio_buzzer::GPIOBuzzer;
use crate::hotspot::NMHotspot; pub use mock::{MockBuzzer, MockHotspot, MockLed};
pub use spi_led::SpiLed;
pub trait StatusLed { pub trait StatusLed {
fn turn_off(&mut self) -> Result<()>; fn turn_off(&mut self) -> Result<()>;
@@ -32,11 +35,11 @@ pub trait Hotspot {
pub fn create_hotspot() -> Result<impl Hotspot> { pub fn create_hotspot() -> Result<impl Hotspot> {
#[cfg(feature = "mock_pi")] #[cfg(feature = "mock_pi")]
{ {
Ok(MockHotspot {}) Ok(mock::MockHotspot {})
} }
#[cfg(not(feature = "mock_pi"))] #[cfg(not(feature = "mock_pi"))]
{ {
NMHotspot::new_from_env() hotspot::NMHotspot::new_from_env()
} }
} }

View File

@@ -1,12 +1,8 @@
#![allow(dead_code)] #![allow(dead_code)]
use activity_fairing::{ActivityNotifier, spawn_idle_watcher};
use anyhow::Result; use anyhow::Result;
use feedback::{Feedback, FeedbackImpl}; use feedback::{Feedback, FeedbackImpl};
use hardware::{Hotspot, create_hotspot};
use id_store::IDStore;
use log::{error, info, warn}; use log::{error, info, warn};
use pm3::run_pm3;
use std::{ use std::{
env::{self, args}, env::{self, args},
sync::Arc, sync::Arc,
@@ -24,20 +20,15 @@ use tokio::{
}; };
use webserver::start_webserver; use webserver::start_webserver;
mod activity_fairing; use crate::{hardware::{create_hotspot, Hotspot}, pm3::run_pm3, store::IDStore, webserver::{spawn_idle_watcher, ActivityNotifier}};
mod feedback; mod feedback;
mod gpio_buzzer;
mod hardware; mod hardware;
mod hotspot;
mod id_mapping;
mod id_store;
mod logger;
mod mock;
mod parser;
mod pm3; mod pm3;
mod spi_led; mod logger;
mod tally_id; mod tally_id;
mod webserver; mod webserver;
mod store;
const STORE_PATH: &str = "./data.json"; const STORE_PATH: &str = "./data.json";
@@ -45,6 +36,7 @@ async fn run_webserver<H>(
store: Arc<Mutex<IDStore>>, store: Arc<Mutex<IDStore>>,
id_channel: Sender<String>, id_channel: Sender<String>,
hotspot: Arc<Mutex<H>>, hotspot: Arc<Mutex<H>>,
user_feedback: Arc<Mutex<FeedbackImpl>>,
) -> Result<()> ) -> Result<()>
where where
H: Hotspot + Send + Sync + 'static, H: Hotspot + Send + Sync + 'static,
@@ -52,8 +44,13 @@ where
let activity_channel = spawn_idle_watcher(Duration::from_secs(60 * 30), move || { let activity_channel = spawn_idle_watcher(Duration::from_secs(60 * 30), move || {
info!("No activity on webserver. Disabling hotspot"); info!("No activity on webserver. Disabling hotspot");
let cloned_hotspot = hotspot.clone(); let cloned_hotspot = hotspot.clone();
let cloned_user_feedback = user_feedback.clone();
tokio::spawn(async move { tokio::spawn(async move {
let _ = cloned_hotspot.lock().await.disable_hotspot().await; let _ = cloned_hotspot.lock().await.disable_hotspot().await;
cloned_user_feedback
.lock()
.await
.set_device_status(feedback::DeviceStatus::Ready);
}); });
}); });
@@ -62,6 +59,7 @@ where
}; };
start_webserver(store, id_channel, notifier).await?; start_webserver(store, id_channel, notifier).await?;
Ok(()) Ok(())
} }
@@ -94,32 +92,38 @@ async fn handle_ids_loop(
hotspot_enable_ids: Vec<TallyID>, hotspot_enable_ids: Vec<TallyID>,
id_store: Arc<Mutex<IDStore>>, id_store: Arc<Mutex<IDStore>>,
hotspot: Arc<Mutex<impl Hotspot>>, hotspot: Arc<Mutex<impl Hotspot>>,
mut user_feedback: FeedbackImpl, user_feedback: Arc<Mutex<FeedbackImpl>>,
) -> Result<()> { ) -> Result<()> {
while let Ok(tally_id_string) = id_channel.recv().await { while let Ok(tally_id_string) = id_channel.recv().await {
let tally_id = TallyID(tally_id_string); let tally_id = TallyID(tally_id_string);
if hotspot_enable_ids.contains(&tally_id) { if hotspot_enable_ids.contains(&tally_id) {
info!("Enableing hotspot"); info!("Enableing hotspot");
hotspot let hotspot_enable_result = hotspot.lock().await.enable_hotspot().await;
.lock()
.await match hotspot_enable_result {
.enable_hotspot() Ok(_) => {
.await user_feedback
.unwrap_or_else(|err| { .lock()
error!("Hotspot: {err}"); .await
}); .set_device_status(feedback::DeviceStatus::HotspotEnabled);
}
Err(e) => {
error!("Hotspot: {e}");
}
}
// TODO: Should the ID be added anyway or ignored ? // TODO: Should the ID be added anyway or ignored ?
} }
if id_store.lock().await.add_id(tally_id) { if id_store.lock().await.add_id(tally_id) {
info!("Added new id to current day"); 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 { if let Err(e) = id_store.lock().await.export_json(STORE_PATH).await {
error!("Failed to save id store to file: {e}"); 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 ? // TODO: How to handle a failure to save ?
} }
} }
@@ -128,8 +132,8 @@ async fn handle_ids_loop(
Ok(()) Ok(())
} }
async fn enter_error_state(mut feedback: FeedbackImpl, hotspot: Arc<Mutex<impl Hotspot>>) { async fn enter_error_state(feedback: Arc<Mutex<FeedbackImpl>>, hotspot: Arc<Mutex<impl Hotspot>>) {
let _ = feedback.activate_error_state().await; let _ = feedback.lock().await.activate_error_state().await;
let _ = hotspot.lock().await.enable_hotspot().await; let _ = hotspot.lock().await.enable_hotspot().await;
let mut sigterm = signal(SignalKind::terminate()).unwrap(); let mut sigterm = signal(SignalKind::terminate()).unwrap();
@@ -142,13 +146,13 @@ async fn main() -> Result<()> {
info!("Starting application"); 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 hotspot = Arc::new(Mutex::new(create_hotspot()?));
let error_flag_set = args().any(|e| e == "--error" || e == "-e"); let error_flag_set = args().any(|e| e == "--error" || e == "-e");
if error_flag_set { if error_flag_set {
error!("Error flag set. Entering error state"); error!("Error flag set. Entering error state");
enter_error_state(user_feedback, hotspot).await; enter_error_state(user_feedback.clone(), hotspot).await;
return Ok(()); return Ok(());
} }
@@ -160,15 +164,22 @@ async fn main() -> Result<()> {
let pm3_handle = run_pm3(tx); let pm3_handle = run_pm3(tx);
user_feedback.lock().await.startup().await;
let loop_handle = handle_ids_loop( let loop_handle = handle_ids_loop(
rx, rx,
hotspot_enable_ids, hotspot_enable_ids,
store.clone(), store.clone(),
hotspot.clone(), hotspot.clone(),
user_feedback, user_feedback.clone(),
); );
let webserver_handle = run_webserver(store.clone(), sse_tx, hotspot.clone()); 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); let run_result = try_join!(pm3_handle, loop_handle, webserver_handle);

4
src/pm3/mod.rs Normal file
View File

@@ -0,0 +1,4 @@
mod runner;
mod parser;
pub use runner::run_pm3;

View File

@@ -1,9 +1,10 @@
use crate::{id_mapping::IDMapping, tally_id::TallyID};
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use tokio::fs; use tokio::fs;
use crate::{store::IDMapping, tally_id::TallyID};
/// Represents a single day that IDs can attend /// Represents a single day that IDs can attend
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct AttendanceDay { pub struct AttendanceDay {

5
src/store/mod.rs Normal file
View File

@@ -0,0 +1,5 @@
mod id_store;
mod id_mapping;
pub use id_store::IDStore;
pub use id_mapping::{IDMapping,Name};

6
src/webserver/mod.rs Normal file
View File

@@ -0,0 +1,6 @@
mod server;
mod activity_fairing;
pub use activity_fairing::{ActivityNotifier,spawn_idle_watcher};
pub use server::start_webserver;

View File

@@ -14,10 +14,9 @@ use tokio::select;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tokio::sync::broadcast::Sender; use tokio::sync::broadcast::Sender;
use crate::activity_fairing::ActivityNotifier; use crate::store::{IDMapping, IDStore, Name};
use crate::id_mapping::{IDMapping, Name};
use crate::id_store::IDStore;
use crate::tally_id::TallyID; use crate::tally_id::TallyID;
use crate::webserver::ActivityNotifier;
#[derive(Embed)] #[derive(Embed)]
#[folder = "web/dist"] #[folder = "web/dist"]

View File

@@ -42,7 +42,9 @@
/> />
</div> </div>
<div> <div>
<IDTable bind:this={idTable} /> <IDTable bind:this={idTable} onEdit={(id,firstName,lastName)=>{
addModal.open(id,firstName,lastName);
}}/>
</div> </div>
<AddIDModal <AddIDModal

View File

@@ -9,8 +9,12 @@
let modal: Modal; let modal: Modal;
export function open(id: string) { export function open(presetID: string, presetFirstName?: string, presetLastName?: string) {
displayID = id; displayID = presetID;
firstName = presetFirstName ?? "";
lastName = presetLastName ?? "";
modal.open(); modal.open();
} }

View File

@@ -3,6 +3,8 @@
import type { IDMapping } from "./IDMapping"; import type { IDMapping } from "./IDMapping";
let data: IDMapping | undefined = $state(); let data: IDMapping | undefined = $state();
let { onEdit }: { onEdit?: (string,string,string) => void } = $props();
export async function reloadData() { export async function reloadData() {
let res = await fetch("/api/mapping"); let res = await fetch("/api/mapping");
@@ -82,6 +84,8 @@
<span class="indicator">{indicator("first")}</span> <span class="indicator">{indicator("first")}</span>
</th> </th>
<th>
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@@ -90,6 +94,9 @@
<td class="whitespace-nowrap pr-5 pl-2 py-1">{row.id}</td> <td class="whitespace-nowrap pr-5 pl-2 py-1">{row.id}</td>
<td class="whitespace-nowrap pr-5">{row.last}</td> <td class="whitespace-nowrap pr-5">{row.last}</td>
<td class="whitespace-nowrap pr-5">{row.first}</td> <td class="whitespace-nowrap pr-5">{row.first}</td>
<td class="pr-5" ><button onclick={()=>{
onEdit && onEdit(row.id,row.first,row.last);
}} class="cursor-pointer">🔧</button></td>
</tr> </tr>
{/each} {/each}
</tbody> </tbody>

View File

@@ -27,7 +27,7 @@
<button <button
class="bg-indigo-500 rounded-2xl px-2 cursor-pointer mx-2" class="bg-indigo-500 rounded-2xl px-2 cursor-pointer mx-2"
onclick={() => { onclick={() => {
if (onAdd && id != "") { if (onAdd) {
onAdd(id); onAdd(id);
} }
}}>+</button }}>+</button