tesla-charge-controller/src/config.rs

60 lines
1.8 KiB
Rust
Raw Normal View History

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