diff --git a/src/api_interface.rs b/src/api_interface.rs index 383ec8f..2f33b79 100644 --- a/src/api_interface.rs +++ b/src/api_interface.rs @@ -8,7 +8,7 @@ use std::{ use teslatte::{ auth::{AccessToken, RefreshToken}, vehicles::{Endpoint, GetVehicleData}, - FleetApi, FleetVehicleApi, VehicleId, + FleetApi, FleetVehicleApi, }; use crate::{errors::*, types::CarState}; @@ -60,6 +60,8 @@ pub struct TeslaInterface { auth_path: PathBuf, metrics: Metrics, last_cop_state: String, + last_climate_keeper: String, + last_hvac_auto: String, } #[derive(Serialize, Deserialize, Clone)] @@ -104,6 +106,8 @@ impl TeslaInterface { vehicle, metrics, last_cop_state: String::new(), + last_climate_keeper: String::new(), + last_hvac_auto: String::new(), }; interface.save_key()?; @@ -137,7 +141,7 @@ impl TeslaInterface { } async fn refresh_state(&mut self) { - match get_state(&self.api, self.vehicle.id, &mut self.last_cop_state).await { + match self.get_state().await { Ok(new_state) => { self.metrics.tesla_online.set(1.); let mut state = self @@ -215,55 +219,62 @@ impl TeslaInterface { } } } -} - -async fn get_state( - api: &FleetApi, - vehicle_id: VehicleId, - last_cop_state: &mut String, -) -> Result { - // Endpoint::VehicleDataCombo or multiple Endpoints in one request - // doesn't seem to reliably get them all, - // so for each endpoint we do a new request - let charge_state = api - .vehicle_data(&GetVehicleData { - vehicle_id, - endpoints: vec![Endpoint::ChargeState].into(), - }) - .await? - .charge_state - .map(|v| v.into()); - - let location_data = api - .vehicle_data(&GetVehicleData { - vehicle_id, - endpoints: vec![Endpoint::LocationData].into(), - }) - .await? - .drive_state - .and_then(|v| v.try_into().ok()); - - let climate_state = api - .vehicle_data(&GetVehicleData { - vehicle_id, - endpoints: vec![Endpoint::ClimateState].into(), - }) - .await? - .climate_state - .and_then(|v| { - if *last_cop_state != v.cabin_overheat_protection { - log::warn!( - "Current cabin overheat protection state: {}", - v.cabin_overheat_protection - ); - *last_cop_state = v.cabin_overheat_protection.clone(); - } - v.try_into().ok() - }); - - Ok(CarState { - charge_state, - location_data, - climate_state, - }) + + async fn get_state(&mut self) -> Result { + // Endpoint::VehicleDataCombo or multiple Endpoints in one request + // doesn't seem to reliably get them all, + // so for each endpoint we do a new request + let charge_state = self + .api + .vehicle_data(&GetVehicleData { + vehicle_id: self.vehicle.id, + endpoints: vec![Endpoint::ChargeState].into(), + }) + .await? + .charge_state + .map(|v| v.into()); + + let location_data = self + .api + .vehicle_data(&GetVehicleData { + vehicle_id: self.vehicle.id, + endpoints: vec![Endpoint::LocationData].into(), + }) + .await? + .drive_state + .and_then(|v| v.try_into().ok()); + + let climate_state = self + .api + .vehicle_data(&GetVehicleData { + vehicle_id: self.vehicle.id, + endpoints: vec![Endpoint::ClimateState].into(), + }) + .await? + .climate_state + .and_then(|v| { + if self.last_cop_state != v.cabin_overheat_protection { + log::warn!( + "Current cabin overheat protection state: {}", + v.cabin_overheat_protection + ); + self.last_cop_state = v.cabin_overheat_protection.clone(); + } + if self.last_climate_keeper != v.climate_keeper_mode { + log::warn!("Current climate keeper mode: {}", v.climate_keeper_mode); + self.last_climate_keeper = v.climate_keeper_mode.clone(); + } + if self.last_hvac_auto != v.hvac_auto_request { + log::warn!("HVAC auto request set to: {}", v.hvac_auto_request); + self.last_hvac_auto = v.hvac_auto_request.clone(); + } + v.try_into().ok() + }); + + Ok(CarState { + charge_state, + location_data, + climate_state, + }) + } }