From 07deeeddc7ed2b483e110352040e718adffad773 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Thu, 18 Jan 2024 10:01:35 +1100 Subject: [PATCH] drive state, bump --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/api_interface.rs | 21 ++++++++++++--------- src/types.rs | 30 ++++++++++++++++++++++-------- vendored/teslatte | 2 +- 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5661abf..c2378fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2256,7 +2256,7 @@ dependencies = [ [[package]] name = "tesla-charge-controller" -version = "1.0.3" +version = "1.0.4" dependencies = [ "async-channel", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 4c85bb3..724e6c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tesla-charge-controller" -version = "1.0.3" +version = "1.0.4" edition = "2021" license = "MITNFA" description = "Controls Tesla charge rate based on solar charge data" diff --git a/src/api_interface.rs b/src/api_interface.rs index ee53871..497dd33 100644 --- a/src/api_interface.rs +++ b/src/api_interface.rs @@ -12,7 +12,10 @@ use teslatte::{ FleetApi, FleetVehicleApi, }; -use crate::{errors::*, types::CarState}; +use crate::{ + errors::*, + types::{CarState, FromDriveState}, +}; struct Metrics { battery_level: Gauge, @@ -163,7 +166,6 @@ struct MonitoredValues { cop_state: String, climate_keeper: String, hvac_auto: String, - shift_state: Option, speed: Option, } @@ -285,7 +287,10 @@ impl TeslaInterface { } if let Some(new_location_data) = new_state.location_data { state.location_data = Some(new_location_data); - self.metrics.drive_power.set(new_location_data.power as f64); + } + if let Some(new_drive_state) = new_state.drive_state { + self.metrics.drive_power.set(new_drive_state.power as f64); + state.drive_state = Some(new_drive_state); } if let Some(new_climate_state) = new_state.climate_state { self.metrics.inside_temp.set(new_climate_state.inside_temp); @@ -366,7 +371,7 @@ impl TeslaInterface { v.into() }); - let location_data = self + let (location_data, drive_state) = self .api .vehicle_data(&GetVehicleData { vehicle_id: self.vehicle.id, @@ -375,16 +380,13 @@ impl TeslaInterface { .await? .drive_state .and_then(|v| { - if self.monitored_values.shift_state != v.shift_state { - log::warn!("Current shift state: \"{:?}\"", v.shift_state); - self.monitored_values.shift_state = v.shift_state.clone(); - } if self.monitored_values.speed != v.speed { log::warn!("Current speed: \"{:?}\"", v.speed); self.monitored_values.speed = v.speed; } v.try_into().ok() - }); + }) + .map_or((None, None), |FromDriveState(a, b)| (Some(a), Some(b))); let climate_state = self .api @@ -417,6 +419,7 @@ impl TeslaInterface { charge_state, location_data, climate_state, + drive_state, }) } } diff --git a/src/types.rs b/src/types.rs index 87a19ab..0ca8bfe 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,6 @@ use chrono::DateTime; use serde::{Deserialize, Serialize}; -use teslatte::vehicles::ChargingState; +use teslatte::vehicles::{ChargingState, ShiftState}; use crate::{config::access_config, errors::TeslaStateParseError}; @@ -9,6 +9,7 @@ pub struct CarState { pub charge_state: Option, pub location_data: Option, pub climate_state: Option, + pub drive_state: Option, } impl CarState { @@ -82,14 +83,22 @@ impl From for ChargeState { } } +pub struct FromDriveState(pub LocationData, pub DriveState); + #[derive(Clone, Copy, Serialize, Deserialize, Debug)] pub struct LocationData { pub gps_as_of: DateTime, pub home: bool, - pub power: i64, } -impl TryFrom for LocationData { +#[derive(Clone, Copy, Serialize, Deserialize, Debug)] +pub struct DriveState { + pub power: i64, + pub speed: i64, + pub shift_state: ShiftState, +} + +impl TryFrom for FromDriveState { type Error = TeslaStateParseError; fn try_from(value: teslatte::vehicles::DriveState) -> Result { @@ -104,11 +113,16 @@ impl TryFrom for LocationData { }; let home = coords.overlaps(&access_config().coords); let power = value.power; - Ok(Self { - gps_as_of, - home, - power, - }) + let speed = value.speed.unwrap_or(0); + let shift_state = value.shift_state; + Ok(Self( + LocationData { gps_as_of, home }, + DriveState { + power, + speed, + shift_state, + }, + )) } } diff --git a/vendored/teslatte b/vendored/teslatte index 636c5fc..cd1ba77 160000 --- a/vendored/teslatte +++ b/vendored/teslatte @@ -1 +1 @@ -Subproject commit 636c5fc4821286555cdab311dd520cd0d08965ce +Subproject commit cd1ba77cf469693b707b21cc8f995c2decc768aa