config updates + adjustable timeout for pli

This commit is contained in:
Alex Janka 2024-01-12 09:27:38 +11:00
parent ab45b37497
commit af67a1e678
5 changed files with 21 additions and 11 deletions

2
Cargo.lock generated
View file

@ -2256,7 +2256,7 @@ dependencies = [
[[package]] [[package]]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "0.1.17" version = "0.1.18"
dependencies = [ dependencies = [
"async-channel", "async-channel",
"chrono", "chrono",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "0.1.17" version = "0.1.18"
edition = "2021" edition = "2021"
license = "MITNFA" license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data" description = "Controls Tesla charge rate based on solar charge data"

View file

@ -4,8 +4,9 @@ use crate::types::Coords;
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Config { pub struct Config {
pub tesla_watch_interval: u64, pub tesla_watch_interval_seconds: u64,
pub pl_watch_interval: u64, pub pl_watch_interval_seconds: u64,
pub pl_timeout_milliseconds: u64,
pub coords: Coords, pub coords: Coords,
pub serial_port: String, pub serial_port: String,
pub baud_rate: u32, pub baud_rate: u32,
@ -14,8 +15,9 @@ pub struct Config {
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
tesla_watch_interval: 120, tesla_watch_interval_seconds: 120,
pl_watch_interval: 30, pl_watch_interval_seconds: 30,
pl_timeout_milliseconds: 250,
coords: Coords { coords: Coords {
latitude: 0., latitude: 0.,
longitude: 0., longitude: 0.,

View file

@ -63,12 +63,16 @@ async fn main() {
let (pli_requests, pli_receiver) = async_channel::unbounded(); let (pli_requests, pli_receiver) = async_channel::unbounded();
// try to spawn the pli loop // 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) => { Ok(mut pli) => {
let pl_state = pli.state.clone(); let pl_state = pli.state.clone();
tokio::task::spawn(async move { tokio::task::spawn(async move {
let mut interval = tokio::time::interval( 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 { loop {
tokio::select! { tokio::select! {
@ -99,7 +103,7 @@ async fn main() {
// spawn the api loop // spawn the api loop
tokio::task::spawn(async move { tokio::task::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs( let mut interval = tokio::time::interval(std::time::Duration::from_secs(
config.tesla_watch_interval, config.tesla_watch_interval_seconds,
)); ));
loop { loop {
// await either the next interval OR a message from the other thread // await either the next interval OR a message from the other thread

View file

@ -123,9 +123,13 @@ pub enum PliRequest {
} }
impl Pli { impl Pli {
pub fn new(serial_port: String, baud_rate: u32) -> Result<Self, serialport::Error> { pub fn new(
serial_port: String,
baud_rate: u32,
timeout: u64,
) -> Result<Self, serialport::Error> {
let port = serialport::new(serial_port, baud_rate) let port = serialport::new(serial_port, baud_rate)
.timeout(Duration::from_millis(250)) .timeout(Duration::from_millis(timeout))
.open()?; .open()?;
describe_gauge!("pl_battery_voltage", "Battery voltage"); describe_gauge!("pl_battery_voltage", "Battery voltage");