max consecutive errors for tristar
This commit is contained in:
parent
8a9b233630
commit
1be2476579
|
@ -47,6 +47,7 @@ pub struct Tristar {
|
||||||
modbus: Modbus,
|
modbus: Modbus,
|
||||||
data_in: [u16; RAM_ARRAY_SIZE],
|
data_in: [u16; RAM_ARRAY_SIZE],
|
||||||
charge_state_gauges: ChargeStateGauges,
|
charge_state_gauges: ChargeStateGauges,
|
||||||
|
consecutive_errors: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Copy)]
|
#[derive(Default, Debug, Clone, Copy)]
|
||||||
|
@ -232,6 +233,8 @@ impl ChargeStateGauges {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MAX_CONSECUTIVE_ERRORS: usize = 5;
|
||||||
|
|
||||||
impl Tristar {
|
impl Tristar {
|
||||||
pub fn new(serial_port: String, baud_rate: i32) -> Result<Self, TristarError> {
|
pub fn new(serial_port: String, baud_rate: i32) -> Result<Self, TristarError> {
|
||||||
let parity = 'N';
|
let parity = 'N';
|
||||||
|
@ -247,6 +250,7 @@ impl Tristar {
|
||||||
modbus,
|
modbus,
|
||||||
data_in: [0; RAM_ARRAY_SIZE],
|
data_in: [0; RAM_ARRAY_SIZE],
|
||||||
charge_state_gauges,
|
charge_state_gauges,
|
||||||
|
consecutive_errors: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,6 +260,7 @@ impl Tristar {
|
||||||
.some_or_print_with("reading tristar state")
|
.some_or_print_with("reading tristar state")
|
||||||
.map(|scaling| TristarState::from_ram(scaling, &self.data_in))
|
.map(|scaling| TristarState::from_ram(scaling, &self.data_in))
|
||||||
{
|
{
|
||||||
|
self.consecutive_errors = 0;
|
||||||
BATTERY_VOLTAGE
|
BATTERY_VOLTAGE
|
||||||
.with_label_values(&[&self.port_name])
|
.with_label_values(&[&self.port_name])
|
||||||
.set(new_state.battery_voltage);
|
.set(new_state.battery_voltage);
|
||||||
|
@ -292,6 +297,14 @@ impl Tristar {
|
||||||
|
|
||||||
self.charge_state_gauges.set(new_state.charge_state);
|
self.charge_state_gauges.set(new_state.charge_state);
|
||||||
self.state = new_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:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue