ccs: pull settings timer out of tristar

This commit is contained in:
Alex Janka 2025-01-13 13:28:52 +11:00
parent d0018b7953
commit b28f39667d
2 changed files with 32 additions and 35 deletions

View file

@ -10,6 +10,7 @@ pub struct Controller {
data: std::sync::Arc<ControllerData>, data: std::sync::Arc<ControllerData>,
voltage_rx: Option<tokio::sync::mpsc::UnboundedReceiver<VoltageCommand>>, voltage_rx: Option<tokio::sync::mpsc::UnboundedReceiver<VoltageCommand>>,
voltage_tx: Option<MultiTx>, voltage_tx: Option<MultiTx>,
settings_last_read: Option<std::time::Instant>,
} }
#[derive(Default, serde::Serialize, Clone)] #[derive(Default, serde::Serialize, Clone)]
@ -94,6 +95,7 @@ impl Controller {
data, data,
voltage_rx, voltage_rx,
voltage_tx: None, voltage_tx: None,
settings_last_read: None,
}, },
voltage_tx, voltage_tx,
)) ))
@ -104,7 +106,7 @@ impl Controller {
} }
pub async fn refresh(&mut self) -> eyre::Result<()> { pub async fn refresh(&mut self) -> eyre::Result<()> {
let (data, settings) = self.inner.refresh().await?; let data = self.inner.refresh().await?;
if let Some(tx) = self.voltage_tx.as_mut() { if let Some(tx) = self.voltage_tx.as_mut() {
if crate::config::access_config() if crate::config::access_config()
@ -123,8 +125,16 @@ impl Controller {
} }
*self.data.write_state().await = Some(data); *self.data.write_state().await = Some(data);
if let Some(settings) = settings { if self.needs_new_settings() {
*self.data.write_settings().await = Some(settings); match self.inner.get_settings().await {
Ok(s) => {
*self.data.write_settings().await = s;
self.settings_last_read = Some(std::time::Instant::now());
}
Err(e) => {
log::error!("couldn't read config from {}: {e:?}", self.name);
}
}
} }
Ok(()) Ok(())
@ -159,6 +169,12 @@ impl Controller {
} }
} }
} }
pub fn needs_new_settings(&self) -> bool {
self.settings_last_read.is_none_or(|t| {
std::time::Instant::now().duration_since(t) >= std::time::Duration::from_secs(60 * 60)
})
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -174,38 +190,33 @@ impl MultiTx {
} }
} }
#[expect(clippy::large_enum_variant)]
pub enum ControllerInner { pub enum ControllerInner {
Pl(pl::Pli), Pl(pl::Pli),
Tristar(tristar::Tristar), Tristar(tristar::Tristar),
} }
impl ControllerInner { impl ControllerInner {
pub async fn refresh(&mut self) -> eyre::Result<(ControllerState, Option<ControllerSettings>)> { pub async fn refresh(&mut self) -> eyre::Result<ControllerState> {
match self { match self {
ControllerInner::Pl(pli) => { ControllerInner::Pl(pli) => {
let pl_data = pli.refresh().await?; let pl_data = pli.refresh().await?;
Ok((ControllerState::Pl(pl_data), None)) Ok(ControllerState::Pl(pl_data))
} }
ControllerInner::Tristar(tristar) => { ControllerInner::Tristar(tristar) => {
let settings = if tristar.needs_new_settings() {
match tristar.read_settings().await {
Ok(v) => Some(ControllerSettings::Tristar(v)),
Err(e) => {
log::error!(
"couldn't read config from tristar {}: {e:?}",
tristar.name()
);
None
}
}
} else {
None
};
let tristar_data = tristar.refresh().await?; let tristar_data = tristar.refresh().await?;
Ok((ControllerState::Tristar(tristar_data), settings)) Ok(ControllerState::Tristar(tristar_data))
}
}
}
pub async fn get_settings(&mut self) -> eyre::Result<Option<ControllerSettings>> {
match self {
ControllerInner::Pl(_) => Ok(None),
ControllerInner::Tristar(tristar) => {
let settings = tristar.read_settings().await?;
Ok(Some(ControllerSettings::Tristar(settings)))
} }
} }
} }

View file

@ -54,7 +54,6 @@ pub struct Tristar {
charge_state_gauges: ChargeStateGauges, charge_state_gauges: ChargeStateGauges,
consecutive_errors: usize, consecutive_errors: usize,
scaling: Scaling, scaling: Scaling,
settings_last_read: Option<std::time::Instant>,
transport_settings: crate::config::Transport, transport_settings: crate::config::Transport,
} }
@ -818,7 +817,6 @@ impl Tristar {
charge_state_gauges, charge_state_gauges,
consecutive_errors: 0, consecutive_errors: 0,
scaling, scaling,
settings_last_read: None,
transport_settings: transport.clone(), transport_settings: transport.clone(),
}) })
} }
@ -906,17 +904,9 @@ impl Tristar {
let charge = ChargeSettings::from_buf(&charge_data, &self.scaling)?; let charge = ChargeSettings::from_buf(&charge_data, &self.scaling)?;
self.settings_last_read = Some(std::time::Instant::now());
Ok(TristarSettings { network, charge }) Ok(TristarSettings { network, charge })
} }
pub fn needs_new_settings(&self) -> bool {
self.settings_last_read.is_none_or(|t| {
std::time::Instant::now().duration_since(t) >= std::time::Duration::from_secs(60 * 60)
})
}
pub async fn set_target_voltage(&mut self, target_voltage: f64) -> eyre::Result<()> { pub async fn set_target_voltage(&mut self, target_voltage: f64) -> eyre::Result<()> {
let scaled_voltage: u16 = self.scale_voltage(target_voltage); let scaled_voltage: u16 = self.scale_voltage(target_voltage);
self.modbus self.modbus
@ -931,10 +921,6 @@ impl Tristar {
Ok(()) Ok(())
} }
pub fn name(&self) -> &str {
&self.friendly_name
}
fn scale_voltage(&self, voltage: f64) -> u16 { fn scale_voltage(&self, voltage: f64) -> u16 {
self.scaling.inverse_voltage(voltage) self.scaling.inverse_voltage(voltage)
} }