2023-12-25 21:22:08 +11:00
|
|
|
use clap::{Parser, Subcommand};
|
2023-12-27 12:20:45 +11:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-12-26 08:58:14 +11:00
|
|
|
use std::{io::BufRead, path::PathBuf};
|
2023-12-27 12:20:45 +11:00
|
|
|
use teslatte::{
|
|
|
|
auth::{AccessToken, RefreshToken},
|
|
|
|
OwnerApi, VehicleApi,
|
|
|
|
};
|
2023-12-26 08:58:14 +11:00
|
|
|
|
|
|
|
use crate::{config::Config, errors::*};
|
2023-12-25 21:22:08 +11:00
|
|
|
|
|
|
|
mod config;
|
2023-12-27 12:20:45 +11:00
|
|
|
mod control;
|
2023-12-26 08:58:14 +11:00
|
|
|
mod errors;
|
2023-12-25 21:22:08 +11:00
|
|
|
|
|
|
|
#[derive(Parser, Debug, Clone)]
|
|
|
|
#[clap(author, version, about, long_about = None)]
|
|
|
|
struct Args {
|
|
|
|
#[command(subcommand)]
|
|
|
|
command: Commands,
|
|
|
|
#[clap(long, default_value = "/etc/tesla-charge-controller")]
|
|
|
|
config_dir: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
|
|
|
enum Commands {
|
|
|
|
/// Run charge controller server
|
2023-12-27 12:20:45 +11:00
|
|
|
Watch {
|
|
|
|
#[clap(long)]
|
|
|
|
flash: bool,
|
|
|
|
},
|
2023-12-25 21:22:08 +11:00
|
|
|
/// Authenticate with Tesla login
|
2023-12-26 08:58:14 +11:00
|
|
|
Auth,
|
|
|
|
/// Print the default config file
|
|
|
|
GenerateConfig,
|
2023-12-25 21:22:08 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn press_y_to_continue() -> bool {
|
|
|
|
println!("Continue? [y/N]");
|
|
|
|
let mut line = String::new();
|
|
|
|
let stdin = std::io::stdin();
|
|
|
|
stdin.lock().read_line(&mut line).unwrap();
|
|
|
|
if line.to_uppercase() == "Y\n" {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
println!("Exiting now!");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let args = Args::parse();
|
|
|
|
let auth_path = args.config_dir.join("auth");
|
2023-12-27 12:20:45 +11:00
|
|
|
// let config_path = args.config_dir.join("config");
|
2023-12-26 08:58:14 +11:00
|
|
|
|
2023-12-25 21:22:08 +11:00
|
|
|
match args.command {
|
2023-12-26 08:58:14 +11:00
|
|
|
Commands::GenerateConfig => {
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Commands::Auth => {
|
2023-12-25 21:22:08 +11:00
|
|
|
if auth_path.exists() {
|
|
|
|
println!("Auth file already exists");
|
|
|
|
if !press_y_to_continue() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Err(e) = log_in(auth_path).await {
|
2023-12-26 08:58:14 +11:00
|
|
|
println!("{}", e.error_string());
|
2023-12-25 21:22:08 +11:00
|
|
|
}
|
|
|
|
}
|
2023-12-27 12:20:45 +11:00
|
|
|
Commands::Watch { flash } => match get_auth(auth_path).await {
|
2023-12-25 21:22:08 +11:00
|
|
|
Ok(api) => {
|
2023-12-27 12:20:45 +11:00
|
|
|
let products = api.products().await;
|
|
|
|
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 {
|
|
|
|
Ok(_r) => println!("flashed"),
|
|
|
|
Err(e) => println!("error: {e:#?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
control::control_loop(api).await;
|
2023-12-25 21:22:08 +11:00
|
|
|
}
|
2023-12-26 08:58:14 +11:00
|
|
|
Err(e) => println!("{}", e.error_string()),
|
2023-12-25 21:22:08 +11:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 12:20:45 +11:00
|
|
|
async fn get_auth(auth_path: PathBuf) -> Result<OwnerApi, 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);
|
|
|
|
api.refresh().await?;
|
|
|
|
println!("Refreshed auth key");
|
|
|
|
save_key(auth_path, &api)?;
|
|
|
|
Ok(api)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct AuthInfo {
|
|
|
|
access_token: AccessToken,
|
|
|
|
refresh_token: Option<RefreshToken>,
|
2023-12-25 21:22:08 +11:00
|
|
|
}
|
|
|
|
|
2023-12-26 08:58:14 +11:00
|
|
|
async fn log_in(auth_path: PathBuf) -> Result<(), LoginError> {
|
2023-12-25 21:22:08 +11:00
|
|
|
let v = OwnerApi::from_interactive_url().await?;
|
|
|
|
|
2023-12-27 12:20:45 +11:00
|
|
|
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> {
|
|
|
|
std::fs::write(
|
|
|
|
auth_path,
|
|
|
|
ron::ser::to_string(&AuthInfo {
|
|
|
|
access_token: api.access_token.clone(),
|
|
|
|
refresh_token: api.refresh_token.clone(),
|
|
|
|
})?,
|
|
|
|
)?;
|
2023-12-25 21:22:08 +11:00
|
|
|
println!("Auth successfully saved");
|
|
|
|
Ok(())
|
2023-12-25 18:05:47 +11:00
|
|
|
}
|