daemonise

This commit is contained in:
Alex Janka 2023-12-27 18:29:12 +11:00
parent 5b19c92bfc
commit af060b8d40

View file

@ -1,12 +1,10 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{io::BufRead, path::PathBuf}; use std::{path::PathBuf, time::Duration};
use teslatte::{ use teslatte::{
auth::{AccessToken, RefreshToken}, auth::{AccessToken, RefreshToken},
vehicles::{ActuateTrunk, SetChargingAmps},
FleetApi, FleetVehicleApi, FleetApi, FleetVehicleApi,
}; };
use tokio::try_join;
use crate::{config::Config, errors::*}; use crate::{config::Config, errors::*};
@ -25,54 +23,9 @@ struct Args {
#[derive(Subcommand, Debug, Clone)] #[derive(Subcommand, Debug, Clone)]
enum Commands { enum Commands {
/// Run charge controller server /// Run charge controller server
Watch { Watch,
#[clap(long)]
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)]
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
}
}
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] #[tokio::main]
@ -80,92 +33,36 @@ 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;
if let Ok(res) = products { match args.command {
if let Some(teslatte::products::Product::Vehicle(vehicle)) = res.first() {
api.wake_up(&vehicle.vin).await.unwrap();
match match args.command {
Commands::GenerateConfig => { Commands::GenerateConfig => {
println!( println!(
"{}", "{}",
ron::ser::to_string_pretty(&Config::default(), Default::default()) ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap()
.unwrap()
); );
Ok(MaybePostResponse::None)
} }
Commands::Watch { flash } => { Commands::Watch => match get_auth(auth_path).await {
if flash { Ok(api) => {
println!("{vehicle:#?}"); let products = api.products().await;
match try_join!( match products {
api.honk_horn(&vehicle.vin), Ok(res) => match res.first() {
api.flash_lights(&vehicle.vin) Some(teslatte::products::Product::Vehicle(vehicle)) => {
) { api.wake_up(&vehicle.vin).await.unwrap();
loop {
match api.flash_lights(&vehicle.vin).await {
Ok(_r) => println!("flashed"), Ok(_r) => println!("flashed"),
Err(e) => println!("error: {e:#?}"), Err(e) => println!("error: {e:#?}"),
} }
std::thread::sleep(Duration::from_secs(10));
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:#?}"),
} }
} }
} _ => println!("No first item"),
Ok(MaybePostResponse::None)
}
Commands::SetChargeRate { amps } => api
.set_charging_amps(
&vehicle.vin,
&SetChargingAmps {
charging_amps: amps as i64,
}, },
) Err(e) => println!("Error getting products: {e:#?}"),
.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()),
},
} }
} }