First set of commits for realtime support

This commit is contained in:
Nahuel Lofeudo 2026-06-06 19:02:02 +01:00
parent cba2b17f57
commit 40f8c46d80
3 changed files with 43 additions and 2 deletions

View File

@ -2,6 +2,7 @@ mod arrival;
mod loader; mod loader;
mod utils; mod utils;
mod refresher; mod refresher;
mod realtime;
pub mod structs; pub mod structs;
use chrono::{DateTime, Local, Timelike}; use chrono::{DateTime, Local, Timelike};
use log::{debug, trace}; use log::{debug, trace};
@ -10,7 +11,7 @@ use gtfs_structures::{Exception, RawTrip};
use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences, Error}}; use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences, Error}};
impl Gtfs { impl Gtfs<'_> {
pub fn get_next_arrivals_for(&self, target_datetime: &DateTime<Local>) -> Option<Vec<Arrival<'_>>> { pub fn get_next_arrivals_for(&self, target_datetime: &DateTime<Local>) -> Option<Vec<Arrival<'_>>> {
let naive_target = target_datetime.naive_local(); let naive_target = target_datetime.naive_local();
@ -98,6 +99,7 @@ impl Gtfs {
let mut zip_reader = zip::ZipArchive::new(zip_file)?; let mut zip_reader = zip::ZipArchive::new(zip_file)?;
let mut gtfs: Gtfs = Gtfs { let mut gtfs: Gtfs = Gtfs {
preferences: prefs,
agencies: Vec::new(), agencies: Vec::new(),
calendar: HashMap::new(), calendar: HashMap::new(),
calendar_dates: HashMap::new(), calendar_dates: HashMap::new(),

36
src/gtfs/realtime.rs Normal file
View File

@ -0,0 +1,36 @@
/***
* Implementation of GTFS-R polling
*/
use std::{str::FromStr, time::Duration};
use reqwest::{blocking::Client, header::{HeaderName, HeaderValue}};
use crate::gtfs::structs::{Arrival, Error, Gtfs};
impl Gtfs<'_> {
fn realtime_update (&self, arrivals: Vec<Arrival<'_>>) -> Result<bool, Error>{
// Poll GTFS-R API
let client = Client::builder()
.timeout(Duration::from_secs(10))
.connect_timeout(Duration::from_secs(5))
.build()?;
let mut response = client
.get(self.preferences.realtime_url)
.header("x-api-key", self.preferences.realtime_api_key)
.send()?;
// Parse response
let response = json::rdr
// Match deltas to existing arrivals
// Update the arrival times
}
}

View File

@ -74,7 +74,10 @@ impl Preferences {
// The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything // The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything
#[derive(Debug)] #[derive(Debug)]
pub struct Gtfs { pub struct Gtfs<'a> {
/// A copy of the preferences struct
pub(crate) preferences: &'a Preferences,
/// 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(crate) agencies: Vec<Agency>, pub(crate) agencies: Vec<Agency>,
/// All Calendar by `service_id` /// All Calendar by `service_id`