This commit is contained in:
Alex Janka 2023-12-27 16:42:29 +11:00
parent f2e0bb6dc5
commit 1ea04ba5ec
3 changed files with 36 additions and 52 deletions

View file

@ -1,3 +0,0 @@
use teslatte::OwnerApi;
pub async fn control_loop(_api: OwnerApi) {}

View file

@ -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(),
}
}
}

View file

@ -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<OwnerApi, AuthLoadError> {
async fn get_auth(auth_path: PathBuf) -> Result<FleetApi, AuthLoadError> {
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<RefreshToken>,
}
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 {