drive state, bump

This commit is contained in:
Alex Janka 2024-01-18 10:01:35 +11:00
parent 1b042f6c94
commit 07deeeddc7
5 changed files with 37 additions and 20 deletions

2
Cargo.lock generated
View file

@ -2256,7 +2256,7 @@ dependencies = [
[[package]] [[package]]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "1.0.3" version = "1.0.4"
dependencies = [ dependencies = [
"async-channel", "async-channel",
"chrono", "chrono",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "1.0.3" version = "1.0.4"
edition = "2021" edition = "2021"
license = "MITNFA" license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data" description = "Controls Tesla charge rate based on solar charge data"

View file

@ -12,7 +12,10 @@ use teslatte::{
FleetApi, FleetVehicleApi, FleetApi, FleetVehicleApi,
}; };
use crate::{errors::*, types::CarState}; use crate::{
errors::*,
types::{CarState, FromDriveState},
};
struct Metrics { struct Metrics {
battery_level: Gauge, battery_level: Gauge,
@ -163,7 +166,6 @@ struct MonitoredValues {
cop_state: String, cop_state: String,
climate_keeper: String, climate_keeper: String,
hvac_auto: String, hvac_auto: String,
shift_state: Option<String>,
speed: Option<i64>, speed: Option<i64>,
} }
@ -285,7 +287,10 @@ impl TeslaInterface {
} }
if let Some(new_location_data) = new_state.location_data { if let Some(new_location_data) = new_state.location_data {
state.location_data = Some(new_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 { if let Some(new_climate_state) = new_state.climate_state {
self.metrics.inside_temp.set(new_climate_state.inside_temp); self.metrics.inside_temp.set(new_climate_state.inside_temp);
@ -366,7 +371,7 @@ impl TeslaInterface {
v.into() v.into()
}); });
let location_data = self let (location_data, drive_state) = self
.api .api
.vehicle_data(&GetVehicleData { .vehicle_data(&GetVehicleData {
vehicle_id: self.vehicle.id, vehicle_id: self.vehicle.id,
@ -375,16 +380,13 @@ impl TeslaInterface {
.await? .await?
.drive_state .drive_state
.and_then(|v| { .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 { if self.monitored_values.speed != v.speed {
log::warn!("Current speed: \"{:?}\"", v.speed); log::warn!("Current speed: \"{:?}\"", v.speed);
self.monitored_values.speed = v.speed; self.monitored_values.speed = v.speed;
} }
v.try_into().ok() v.try_into().ok()
}); })
.map_or((None, None), |FromDriveState(a, b)| (Some(a), Some(b)));
let climate_state = self let climate_state = self
.api .api
@ -417,6 +419,7 @@ impl TeslaInterface {
charge_state, charge_state,
location_data, location_data,
climate_state, climate_state,
drive_state,
}) })
} }
} }

View file

@ -1,6 +1,6 @@
use chrono::DateTime; use chrono::DateTime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use teslatte::vehicles::ChargingState; use teslatte::vehicles::{ChargingState, ShiftState};
use crate::{config::access_config, errors::TeslaStateParseError}; use crate::{config::access_config, errors::TeslaStateParseError};
@ -9,6 +9,7 @@ pub struct CarState {
pub charge_state: Option<ChargeState>, pub charge_state: Option<ChargeState>,
pub location_data: Option<LocationData>, pub location_data: Option<LocationData>,
pub climate_state: Option<ClimateState>, pub climate_state: Option<ClimateState>,
pub drive_state: Option<DriveState>,
} }
impl CarState { impl CarState {
@ -82,14 +83,22 @@ impl From<teslatte::vehicles::ChargeState> for ChargeState {
} }
} }
pub struct FromDriveState(pub LocationData, pub DriveState);
#[derive(Clone, Copy, Serialize, Deserialize, Debug)] #[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct LocationData { pub struct LocationData {
pub gps_as_of: DateTime<chrono::Utc>, pub gps_as_of: DateTime<chrono::Utc>,
pub home: bool, pub home: bool,
pub power: i64,
} }
impl TryFrom<teslatte::vehicles::DriveState> for LocationData { #[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct DriveState {
pub power: i64,
pub speed: i64,
pub shift_state: ShiftState,
}
impl TryFrom<teslatte::vehicles::DriveState> for FromDriveState {
type Error = TeslaStateParseError; type Error = TeslaStateParseError;
fn try_from(value: teslatte::vehicles::DriveState) -> Result<Self, Self::Error> { fn try_from(value: teslatte::vehicles::DriveState) -> Result<Self, Self::Error> {
@ -104,11 +113,16 @@ impl TryFrom<teslatte::vehicles::DriveState> for LocationData {
}; };
let home = coords.overlaps(&access_config().coords); let home = coords.overlaps(&access_config().coords);
let power = value.power; let power = value.power;
Ok(Self { let speed = value.speed.unwrap_or(0);
gps_as_of, let shift_state = value.shift_state;
home, Ok(Self(
LocationData { gps_as_of, home },
DriveState {
power, power,
}) speed,
shift_state,
},
))
} }
} }

@ -1 +1 @@
Subproject commit 636c5fc4821286555cdab311dd520cd0d08965ce Subproject commit cd1ba77cf469693b707b21cc8f995c2decc768aa