#![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 tesla_charge_rate::TeslaChargeRateController; use crate::config::Config; mod api_interface; mod charge_controllers; mod config; mod errors; mod server; mod tesla_charge_rate; 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::builder() .format_module_path(false) .format_timestamp( if std::env::var("LOG_TIMESTAMP").is_ok_and(|v| v == "false") { None } else { Some(env_logger::TimestampPrecision::Seconds) }, ) .init(); let auth_path = args.config_dir.join("auth"); let _ = CONFIG_PATH.set(args.config_dir.join("config.json")); let _recorder = metrics_prometheus::install(); match args.command { Commands::GenerateConfig => { println!( "{}", serde_json::ser::to_string_pretty(&Config::default()).unwrap(), ); } Commands::Watch => { if let Some(mut interface) = TeslaInterface::load(auth_path) .await .some_or_print_with("loading tesla interface") { 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(); // and to the charge rate controller thread let (tcrc_requests, tcrc_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) => panic!("Error on PLI receive channel: {e:?}") } } } }); Some(pl_state) } Err(e) => { log::error!("Error connecting to serial device for PLI: {e:?}"); None } }; let mut tesla_charge_rate_controller = TeslaChargeRateController::new(interface.state.clone(), pl_state.clone()); let server_handle = server::launch_server(server::ServerState { car_state: interface.state.clone(), pl_state, tcrc_state: tesla_charge_rate_controller.tcrc_state.clone(), api_requests, pli_requests, tcrc_requests, }); // spawn the api loop tokio::task::spawn(async move { let mut interval = tokio::time::interval(std::time::Duration::from_secs( config.tesla_update_interval_seconds, )); let mut charge_interval = tokio::time::interval(std::time::Duration::from_secs( config.tesla_update_interval_while_charging_seconds, )); loop { // await either the next interval OR a message from the other thread tokio::select! { _ = interval.tick() => { if !interface.state.read().unwrap().is_charging_at_home() { interface.refresh().await } }, _ = charge_interval.tick() => { if interface.state.read().unwrap().is_charging_at_home() { if let Some(request) = tesla_charge_rate_controller.control_charge_rate() { interface.process_request(request).await; } interface.refresh().await } } api_message = api_receiver.recv() => match api_message { Ok(message) => interface.process_request(message).await, Err(e) => panic!("Error on Tesla receive channel: {e:?}") }, tcrc_message = tcrc_receiver.recv() => match tcrc_message { Ok(message) => tesla_charge_rate_controller.process_request(message), Err(e) => panic!("Error on TCRC receive channel: {e:?}") } } } }); server_handle.await; } } } }