From 9e3821ec29de0e1c9b3ec0dccc8b6439537c7be2 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 15 Jan 2024 09:31:56 +1100 Subject: [PATCH] battery temp --- src/charge_controllers/gauge_names.rs | 1 + src/charge_controllers/mod.rs | 5 +-- src/charge_controllers/pl.rs | 45 ++++++++++++++------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/charge_controllers/gauge_names.rs b/src/charge_controllers/gauge_names.rs index 26fc859..3c6ee40 100644 --- a/src/charge_controllers/gauge_names.rs +++ b/src/charge_controllers/gauge_names.rs @@ -5,6 +5,7 @@ pub const BATTERY_VOLTAGE: &str = "battery_voltage"; pub const TARGET_VOLTAGE: &str = "target_voltage"; pub const INPUT_CURRENT: &str = "input_current"; pub const CHARGE_STATE: &str = "charge_state"; +pub const BATTERY_TEMP: &str = "battery_temp"; pub const PL_DUTY_CYCLE: &str = "pl_duty_cycle"; pub const PL_LOAD_CURRENT: &str = "pl_internal_load_current"; diff --git a/src/charge_controllers/mod.rs b/src/charge_controllers/mod.rs index 4c710f0..254f17b 100644 --- a/src/charge_controllers/mod.rs +++ b/src/charge_controllers/mod.rs @@ -1,4 +1,4 @@ -use metrics::describe_gauge; +use metrics::{describe_gauge, Unit}; mod gauge_names; pub mod pl; @@ -8,10 +8,11 @@ pub fn register_metrics() { describe_gauge!(gauge_names::TARGET_VOLTAGE, "Target voltage"); describe_gauge!(gauge_names::INPUT_CURRENT, "Internal charge current"); describe_gauge!(gauge_names::CHARGE_STATE, "Regulator state"); + describe_gauge!(gauge_names::BATTERY_TEMP, "Battery temperature"); register_pl_metrics(); } fn register_pl_metrics() { - describe_gauge!(gauge_names::PL_DUTY_CYCLE, "Duty cycle"); + describe_gauge!(gauge_names::PL_DUTY_CYCLE, Unit::Percent, "Duty cycle"); describe_gauge!(gauge_names::PL_LOAD_CURRENT, "Internal load current"); } diff --git a/src/charge_controllers/pl.rs b/src/charge_controllers/pl.rs index 4e21947..8c56266 100644 --- a/src/charge_controllers/pl.rs +++ b/src/charge_controllers/pl.rs @@ -16,37 +16,26 @@ use super::gauge_names; pub struct Pli { pub state: Arc>, port: Box, - voltage_gauge: Gauge, - target_voltage_gauge: Gauge, + battery_voltage: Gauge, + target_voltage: Gauge, duty_cycle: Gauge, internal_charge_current: Gauge, internal_load_current: Gauge, + battery_temp: Gauge, regulator_gauges: RegulatorGauges, } -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)] pub struct PlState { pub battery_voltage: f64, pub target_voltage: f64, pub duty_cycle: f64, pub internal_charge_current: f64, pub internal_load_current: f64, + pub battery_temp: f64, pub regulator_state: RegulatorState, } -impl Default for PlState { - fn default() -> Self { - Self { - battery_voltage: Default::default(), - target_voltage: Default::default(), - duty_cycle: Default::default(), - internal_charge_current: Default::default(), - internal_load_current: Default::default(), - regulator_state: RegulatorState::Absorption, - } - } -} - #[derive(Debug, Clone, Copy, Serialize, Deserialize)] pub enum RegulatorState { Boost, @@ -67,6 +56,12 @@ impl From for RegulatorState { } } +impl Default for RegulatorState { + fn default() -> Self { + Self::Absorption + } +} + struct RegulatorGauges { boost: Gauge, equalise: Gauge, @@ -162,33 +157,36 @@ impl Pli { Label::new(gauge_names::PL_LABEL, serial_port), ]; - let voltage_gauge = gauge!(gauge_names::BATTERY_VOLTAGE, device_labels.clone()); - let target_voltage_gauge = gauge!(gauge_names::TARGET_VOLTAGE, device_labels.clone()); + let battery_voltage = gauge!(gauge_names::BATTERY_VOLTAGE, device_labels.clone()); + let target_voltage = gauge!(gauge_names::TARGET_VOLTAGE, device_labels.clone()); let duty_cycle = gauge!(gauge_names::PL_DUTY_CYCLE, device_labels.clone()); let internal_charge_current = gauge!(gauge_names::INPUT_CURRENT, device_labels.clone()); let internal_load_current = gauge!(gauge_names::PL_LOAD_CURRENT, device_labels.clone()); + let battery_temp = gauge!(gauge_names::BATTERY_TEMP, device_labels.clone()); Ok(Self { state: Arc::new(RwLock::new(Default::default())), port, - voltage_gauge, - target_voltage_gauge, + battery_voltage, + target_voltage, duty_cycle, internal_charge_current, internal_load_current, + battery_temp, regulator_gauges: RegulatorGauges::new(device_labels), }) } pub fn refresh(&mut self) { if let Some(new_state) = self.read_state().some_or_print() { - self.voltage_gauge.set(new_state.battery_voltage); - self.target_voltage_gauge.set(new_state.target_voltage); + self.battery_voltage.set(new_state.battery_voltage); + self.target_voltage.set(new_state.target_voltage); self.duty_cycle.set(new_state.duty_cycle); self.internal_charge_current .set(new_state.internal_charge_current); self.internal_load_current .set(new_state.internal_load_current); + self.battery_temp.set(new_state.battery_temp); self.regulator_gauges.set(&new_state.regulator_state); *self.state.write().expect("PLI state handler panicked!!") = new_state; @@ -221,6 +219,7 @@ impl Pli { duty_cycle: (self.read_ram(PlRamAddress::Dutycyc)? as f64) / 255., internal_charge_current: (self.read_ram(PlRamAddress::Cint)? as f64) * (4. / 10.), internal_load_current: (self.read_ram(PlRamAddress::Lint)? as f64) * (4. / 10.), + battery_temp: self.read_ram(PlRamAddress::Battemp)? as f64, regulator_state: self.read_ram(PlRamAddress::Rstate)?.into(), }) } @@ -266,6 +265,7 @@ impl Pli { enum PlRamAddress { Dutycyc, Batv, + Battemp, Rstate, Vreg, Cint, @@ -277,6 +277,7 @@ impl From for u8 { match value { PlRamAddress::Dutycyc => 39, PlRamAddress::Batv => 50, + PlRamAddress::Battemp => 52, PlRamAddress::Rstate => 101, PlRamAddress::Vreg => 105, PlRamAddress::Cint => 213,