tesla-charge-controller/server/src/main.rs

210 lines
6.7 KiB
Rust
Raw Normal View History

2023-12-25 21:22:08 +11:00
use clap::{Parser, Subcommand};
2023-12-27 12:20:45 +11:00
use serde::{Deserialize, Serialize};
use std::{io::BufRead, path::PathBuf};
2023-12-27 12:20:45 +11:00
use teslatte::{
auth::{AccessToken, RefreshToken},
2023-12-27 17:31:40 +11:00
vehicles::{ActuateTrunk, SetChargingAmps},
2023-12-27 16:42:29 +11:00
FleetApi, FleetVehicleApi,
2023-12-27 12:20:45 +11:00
};
2023-12-27 16:42:29 +11:00
use tokio::try_join;
use crate::{config::Config, errors::*};
2023-12-25 21:22:08 +11:00
mod config;
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-27 17:31:40 +11:00
/// Run charge controller server
SetChargeRate {
#[clap(long)]
amps: usize,
},
/// Print the default config file
GenerateConfig,
2023-12-27 17:31:40 +11:00
/// Start charging
StartCharge,
/// Stop charging
StopCharge,
/// Toggle boot
ToggleBoot,
/// Open charger door
ChargerOpen,
/// Close charger door
ChargerClose,
2023-12-25 21:22:08 +11:00
}
2023-12-27 16:42:29 +11:00
#[allow(unused)]
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
}
}
2023-12-27 16:42:29 +11:00
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
}
}
2023-12-25 21:22:08 +11:00
#[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-27 17:31:40 +11:00
match get_auth(auth_path).await {
Ok(api) => {
let products = api.products().await;
2023-12-27 17:31:40 +11:00
if let Ok(res) = products {
if let Some(teslatte::products::Product::Vehicle(vehicle)) = res.first() {
api.wake_up(&vehicle.vin).await.unwrap();
2023-12-27 16:42:29 +11:00
2023-12-27 17:31:40 +11:00
match match args.command {
Commands::GenerateConfig => {
println!(
"{}",
ron::ser::to_string_pretty(&Config::default(), Default::default())
.unwrap()
);
Ok(MaybePostResponse::None)
}
Commands::Watch { flash } => {
if flash {
println!("{vehicle:#?}");
2023-12-27 16:42:29 +11:00
match try_join!(
api.honk_horn(&vehicle.vin),
api.flash_lights(&vehicle.vin)
) {
2023-12-27 17:31:40 +11:00
Ok(_r) => println!("flashed"),
2023-12-27 16:42:29 +11:00
Err(e) => println!("error: {e:#?}"),
}
2023-12-27 17:31:40 +11:00
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:#?}"),
}
}
2023-12-27 16:42:29 +11:00
}
2023-12-27 17:31:40 +11:00
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())
2023-12-27 12:20:45 +11:00
}
2023-12-27 17:31:40 +11:00
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:#?}"),
2023-12-27 12:20:45 +11:00
}
}
2023-12-25 21:22:08 +11:00
}
2023-12-27 17:31:40 +11:00
}
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)
2023-12-25 21:22:08 +11:00
}
}
2023-12-27 16:42:29 +11:00
async fn get_auth(auth_path: PathBuf) -> Result<FleetApi, AuthLoadError> {
2023-12-27 12:20:45 +11:00
let key: AuthInfo = ron::from_str(&std::fs::read_to_string(&auth_path)?)?;
2023-12-27 16:42:29 +11:00
let mut api = FleetApi::new(key.access_token, key.refresh_token);
2023-12-27 12:20:45 +11:00
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-27 16:42:29 +11:00
fn save_key(auth_path: PathBuf, api: &FleetApi) -> Result<(), SaveError> {
2023-12-27 12:20:45 +11:00
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
}