use std::{path::PathBuf, sync::OnceLock}; use serde::{Deserialize, Serialize}; use crate::types::Coords; pub(super) static CONFIG_PATH: OnceLock = OnceLock::new(); static CONFIG: OnceLock = OnceLock::new(); pub fn access_config<'a>() -> &'a Config { CONFIG.get_or_init(|| { serde_json::from_str(&std::fs::read_to_string(CONFIG_PATH.get().unwrap()).unwrap()).unwrap() }) } #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(default)] pub struct Config { pub charge_rate_update_interval_seconds: u64, pub tesla_update_interval_seconds: u64, pub tesla_update_interval_while_charging_seconds: u64, pub pl_watch_interval_seconds: u64, pub pl_timeout_milliseconds: u64, pub coords: Coords, pub serial_port: String, pub baud_rate: u32, pub shutoff_voltage: f64, pub shutoff_voltage_time_seconds: u64, pub min_rate: i64, pub max_rate: i64, pub duty_cycle_too_high: f64, pub duty_cycle_too_low: f64, pub optional_string: Option, } impl Default for Config { fn default() -> Self { Self { charge_rate_update_interval_seconds: 30, tesla_update_interval_seconds: 120, tesla_update_interval_while_charging_seconds: 5, pl_watch_interval_seconds: 30, pl_timeout_milliseconds: 250, coords: Coords { latitude: 0., longitude: 0., }, serial_port: String::from("/dev/ttyUSB0"), baud_rate: 9600, shutoff_voltage: 52., shutoff_voltage_time_seconds: 15, min_rate: 5, max_rate: 10, duty_cycle_too_high: 0.9, duty_cycle_too_low: 0.7, optional_string: None, } } }