max consecutive errors for tristar

This commit is contained in:
Alex Janka 2024-02-12 13:54:03 +11:00
parent 8a9b233630
commit 1be2476579

View file

@ -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<Self, TristarError> {
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:?}");
}
}
}
}