Compare commits

..

2 Commits

9 changed files with 208 additions and 47 deletions

View File

@ -14,7 +14,7 @@ zip = "8.3"
csv = "1.4" csv = "1.4"
[profile.dev] [profile.dev]
opt-level = 0 opt-level = 3
[profile.release] [profile.release]
opt-level = 3 opt-level = 3

132
resources/gtfs.rs Normal file
View File

@ -0,0 +1,132 @@
use gtfs_structures::{Agency, Calendar, CalendarDate, Route, Stop};
use serde::{de::DeserializeOwned};
use std::{collections::HashMap, fs::File};
use zip::ZipArchive;
// The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything
#[derive(Debug)]
pub struct Gtfs {
/// All agencies. They can not be read by `agency_id`, as it is not a required field
pub agencies: Vec<Agency>,
/// All Calendar by `service_id`
pub calendar: HashMap<String, Calendar>,
/// All calendar dates grouped by service_id
pub calendar_dates: HashMap<String, Vec<CalendarDate>>,
/// All routes by `route_id`
pub routes: HashMap<String, Route>,
/// All stop by `stop_id`.
pub stops: HashMap<String, Stop>,
}
// Utility function to load all records in a dataset
fn load_all<V>(_: &V) -> bool { true }
// Loads a vector of the selected type
fn load_vector<T: serde::de::DeserializeOwned>(
destination: &mut Vec<T>,
zip_reader: &mut ZipArchive<File>,
table_name: &str,
) {
let file_reader = zip_reader.by_name(table_name).unwrap();
let mut rdr = csv::Reader::from_reader(file_reader);
for row in rdr.deserialize() {
let record: T = row.unwrap();
destination.push(record);
}
}
// Loads a HashMap of the selected type, using the provided index function as the key
fn load_map<'a, V: DeserializeOwned>(
destination: &mut HashMap<String, V>,
zip_reader: &mut ZipArchive<File>,
table_name: &str,
index: fn(&V) -> String,
accept: fn(&V) -> bool,
) {
let file_reader = zip_reader.by_name(table_name).unwrap();
let mut rdr = csv::Reader::from_reader(file_reader);
for row in rdr.deserialize() {
let record: V = row.unwrap();
if accept(&record) {
let idx: String = index(&record);
destination.insert(idx, record);
}
}
}
// Loads a HashMap of a vector of the selected type, using the provided index function as the key
// And a predicate as a filter
fn load_vector_map<'a, V: DeserializeOwned + Clone>(
destination: &mut HashMap<String, Vec<V>>,
zip_reader: &mut ZipArchive<File>,
table_name: &str,
index: fn(&V) -> String,
accept: fn(&V) -> bool,
) {
let file_reader = zip_reader.by_name(table_name).unwrap();
let mut rdr = csv::Reader::from_reader(file_reader);
for row in rdr.deserialize() {
let record: V = row.unwrap();
if accept(&record) {
let idx: String = index(&record);
destination.entry(idx).or_insert_with(Vec::new).push(record);
}
}
}
pub fn init(src_file: &str, routes: Vec<String>) -> Gtfs {
// Open zip file
let mut zip_reader = zip::ZipArchive::new(File::open(src_file).unwrap()).unwrap();
let mut gtfs: Gtfs = Gtfs {
agencies: Vec::new(),
calendar: HashMap::new(),
calendar_dates: HashMap::new(),
routes: HashMap::new(),
stops: HashMap::new(),
};
// Agencies
load_vector(&mut gtfs.agencies, &mut zip_reader, "agency.txt");
// Calendars
load_map(
&mut gtfs.calendar,
&mut zip_reader,
"calendar.txt",
|c: &Calendar| String::from(&c.id),
load_all
);
// Calendar Dates
load_vector_map(
&mut gtfs.calendar_dates,
&mut zip_reader,
"calendar_dates.txt",
|d: &CalendarDate| String::from(&d.service_id),
load_all
);
// Stops
load_map(&mut gtfs.stops,
&mut zip_reader,
"stops.txt",
|s: &Stop| {String::from(&s.id)},
load_all
);
// Routes
let accept_filter: fn(&Route) -> bool = (| rs: Vec<String>, r: &Route | {rs.contains(&r.short_name.unwrap())}).curry(routes);
load_map(
&mut gtfs.routes,
&mut zip_reader,
"routes.txt",
|r: &Route| String::from(&r.id),
accept_filter
);
return gtfs;
}

View File

@ -1,6 +1,6 @@
use log::debug; use log::debug;
use gtfs_structures::{Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop}; use gtfs_structures::{Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
use serde::de::{DeserializeOwned, value::MapAccessDeserializer}; use serde::de::{DeserializeOwned};
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
fs::File, fs::File,

View File

@ -2,8 +2,9 @@ mod arrival;
mod loader; mod loader;
mod utils; mod utils;
pub mod structs; pub mod structs;
use chrono::{DateTime, Local, NaiveTime, Timelike}; use chrono::{DateTime, Local, Timelike};
use log::{debug}; use log::{debug};
use sdl3::sys::pixels::SDL_ArrayOrder;
use std::{ use std::{
collections::{HashMap, HashSet}, fs::File, io::Error collections::{HashMap, HashSet}, fs::File, io::Error
}; };
@ -66,7 +67,6 @@ impl Gtfs {
for (_id, stop_time) in self.stop_times.iter() { for (_id, stop_time) in self.stop_times.iter() {
if trips.contains_key(&stop_time.trip_id) { if trips.contains_key(&stop_time.trip_id) {
let stop_timestamp = stop_time.departure_time.or(stop_time.arrival_time)?; let stop_timestamp = stop_time.departure_time.or(stop_time.arrival_time)?;
debug!("Stop timestamp {} current timestamp {}", stop_timestamp, current_timestamp);
let trip= &self.trips.get(&stop_time.trip_id).unwrap(); let trip= &self.trips.get(&stop_time.trip_id).unwrap();
if current_timestamp < stop_timestamp.into() { if current_timestamp < stop_timestamp.into() {
let arrival: Arrival = Arrival { let arrival: Arrival = Arrival {
@ -76,6 +76,7 @@ impl Gtfs {
trip: &trip, trip: &trip,
departure_time: stop_timestamp.into() departure_time: stop_timestamp.into()
}; };
debug!("Arrival to {:#?} for trip ID {:#?}.", arrival.trip.trip_headsign.as_ref().unwrap(), arrival.trip.id);
arrivals.push(arrival); arrivals.push(arrival);
} }
} }

View File

@ -1,6 +1,5 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use chrono::NaiveTime;
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop}; use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
// This is to store the preferences for the GTFS(-R) side of the code. // This is to store the preferences for the GTFS(-R) side of the code.
@ -32,7 +31,7 @@ pub struct Gtfs {
#[derive(Debug)] #[derive(Debug)]
pub struct Arrival<'a> { pub struct Arrival<'a> {
pub departure_time: i64, pub departure_time: u32,
pub route: &'a Route, pub route: &'a Route,
pub stop: &'a Stop, pub stop: &'a Stop,
pub stop_time: &'a RawStopTime, pub stop_time: &'a RawStopTime,

View File

@ -1,7 +1,7 @@
use std::collections::HashSet; use std::collections::HashSet;
use crate::gtfs::Gtfs; use crate::gtfs::Gtfs;
pub fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) -> HashSet<String> { pub(crate) fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) -> HashSet<String> {
let mut ids: HashSet<String> = HashSet::new(); let mut ids: HashSet<String> = HashSet::new();
for stop in &gtfs.stops { for stop in &gtfs.stops {
@ -13,7 +13,7 @@ pub fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) -> HashSet
return ids; return ids;
} }
pub fn route_ids_from_numbers(gtfs: &Gtfs, route_numbers: &HashSet<String>) -> HashSet<String> { pub(crate) fn route_ids_from_numbers(gtfs: &Gtfs, route_numbers: &HashSet<String>) -> HashSet<String> {
let mut ids: HashSet<String> = HashSet::new(); let mut ids: HashSet<String> = HashSet::new();
for route in &gtfs.routes { for route in &gtfs.routes {

View File

@ -1,13 +1,15 @@
mod gtfs; mod gtfs;
mod renderer; mod renderer;
use std::{collections::HashSet, ops::Add, os::unix::process::ExitStatusExt, process, thread::Builder, time::SystemTime}; use std::{collections::HashSet, ops::Add, process, thread::Builder, time::SystemTime};
use chrono::{DateTime, Duration, Local, NaiveTime, Timelike}; use chrono::{DateTime, Duration, Local, NaiveTime};
use log::{Metadata, Record, debug, error, info}; use log::{Metadata, Record, error, info};
use sdl3::event::Event; use sdl3::event::Event;
use crate::{gtfs::structs::{Arrival, Gtfs}, renderer::structs::{DisplayData, DisplayEntry, Screen}}; use crate::{gtfs::structs::{Arrival, Gtfs}, renderer::structs::{DisplayData, DisplayEntry, 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; const NUM_ARRIVALS: usize = 4;
const UPDATE_INTERVAL_SECONDS: u64 = 62;
// Custom Event to signal data refresh // Custom Event to signal data refresh
#[derive(Debug)] #[derive(Debug)]
@ -31,8 +33,6 @@ fn refresh_schedule<'a>(gtfs: &'a Gtfs, screen : &mut Screen<'a>) -> Option<Vec<
next_arrivals.sort(); next_arrivals.sort();
// Create the DisplayData structure to render the information to screen // Create the DisplayData structure to render the information to screen
let now =Local::now();
let current_time: i64 = (now.hour() * 3600 + now.minute() * 60 + now.second()).into();
let mut display_data: DisplayData = DisplayData { let mut display_data: DisplayData = DisplayData {
lines: Vec::<DisplayEntry>::new(), lines: Vec::<DisplayEntry>::new(),
status: None status: None
@ -46,7 +46,7 @@ fn refresh_schedule<'a>(gtfs: &'a Gtfs, screen : &mut Screen<'a>) -> Option<Vec<
.or(Option::Some(String::from("Unknown") .or(Option::Some(String::from("Unknown")
))).unwrap(), ))).unwrap(),
route: arrival.route.short_name.clone().or(arrival.route.long_name.clone()).unwrap(), route: arrival.route.short_name.clone().or(arrival.route.long_name.clone()).unwrap(),
due_in: (arrival.departure_time - current_time) / 60, departure_time: arrival.departure_time,
} }
})); }));
@ -83,7 +83,7 @@ fn main() {
// Create preferences structures from config // Create preferences structures from config
let gtfs_prefs = gtfs::structs::Preferences { let gtfs_prefs = gtfs::structs::Preferences {
route_numbers: HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]), route_numbers: HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]),
stop_codes: HashSet::from([String::from("1117")]) stop_codes: HashSet::from([String::from("1114")])
}; };
let screen_prefs = renderer::structs::Prefs { let screen_prefs = renderer::structs::Prefs {
@ -117,7 +117,7 @@ fn main() {
.name("updater".to_string()) .name("updater".to_string())
.spawn(move || { .spawn(move || {
loop { loop {
std::thread::sleep(std::time::Duration::new(60,0)); std::thread::sleep(std::time::Duration::new(UPDATE_INTERVAL_SECONDS,0));
let event = RefreshDataEvent {}; let event = RefreshDataEvent {};
let send_result = event_sender.push_custom_event(event); let send_result = event_sender.push_custom_event(event);
if send_result.is_err() { if send_result.is_err() {

View File

@ -1,20 +1,18 @@
pub mod structs; pub mod structs;
use std::cmp::min; use std::{cmp::min};
use log::{error, warn};
use sdl3::{Sdl, pixels::Color, rect::Rect}; use sdl3::{Sdl, pixels::Color, rect::Rect};
use structs::{Prefs, DisplayData}; use structs::{Prefs, DisplayData};
use crate::renderer::structs::Screen; use crate::renderer::structs::Screen;
const LINE_COUNT: i32 = 6; const LINE_COUNT: i32 = 5;
const COLOR_LCD_AMBER : Color = Color::RGB(0xf4, 0xcb, 0x60); const COLOR_LCD_AMBER : Color = Color::RGB(0xf4, 0xcb, 0x60);
const COLOR_LCD_GREEN : Color = Color::RGB(0xb3, 0xff, 0x00); const COLOR_LCD_GREEN : Color = Color::RGB(0xb3, 0xff, 0x00);
const COLOR_LCD_RED : Color = Color::RGB(0xff, 0x3a, 0x4a); const COLOR_LCD_RED : Color = Color::RGB(0xff, 0x3a, 0x4a);
const COLOR_BACKGROUND : Color = Color::RGB(0x0, 0x0, 0x0 );
//const COLOR_BACKGROUND = pygame.Color(0, 0, 0) const COLOR_TEXT_BG : Color = Color::RGBA(0x0, 0x0, 0x0, 0x0);
const UPDATE_INTERVAL_SECONDS: u32 = 62; const TEXT_SIZE: f32 = 160.0; // Size of the font in pixels
const TEXT_SIZE: u32 = 160; // Size of the font in pixels
// Offsets of each part within a line // Offsets of each part within a line
const XOFFSET_ROUTE: u32 = 24; const XOFFSET_ROUTE: u32 = 24;
@ -22,7 +20,6 @@ const XOFFSET_DESTINATION: u32 = 300;
const XOFFSEET_TIME_LEFT: u32 = 1606; const XOFFSEET_TIME_LEFT: u32 = 1606;
const INTER_LINE_OVERLAP: u32 = 15; const INTER_LINE_OVERLAP: u32 = 15;
impl Screen<'_> { impl Screen<'_> {
pub fn get_context(&self) -> &Sdl { pub fn get_context(&self) -> &Sdl {
@ -35,48 +32,80 @@ impl Screen<'_> {
return COLOR_LCD_RED; return COLOR_LCD_RED;
} }
fn format_due_for(&self, due_in: i32, departure_time: u32) -> String {
if due_in < 60 {
return String::from("due");
}
if due_in < 3600 {
return ((due_in / 60) as i32).to_string() + "min";
}
return format!("{:02}:{:02}", (departure_time / 3600) as i32, ((departure_time % 3600) / 60) as i32);
}
pub fn update_information(&mut self, display_data: &DisplayData) { pub fn update_information(&mut self, display_data: &DisplayData) {
self.do_clear(); self.do_clear();
// TODO: Add header // Print status first
if display_data.status.is_some() {
// If the update has some information text, show it
self.do_print_at(5, display_data.status.as_ref().unwrap(), 0);
} else {
// Display date and time otherwise
self.do_print_at(5, &format!("TODO: DATE/TIME GOES HERE").to_string(), 0);
}
let num_arrivals: i32 = min(if display_data.status.is_some() {LINE_COUNT - 1} else {LINE_COUNT}, display_data.lines.len().try_into().unwrap()); // Then data lines from the bottom up
for line in 0..num_arrivals { let num_arrivals: i32 = min(LINE_COUNT, display_data.lines.len() as i32);
for index in 0..num_arrivals {
let line: u32 = ((num_arrivals - 1) - index) as u32;
// Compose a line of text with all the information // Compose a line of text with all the information
let entry = display_data.lines.get(line as usize).unwrap(); let entry = display_data.lines.get(line as usize).unwrap();
let line: u32 = (line + 1).try_into().unwrap(); let due_in_mins = (entry.departure_time / 60) as i32;
let due_in_mins = entry.due_in as i32; let due_color: Color = self.color_for(due_in_mins);
let arrival_color: Color = self.color_for(due_in_mins); let due_text = self.format_due_for(due_in_mins, entry.departure_time);
self.color = COLOR_LCD_AMBER; self.color = COLOR_LCD_AMBER;
self.do_print_at(line, &entry.route, XOFFSET_ROUTE); self.do_print_at(line, &entry.route, XOFFSET_ROUTE);
self.do_print_at(line, &entry.destination, XOFFSET_DESTINATION); self.do_print_at(line, &entry.destination, XOFFSET_DESTINATION);
self.color = arrival_color; self.color = due_color;
self.do_print_at(line, &due_in_mins.to_string(), XOFFSEET_TIME_LEFT); self.do_print_at(line, &due_text, XOFFSEET_TIME_LEFT);
}; };
if display_data.status.is_some() {
self.do_print_at(5, display_data.status.as_ref().unwrap(), 0);
}
self.do_update(); self.do_update();
} }
pub fn print(&mut self, line: u32, text: &str) { pub fn _print(&mut self, line: u32, text: &str) {
self.do_print_at(line, text, 0); self.do_print_at(line, text, 0);
self.do_update(); self.do_update();
} }
fn do_print_at(&mut self, line: u32, text: &str, left: u32) -> u32 { fn do_print_at(&mut self, line: u32, text: &str, left: u32) {
let rendered_text = self.font.render(text).solid(self.color).unwrap(); if text.len() == 0 {
warn!("do_print_at called with a 0-length string");
return;
}
let render_result = self.font.render(text).lcd(self.color, COLOR_BACKGROUND);
if render_result.is_err() {
error!("Error rendering text \"{}\": {:#?}", text, render_result.err());
return;
}
let rendered_text = render_result.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);
let _= self.canvas.copy(&texture,
if texture.is_err() {
error!("Error creating texture from rendered text: {:#?}", texture.err());
return;
}
let _= self.canvas.copy(&texture.unwrap(),
Rect::new(0, 0, rendered_text.width(), rendered_text.height()), Rect::new(0, 0, rendered_text.width(), rendered_text.height()),
Rect::new(left.try_into().unwrap(), (line * (rendered_text.height() - INTER_LINE_OVERLAP)).try_into().unwrap(), rendered_text.width(), rendered_text.height())); Rect::new(left.try_into().unwrap(), (line * (rendered_text.height() - INTER_LINE_OVERLAP)).try_into().unwrap(), rendered_text.width(), rendered_text.height()));
return left + rendered_text.width();
} }
@ -92,7 +121,7 @@ impl Screen<'_> {
fn do_clear(&mut self) { fn do_clear(&mut self) {
self.canvas.set_draw_color(Color::BLACK); self.canvas.set_draw_color(COLOR_BACKGROUND);
self.canvas.clear(); self.canvas.clear();
} }
@ -112,8 +141,8 @@ impl Screen<'_> {
// Load font // Load font
let ttf_context = sdl3::ttf::init().unwrap(); let ttf_context = sdl3::ttf::init().unwrap();
let font = ttf_context.load_font(&prefs.font_path, 128.0).unwrap(); let font = ttf_context.load_font(&prefs.font_path, TEXT_SIZE).unwrap();
let mut screen: Screen = Screen { let mut screen: Screen = Screen {
canvas: Box::new(window.into_canvas()), canvas: Box::new(window.into_canvas()),
font: Box::new(font), font: Box::new(font),

View File

@ -18,7 +18,7 @@ pub struct Prefs {
pub struct DisplayEntry { pub struct DisplayEntry {
pub route: String, pub route: String,
pub destination: String, pub destination: String,
pub due_in: i64, pub departure_time: u32,
} }
pub struct DisplayData { pub struct DisplayData {