diff --git a/Cargo.toml b/Cargo.toml index 7562955..9a735e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,13 +5,14 @@ edition = "2024" host = "x86_64-unknown-linux-gnu" [dependencies] -sdl3 = {version = "0.17", features = ["ttf"]} -serde = "1.0" -gtfs-structures = "0.47" chrono = "0.4" -log = "0.4" -zip = "8.3" csv = "1.4" +gtfs-structures = "0.47" +log = "0.4" +sdl3 = {version = "0.17", features = ["ttf"]} +serde = { "1.0", features = ["derive"]} +serde_yaml = "0.8" +zip = "8.3" [profile.dev] opt-level = 3 diff --git a/resources/dublinbus.yaml b/resources/dublinbus.yaml new file mode 100644 index 0000000..ded3d30 --- /dev/null +++ b/resources/dublinbus.yaml @@ -0,0 +1,18 @@ +screen: + width: 1920 + height: 720 + font-path: "resources/jd_lcd_rounded.ttf" + +gtfs: + routes: + - 15A + - F1 + - F2 + stops: + - 1114 + - 2410 + data-folder: "/home/nahuel/Downloads" + gtfs-url: "https://www.transportforireland.ie/transitData/Data/GTFS_Realtime.zip" + realtime-url: "https://api.nationaltransport.ie/gtfsr/v2/gtfsr" + realtime-api-key: "Not defined" + refresh-seconds: 61 \ No newline at end of file diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..4583089 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,22 @@ +use crate::{gtfs, renderer}; +use std::collections::HashSet; +extern crate serde_derive; +extern crate serde_yaml; + +struct Error { + message: String +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +struct Config { + #[serde(rename = "gtfs")] + pub gtfs_prefs: gtfs::structs::Preferences, + + #[serde(rename = "screen")] + pub screen_prefs: renderer::structs::Prefs, +} + + +pub fn load_config(config_name: String) -> Result { + return Err(Error { message: String::from("Foo") }); +} \ No newline at end of file diff --git a/src/gtfs/structs.rs b/src/gtfs/structs.rs index 5842b5e..4ec21c8 100644 --- a/src/gtfs/structs.rs +++ b/src/gtfs/structs.rs @@ -1,11 +1,29 @@ use std::collections::{HashMap, HashSet}; - use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop}; // This is to store the preferences for the GTFS(-R) side of the code. +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Preferences { + #[serde(rename = "routes")] pub route_numbers: HashSet, + + #[serde(rename = "stops")] pub stop_codes: HashSet, + + #[serde(rename = "data-folder")] + pub data_folder: String, + + #[serde(rename = "gtfs-url")] + pub gtfs_url: String, + + #[serde(rename = "realtime-url")] + pub realtime_url: String, + + #[serde(rename = "realtime-api-key")] + pub realtime_api_key: String, + + #[serde(rename = "refresh-seconds")] + pub refresh_seconds: u32, } diff --git a/src/main.rs b/src/main.rs index cda391b..a909793 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,11 @@ mod gtfs; mod renderer; -use std::{collections::HashSet, ops::Add, process, thread::Builder, time::SystemTime}; +mod config; +use std::{ops::Add, process, thread::Builder, time::SystemTime}; use chrono::{DateTime, Duration, Local, NaiveTime}; use log::{Metadata, Record, error, info}; use sdl3::event::Event; -use crate::{gtfs::structs::{Arrival, Gtfs}, renderer::structs::{DisplayData, DisplayEntry, Screen}}; +use crate::{config::load_config, gtfs::structs::{Arrival, Gtfs}, renderer::structs::{DisplayData, DisplayEntry, Screen}}; const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip"; const NUM_ARRIVALS: usize = 4; @@ -80,17 +81,15 @@ fn main() { } log::set_max_level(log::LevelFilter::Trace); - // Create preferences structures from config - let gtfs_prefs = gtfs::structs::Preferences { - route_numbers: HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]), - stop_codes: HashSet::from([String::from("1114")]) - }; - let screen_prefs = renderer::structs::Prefs { - font_path: String::from("resources/jd_lcd_rounded/jd-lcd-rounded.ttf"), - screen_width: 1920, - screen_height: 720, - }; + let config_result = load_config(String::from("resources/dublinbus.yaml")); + if config_result.is_err() { + error!("Error loading the config file: {:#?}", config_result.err()); + process::exit(-1); + } + // Create preferences structures from config + let gtfs_prefs = config_result.unwrap().gtfs_prefs; + let screen_prefs = config_result.unwrap().screen_prefs; // Init GTFS static info info!("Loading GTFS data..."); diff --git a/src/renderer/structs.rs b/src/renderer/structs.rs index 6e96573..eb71d47 100644 --- a/src/renderer/structs.rs +++ b/src/renderer/structs.rs @@ -1,5 +1,6 @@ use sdl3::{Sdl, pixels::Color, render::Canvas, ttf::Font, video::Window}; - +extern crate serde; +extern crate serde_derive; pub struct Screen<'a> { pub(crate) canvas: Box>, @@ -8,9 +9,16 @@ pub struct Screen<'a> { pub(crate) context: Box } +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Prefs { + + #[serde(rename = "font-path")] pub font_path: String, + + #[serde(rename = "width")] pub screen_width: u32, + + #[serde(rename = "height")] pub screen_height: u32, }