tesla-charge-controller/src/main.rs

104 lines
3.3 KiB
Rust

#![feature(never_type)]
#[macro_use]
extern crate rocket;
use api_interface::TeslaInterface;
use clap::{Parser, Subcommand};
use pl_interface::Pli;
use std::path::PathBuf;
use crate::config::Config;
mod api_interface;
mod config;
mod errors;
mod pl_interface;
mod server;
mod types;
#[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
Watch,
/// Print the default config file
GenerateConfig,
}
#[tokio::main]
async fn main() {
let args = Args::parse();
env_logger::init();
let auth_path = args.config_dir.join("auth");
let config_path = args.config_dir.join("config");
let _recorder = metrics_prometheus::install();
match args.command {
Commands::GenerateConfig => {
println!(
"{}",
ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap()
);
}
Commands::Watch => match TeslaInterface::load(auth_path).await {
Ok(mut interface) => {
let config: Config =
ron::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
// build the channel that takes messages from the webserver thread to the api thread
let (api_requests, receiver) = async_channel::unbounded();
let server_handle = server::launch_server(server::ServerState {
config: config.clone(),
state: interface.state.clone(),
api_requests,
});
// spawn the api loop
tokio::task::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(120));
loop {
// await either the next interval OR a message from the other thread
tokio::select! {
_ = interval.tick() => interface.refresh().await,
message = receiver.recv() => match message {
Ok(message) => interface.process_request(message).await,
Err(e) => error!("Error on receive channel: {e:#?}")
}
}
}
});
// try to spawn the pli loop
match Pli::new(config.serial_port, config.baud_rate) {
Ok(mut pli) => {
tokio::task::spawn(async move {
let mut interval =
tokio::time::interval(std::time::Duration::from_secs(30));
loop {
interval.tick().await;
pli.refresh();
}
});
}
Err(e) => log::error!("Error connecting to serial device for PLI: {e:?}"),
}
server_handle.await;
}
Err(e) => error!("{}", e.error_string()),
},
}
}