offline DB
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
{
|
{
|
||||||
"DURATION": 600,
|
"DURATION": 600,
|
||||||
"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
|
||||||
|
}
|
||||||
@@ -11,7 +11,6 @@ pub struct Config {
|
|||||||
pub DURATION: u64,
|
pub DURATION: 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),
|
|
||||||
}
|
|
||||||
@@ -10,12 +10,15 @@ 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 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;
|
||||||
|
|
||||||
|
|
||||||
@@ -23,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,
|
||||||
}
|
}
|
||||||
@@ -38,7 +65,6 @@ impl Default for State {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
racers: vec![],
|
racers: vec![],
|
||||||
db: Default::default(),
|
|
||||||
time: Instant::now(),
|
time: Instant::now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,20 +78,13 @@ 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,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
|
|
||||||
struct Racer{
|
|
||||||
starting_number: usize,
|
|
||||||
card_id: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Message{
|
enum Message{
|
||||||
CardReader(card_reader::Event),
|
CardReader(card_reader::Event),
|
||||||
RacerRecieved(db_update::Event),
|
|
||||||
Tick(Instant),
|
Tick(Instant),
|
||||||
Nothing,
|
Nothing,
|
||||||
}
|
}
|
||||||
@@ -77,9 +96,9 @@ impl std::fmt::Debug for Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn station_register(card_id: usize, time: DateTime::<Local>) -> Result<(), reqwest::Error>{
|
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")),
|
||||||
};
|
};
|
||||||
@@ -99,20 +118,6 @@ async fn register_and_dump(card_id: usize, datetime: DateTime<Local>, json: Stri
|
|||||||
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();
|
|
||||||
let old_racer = self.db.insert(racer.card_id, racer.clone());
|
|
||||||
if old_racer != Some(racer){
|
|
||||||
let json = serde_json::to_string(self).unwrap();
|
|
||||||
Task::perform(dump_state(json), |_| Message::Nothing)
|
|
||||||
}else{
|
|
||||||
Task::none()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Message::Tick(time) => {
|
Message::Tick(time) => {
|
||||||
self.time = time;
|
self.time = time;
|
||||||
Task::none()
|
Task::none()
|
||||||
@@ -121,12 +126,10 @@ impl State{
|
|||||||
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.starting_number == racer.starting_number){
|
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)
|
||||||
}
|
}
|
||||||
let json = serde_json::to_string(self).unwrap();
|
let json = serde_json::to_string(self).unwrap();
|
||||||
@@ -174,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
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -184,7 +187,6 @@ impl State{
|
|||||||
Subscription::batch(vec![
|
Subscription::batch(vec![
|
||||||
time::every(milliseconds(10)).map(Message::Tick),
|
time::every(milliseconds(10)).map(Message::Tick),
|
||||||
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user