Compare commits

..

6 Commits

Author SHA1 Message Date
Martin Quarda
f44a68e5e8 offline DB 2025-07-11 14:42:19 +02:00
Martin Quarda
bf531b477f POC in Rust save again only on change db and dont delete racers after leave 2025-05-13 15:56:12 +02:00
Martin Quarda
6aeb5e1c2a POC in Rust lower binary size 2025-05-11 01:13:19 +02:00
Martin Quarda
ecb51125cc POC in Rust Saving automically on change 2025-05-10 20:25:16 +02:00
Martin Quarda
d7c5f0f048 POC in Rust get db from websocket 2025-05-10 19:59:24 +02:00
Martin Quarda
2a89528b8f POC in Rust remove authorization in /api/station/register 2025-05-10 17:56:17 +02:00
8 changed files with 181 additions and 79 deletions

1
POC_in_rust/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target

View File

@@ -3,15 +3,19 @@ name = "POC_in_rust"
version = "0.1.0"
edition = "2024"
[profile.release]
opt-level="s"
lto="fat"
[dependencies]
async-tungstenite = {version="0.29.1", features=["tokio-rustls-webpki-roots"]}
chrono = "0.4.41"
env_logger = "0.11.8"
futures-lite = "2.6.0"
iced = {"git"= "https://github.com/iced-rs/iced.git", "features"=["tokio", "auto-detect-theme", "fira-sans", "tiny-skia", "wgpu", "sipper"]}
iced = {"git"= "https://github.com/iced-rs/iced.git", "features"=["tokio", "auto-detect-theme", "fira-sans", "tiny-skia", "sipper"]}
lazy_static = "1.5.0"
nusb = "0.1.13"
reqwest = { version = "0.12.15", features = ["json", "multipart", "http2", "cookies"] }
reqwest-middleware = "0.4.2"
reqwest = { version = "0.12.15", features = ["json"] }
rustls = {version = "0.23.27", features = ["std", "prefer-post-quantum", "tls12", "logging"]}
serde = {version = "1.0.219", features=["derive"]}
serde_json = "1.0.140"
tokio = {version = "1.45.0", features=["time", "fs", "io-util", "macros"]}

View File

@@ -1,8 +1,5 @@
{
"DURATION": 600,
"SAVING_INTERVAL": 60,
"STATION_ID": 2,
"HOST": "https://beta.alkator.cz",
"LOGIN": "station_register@alkator.cz",
"PASSWORD": "password_heslo"
"HOST": "https://beta.alkator.cz"
}

102
POC_in_rust/people.json Normal file
View File

@@ -0,0 +1,102 @@
{
"1": 1114418469,
"2": 1111554085,
"3": 1112605381,
"4": 1362080126,
"5": 1632101230,
"6": 1114533941,
"7": 1364901054,
"8": 1364587710,
"9": 1364867822,
"10": 1364554558,
"11": 1364968750,
"12": 1111614357,
"13": 1362569198,
"14": 1364867662,
"15": 1112618821,
"16": 1364967150,
"17": 1364592718,
"18": 1364487310,
"19": 458336708,
"20": 1111590293,
"21": 1114448773,
"22": 1364550814,
"23": 1112421509,
"24": 1364529422,
"25": 1364916590,
"26": 455988804,
"27": 1631963022,
"28": 1114508533,
"29": 1364590606,
"30": 1364796894,
"31": 1112489029,
"32": 1114588261,
"33": 1111733589,
"34": 1112469509,
"35": 1632521678,
"36": 1631957710,
"37": 1111682853,
"38": 1111882197,
"39": 1631960974,
"40": 1362260606,
"41": 1364521806,
"42": 1364489406,
"43": 1361969902,
"44": 1364581838,
"45": 1364520526,
"46": 1361806574,
"47": 1364581310,
"48": 1364551150,
"49": 1364916158,
"50": 1364529662,
"51": 1632026766,
"52": 1364588798,
"53": 1631955550,
"54": 1631972606,
"55": 1364797086,
"56": 1364627198,
"57": 1364556446,
"58": 1632070414,
"59": 1631951758,
"60": 1364794654,
"61": 1632101166,
"62": 1361810574,
"63": 1114573589,
"64": 1361967886,
"65": 1632544222,
"66": 1111582085,
"67": 1362569438,
"68": 1364593198,
"69": 1364761406,
"70": 1631927918,
"71": 1364544414,
"72": 1362264958,
"73": 1364767006,
"74": 1364590110,
"75": 1362082334,
"76": 1364799166,
"77": 1632028398,
"78": 1361970078,
"79": 1364485118,
"80": 1364490846,
"81": 1632150846,
"82": 1362569342,
"83": 1362599998,
"84": 1632523854,
"85": 1364765134,
"86": 1631952062,
"87": 1364559822,
"88": 1364783854,
"89": 1362081950,
"90": 1111644021,
"91": 1631959326,
"92": 1364901422,
"93": 1632100830,
"94": 1111707429,
"95": 1632026702,
"96": 1362569022,
"97": 1114560181,
"98": 1364588510,
"99": 1364785486,
"100": 1361969950
}

View File

@@ -9,11 +9,8 @@ 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 LOGIN: String,
pub PASSWORD: String,
}
fn read_config_from_file<P: AsRef<Path>>(path: P) -> Result<Config, Box<dyn Error>> {

View File

@@ -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,12 +9,16 @@ use tokio::io::AsyncWriteExt;
use std::fs::read_to_string;
use reqwest::Client;
use chrono::{DateTime, Local};
use iced::futures::join;
use lazy_static::lazy_static;
use std::io::BufReader;
use std::error::Error;
use std::fs::File as stdFile;
use std::path::Path;
mod card_reader;
mod approx_time;
mod config;
mod db_update;
use config::CONFIG;
@@ -22,13 +26,37 @@ use config::CONFIG;
struct RacerTime{
#[serde(with = "approx_time")]
time: Instant,
racer: Racer,
racer: usize,
}
fn invert_hashmap<K, V>(map: HashMap<K, V>) -> HashMap<V, K>
where
K: Eq + std::hash::Hash,
V: Eq + std::hash::Hash,
{
map.into_iter().map(|(k, v)| (v, k)).collect()
}
fn read_db_from_file<P: AsRef<Path>>(path: P) -> Result<HashMap<usize, usize>, Box<dyn Error>> {
// Open the file in read-only mode with buffer.
let file = stdFile::open(path)?;
let reader = BufReader::new(file);
// Read the JSON contents of the file as an instance of `User`.
let db = serde_json::from_reader(reader)?;
// Return the `User`.
Ok(db)
}
lazy_static!{
// invert it so it is db[card_id] = starting number
pub static ref DB: HashMap<usize, usize> = invert_hashmap(read_db_from_file("people.json").unwrap());
}
#[derive(Serialize, Deserialize)]
struct State{
racers: Vec<RacerTime>,
db: HashMap<usize, Racer>,
#[serde(with = "approx_time")]
time: Instant,
}
@@ -36,8 +64,7 @@ struct State{
impl Default for State {
fn default() -> Self {
Self {
racers: vec![RacerTime{time: Instant::now(), racer: Racer{starting_number:11, card_id: 0}}],
db: Default::default(),
racers: vec![],
time: Instant::now(),
}
}
@@ -51,68 +78,62 @@ async fn dump_state(json: String) -> Result<(), tokio::io::Error> {
#[derive(Serialize)]
struct RegisterRacer{
card_id: usize,
starting_number: usize,
time: String,
station_id: usize,
}
async fn station_register(card_id: usize, time: DateTime::<Local>) -> Result<(), reqwest::Error>{
let client = reqwest::Client::builder()
.cookie_store(true)
.build()
.unwrap();
let result = client
.post(CONFIG.HOST.clone() + "/api/login")
.form(&[
("login", CONFIG.LOGIN.as_str()),
("password", CONFIG.PASSWORD.as_str()),
]);
.send()
.await?;
enum Message{
CardReader(card_reader::Event),
Tick(Instant),
Nothing,
}
impl std::fmt::Debug for Message {
// Required method
fn fmt(&self, _f: &mut Formatter<'_>) -> Result<(), fmtError>{
todo!()
}
}
async fn station_register(starting_number: usize, time: DateTime::<Local>) -> Result<(), reqwest::Error>{
let register_racer = RegisterRacer{
card_id: card_id,
starting_number: starting_number,
station_id: CONFIG.STATION_ID,
time: format!("{}", time.format("%d.%m.%Y %H:%M:%S.%f")),
time: format!("{}", time.format("%d.%m.%Y %H:%M:%S.%6f")),
};
client
Client::new()
.post(CONFIG.HOST.clone() + "/api/station/register")
.json(&register_racer)
.send()
.await?;
// Result can be error (400 and so on), whatever for now
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 {
Message::RacerAdded(racer) => {
self.db.insert(racer.card_id, racer);
Task::none()
},
Message::Tick(time) => {
if let Some(racer_time) = self.racers.iter().next() {
if time - racer_time.time > Duration::new(CONFIG.DURATION + 10, 0){
self.racers.remove(0);
}
}
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();
let datetime = Local::now();
if let Some(racer) = self.db.get(&card_id){
let racer_time = RacerTime{time, racer: racer.clone()};
if let Some(&racer_starting_number) = DB.get(&card_id){
let racer_time = RacerTime{time, racer: racer_starting_number};
self.racers.push(racer_time);
}else{
} 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!");
@@ -141,10 +162,12 @@ impl State{
} else {
(duration - time, false)
};
let seconds = duration.as_secs();
if negative && seconds >= 10 {
continue
}
let duration = text!(
"{}{:0>2}:{:0>2}.{:0>2}",
if negative {"-"} else {""},
@@ -154,7 +177,7 @@ impl State{
).size(20).width(120).align_x(Center).align_y(Center);
content = content.push(
row![
text(racer_time.racer.starting_number.to_string()).size(20).width(120).align_x(Center).align_y(Center), duration
text(racer_time.racer.to_string()).size(20).width(120).align_x(Center).align_y(Center), duration
]
);
}
@@ -163,7 +186,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),
])
}
@@ -172,31 +194,10 @@ impl State{
}
}
#[derive(Serialize, Deserialize, Clone)]
struct Racer{
starting_number: usize,
card_id: usize,
}
enum Message{
CardReader(card_reader::Event),
RacerAdded(Racer),
Tick(Instant),
DumpState(Instant),
Nothing,
}
impl std::fmt::Debug for Message {
// Required method
fn fmt(&self, _f: &mut Formatter<'_>) -> Result<(), fmtError>{
todo!()
}
}
pub fn main() -> iced::Result {
let init_state = || -> State {read_to_string("state.json").ok().map(|s| serde_json::from_str(&s).ok()).flatten().unwrap_or_else(||Default::default())} ;
env_logger::init();
rustls::crypto::aws_lc_rs::default_provider().install_default().unwrap();
iced::application(init_state, State::update, State::view)
.subscription(State::subscription)
.theme(State::theme)

View File

@@ -1 +1 @@
[]
[{"url": "https://beta.alkator.cz/api/card/register", "json": {"racer_id": 60, "starting_number": 2, "card_id": 1364550814, "time": "10.05.2025 17:40:13.486403"}}, {"url": "https://beta.alkator.cz/api/station/register", "json": {"card_id": 1364550814, "time": "10.05.2025 17:42:45.425016", "station_id": 1}}]