diff --git a/POC_in_rust/.gitignore b/POC_in_rust/.gitignore new file mode 100644 index 0000000..1de5659 --- /dev/null +++ b/POC_in_rust/.gitignore @@ -0,0 +1 @@ +target \ No newline at end of file diff --git a/POC_in_rust/Cargo.toml b/POC_in_rust/Cargo.toml index eb96ea9..8e7812f 100644 --- a/POC_in_rust/Cargo.toml +++ b/POC_in_rust/Cargo.toml @@ -13,7 +13,7 @@ lazy_static = "1.5.0" nusb = "0.1.13" reqwest = { version = "0.12.15", features = ["json", "multipart", "http2", "cookies"] } reqwest-middleware = "0.4.2" -rustls = "0.23.27" +rustls = {version = "0.23.27", features = ["std", "prefer-post-quantum", "tls12", "logging", "ring"]} serde = {version = "1.0.219", features=["derive"]} serde_json = "1.0.140" tokio = {version = "1.45.0", features=["time", "fs", "io-util", "macros"]} diff --git a/POC_in_rust/config.json b/POC_in_rust/config.json index 9f55208..03ee480 100644 --- a/POC_in_rust/config.json +++ b/POC_in_rust/config.json @@ -1,6 +1,5 @@ { "DURATION": 600, - "SAVING_INTERVAL": 60, "STATION_ID": 2, "HOST": "https://beta.alkator.cz", "WSS_HOST": "wss://beta.alkator.cz" diff --git a/POC_in_rust/src/config.rs b/POC_in_rust/src/config.rs index f69e959..938e7e1 100644 --- a/POC_in_rust/src/config.rs +++ b/POC_in_rust/src/config.rs @@ -9,7 +9,6 @@ use lazy_static::lazy_static; #[derive(Deserialize)] pub struct Config { pub DURATION: u64, - pub SAVING_INTERVAL: u64, pub STATION_ID: usize, pub HOST: String, pub WSS_HOST: String, diff --git a/POC_in_rust/src/main.rs b/POC_in_rust/src/main.rs index e93c7f8..00544ae 100644 --- a/POC_in_rust/src/main.rs +++ b/POC_in_rust/src/main.rs @@ -1,4 +1,4 @@ -use iced::time::{self, Duration, Instant, milliseconds, seconds}; +use iced::time::{self, Duration, Instant, milliseconds}; use std::collections::HashMap; use std::fmt::{Formatter, Error as fmtError}; use serde::{Deserialize, Serialize}; @@ -9,6 +9,7 @@ use tokio::io::AsyncWriteExt; use std::fs::read_to_string; use reqwest::Client; use chrono::{DateTime, Local}; +use iced::futures::join; mod card_reader; @@ -71,6 +72,10 @@ async fn station_register(card_id: usize, time: DateTime::) -> Result<(), Ok(()) } +async fn register_and_dump(card_id: usize, datetime: DateTime, json: String) -> (Result<(), tokio::io::Error>, Result<(), reqwest::Error>){ + return join!(dump_state(json), station_register(card_id, datetime)) +} + impl State{ fn update(&mut self, message: Message) -> Task{ match message { @@ -81,6 +86,8 @@ impl State{ self.db.insert(racer.card_id, racer); }, }; + let json = serde_json::to_string(self).unwrap(); + Task::perform(dump_state(json), |_| Message::Nothing) } Message::Tick(time) => { if let Some(racer_time) = self.racers.iter().next() { @@ -91,10 +98,6 @@ impl State{ self.time = time; Task::none() }, - Message::DumpState(_) => { - let json = serde_json::to_string(self).unwrap(); - Task::perform(dump_state(json), |_| Message::Nothing) - }, Message::Nothing => Task::none(), Message::CardReader(card_reader::Event::ReadedCardId(card_id)) => { let time = Instant::now(); @@ -107,7 +110,8 @@ impl State{ }else{ println!("Racer {} not found in db!", card_id) } - Task::perform(station_register(card_id, datetime), |_| Message::Nothing) + let json = serde_json::to_string(self).unwrap(); + Task::perform(register_and_dump(card_id, datetime, json), |_| Message::Nothing) }, Message::CardReader(card_reader::Event::Connected) => { println!("Card Reader Connected!"); @@ -158,7 +162,6 @@ impl State{ fn subscription(&self) -> Subscription { Subscription::batch(vec![ time::every(milliseconds(10)).map(Message::Tick), - time::every(seconds(CONFIG.SAVING_INTERVAL)).map(Message::DumpState), Subscription::run(card_reader::connect).map(Message::CardReader), Subscription::run(db_update::connect).map(Message::RacerRecieved) ]) @@ -178,7 +181,6 @@ enum Message{ CardReader(card_reader::Event), RacerRecieved(db_update::Event), Tick(Instant), - DumpState(Instant), Nothing, }