From af67a1e6780c3ac63bfcbfa4fa70f1826d8c8165 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Fri, 12 Jan 2024 09:27:38 +1100 Subject: [PATCH] config updates + adjustable timeout for pli --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/config.rs | 10 ++++++---- src/main.rs | 10 +++++++--- src/pl_interface.rs | 8 ++++++-- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1f73a4f..763ec42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2256,7 +2256,7 @@ dependencies = [ [[package]] name = "tesla-charge-controller" -version = "0.1.17" +version = "0.1.18" dependencies = [ "async-channel", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 7626131..fa3d80a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tesla-charge-controller" -version = "0.1.17" +version = "0.1.18" edition = "2021" license = "MITNFA" description = "Controls Tesla charge rate based on solar charge data" diff --git a/src/config.rs b/src/config.rs index 1cdfce2..565d73b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,8 +4,9 @@ use crate::types::Coords; #[derive(Serialize, Deserialize, Clone, Debug)] pub struct Config { - pub tesla_watch_interval: u64, - pub pl_watch_interval: u64, + pub tesla_watch_interval_seconds: u64, + pub pl_watch_interval_seconds: u64, + pub pl_timeout_milliseconds: u64, pub coords: Coords, pub serial_port: String, pub baud_rate: u32, @@ -14,8 +15,9 @@ pub struct Config { impl Default for Config { fn default() -> Self { Self { - tesla_watch_interval: 120, - pl_watch_interval: 30, + tesla_watch_interval_seconds: 120, + pl_watch_interval_seconds: 30, + pl_timeout_milliseconds: 250, coords: Coords { latitude: 0., longitude: 0., diff --git a/src/main.rs b/src/main.rs index 69837dc..c213b52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,12 +63,16 @@ async fn main() { let (pli_requests, pli_receiver) = async_channel::unbounded(); // try to spawn the pli loop - let pl_state = match Pli::new(config.serial_port.clone(), config.baud_rate) { + let pl_state = match Pli::new( + config.serial_port.clone(), + config.baud_rate, + config.pl_timeout_milliseconds, + ) { Ok(mut pli) => { let pl_state = pli.state.clone(); tokio::task::spawn(async move { let mut interval = tokio::time::interval( - std::time::Duration::from_secs(config.pl_watch_interval), + std::time::Duration::from_secs(config.pl_watch_interval_seconds), ); loop { tokio::select! { @@ -99,7 +103,7 @@ async fn main() { // spawn the api loop tokio::task::spawn(async move { let mut interval = tokio::time::interval(std::time::Duration::from_secs( - config.tesla_watch_interval, + config.tesla_watch_interval_seconds, )); loop { // await either the next interval OR a message from the other thread diff --git a/src/pl_interface.rs b/src/pl_interface.rs index f7ed690..3d5143c 100644 --- a/src/pl_interface.rs +++ b/src/pl_interface.rs @@ -123,9 +123,13 @@ pub enum PliRequest { } impl Pli { - pub fn new(serial_port: String, baud_rate: u32) -> Result { + pub fn new( + serial_port: String, + baud_rate: u32, + timeout: u64, + ) -> Result { let port = serialport::new(serial_port, baud_rate) - .timeout(Duration::from_millis(250)) + .timeout(Duration::from_millis(timeout)) .open()?; describe_gauge!("pl_battery_voltage", "Battery voltage");