many commands

This commit is contained in:
Alex Janka 2023-12-27 17:31:40 +11:00
parent 1ea04ba5ec
commit 5672331f5c
2 changed files with 102 additions and 28 deletions

View file

@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
use std::{io::BufRead, path::PathBuf}; use std::{io::BufRead, path::PathBuf};
use teslatte::{ use teslatte::{
auth::{AccessToken, RefreshToken}, auth::{AccessToken, RefreshToken},
vehicles::{ActuateTrunk, SetChargingAmps},
FleetApi, FleetVehicleApi, FleetApi, FleetVehicleApi,
}; };
use tokio::try_join; use tokio::try_join;
@ -28,8 +29,23 @@ enum Commands {
#[clap(long)] #[clap(long)]
flash: bool, flash: bool,
}, },
/// Run charge controller server
SetChargeRate {
#[clap(long)]
amps: usize,
},
/// Print the default config file /// Print the default config file
GenerateConfig, GenerateConfig,
/// Start charging
StartCharge,
/// Stop charging
StopCharge,
/// Toggle boot
ToggleBoot,
/// Open charger door
ChargerOpen,
/// Close charger door
ChargerClose,
} }
#[allow(unused)] #[allow(unused)]
@ -64,46 +80,104 @@ async fn main() {
let args = Args::parse(); let args = Args::parse();
let auth_path = args.config_dir.join("auth"); let auth_path = args.config_dir.join("auth");
// let config_path = args.config_dir.join("config"); // let config_path = args.config_dir.join("config");
match get_auth(auth_path).await {
Ok(api) => {
let products = api.products().await;
match args.command { if let Ok(res) = products {
Commands::GenerateConfig => { if let Some(teslatte::products::Product::Vehicle(vehicle)) = res.first() {
println!( api.wake_up(&vehicle.vin).await.unwrap();
"{}",
ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap()
);
}
Commands::Watch { flash } => match get_auth(auth_path).await {
Ok(api) => {
let products = api.products().await;
// println!("got products: {:#?}", products);
if flash { match match args.command {
if let Ok(res) = products { Commands::GenerateConfig => {
if let Some(teslatte::products::Product::Vehicle(vehicle)) = res.first() { println!(
println!("{vehicle:#?}"); "{}",
match try_join!( ron::ser::to_string_pretty(&Config::default(), Default::default())
api.honk_horn(&vehicle.vin), .unwrap()
api.flash_lights(&vehicle.vin) );
) { Ok(MaybePostResponse::None)
Ok(_r) => println!("flashed"), }
Err(e) => println!("error: {e:#?}"), Commands::Watch { flash } => {
} if flash {
println!("{vehicle:#?}");
while loop_prompt() {
match try_join!( match try_join!(
api.honk_horn(&vehicle.vin), api.honk_horn(&vehicle.vin),
api.flash_lights(&vehicle.vin) api.flash_lights(&vehicle.vin)
) { ) {
Ok(_r) => println!("honked"), Ok(_r) => println!("flashed"),
Err(e) => println!("error: {e:#?}"), Err(e) => println!("error: {e:#?}"),
} }
while loop_prompt() {
match try_join!(
api.honk_horn(&vehicle.vin),
api.flash_lights(&vehicle.vin)
) {
Ok(_r) => println!("honked"),
Err(e) => println!("error: {e:#?}"),
}
}
} }
Ok(MaybePostResponse::None)
} }
Commands::SetChargeRate { amps } => api
.set_charging_amps(
&vehicle.vin,
&SetChargingAmps {
charging_amps: amps as i64,
},
)
.await
.map(|v| v.into()),
Commands::StartCharge => {
api.charge_start(&vehicle.vin).await.map(|v| v.into())
}
Commands::StopCharge => {
api.charge_stop(&vehicle.vin).await.map(|v| v.into())
}
Commands::ToggleBoot => api
.actuate_trunk(
&vehicle.vin,
&ActuateTrunk {
which_trunk: teslatte::vehicles::Trunk::rear,
},
)
.await
.map(|v| v.into()),
Commands::ChargerOpen => api
.charge_port_door_open(&vehicle.vin)
.await
.map(|v| v.into()),
Commands::ChargerClose => api
.charge_port_door_close(&vehicle.vin)
.await
.map(|v| v.into()),
} {
Ok(MaybePostResponse::PostResponse(response)) => {
println!("post response: {response:#?}")
}
Ok(_) => {}
Err(error) => println!("teslatte error: {error:#?}"),
} }
} }
} }
Err(e) => println!("{}", e.error_string()), }
}, Err(e) => println!("{}", e.error_string()),
}
}
#[derive(Debug)]
pub enum MaybePostResponse {
None,
PostResponse(teslatte::PostResponse),
}
impl From<teslatte::PostResponse> for MaybePostResponse {
fn from(value: teslatte::PostResponse) -> Self {
MaybePostResponse::PostResponse(value)
} }
} }

@ -1 +1 @@
Subproject commit 491d9a58a8183b91314990274da805c156a6f48c Subproject commit 22bfecd8138ecacfe171255244360917f77ddb00