teslatte/examples/basic.rs

53 lines
1.5 KiB
Rust
Raw Normal View History

use std::env;
2022-07-25 13:47:43 +10:00
use teslatte::auth::AccessToken;
use teslatte::products::Product;
2024-01-24 10:22:08 +11:00
use teslatte::OwnerApi;
2022-07-21 13:46:49 +10:00
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
2022-07-25 13:47:43 +10:00
let api = match env::var("TESLA_ACCESS_TOKEN") {
2023-10-20 12:18:38 +11:00
Ok(t) => OwnerApi::new(AccessToken(t), None),
Err(_) => {
2023-10-20 12:18:38 +11:00
let api = OwnerApi::from_interactive_url().await.unwrap();
println!("TOKEN: {:?}", api.access_token);
api
}
};
2022-07-21 13:46:49 +10:00
let products = api.products().await.unwrap();
2024-01-24 10:22:08 +11:00
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!");
}
2022-07-21 13:46:49 +10:00
}