POC in Rust Saving automically on change
This commit is contained in:
parent
d7c5f0f048
commit
ecb51125cc
1
POC_in_rust/.gitignore
vendored
Normal file
1
POC_in_rust/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
target
|
@ -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"]}
|
||||
|
@ -1,6 +1,5 @@
|
||||
{
|
||||
"DURATION": 600,
|
||||
"SAVING_INTERVAL": 60,
|
||||
"STATION_ID": 2,
|
||||
"HOST": "https://beta.alkator.cz",
|
||||
"WSS_HOST": "wss://beta.alkator.cz"
|
||||
|
@ -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,
|
||||
|
@ -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::<Local>) -> Result<(),
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn register_and_dump(card_id: usize, datetime: DateTime<Local>, 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<Message>{
|
||||
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<Message> {
|
||||
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,
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user