Compare commits
4 Commits
d7c5f0f048
...
online
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f44a68e5e8 | ||
|
|
bf531b477f | ||
|
|
6aeb5e1c2a | ||
|
|
ecb51125cc |
1
POC_in_rust/.gitignore
vendored
Normal file
1
POC_in_rust/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target
|
||||||
@@ -3,17 +3,19 @@ name = "POC_in_rust"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
opt-level="s"
|
||||||
|
lto="fat"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-tungstenite = {version="0.29.1", features=["tokio-rustls-webpki-roots"]}
|
async-tungstenite = {version="0.29.1", features=["tokio-rustls-webpki-roots"]}
|
||||||
chrono = "0.4.41"
|
chrono = "0.4.41"
|
||||||
env_logger = "0.11.8"
|
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", "sipper"]}
|
||||||
iced = {"git"= "https://github.com/iced-rs/iced.git", "features"=["tokio", "auto-detect-theme", "fira-sans", "tiny-skia", "wgpu", "sipper"]}
|
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
nusb = "0.1.13"
|
nusb = "0.1.13"
|
||||||
reqwest = { version = "0.12.15", features = ["json", "multipart", "http2", "cookies"] }
|
reqwest = { version = "0.12.15", features = ["json"] }
|
||||||
reqwest-middleware = "0.4.2"
|
rustls = {version = "0.23.27", features = ["std", "prefer-post-quantum", "tls12", "logging"]}
|
||||||
rustls = "0.23.27"
|
|
||||||
serde = {version = "1.0.219", features=["derive"]}
|
serde = {version = "1.0.219", features=["derive"]}
|
||||||
serde_json = "1.0.140"
|
serde_json = "1.0.140"
|
||||||
tokio = {version = "1.45.0", features=["time", "fs", "io-util", "macros"]}
|
tokio = {version = "1.45.0", features=["time", "fs", "io-util", "macros"]}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
{
|
{
|
||||||
"DURATION": 600,
|
"DURATION": 600,
|
||||||
"SAVING_INTERVAL": 60,
|
|
||||||
"STATION_ID": 2,
|
"STATION_ID": 2,
|
||||||
"HOST": "https://beta.alkator.cz",
|
"HOST": "https://beta.alkator.cz"
|
||||||
"WSS_HOST": "wss://beta.alkator.cz"
|
|
||||||
}
|
}
|
||||||
102
POC_in_rust/people.json
Normal file
102
POC_in_rust/people.json
Normal 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
|
||||||
|
}
|
||||||
@@ -9,10 +9,8 @@ use lazy_static::lazy_static;
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub DURATION: u64,
|
pub DURATION: u64,
|
||||||
pub SAVING_INTERVAL: u64,
|
|
||||||
pub STATION_ID: usize,
|
pub STATION_ID: usize,
|
||||||
pub HOST: String,
|
pub HOST: String,
|
||||||
pub WSS_HOST: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config_from_file<P: AsRef<Path>>(path: P) -> Result<Config, Box<dyn Error>> {
|
fn read_config_from_file<P: AsRef<Path>>(path: P) -> Result<Config, Box<dyn Error>> {
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
use iced::futures;
|
|
||||||
use iced::task::{Never, Sipper, sipper};
|
|
||||||
|
|
||||||
use futures::stream::StreamExt;
|
|
||||||
|
|
||||||
use async_tungstenite::tungstenite;
|
|
||||||
use crate::config::CONFIG;
|
|
||||||
|
|
||||||
pub fn connect() -> impl Sipper<Never, Event> {
|
|
||||||
sipper(async |mut output| {
|
|
||||||
let endpoint_addr: String = CONFIG.WSS_HOST.clone() + "/api/live/racers";
|
|
||||||
loop {
|
|
||||||
|
|
||||||
let mut websocket =
|
|
||||||
match async_tungstenite::tokio::connect_async(endpoint_addr.as_str()).await
|
|
||||||
{
|
|
||||||
Ok((websocket, _)) => {
|
|
||||||
websocket.fuse()
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(1))
|
|
||||||
.await;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
println!("Websocket Connected!");
|
|
||||||
loop {
|
|
||||||
futures::select! {
|
|
||||||
received = websocket.select_next_some() => {
|
|
||||||
match received {
|
|
||||||
Ok(tungstenite::Message::Text(message)) => {
|
|
||||||
output.send(Event::MessageReceived(String::from(message.as_str()))).await;
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
println!("Websocket Disconnected!");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Ok(_) => {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum Event {
|
|
||||||
MessageReceived(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::collections::HashMap;
|
||||||
use std::fmt::{Formatter, Error as fmtError};
|
use std::fmt::{Formatter, Error as fmtError};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -9,12 +9,16 @@ use tokio::io::AsyncWriteExt;
|
|||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use chrono::{DateTime, Local};
|
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 card_reader;
|
||||||
mod approx_time;
|
mod approx_time;
|
||||||
mod config;
|
mod config;
|
||||||
mod db_update;
|
|
||||||
use config::CONFIG;
|
use config::CONFIG;
|
||||||
|
|
||||||
|
|
||||||
@@ -22,13 +26,37 @@ use config::CONFIG;
|
|||||||
struct RacerTime{
|
struct RacerTime{
|
||||||
#[serde(with = "approx_time")]
|
#[serde(with = "approx_time")]
|
||||||
time: Instant,
|
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)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct State{
|
struct State{
|
||||||
racers: Vec<RacerTime>,
|
racers: Vec<RacerTime>,
|
||||||
db: HashMap<usize, Racer>,
|
|
||||||
#[serde(with = "approx_time")]
|
#[serde(with = "approx_time")]
|
||||||
time: Instant,
|
time: Instant,
|
||||||
}
|
}
|
||||||
@@ -36,8 +64,7 @@ struct State{
|
|||||||
impl Default for State {
|
impl Default for State {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
racers: vec![RacerTime{time: Instant::now(), racer: Racer{starting_number:11, card_id: 0}}],
|
racers: vec![],
|
||||||
db: Default::default(),
|
|
||||||
time: Instant::now(),
|
time: Instant::now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,14 +78,27 @@ async fn dump_state(json: String) -> Result<(), tokio::io::Error> {
|
|||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct RegisterRacer{
|
struct RegisterRacer{
|
||||||
card_id: usize,
|
starting_number: usize,
|
||||||
time: String,
|
time: String,
|
||||||
station_id: usize,
|
station_id: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn station_register(card_id: usize, time: DateTime::<Local>) -> Result<(), reqwest::Error>{
|
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{
|
let register_racer = RegisterRacer{
|
||||||
card_id: card_id,
|
starting_number: starting_number,
|
||||||
station_id: CONFIG.STATION_ID,
|
station_id: CONFIG.STATION_ID,
|
||||||
time: format!("{}", time.format("%d.%m.%Y %H:%M:%S.%6f")),
|
time: format!("{}", time.format("%d.%m.%Y %H:%M:%S.%6f")),
|
||||||
};
|
};
|
||||||
@@ -71,43 +111,29 @@ async fn station_register(card_id: usize, time: DateTime::<Local>) -> Result<(),
|
|||||||
Ok(())
|
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{
|
impl State{
|
||||||
fn update(&mut self, message: Message) -> Task<Message>{
|
fn update(&mut self, message: Message) -> Task<Message>{
|
||||||
match message {
|
match message {
|
||||||
Message::RacerRecieved(event) => {
|
|
||||||
match event {
|
|
||||||
db_update::Event::MessageReceived(s) => {
|
|
||||||
let racer: Racer = serde_json::from_str(s.as_str()).unwrap();
|
|
||||||
self.db.insert(racer.card_id, racer);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Message::Tick(time) => {
|
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;
|
self.time = time;
|
||||||
Task::none()
|
Task::none()
|
||||||
},
|
},
|
||||||
Message::DumpState(_) => {
|
|
||||||
let json = serde_json::to_string(self).unwrap();
|
|
||||||
Task::perform(dump_state(json), |_| Message::Nothing)
|
|
||||||
},
|
|
||||||
Message::Nothing => Task::none(),
|
Message::Nothing => Task::none(),
|
||||||
Message::CardReader(card_reader::Event::ReadedCardId(card_id)) => {
|
Message::CardReader(card_reader::Event::ReadedCardId(card_id)) => {
|
||||||
let time = Instant::now();
|
let time = Instant::now();
|
||||||
let datetime = Local::now();
|
let datetime = Local::now();
|
||||||
if let Some(racer) = self.db.get(&card_id){
|
if let Some(&racer_starting_number) = DB.get(&card_id){
|
||||||
if !self.racers.iter().any(|rt| rt.racer.card_id == card_id){
|
let racer_time = RacerTime{time, racer: racer_starting_number};
|
||||||
let racer_time = RacerTime{time, racer: racer.clone()};
|
self.racers.push(racer_time);
|
||||||
self.racers.push(racer_time);
|
} else {
|
||||||
}
|
|
||||||
}else{
|
|
||||||
println!("Racer {} not found in db!", card_id)
|
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) => {
|
Message::CardReader(card_reader::Event::Connected) => {
|
||||||
println!("Card Reader Connected!");
|
println!("Card Reader Connected!");
|
||||||
@@ -136,10 +162,12 @@ impl State{
|
|||||||
} else {
|
} else {
|
||||||
(duration - time, false)
|
(duration - time, false)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
let seconds = duration.as_secs();
|
let seconds = duration.as_secs();
|
||||||
|
|
||||||
|
if negative && seconds >= 10 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
let duration = text!(
|
let duration = text!(
|
||||||
"{}{:0>2}:{:0>2}.{:0>2}",
|
"{}{:0>2}:{:0>2}.{:0>2}",
|
||||||
if negative {"-"} else {""},
|
if negative {"-"} else {""},
|
||||||
@@ -149,7 +177,7 @@ impl State{
|
|||||||
).size(20).width(120).align_x(Center).align_y(Center);
|
).size(20).width(120).align_x(Center).align_y(Center);
|
||||||
content = content.push(
|
content = content.push(
|
||||||
row![
|
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
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -158,9 +186,7 @@ impl State{
|
|||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
Subscription::batch(vec![
|
Subscription::batch(vec![
|
||||||
time::every(milliseconds(10)).map(Message::Tick),
|
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(card_reader::connect).map(Message::CardReader),
|
||||||
Subscription::run(db_update::connect).map(Message::RacerRecieved)
|
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
fn theme(&self) -> Theme {
|
fn theme(&self) -> Theme {
|
||||||
@@ -168,28 +194,6 @@ impl State{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
|
||||||
struct Racer{
|
|
||||||
starting_number: usize,
|
|
||||||
card_id: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Message{
|
|
||||||
CardReader(card_reader::Event),
|
|
||||||
RacerRecieved(db_update::Event),
|
|
||||||
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 {
|
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())} ;
|
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();
|
env_logger::init();
|
||||||
|
|||||||
Reference in New Issue
Block a user