teslatte/examples/basic.rs
gak 6facc27d8b
change!: vehicle_data now accepts a struct instead of VehicleId
To support "endpoints", e.g. requesting GPS.

From https://developer.tesla.com/docs/fleet-api#vehicle_data

String of URL-encoded, semicolon-separated values. Can be many of 'charge_state',
'climate_state', 'closures_state', 'drive_state', 'gui_settings', 'location_data',
'vehicle_config', 'vehicle_state', 'vehicle_data_combo'.

Before:

let vehicle_data = api.vehicle_data(&vehicle_id).await.unwrap();

After:

let get_vehicle_data = GetVehicleData::new(vehicles_id);
let vehicle_data = api.vehicle_data(&get_vehicle_data).await.unwrap();

Or with a endpoints:

let get_vehicle_data = GetVehicleData::new_with_endpoints(123u64, vec![Endpoint::ChargeState, Endpoint::ClimateState]);
let vehicle_data = api.vehicle_data(&get_vehicle_data).await.unwrap();

CLI:

You can still use vehicle-data without endpoints, but you won't get
location data. To fetch location_data:

teslatte api vehicle 123 vehicle-data location_data
2023-11-11 11:07:24 +11:00

65 lines
1.8 KiB
Rust

use std::env;
use teslatte::auth::AccessToken;
use teslatte::products::Product;
use teslatte::vehicles::GetVehicleData;
use teslatte::{OwnerApi, VehicleApi};
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let api = match env::var("TESLA_ACCESS_TOKEN") {
Ok(t) => OwnerApi::new(AccessToken(t), None),
Err(_) => {
let api = OwnerApi::from_interactive_url().await.unwrap();
println!("TOKEN: {:?}", api.access_token);
api
}
};
let vehicles = api.vehicles().await.unwrap();
dbg!(&*vehicles);
if !vehicles.is_empty() {
let get_vehicle_data = GetVehicleData::new(vehicles[0].id.clone());
let vehicle_data = api.vehicle_data(&get_vehicle_data).await.unwrap();
dbg!(&vehicle_data);
} else {
println!("No vehicles found!");
}
let products = api.products().await.unwrap();
dbg!(&*products);
if !products.is_empty() {
for product in &*products {
match product {
Product::Vehicle(v) => {
dbg!(v);
}
Product::Solar(e) => {
let site_info = api.energy_sites_site_info(&e.energy_site_id).await.unwrap();
dbg!(&site_info);
let live_info = api
.energy_sites_live_status(&e.energy_site_id)
.await
.unwrap();
dbg!(&live_info);
}
Product::Powerwall(p) => {
let live_info = api
.energy_sites_live_status(&p.energy_site_id)
.await
.unwrap();
dbg!(&live_info);
}
}
}
} else {
println!("No products found!");
}
}