Finished implementing refresh in a separate thread.

This commit is contained in:
Nahuel Lofeudo 2026-05-03 17:32:05 +01:00
parent ca0003edfd
commit 3973ff94b0
7 changed files with 88 additions and 34 deletions

View File

@ -8,13 +8,13 @@ host = "x86_64-unknown-linux-gnu"
sdl3 = {version = "0.17", features = ["ttf"]} sdl3 = {version = "0.17", features = ["ttf"]}
serde = "1.0" serde = "1.0"
gtfs-structures = "0.47" gtfs-structures = "0.47"
chrono = "*" chrono = "0.4"
log = "*" log = "0.4"
zip = "8.3" zip = "8.3"
csv = "1.4" csv = "1.4"
[profile.dev] [profile.dev]
opt-level = 0 opt-level = 1
[profile.release] [profile.release]
opt-level = 3 opt-level = 3

33
src/gtfs/arrival.rs Normal file
View File

@ -0,0 +1,33 @@
use std::cmp::Ordering;
use crate::gtfs::structs::Arrival;
impl Ord for Arrival<'_> {
fn cmp(&self, other: &Self) -> Ordering {
return self.departure_time.cmp(&other.departure_time);
}
}
impl PartialOrd for Arrival<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match self.departure_time.partial_cmp(&other.departure_time) {
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
match self.route.id.partial_cmp(&other.route.id) {
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
self.stop.id.partial_cmp(&other.stop.id)
}
}
impl PartialEq for Arrival<'_> {
fn eq(&self, other: &Self) -> bool {
self.departure_time == other.departure_time && self.route.id == other.route.id && self.stop.id == other.stop.id
}
}
impl Eq for Arrival<'_> {
}

View File

@ -1,3 +1,4 @@
mod arrival;
mod loader; mod loader;
mod utils; mod utils;
pub mod structs; pub mod structs;

View File

@ -14,26 +14,25 @@ pub struct Preferences {
#[derive(Debug)] #[derive(Debug)]
pub struct Gtfs { pub struct Gtfs {
/// All agencies. They can not be read by `agency_id`, as it is not a required field /// All agencies. They can not be read by `agency_id`, as it is not a required field
pub agencies: Vec<Agency>, pub(crate) agencies: Vec<Agency>,
/// All Calendar by `service_id` /// All Calendar by `service_id`
pub calendar: HashMap<String, Calendar>, pub(crate) calendar: HashMap<String, Calendar>,
/// All calendar dates grouped by service_id /// All calendar dates grouped by service_id
pub calendar_dates: HashMap<String, Vec<CalendarDate>>, pub(crate) calendar_dates: HashMap<String, Vec<CalendarDate>>,
/// All routes by `route_id` /// All routes by `route_id`
pub routes: HashMap<String, Route>, pub(crate) routes: HashMap<String, Route>,
/// All stop by `stop_id`. /// All stop by `stop_id`.
pub stops: HashMap<String, Stop>, pub(crate) stops: HashMap<String, Stop>,
/// All trips by trip_id /// All trips by trip_id
pub trips: HashMap<String, RawTrip>, pub(crate) trips: HashMap<String, RawTrip>,
/// Stop times for the chosen stops and the chosen routes /// Stop times for the chosen stops and the chosen routes
pub stop_times: HashMap<(String, u32), RawStopTime>, pub(crate) stop_times: HashMap<(String, u32), RawStopTime>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Arrival<'a> { pub struct Arrival<'a> {
pub departure_time: NaiveTime,
pub route: &'a Route, pub route: &'a Route,
pub stop: &'a Stop, pub stop: &'a Stop,
pub departure_time: NaiveTime,
} }

View File

@ -1,19 +1,28 @@
mod gtfs; mod gtfs;
mod renderer; mod renderer;
use std::{collections::HashSet, process, time::SystemTime}; use std::{collections::HashSet, ops::Add, thread::{Builder}, time::SystemTime};
use chrono::{DateTime, Local}; use chrono::{DateTime, Duration, Local, NaiveTime};
use sdl3::{event::Event, libc::statx_timestamp}; use log::{Metadata, Record, debug, info};
use log::{Level, Metadata, Record, debug, error, info, warn}; use crate::gtfs::structs::{Arrival, Gtfs};
use crate::{gtfs::structs::Gtfs, renderer::Screen};
const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip"; const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip";
const NUM_ARRIVALS: usize = 4;
fn refresh_schedule(gtfs: &Gtfs) { fn refresh_schedule(gtfs: &Gtfs) -> Box<Vec<Arrival<'_>>> {
let current_timestamp = SystemTime::now(); let current_timestamp = SystemTime::now();
let datetime: DateTime<Local> = current_timestamp.clone().into(); let datetime: DateTime<Local> = current_timestamp.clone().into();
let next_arrivals = gtfs.get_next_arrivals_for(&datetime); let mut next_arrivals: Box<Vec<Arrival<'_>>> = gtfs.get_next_arrivals_for(&datetime);
debug!("Next arrivals: {:#?}", next_arrivals);
if next_arrivals.len() < NUM_ARRIVALS {
// If we don't have enough entries today, look for arrivals tomorrow.
let mut tomorrow: DateTime<Local> = datetime.clone();
tomorrow = tomorrow.with_time(NaiveTime::from_hms_opt(0, 0, 0).unwrap()).unwrap();
tomorrow = tomorrow.add(Duration::days(1));
next_arrivals.append(&mut gtfs.get_next_arrivals_for(&tomorrow));
}
next_arrivals.sort();
return next_arrivals;
} }
@ -45,7 +54,7 @@ fn main() {
stop_codes: HashSet::from([String::from("1117")]) stop_codes: HashSet::from([String::from("1117")])
}; };
let screen_prefs = renderer::structs::Prefs { let _screen_prefs = renderer::structs::Prefs {
font_path: String::from("resources/jd_lcd_rounded/jd-lcd-rounded.ttf"), font_path: String::from("resources/jd_lcd_rounded/jd-lcd-rounded.ttf"),
screen_width: 1920, screen_width: 1920,
screen_height: 720, screen_height: 720,
@ -61,10 +70,18 @@ fn main() {
//println!("Startup done."); //println!("Startup done.");
// Start an asynchronous refresh thread // Start an asynchronous refresh thread
//let _update_thread = thread::Builder::new().name("updater".to_string()).spawn(move || { refresh_schedule(&gtfs) }); let update_thread = Builder::new()
.name("updater".to_string())
.spawn(move || {
loop {
let _data = refresh_schedule(&gtfs);
debug!("-------------------------------- Refresh done.");
std::thread::sleep(std::time::Duration::new(60,0));
};
}).unwrap();
let _ = update_thread.join();
refresh_schedule(&gtfs);
debug!("Refresh done.");
// Main event loop // Main event loop
/* /*

View File

@ -1,22 +1,18 @@
pub mod structs; pub mod structs;
use sdl3::{Sdl, pixels::Color, rect::Rect, render::Canvas, ttf::Font, video::Window}; use sdl3::{Sdl, pixels::Color, rect::Rect};
use structs::{Prefs}; use structs::{Prefs};
pub struct Screen<'a> { use crate::renderer::structs::Screen;
canvas: Box<Canvas<Window>>,
font: Box<Font<'a>>,
context: Box<Sdl>
}
impl Screen<'_> { impl Screen<'_> {
pub fn get_context(&self) -> &Sdl { pub fn _get_context(&self) -> &Sdl {
return &self.context; return &self.context;
} }
pub fn print(&mut self, line: u32, text: &str) { pub fn _print(&mut self, line: u32, text: &str) {
let rendered_text = self.font.render(text).solid(Color::RED).unwrap(); let rendered_text = self.font.render(text).solid(Color::RED).unwrap();
let texture_creator = self.canvas.texture_creator(); let texture_creator = self.canvas.texture_creator();
let texture = rendered_text.as_texture(&texture_creator).unwrap(); let texture = rendered_text.as_texture(&texture_creator).unwrap();
@ -35,7 +31,7 @@ impl Screen<'_> {
/// Initialize video, allocate buffers and load fonts /// Initialize video, allocate buffers and load fonts
/// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs /// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs
pub fn init(prefs: &Prefs) -> Screen<'_> { pub fn _init(prefs: &Prefs) -> Screen<'_> {
// Initialize the screen // Initialize the screen
let sdl_context = sdl3::init().unwrap(); let sdl_context = sdl3::init().unwrap();
let video_subsys = sdl_context.video().unwrap(); let video_subsys = sdl_context.video().unwrap();

View File

@ -1,3 +1,11 @@
use sdl3::{Sdl, render::Canvas, ttf::Font, video::Window};
pub struct Screen<'a> {
pub(crate) canvas: Box<Canvas<Window>>,
pub(crate) font: Box<Font<'a>>,
pub(crate) context: Box<Sdl>
}
pub struct Prefs { pub struct Prefs {
pub font_path: String, pub font_path: String,