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

161 lines
4.4 KiB
Rust
Raw Normal View History

2024-01-06 09:33:56 +11:00
#![feature(async_closure)]
2023-12-28 12:41:05 +11:00
#[macro_use]
extern crate rocket;
use anyhow::Result;
2023-12-25 21:22:08 +11:00
use clap::{Parser, Subcommand};
2023-12-28 12:41:05 +11:00
2023-12-27 12:20:45 +11:00
use serde::{Deserialize, Serialize};
2024-01-06 09:33:56 +11:00
use std::{
path::PathBuf,
time::{Duration, Instant},
};
2023-12-27 12:20:45 +11:00
use teslatte::{
auth::{AccessToken, RefreshToken},
2023-12-28 12:41:05 +11:00
FleetApi,
2023-12-27 12:20:45 +11:00
};
use crate::{config::Config, errors::*};
2023-12-25 21:22:08 +11:00
mod config;
mod errors;
2023-12-28 12:41:05 +11:00
mod server;
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 18:29:12 +11:00
Watch,
/// Print the default config file
GenerateConfig,
2023-12-27 16:42:29 +11:00
}
2023-12-25 21:22:08 +11:00
#[tokio::main]
async fn main() {
let args = Args::parse();
2023-12-28 12:41:05 +11:00
2023-12-25 21:22:08 +11:00
let auth_path = args.config_dir.join("auth");
2023-12-28 12:41:05 +11:00
let config_path = args.config_dir.join("config");
2023-12-27 16:42:29 +11:00
2023-12-27 18:29:12 +11:00
match args.command {
Commands::GenerateConfig => {
println!(
"{}",
ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap()
);
}
Commands::Watch => match get_auth(auth_path).await {
2024-01-06 09:33:56 +11:00
Ok(auth) => {
2023-12-28 12:41:05 +11:00
let config: Config =
ron::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
2024-01-06 09:33:56 +11:00
let products = auth.api().products().await;
2023-12-27 18:29:12 +11:00
match products {
Ok(res) => match res.first() {
Some(teslatte::products::Product::Vehicle(vehicle)) => {
2023-12-28 12:41:05 +11:00
server::launch_server(server::ServerState {
config,
2024-01-06 09:33:56 +11:00
auth,
2023-12-28 12:41:05 +11:00
vehicle: vehicle.clone(),
})
.await;
2023-12-27 17:31:40 +11:00
}
2023-12-27 18:29:12 +11:00
_ => println!("No first item"),
},
Err(e) => println!("Error getting products: {e:#?}"),
2023-12-27 12:20:45 +11:00
}
2023-12-25 21:22:08 +11:00
}
2023-12-27 18:29:12 +11:00
Err(e) => println!("{}", e.error_string()),
},
2023-12-27 17:31:40 +11:00
}
}
2023-12-28 12:41:05 +11:00
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Coords {
pub latitude: f64,
pub longitude: f64,
2023-12-27 17:31:40 +11:00
}
2023-12-28 12:41:05 +11:00
const COORD_PRECISION: f64 = 0.001;
impl Coords {
fn overlaps(&self, other: &Coords) -> bool {
(self.latitude - other.latitude).abs() < COORD_PRECISION
&& (self.longitude - other.longitude).abs() < COORD_PRECISION
2023-12-25 21:22:08 +11:00
}
}
2024-01-06 09:33:56 +11:00
async fn get_auth(auth_path: PathBuf) -> Result<FleetApiAuth, 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");
2024-01-06 09:33:56 +11:00
save_key(&auth_path, &api)?;
2023-12-28 12:41:05 +11:00
// api.print_responses = teslatte::PrintResponses::Pretty;
2024-01-06 09:33:56 +11:00
Ok(FleetApiAuth::new(api, Instant::now(), auth_path))
}
struct FleetApiAuth {
api: FleetApi,
last_refresh: Instant,
auth_path: PathBuf,
}
const REFRESH_INTERVAL: Duration = Duration::from_secs(12 * 60 * 60);
impl FleetApiAuth {
fn new(api: FleetApi, last_refresh: Instant, auth_path: PathBuf) -> Self {
Self {
api,
last_refresh,
auth_path,
}
}
fn api(&self) -> &FleetApi {
&self.api
}
#[allow(unused)]
async fn refresh(&mut self) {
if Instant::now().duration_since(self.last_refresh) >= REFRESH_INTERVAL {
match self.api.refresh().await {
Ok(_) => {
let now = Instant::now();
match save_key(&self.auth_path, &self.api) {
Ok(_) => self.last_refresh = now,
Err(e) => eprintln!("error saving auth token: {e:?}"),
}
}
Err(e) => eprintln!("error refreshing auth token: {e:?}"),
}
}
}
2023-12-27 12:20:45 +11:00
}
2024-01-06 09:33:56 +11:00
#[derive(Serialize, Deserialize, Clone)]
2023-12-27 12:20:45 +11:00
struct AuthInfo {
access_token: AccessToken,
refresh_token: Option<RefreshToken>,
2023-12-25 21:22:08 +11:00
}
2024-01-06 09:33:56 +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
}