tesla-charge-controller/src/main.rs

126 lines
4.3 KiB
Rust

#![feature(never_type)]
#[macro_use]
extern crate rocket;
use api_interface::TeslaInterface;
use charge_controllers::pl::Pli;
use clap::{Parser, Subcommand};
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 = 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: 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, 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 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 {
config: config.clone(),
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 receive channel: {e:?}")
}
}
}
});
server_handle.await;
}
}
}
}