From 1ea04ba5ecf243e44aeddfb661920490bd0b4eff Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Wed, 27 Dec 2023 16:42:29 +1100 Subject: [PATCH] whoop --- src/control.rs | 3 --- src/errors.rs | 17 ------------- src/main.rs | 68 ++++++++++++++++++++++++++------------------------ 3 files changed, 36 insertions(+), 52 deletions(-) delete mode 100644 src/control.rs diff --git a/src/control.rs b/src/control.rs deleted file mode 100644 index 1de702b..0000000 --- a/src/control.rs +++ /dev/null @@ -1,3 +0,0 @@ -use teslatte::OwnerApi; - -pub async fn control_loop(_api: OwnerApi) {} diff --git a/src/errors.rs b/src/errors.rs index 223a3ec..644d0c6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -38,20 +38,3 @@ impl AuthLoadError { } } } - -#[derive(Error, Debug)] -pub enum LoginError { - #[error("teslatte error")] - Teslatte(#[from] teslatte::error::TeslatteError), - #[error("save error")] - Save(#[from] SaveError), -} - -impl LoginError { - pub fn error_string(&self) -> String { - match self { - LoginError::Teslatte(e) => format!("Authentication flow error: {e:?}"), - LoginError::Save(e) => e.error_string(), - } - } -} diff --git a/src/main.rs b/src/main.rs index 15fbe31..288d8cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,13 @@ use serde::{Deserialize, Serialize}; use std::{io::BufRead, path::PathBuf}; use teslatte::{ auth::{AccessToken, RefreshToken}, - OwnerApi, VehicleApi, + FleetApi, FleetVehicleApi, }; +use tokio::try_join; use crate::{config::Config, errors::*}; mod config; -mod control; mod errors; #[derive(Parser, Debug, Clone)] @@ -28,12 +28,11 @@ enum Commands { #[clap(long)] flash: bool, }, - /// Authenticate with Tesla login - Auth, /// Print the default config file GenerateConfig, } +#[allow(unused)] fn press_y_to_continue() -> bool { println!("Continue? [y/N]"); let mut line = String::new(); @@ -47,6 +46,19 @@ fn press_y_to_continue() -> bool { } } +fn loop_prompt() -> bool { + println!("Again? [Y/n]"); + let mut line = String::new(); + let stdin = std::io::stdin(); + stdin.lock().read_line(&mut line).unwrap(); + if line.to_uppercase() == "N\n" { + println!("Exiting now!"); + false + } else { + true + } +} + #[tokio::main] async fn main() { let args = Args::parse(); @@ -60,42 +72,44 @@ async fn main() { ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap() ); } - Commands::Auth => { - if auth_path.exists() { - println!("Auth file already exists"); - if !press_y_to_continue() { - return; - } - } - if let Err(e) = log_in(auth_path).await { - println!("{}", e.error_string()); - } - } Commands::Watch { flash } => match get_auth(auth_path).await { Ok(api) => { let products = api.products().await; - println!("got products: {:#?}", products); + // println!("got products: {:#?}", products); + if flash { if let Ok(res) = products { if let Some(teslatte::products::Product::Vehicle(vehicle)) = res.first() { - let _ = api.honk_horn(&vehicle.id).await; - match api.flash_lights(&vehicle.id).await { + println!("{vehicle:#?}"); + match try_join!( + api.honk_horn(&vehicle.vin), + api.flash_lights(&vehicle.vin) + ) { Ok(_r) => println!("flashed"), 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:#?}"), + } + } } } } - control::control_loop(api).await; } Err(e) => println!("{}", e.error_string()), }, } } -async fn get_auth(auth_path: PathBuf) -> Result { +async fn get_auth(auth_path: PathBuf) -> Result { let key: AuthInfo = ron::from_str(&std::fs::read_to_string(&auth_path)?)?; - let mut api = OwnerApi::new(key.access_token, key.refresh_token); + let mut api = FleetApi::new(key.access_token, key.refresh_token); api.refresh().await?; println!("Refreshed auth key"); save_key(auth_path, &api)?; @@ -108,17 +122,7 @@ struct AuthInfo { refresh_token: Option, } -async fn log_in(auth_path: PathBuf) -> Result<(), LoginError> { - let v = OwnerApi::from_interactive_url().await?; - - let products = v.products().await; - println!("got products: {:#?}", products); - - save_key(auth_path, &v)?; - Ok(()) -} - -fn save_key(auth_path: PathBuf, api: &OwnerApi) -> Result<(), SaveError> { +fn save_key(auth_path: PathBuf, api: &FleetApi) -> Result<(), SaveError> { std::fs::write( auth_path, ron::ser::to_string(&AuthInfo {