From bc0da6be11f3ec1d4c07c5b0e12af5b2b7f351f7 Mon Sep 17 00:00:00 2001 From: Alex Janka <alex@alexjanka.com> Date: Sat, 28 Dec 2024 21:09:19 +1100 Subject: [PATCH] cleanup+change label names --- Cargo.lock | 4 +-- Cargo.toml | 2 +- charge-controller-supervisor/debian/service | 1 - .../src/controller.rs | 5 +-- charge-controller-supervisor/src/pl.rs | 25 +++++++-------- charge-controller-supervisor/src/tristar.rs | 32 +++++++++---------- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index adf4fd3..e889205 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -239,7 +239,7 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" [[package]] name = "charge-controller-supervisor" -version = "1.9.9-pre-2" +version = "1.9.9-pre-3" dependencies = [ "chrono", "clap", @@ -2202,7 +2202,7 @@ dependencies = [ [[package]] name = "tesla-charge-controller" -version = "1.9.9-pre-2" +version = "1.9.9-pre-3" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index 4140274..94825f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ default-members = ["charge-controller-supervisor"] resolver = "2" [workspace.package] -version = "1.9.9-pre-2" +version = "1.9.9-pre-3" [workspace.lints.clippy] pedantic = "warn" diff --git a/charge-controller-supervisor/debian/service b/charge-controller-supervisor/debian/service index 2888638..699e8dc 100644 --- a/charge-controller-supervisor/debian/service +++ b/charge-controller-supervisor/debian/service @@ -7,7 +7,6 @@ StartLimitIntervalSec=0 Type=simple Restart=always RestartSec=10s -User=tesla Environment="RUST_LOG=error,warn" Environment="LOG_TIMESTAMP=false" ExecStart=/usr/bin/charge-controller-supervisor watch diff --git a/charge-controller-supervisor/src/controller.rs b/charge-controller-supervisor/src/controller.rs index 846c7ce..5df03a5 100644 --- a/charge-controller-supervisor/src/controller.rs +++ b/charge-controller-supervisor/src/controller.rs @@ -16,12 +16,13 @@ impl Controller { pub fn new(config: crate::config::ChargeControllerConfig) -> eyre::Result<Self> { let inner = match config.variant { crate::config::ChargeControllerVariant::Tristar => ControllerInner::Tristar( - crate::tristar::Tristar::new(config.serial_port, config.baud_rate)?, + crate::tristar::Tristar::new(&config.serial_port, &config.name, config.baud_rate)?, ), crate::config::ChargeControllerVariant::Pl { timeout_milliseconds, } => ControllerInner::Pl(crate::pl::Pli::new( - config.serial_port, + &config.serial_port, + &config.name, config.baud_rate, timeout_milliseconds, )?), diff --git a/charge-controller-supervisor/src/pl.rs b/charge-controller-supervisor/src/pl.rs index fb968d0..f684d9b 100644 --- a/charge-controller-supervisor/src/pl.rs +++ b/charge-controller-supervisor/src/pl.rs @@ -10,8 +10,7 @@ use crate::gauges::{ }; pub struct Pli { - // pub state: Arc<RwLock<PlState>>, - port_name: String, + friendly_name: String, port: Box<dyn SerialPort>, } @@ -108,17 +107,17 @@ pub enum PliRequest { impl Pli { pub fn new( - serial_port: String, + serial_port: &str, + friendly_name: &str, baud_rate: u32, timeout: u64, ) -> Result<Self, serialport::Error> { - let port = serialport::new(serial_port.clone(), baud_rate) + let port = serialport::new(serial_port, baud_rate) .timeout(Duration::from_millis(timeout)) .open()?; Ok(Self { - // state: Arc::new(RwLock::new(Default::default())), - port_name: serial_port, + friendly_name: friendly_name.to_owned(), port, }) } @@ -126,25 +125,25 @@ impl Pli { pub fn refresh(&mut self) -> eyre::Result<crate::controller::CommonData> { let new_state = self.read_state()?; BATTERY_VOLTAGE - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.battery_voltage); TARGET_VOLTAGE - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.target_voltage); PL_DUTY_CYCLE - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.duty_cycle); INPUT_CURRENT - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.internal_charge_current); PL_LOAD_CURRENT - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.internal_load_current); BATTERY_TEMP - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.battery_temp); - set_regulator_gauges(new_state.regulator_state, &self.port_name); + set_regulator_gauges(new_state.regulator_state, &self.friendly_name); Ok(crate::controller::CommonData { battery_voltage: new_state.battery_voltage, diff --git a/charge-controller-supervisor/src/tristar.rs b/charge-controller-supervisor/src/tristar.rs index 12e685d..039438a 100644 --- a/charge-controller-supervisor/src/tristar.rs +++ b/charge-controller-supervisor/src/tristar.rs @@ -42,7 +42,7 @@ impl Scaling { } pub struct Tristar { - port_name: String, + friendly_name: String, modbus: tokio_modbus::client::Context, charge_state_gauges: ChargeStateGauges, consecutive_errors: usize, @@ -233,14 +233,14 @@ impl ChargeStateGauges { } impl Tristar { - pub fn new(serial_port: String, baud_rate: u32) -> eyre::Result<Self> { + pub fn new(serial_port: &str, friendly_name: &str, baud_rate: u32) -> eyre::Result<Self> { let modbus_serial = - tokio_serial::SerialStream::open(&tokio_serial::new(&serial_port, baud_rate))?; + tokio_serial::SerialStream::open(&tokio_serial::new(serial_port, baud_rate))?; let slave = tokio_modbus::Slave(DEVICE_ID); let modbus = tokio_modbus::client::rtu::attach_slave(modbus_serial, slave); - let charge_state_gauges = ChargeStateGauges::new(&serial_port); + let charge_state_gauges = ChargeStateGauges::new(friendly_name); Ok(Self { - port_name: serial_port, + friendly_name: friendly_name.to_owned(), modbus, charge_state_gauges, consecutive_errors: 0, @@ -251,37 +251,37 @@ impl Tristar { let new_state = self.get_data().await?; self.consecutive_errors = 0; BATTERY_VOLTAGE - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.battery_voltage); TARGET_VOLTAGE - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.target_voltage); INPUT_CURRENT - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.input_current); BATTERY_TEMP - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.battery_temp.into()); TRISTAR_INPUT_VOLTAGE - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_input_voltage); TRISTAR_CHARGE_CURRENT - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_charge_current); TRISTAR_POWER_OUT - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_power_out); TRISTAR_POWER_IN - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_power_in); TRISTAR_MAX_ARRAY_POWER - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_max_array_power); TRISTAR_MAX_ARRAY_VOLTAGE - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_max_array_voltage); TRISTAR_OPEN_CIRCUIT_VOLTAGE - .with_label_values(&[&self.port_name]) + .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_open_circuit_voltage); self.charge_state_gauges.set(new_state.charge_state);