tesla-charge-controller/src/main.rs
2024-01-18 16:26:58 +11:00

228 lines
9.8 KiB
Rust

#[macro_use]
extern crate rocket;
use api_interface::TeslaInterface;
use charge_controllers::{pl::Pli, tristar::Tristar};
use clap::{Parser, Subcommand};
use config::{access_config, ChargeControllerConfig, CONFIG_PATH};
use errors::PrintErrors;
use std::path::PathBuf;
use tesla_charge_rate::TeslaChargeRateController;
use teslatte::vehicles::ChargingState;
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();
// 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 _additional_controllers: Vec<_> = config
.additional_charge_controllers
.iter()
.filter_map(|v| match v {
ChargeControllerConfig::Pl {
serial_port,
baud_rate,
timeout_milliseconds,
watch_interval_seconds,
} => Pli::new(serial_port.clone(), *baud_rate, *timeout_milliseconds)
.some_or_print_with("Failed to connect to additional PLI")
.map(|mut pli| {
tokio::task::spawn(async move {
let mut interval = tokio::time::interval(
std::time::Duration::from_secs(*watch_interval_seconds),
);
loop {
interval.tick().await;
pli.refresh();
}
})
}),
ChargeControllerConfig::Tristar {
serial_port,
baud_rate,
watch_interval_seconds,
} => Tristar::new(serial_port.clone(), *baud_rate)
.some_or_print_with("Failed to connect to additional Tristar")
.map(|mut tristar| {
tokio::task::spawn_local(async move {
let mut interval = tokio::time::interval(
std::time::Duration::from_secs(*watch_interval_seconds),
);
loop {
interval.tick().await;
tristar.refresh();
}
})
}),
})
.collect();
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 normal_data_update_interval = tokio::time::interval(
std::time::Duration::from_secs(config.tesla_update_interval_seconds),
);
let mut charge_data_update_interval =
tokio::time::interval(std::time::Duration::from_secs(
config.tesla_update_interval_while_charging_seconds,
));
let mut charge_rate_update_interval = tokio::time::interval(
std::time::Duration::from_secs(config.charge_rate_update_interval_seconds),
);
let mut was_connected = false;
loop {
// await either the next interval OR a message from the other thread
tokio::select! {
_ = normal_data_update_interval.tick() => {
if !interface.state.read().unwrap().is_charging_at_home() {
interface.refresh().await
}
},
_ = charge_data_update_interval.tick() => {
if interface.state.read().unwrap().is_charging_at_home() {
interface.refresh().await
}
},
_ = charge_rate_update_interval.tick() => {
if interface.state.read().unwrap().is_charging_at_home() {
was_connected = true;
if let Some(request) = tesla_charge_rate_controller.control_charge_rate() {
interface.process_request(request).await;
}
} else if was_connected
&& interface
.state
.read()
.unwrap()
.charge_state
.is_some_and(|v| v.charging_state == ChargingState::Disconnected)
{
was_connected = false;
tesla_charge_rate_controller.process_request(
tesla_charge_rate::TcrcRequest::EnableAutomaticControl,
);
}
},
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;
}
}
}
}