From 1be247657920de839f6cfd9cc06466942675750c Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 12 Feb 2024 13:54:03 +1100 Subject: [PATCH] max consecutive errors for tristar --- src/charge_controllers/tristar.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/charge_controllers/tristar.rs b/src/charge_controllers/tristar.rs index f4af5f1..f903de1 100644 --- a/src/charge_controllers/tristar.rs +++ b/src/charge_controllers/tristar.rs @@ -47,6 +47,7 @@ pub struct Tristar { modbus: Modbus, data_in: [u16; RAM_ARRAY_SIZE], charge_state_gauges: ChargeStateGauges, + consecutive_errors: usize, } #[derive(Default, Debug, Clone, Copy)] @@ -232,6 +233,8 @@ impl ChargeStateGauges { } } +const MAX_CONSECUTIVE_ERRORS: usize = 5; + impl Tristar { pub fn new(serial_port: String, baud_rate: i32) -> Result { let parity = 'N'; @@ -247,6 +250,7 @@ impl Tristar { modbus, data_in: [0; RAM_ARRAY_SIZE], charge_state_gauges, + consecutive_errors: 0, }) } @@ -256,6 +260,7 @@ impl Tristar { .some_or_print_with("reading tristar state") .map(|scaling| TristarState::from_ram(scaling, &self.data_in)) { + self.consecutive_errors = 0; BATTERY_VOLTAGE .with_label_values(&[&self.port_name]) .set(new_state.battery_voltage); @@ -292,6 +297,14 @@ impl Tristar { self.charge_state_gauges.set(new_state.charge_state); self.state = new_state; + } else { + self.consecutive_errors += 1; + if self.consecutive_errors >= MAX_CONSECUTIVE_ERRORS { + self.modbus.close(); + if let Err(e) = self.modbus.connect() { + log::error!("error reconnecting to modbus device: {e:?}"); + } + } } }