diff --git a/Cargo.toml b/Cargo.toml index e489005..b4fcca9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,3 +38,11 @@ reqwest = "0.12.4" # [patch."https://git.alexjanka.com/alex/tesla-common"] # tesla-common = { path = "../tesla-common" } + +[lints.clippy] +pedantic = "warn" +struct-excessive-bools = { level = "allow", priority = 1 } +too-many-lines = { level = "allow", priority = 1 } +default-trait-access = { level = "allow", priority = 1 } +cast-precision-loss = { level = "allow", priority = 1 } +cast-possible-truncation = { level = "allow", priority = 1 } diff --git a/src/api_interface.rs b/src/api_interface.rs index d44de79..cb847c8 100644 --- a/src/api_interface.rs +++ b/src/api_interface.rs @@ -19,7 +19,7 @@ use teslatte::{ }; use crate::{ - errors::*, + errors::{AuthLoadError, PrintErrors}, types::{CarState, FromDriveState}, }; @@ -290,11 +290,10 @@ impl TeslaInterface { .products() .await? .into_iter() - .filter_map(|v| match v { + .find_map(|v| match v { teslatte::products::Product::Vehicle(vehicle) => Some(vehicle), _ => None, }) - .next() .ok_or(AuthLoadError::NoVehicles)?; let interface = Self { @@ -419,18 +418,18 @@ impl TeslaInterface { } Err(e) => { TESLA_ONLINE.set(0); - match e { - teslatte::error::TeslatteError::DecodeJsonError { - source, - request: _, - body, - } => { - if body.contains("vehicle is offline or asleep") { - return; - } - log::error!("Error {source} getting state: {body}") + if let teslatte::error::TeslatteError::DecodeJsonError { + source, + request: _, + body, + } = e + { + if body.contains("vehicle is offline or asleep") { + return; } - _ => log::error!("Teslatte error: {e:?}"), + log::error!("Error {source} getting state: {body}"); + } else { + log::error!("Teslatte error: {e:?}"); } } } @@ -485,7 +484,7 @@ impl TeslaInterface { }) .await? .vehicle_state - .map(|v| v.into()); + .map(std::convert::Into::into); let (location_data, _) = self .api @@ -526,11 +525,7 @@ impl TeslaInterface { } fn bi(value: bool) -> i64 { - if value { - 1 - } else { - 0 - } + i64::from(value) } fn obi(value: Option) -> i64 { diff --git a/src/charge_controllers/pl.rs b/src/charge_controllers/pl.rs index 75df3a4..106be0e 100644 --- a/src/charge_controllers/pl.rs +++ b/src/charge_controllers/pl.rs @@ -9,7 +9,10 @@ use serde::{Deserialize, Serialize}; use serialport::SerialPort; use crate::{ - charge_controllers::gauges::*, + charge_controllers::gauges::{ + BATTERY_TEMP, BATTERY_VOLTAGE, CHARGE_STATE, INPUT_CURRENT, PL_DUTY_CYCLE, PL_LOAD_CURRENT, + TARGET_VOLTAGE, + }, errors::{PliError, PrintErrors}, }; @@ -67,7 +70,7 @@ impl Default for RegulatorState { } } -fn set_regulator_gauges(state: &RegulatorState, label: &str) { +fn set_regulator_gauges(state: RegulatorState, label: &str) { let boost = CHARGE_STATE.with_label_values(&[label, "boost"]); let equalise = CHARGE_STATE.with_label_values(&[label, "equalise"]); let absorption = CHARGE_STATE.with_label_values(&[label, "absorption"]); @@ -147,7 +150,7 @@ impl Pli { .with_label_values(&[&self.port_name]) .set(new_state.battery_temp); - set_regulator_gauges(&new_state.regulator_state, &self.port_name); + set_regulator_gauges(new_state.regulator_state, &self.port_name); *self.state.write().expect("PLI state handler panicked!!") = new_state; } @@ -204,12 +207,12 @@ impl Pli { fn read_state(&mut self) -> Result { Ok(PlState { - battery_voltage: (self.read_ram(PlRamAddress::Batv)? as f64) * (4. / 10.), - target_voltage: (self.read_ram(PlRamAddress::Vreg)? as f64) * (4. / 10.), - 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, + battery_voltage: f64::from(self.read_ram(PlRamAddress::Batv)?) * (4. / 10.), + target_voltage: f64::from(self.read_ram(PlRamAddress::Vreg)?) * (4. / 10.), + duty_cycle: f64::from(self.read_ram(PlRamAddress::Dutycyc)?) / 255., + internal_charge_current: f64::from(self.read_ram(PlRamAddress::Cint)?) * (4. / 10.), + internal_load_current: f64::from(self.read_ram(PlRamAddress::Lint)?) * (4. / 10.), + battery_temp: f64::from(self.read_ram(PlRamAddress::Battemp)?), regulator_state: self.read_ram(PlRamAddress::Rstate)?.into(), }) } @@ -225,9 +228,8 @@ impl Pli { while let Ok(num) = self.port.bytes_to_read() { if num == 0 { return Ok(()); - } else { - let _ = self.port.read(&mut [0; 8]); } + let _ = self.port.read(&mut [0; 8]); } Ok(()) } diff --git a/src/charge_controllers/tristar.rs b/src/charge_controllers/tristar.rs index 24f0776..ac9ae7a 100644 --- a/src/charge_controllers/tristar.rs +++ b/src/charge_controllers/tristar.rs @@ -2,7 +2,12 @@ use prometheus::core::{AtomicI64, GenericGauge}; use tokio_modbus::client::Reader; use crate::{ - charge_controllers::gauges::*, + charge_controllers::gauges::{ + BATTERY_TEMP, BATTERY_VOLTAGE, CHARGE_STATE, INPUT_CURRENT, TARGET_VOLTAGE, + TRISTAR_CHARGE_CURRENT, TRISTAR_INPUT_VOLTAGE, TRISTAR_MAX_ARRAY_POWER, + TRISTAR_MAX_ARRAY_VOLTAGE, TRISTAR_OPEN_CIRCUIT_VOLTAGE, TRISTAR_POWER_IN, + TRISTAR_POWER_OUT, + }, errors::{PrintErrors, TristarError}, }; @@ -10,7 +15,7 @@ const DEVICE_ID: u8 = 0x01; const RAM_DATA_SIZE: u16 = 0x005B; const RAM_ARRAY_SIZE: usize = RAM_DATA_SIZE as usize + 1; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct Scaling { pub v_scale: f64, pub i_scale: f64, @@ -23,21 +28,21 @@ impl Scaling { fn from_internal(v_pu_hi: u16, v_pu_lo: u16, i_pu_hi: u16, i_pu_lo: u16) -> Self { Self { - v_scale: v_pu_hi as f64 + (v_pu_lo as f64 / f64::powf(2., 16.)), - i_scale: i_pu_hi as f64 + (i_pu_lo as f64 / f64::powf(2., 16.)), + v_scale: f64::from(v_pu_hi) + (f64::from(v_pu_lo) / f64::powf(2., 16.)), + i_scale: f64::from(i_pu_hi) + (f64::from(i_pu_lo) / f64::powf(2., 16.)), } } fn get_voltage(&self, data: u16) -> f64 { - data as f64 * self.v_scale * f64::powf(2., -15.) + f64::from(data) * self.v_scale * f64::powf(2., -15.) } fn get_current(&self, data: u16) -> f64 { - data as f64 * self.i_scale * f64::powf(2., -15.) + f64::from(data) * self.i_scale * f64::powf(2., -15.) } fn get_power(&self, data: u16) -> f64 { - data as f64 * self.v_scale * self.i_scale * f64::powf(2., -17.) + f64::from(data) * self.v_scale * self.i_scale * f64::powf(2., -17.) } } diff --git a/src/config.rs b/src/config.rs index 61d924c..8c1abf6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -90,13 +90,13 @@ impl<'a> core::ops::Deref for ConfigHandle<'a> { } } -impl<'a> DerefMut for ConfigHandle<'a> { +impl DerefMut for ConfigHandle<'_> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.handle } } -impl<'a> Drop for ConfigHandle<'a> { +impl Drop for ConfigHandle<'_> { fn drop(&mut self) { let _ = self.save().some_or_print_with("saving config"); } diff --git a/src/main.rs b/src/main.rs index 8d8b81e..3dbe3ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -193,7 +193,7 @@ async fn main() { tokio::select! { _ = normal_data_update_interval.tick() => { if !interface.state.read().unwrap().is_charging_at_home() { - interface.refresh().await + interface.refresh().await; } }, _ = charge_rate_update_interval.tick() => { @@ -202,7 +202,7 @@ async fn main() { if let Some(request) = tesla_charge_rate_controller.control_charge_rate() { interface.process_request(request).await; } - interface.refresh().await + interface.refresh().await; } else if was_connected && interface .state diff --git a/src/server/mod.rs b/src/server/mod.rs index 10b4fb2..eebd190 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -131,9 +131,7 @@ lazy_static::lazy_static! { mod authentication; #[get("/reauthenticate")] -async fn reauthenticate( - state: &State, -) -> Result { +fn reauthenticate(state: &State) -> Result { let state_random: String = rand::thread_rng() .sample_iter(&rand::distributions::Alphanumeric) .take(16) @@ -182,7 +180,7 @@ async fn auth( } #[get("/home")] -async fn home(state: &State) -> Result, ServerError> { +fn home(state: &State) -> Result, ServerError> { let location_data = &state .car_state .read()? @@ -192,7 +190,7 @@ async fn home(state: &State) -> Result, ServerError> { } #[get("/car-state")] -async fn car_state(state: &State) -> Result, ServerError> { +fn car_state(state: &State) -> Result, ServerError> { Ok(Json(*state.car_state.read()?)) } @@ -205,7 +203,7 @@ struct ControlState { } #[get("/control-state")] -async fn control_state(state: &State) -> Result, ServerError> { +fn control_state(state: &State) -> Result, ServerError> { let control_enable = state.tcrc_state.read()?.control_enable; let is_charging_at_home = state.car_state.read()?.is_charging_at_home(); let config = access_config(); @@ -221,13 +219,13 @@ async fn control_state(state: &State) -> Result, } #[post("/flash")] -async fn flash(state: &State, remote_addr: std::net::IpAddr) { +fn flash(state: &State, remote_addr: std::net::IpAddr) { log::warn!("flash requested: {remote_addr:?}"); let _ = state.api_requests.send(InterfaceRequest::FlashLights); } #[post("/disable-control")] -async fn disable_control(state: &State, remote_addr: std::net::IpAddr) { +fn disable_control(state: &State, remote_addr: std::net::IpAddr) { log::warn!("disabling control: {remote_addr:?}"); match state.tcrc_state.write() { Ok(mut handle) => handle.set_control_enable(false), @@ -236,7 +234,7 @@ async fn disable_control(state: &State, remote_addr: std::net::IpAd } #[post("/enable-control")] -async fn enable_control(state: &State, remote_addr: std::net::IpAddr) { +fn enable_control(state: &State, remote_addr: std::net::IpAddr) { log::warn!("enabling control: {remote_addr:?}"); match state.tcrc_state.write() { Ok(mut handle) => handle.set_control_enable(true), @@ -245,14 +243,14 @@ async fn enable_control(state: &State, remote_addr: std::net::IpAdd } #[post("/shutoff/voltage/")] -async fn set_shutoff(voltage: f64, remote_addr: std::net::IpAddr) { +fn set_shutoff(voltage: f64, remote_addr: std::net::IpAddr) { log::warn!("setting shutoff voltage: {remote_addr:?}"); let voltage = voltage.clamp(40., 60.); write_to_config().shutoff_voltage = voltage; } #[post("/shutoff/time/