speed & shift state to prometheus

This commit is contained in:
Alex Janka 2024-01-18 10:21:17 +11:00
parent f4bb790b01
commit 8809e3de4d
2 changed files with 75 additions and 2 deletions

View file

@ -8,7 +8,7 @@ use std::{
use teslatte::{
auth::{AccessToken, RefreshToken},
error::TeslatteError,
vehicles::{ChargingState, Endpoint, GetVehicleData, SetChargingAmps},
vehicles::{ChargingState, Endpoint, GetVehicleData, SetChargingAmps, ShiftState},
FleetApi, FleetVehicleApi,
};
@ -32,6 +32,8 @@ struct Metrics {
tesla_online: Gauge,
charging_state: ChargingStateGauges,
drive_power: Gauge,
speed: Gauge,
shift_state: ShiftStateGauges,
}
impl Metrics {
@ -67,6 +69,10 @@ impl Metrics {
let charging_state = ChargingStateGauges::new();
describe_gauge!("tesla_drive_power", "Tesla drive power");
let drive_power = gauge!("tesla_drive_power");
describe_gauge!("tesla_speed", "Tesla speed");
let speed = gauge!("tesla_speed");
describe_gauge!("tesla_shift_state", "Currently selected gear");
let shift_state = ShiftStateGauges::new();
Self {
battery_level,
@ -83,6 +89,8 @@ impl Metrics {
tesla_online,
charging_state,
drive_power,
speed,
shift_state,
}
}
}
@ -150,6 +158,69 @@ impl ChargingStateGauges {
}
}
struct ShiftStateGauges {
drive: Gauge,
neutral: Gauge,
reverse: Gauge,
park: Gauge,
}
impl ShiftStateGauges {
fn new() -> Self {
let drive = gauge!(
"tesla_shift_state",
vec![Label::new("state", String::from("drive"))]
);
let reverse = gauge!(
"tesla_shift_state",
vec![Label::new("state", String::from("reverse"))]
);
let neutral = gauge!(
"tesla_shift_state",
vec![Label::new("state", String::from("neutral"))]
);
let park = gauge!(
"tesla_shift_state",
vec![Label::new("state", String::from("park"))]
);
Self {
drive,
reverse,
neutral,
park,
}
}
fn set(&mut self, state: ShiftState) {
match state {
ShiftState::Drive => {
self.drive.set(1.);
self.neutral.set(0.);
self.reverse.set(0.);
self.park.set(0.);
}
ShiftState::Neutral => {
self.drive.set(0.);
self.neutral.set(1.);
self.reverse.set(0.);
self.park.set(0.);
}
ShiftState::Reverse => {
self.drive.set(0.);
self.neutral.set(0.);
self.reverse.set(1.);
self.park.set(0.);
}
ShiftState::Park => {
self.drive.set(0.);
self.neutral.set(0.);
self.reverse.set(0.);
self.park.set(1.);
}
}
}
}
pub struct TeslaInterface {
pub state: Arc<RwLock<CarState>>,
api: FleetApi,
@ -290,6 +361,8 @@ impl TeslaInterface {
}
if let Some(new_drive_state) = new_state.drive_state {
self.metrics.drive_power.set(new_drive_state.power as f64);
self.metrics.speed.set(new_drive_state.speed as f64);
self.metrics.shift_state.set(new_drive_state.shift_state);
state.drive_state = Some(new_drive_state);
}
if let Some(new_climate_state) = new_state.climate_state {

@ -1 +1 @@
Subproject commit cd1ba77cf469693b707b21cc8f995c2decc768aa
Subproject commit db77d944cb51f949a0235b7112ca7a5adad84b57