mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2025-07-04 09:54:17 +00:00
save store on new id
This commit is contained in:
parent
57af495ac6
commit
8aa6714697
@ -1,4 +1,3 @@
|
|||||||
use log::info;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
@ -44,11 +43,15 @@ impl IDStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new id for the current day
|
/// Add a new id for the current day
|
||||||
/// Duplicates will get ignored
|
/// Returns false if ID is already present at the current day.
|
||||||
pub fn add_id(&mut self, id: TallyID) {
|
/// Can fail because the store will be saved to a file.
|
||||||
let day = self.get_current_day();
|
pub fn add_id(&mut self, id: TallyID) -> Result<bool, Box<dyn Error>> {
|
||||||
|
if self.get_current_day().add_id(id) {
|
||||||
|
self.export_json("./data.json")?;
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
|
||||||
day.add_id(id);
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the `AttendanceDay` of the current day
|
/// Get the `AttendanceDay` of the current day
|
||||||
@ -126,12 +129,14 @@ impl AttendanceDay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_id(&mut self, id: TallyID) {
|
// Add an ID to the day.
|
||||||
|
// Returns false if ID was already present
|
||||||
|
fn add_id(&mut self, id: TallyID) -> bool {
|
||||||
if self.ids.contains(&id) {
|
if self.ids.contains(&id) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
info!("Adding id: {}", id);
|
|
||||||
self.ids.push(id);
|
self.ids.push(id);
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31
src/main.rs
31
src/main.rs
@ -1,15 +1,19 @@
|
|||||||
use id_store::IDStore;
|
use id_store::IDStore;
|
||||||
use log::{LevelFilter, error, info, warn};
|
use log::{LevelFilter, debug, error, info, warn};
|
||||||
use pm3::{pm3_mock, run_pm3};
|
use pm3::{pm3_mock, run_pm3};
|
||||||
use simplelog::{ConfigBuilder, SimpleLogger};
|
use simplelog::{ConfigBuilder, SimpleLogger};
|
||||||
use std::{env, error::Error, sync::Arc};
|
use std::{env, error::Error, sync::Arc};
|
||||||
use tokio::{fs, sync::{mpsc, Mutex}};
|
use tokio::{
|
||||||
|
fs,
|
||||||
|
sync::{Mutex, mpsc},
|
||||||
|
};
|
||||||
use webserver::start_webserver;
|
use webserver::start_webserver;
|
||||||
|
|
||||||
mod id_store;
|
mod id_store;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod pm3;
|
mod pm3;
|
||||||
mod webserver;
|
mod webserver;
|
||||||
|
|
||||||
const STORE_PATH: &str = "./data.json";
|
const STORE_PATH: &str = "./data.json";
|
||||||
|
|
||||||
fn setup_logger() {
|
fn setup_logger() {
|
||||||
@ -32,7 +36,7 @@ fn setup_logger() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(),Box<dyn Error>>{
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
setup_logger();
|
setup_logger();
|
||||||
|
|
||||||
info!("Starting application");
|
info!("Starting application");
|
||||||
@ -50,11 +54,10 @@ async fn main() -> Result<(),Box<dyn Error>>{
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
let raw_store = if fs::try_exists(STORE_PATH).await? {
|
let raw_store = if fs::try_exists(STORE_PATH).await? {
|
||||||
info!("Loading data from file");
|
info!("Loading data from file");
|
||||||
IDStore::new_from_json(STORE_PATH)?
|
IDStore::new_from_json(STORE_PATH)?
|
||||||
}else {
|
} else {
|
||||||
info!("No data file found. Creating empty one.");
|
info!("No data file found. Creating empty one.");
|
||||||
IDStore::new()
|
IDStore::new()
|
||||||
};
|
};
|
||||||
@ -64,7 +67,22 @@ async fn main() -> Result<(),Box<dyn Error>>{
|
|||||||
let channel_store = store.clone();
|
let channel_store = store.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
while let Some(tally_id_string) = rx.recv().await {
|
while let Some(tally_id_string) = rx.recv().await {
|
||||||
channel_store.lock().await.add_id(id_store::TallyID(tally_id_string));
|
match channel_store
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.add_id(id_store::TallyID(tally_id_string))
|
||||||
|
{
|
||||||
|
Ok(added) => {
|
||||||
|
if added {
|
||||||
|
debug!("~Beep~ Added new ID");
|
||||||
|
// TODO: Add buzzer here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to save id to the store: {}", e);
|
||||||
|
// TODO: What to do if the ID could not be saved ?
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -74,5 +92,6 @@ async fn main() -> Result<(),Box<dyn Error>>{
|
|||||||
error!("Failed to start webserver: {}", e);
|
error!("Failed to start webserver: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user