2024-01-08 12:00:09 +11:00
|
|
|
#![feature(never_type)]
|
2024-01-06 09:33:56 +11:00
|
|
|
|
2023-12-28 12:41:05 +11:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
use api_interface::TeslaInterface;
|
2023-12-25 21:22:08 +11:00
|
|
|
use clap::{Parser, Subcommand};
|
2024-01-08 12:00:09 +11:00
|
|
|
use std::path::PathBuf;
|
2023-12-28 12:41:05 +11:00
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
use crate::config::Config;
|
2023-12-25 21:22:08 +11:00
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
mod api_interface;
|
2023-12-25 21:22:08 +11:00
|
|
|
mod config;
|
2023-12-26 08:58:14 +11:00
|
|
|
mod errors;
|
2023-12-28 12:41:05 +11:00
|
|
|
mod server;
|
2024-01-07 10:08:16 +11:00
|
|
|
mod types;
|
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,
|
2023-12-26 08:58:14 +11:00
|
|
|
/// 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()
|
|
|
|
);
|
|
|
|
}
|
2024-01-08 12:00:09 +11:00
|
|
|
Commands::Watch => match TeslaInterface::load(auth_path).await {
|
|
|
|
Ok(mut interface) => {
|
2023-12-28 12:41:05 +11:00
|
|
|
let config: Config =
|
|
|
|
ron::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
|
2023-12-27 17:31:40 +11:00
|
|
|
|
2024-01-08 12:16:25 +11:00
|
|
|
let (api_requests, receiver) = async_channel::unbounded();
|
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
let server_handle = server::launch_server(server::ServerState {
|
|
|
|
config,
|
|
|
|
state: interface.state.clone(),
|
2024-01-08 12:16:25 +11:00
|
|
|
api_requests,
|
2024-01-08 12:00:09 +11:00
|
|
|
});
|
2024-01-06 09:33:56 +11:00
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
tokio::task::spawn(async move {
|
|
|
|
let mut interval = tokio::time::interval(std::time::Duration::from_secs(120));
|
|
|
|
loop {
|
2024-01-08 12:16:25 +11:00
|
|
|
tokio::select! {
|
|
|
|
_ = interval.tick() => interface.refresh().await,
|
|
|
|
message = receiver.recv() => match message {
|
|
|
|
Ok(message) => interface.process_request(message).await,
|
|
|
|
Err(e) => eprintln!("Error on receive channel: {e:#?}")
|
|
|
|
}
|
|
|
|
}
|
2024-01-06 09:33:56 +11:00
|
|
|
}
|
2024-01-08 12:00:09 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
server_handle.await;
|
2024-01-06 09:33:56 +11:00
|
|
|
}
|
2024-01-08 12:00:09 +11:00
|
|
|
Err(e) => println!("{}", e.error_string()),
|
|
|
|
},
|
2024-01-06 09:33:56 +11:00
|
|
|
}
|
2023-12-27 12:20:45 +11:00
|
|
|
}
|