#![feature(never_type)] #[macro_use] extern crate rocket; use api_interface::TeslaInterface; use charge_controllers::pl::Pli; use clap::{Parser, Subcommand}; use config::{access_config, CONFIG_PATH}; use errors::PrintErrors; use std::path::PathBuf; use crate::config::Config; mod api_interface; mod charge_controllers; mod config; mod errors; 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.set(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 => { if let Some(mut interface) = TeslaInterface::load(auth_path).await.some_or_print() { let config = access_config(); // build the channel that takes messages from the webserver thread to the api thread let (api_requests, api_receiver) = async_channel::unbounded(); // and to the pli thread let (pli_requests, pli_receiver) = async_channel::unbounded(); charge_controllers::register_metrics(); // try to spawn the pli loop let pl_state = match Pli::new( config.serial_port.clone(), config.baud_rate, config.pl_timeout_milliseconds, ) { Ok(mut pli) => { let pl_state = pli.state.clone(); tokio::task::spawn(async move { let mut interval = tokio::time::interval( std::time::Duration::from_secs(config.pl_watch_interval_seconds), ); loop { tokio::select! { _ = interval.tick() => pli.refresh(), message = pli_receiver.recv() => match message { Ok(message) => pli.process_request(message), Err(e) => error!("Error on PLI receive channel: {e:?}") } } } }); Some(pl_state) } Err(e) => { log::error!("Error connecting to serial device for PLI: {e:?}"); None } }; let server_handle = server::launch_server(server::ServerState { car_state: interface.state.clone(), pl_state, api_requests, pli_requests, }); // spawn the api loop tokio::task::spawn(async move { let mut interval = tokio::time::interval(std::time::Duration::from_secs( config.tesla_watch_interval_seconds, )); loop { // await either the next interval OR a message from the other thread tokio::select! { _ = interval.tick() => interface.refresh().await, message = api_receiver.recv() => match message { Ok(message) => interface.process_request(message).await, Err(e) => error!("Error on Tesla receive channel: {e:?}") } } } }); server_handle.await; } } } }