tesla-charge-controller/src/types.rs

104 lines
3 KiB
Rust
Raw Normal View History

2024-01-08 12:00:09 +11:00
use chrono::DateTime;
2024-01-07 10:08:16 +11:00
use serde::{Deserialize, Serialize};
2024-01-12 08:47:21 +11:00
use crate::errors::TeslaStateParseError;
2024-01-08 12:00:09 +11:00
#[derive(Default)]
pub struct CarState {
pub charge_state: Option<ChargeState>,
pub location_data: Option<LocationData>,
2024-01-09 11:11:16 +11:00
pub climate_state: Option<ClimateState>,
}
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct ClimateState {
pub inside_temp: f64,
2024-01-09 12:06:18 +11:00
pub outside_temp: f64,
pub battery_heater: bool,
2024-01-09 11:11:16 +11:00
}
impl TryFrom<teslatte::vehicles::ClimateState> for ClimateState {
2024-01-12 08:47:21 +11:00
type Error = TeslaStateParseError;
2024-01-09 11:11:16 +11:00
fn try_from(value: teslatte::vehicles::ClimateState) -> Result<Self, Self::Error> {
Ok(Self {
2024-01-12 08:47:21 +11:00
inside_temp: value.inside_temp.ok_or(TeslaStateParseError::NoValue)?,
2024-01-09 12:06:18 +11:00
2024-01-12 08:47:21 +11:00
outside_temp: value.outside_temp.ok_or(TeslaStateParseError::NoValue)?,
2024-01-09 12:06:18 +11:00
battery_heater: value.battery_heater,
2024-01-09 11:11:16 +11:00
})
}
2024-01-08 12:00:09 +11:00
}
2024-01-07 10:08:16 +11:00
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct ChargeState {
pub battery_level: i64,
pub battery_range: f64,
pub charge_amps: i64,
2024-01-07 10:33:32 +11:00
pub charge_rate: f64,
2024-01-07 10:08:16 +11:00
pub charge_current_request: i64,
pub charge_current_request_max: i64,
pub charge_enable_request: bool,
2024-01-07 10:33:32 +11:00
pub charge_limit_soc: i64,
2024-01-07 10:08:16 +11:00
}
impl ChargeState {
#[allow(unused)]
pub fn range_km(&self) -> f64 {
self.battery_range * 1.60934
}
}
impl From<teslatte::vehicles::ChargeState> for ChargeState {
fn from(value: teslatte::vehicles::ChargeState) -> Self {
ChargeState {
battery_level: value.battery_level,
battery_range: value.battery_range,
charge_amps: value.charge_amps,
2024-01-07 10:33:32 +11:00
charge_rate: value.charge_rate,
2024-01-07 10:08:16 +11:00
charge_current_request: value.charge_current_request,
charge_current_request_max: value.charge_current_request_max,
charge_enable_request: value.charge_enable_request,
2024-01-07 10:33:32 +11:00
charge_limit_soc: value.charge_limit_soc,
2024-01-07 10:08:16 +11:00
}
}
}
2024-01-08 12:00:09 +11:00
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct LocationData {
pub coords: Coords,
pub gps_as_of: DateTime<chrono::Utc>,
}
impl TryFrom<teslatte::vehicles::DriveState> for LocationData {
2024-01-12 08:47:21 +11:00
type Error = TeslaStateParseError;
2024-01-08 12:00:09 +11:00
fn try_from(value: teslatte::vehicles::DriveState) -> Result<Self, Self::Error> {
2024-01-12 08:47:21 +11:00
let gps_as_of = chrono::DateTime::from_timestamp(
value.gps_as_of.ok_or(TeslaStateParseError::NoValue)?,
0,
)
.ok_or(TeslaStateParseError::InvalidTimestamp)?;
2024-01-08 12:00:09 +11:00
let coords = Coords {
2024-01-12 08:47:21 +11:00
latitude: value.latitude.ok_or(TeslaStateParseError::NoValue)?,
longitude: value.longitude.ok_or(TeslaStateParseError::NoValue)?,
2024-01-08 12:00:09 +11:00
};
Ok(Self { coords, gps_as_of })
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Coords {
pub latitude: f64,
pub longitude: f64,
}
const COORD_PRECISION: f64 = 0.001;
impl Coords {
pub fn overlaps(&self, other: &Coords) -> bool {
(self.latitude - other.latitude).abs() < COORD_PRECISION
&& (self.longitude - other.longitude).abs() < COORD_PRECISION
}
}