use std::path::PathBuf;
use std::process::ExitCode;
use std::sync::Arc;
use fleetwire::api::vehicles::{DataEndpoint, GetVehicleData};
use fleetwire::{Billing, Client, FleetGet, Guarded, HttpRefresher, Pricing, TokenManager};
fn main() -> ExitCode {
match run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("error: {e}");
if let Some(err) = e.downcast_ref::<fleetwire::Error>() {
eprintln!("{}", spend_after_failure(err.billing()));
}
eprintln!("See \"If it fails\" in crates/fleetwire/README.md for the usual causes.");
ExitCode::FAILURE
}
}
}
fn spend_after_failure(billing: Billing) -> &'static str {
match billing {
Billing::Billed => "spent: this failed call was billed",
Billing::NotBilled => "spent: nothing, this failed call was not billed",
_ => "spent: unknown, the request may have reached Tesla before it failed",
}
}
fn run() -> Result<(), Box<dyn std::error::Error>> {
let client_id = std::env::var("TESLA_CLIENT_ID").ok();
let dir = std::env::var_os("FLEETWIRE_TOKEN_DIR").map(PathBuf::from);
let (Some(client_id), Some(dir)) = (client_id, dir) else {
return Err(
"set TESLA_CLIENT_ID to your Tesla developer app's client id \
and FLEETWIRE_TOKEN_DIR to the directory that holds tokens.json"
.into(),
);
};
let vin = std::env::args()
.nth(1)
.ok_or("pass the vehicle's VIN as the first argument")?;
let tokens = TokenManager::new(dir, Arc::new(HttpRefresher::new(client_id)));
let client = Client::builder(tokens).build()?;
let req = GetVehicleData::new(vin, vec![DataEndpoint::ChargeState])?;
let price = match req.pricing() {
Pricing::Data { usd_per_request } => format!("${usd_per_request}"),
_ => "nothing".to_owned(),
};
let metered = client.send_guarded(&req)?;
match metered.value {
Guarded::Skipped { state, .. } => {
let state = if state.is_empty() { "unknown" } else { &state };
println!("car is {state}; skipped the billed call");
println!("spent: nothing (the state check is a free call)");
}
Guarded::Executed(data) => {
let battery = data
.charge_state
.as_ref()
.and_then(|c| c.get("battery_level"))
.and_then(|v| v.as_u64());
match battery {
Some(level) => println!("battery {level}%"),
None => println!("the response carried no charge_state.battery_level"),
}
println!("spent: one billed request ({price})");
}
_ => println!("unrecognised gate outcome"),
}
if let Some(rl) = metered.rate_limit {
println!("quota: {} of {} left", rl.remaining, rl.limit);
}
Ok(())
}