2024-01-08 12:00:09 +11:00
|
|
|
#![feature(never_type)]
|
2024-01-06 09:33:56 +11:00
|
|
|
|
2023-12-28 12:41:05 +11:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
use api_interface::TeslaInterface;
|
2024-01-13 17:06:48 +11:00
|
|
|
use charge_controllers::pl::Pli;
|
2023-12-25 21:22:08 +11:00
|
|
|
use clap::{Parser, Subcommand};
|
2024-01-14 09:40:34 +11:00
|
|
|
use config::{access_config, CONFIG_PATH};
|
2024-01-12 09:09:42 +11:00
|
|
|
use errors::PrintErrors;
|
2024-01-08 12:00:09 +11:00
|
|
|
use std::path::PathBuf;
|
2023-12-28 12:41:05 +11:00
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
use crate::config::Config;
|
2023-12-25 21:22:08 +11:00
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
mod api_interface;
|
2024-01-13 17:06:48 +11:00
|
|
|
mod charge_controllers;
|
2023-12-25 21:22:08 +11:00
|
|
|
mod config;
|
2023-12-26 08:58:14 +11:00
|
|
|
mod errors;
|
2023-12-28 12:41:05 +11:00
|
|
|
mod server;
|
2024-01-07 10:08:16 +11:00
|
|
|
mod types;
|
2023-12-25 21:22:08 +11:00
|
|
|
|
|
|
|
#[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
|
2023-12-27 18:29:12 +11:00
|
|
|
Watch,
|
2023-12-26 08:58:14 +11:00
|
|
|
/// Print the default config file
|
|
|
|
GenerateConfig,
|
2023-12-27 16:42:29 +11:00
|
|
|
}
|
|
|
|
|
2023-12-25 21:22:08 +11:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let args = Args::parse();
|
2024-01-15 10:22:10 +11:00
|
|
|
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();
|
2023-12-28 12:41:05 +11:00
|
|
|
|
2023-12-25 21:22:08 +11:00
|
|
|
let auth_path = args.config_dir.join("auth");
|
2024-01-14 09:40:34 +11:00
|
|
|
|
|
|
|
let _ = CONFIG_PATH.set(args.config_dir.join("config"));
|
2023-12-27 16:42:29 +11:00
|
|
|
|
2024-01-10 15:22:28 +11:00
|
|
|
let _recorder = metrics_prometheus::install();
|
|
|
|
|
2023-12-27 18:29:12 +11:00
|
|
|
match args.command {
|
|
|
|
Commands::GenerateConfig => {
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap()
|
|
|
|
);
|
|
|
|
}
|
2024-01-12 09:09:42 +11:00
|
|
|
Commands::Watch => {
|
2024-01-15 10:13:46 +11:00
|
|
|
if let Some(mut interface) = TeslaInterface::load(auth_path)
|
|
|
|
.await
|
|
|
|
.some_or_print_with("loading tesla interface")
|
|
|
|
{
|
2024-01-14 09:40:34 +11:00
|
|
|
let config = access_config();
|
2024-01-09 11:50:37 +11:00
|
|
|
// build the channel that takes messages from the webserver thread to the api thread
|
2024-01-11 08:57:41 +11:00
|
|
|
let (api_requests, api_receiver) = async_channel::unbounded();
|
|
|
|
// and to the pli thread
|
|
|
|
let (pli_requests, pli_receiver) = async_channel::unbounded();
|
2024-01-13 17:06:48 +11:00
|
|
|
charge_controllers::register_metrics();
|
2024-01-08 12:16:25 +11:00
|
|
|
|
2024-01-11 10:28:01 +11:00
|
|
|
// try to spawn the pli loop
|
2024-01-12 09:27:38 +11:00
|
|
|
let pl_state = match Pli::new(
|
|
|
|
config.serial_port.clone(),
|
|
|
|
config.baud_rate,
|
|
|
|
config.pl_timeout_milliseconds,
|
|
|
|
) {
|
2024-01-11 10:28:01 +11:00
|
|
|
Ok(mut pli) => {
|
|
|
|
let pl_state = pli.state.clone();
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
let mut interval = tokio::time::interval(
|
2024-01-12 09:27:38 +11:00
|
|
|
std::time::Duration::from_secs(config.pl_watch_interval_seconds),
|
2024-01-11 10:28:01 +11:00
|
|
|
);
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
_ = interval.tick() => pli.refresh(),
|
|
|
|
message = pli_receiver.recv() => match message {
|
|
|
|
Ok(message) => pli.process_request(message),
|
2024-01-15 09:26:38 +11:00
|
|
|
Err(e) => panic!("Error on PLI receive channel: {e:?}")
|
2024-01-11 10:28:01 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Some(pl_state)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Error connecting to serial device for PLI: {e:?}");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-08 12:00:09 +11:00
|
|
|
let server_handle = server::launch_server(server::ServerState {
|
2024-01-11 10:28:01 +11:00
|
|
|
car_state: interface.state.clone(),
|
|
|
|
pl_state,
|
2024-01-08 12:16:25 +11:00
|
|
|
api_requests,
|
2024-01-11 08:57:41 +11:00
|
|
|
pli_requests,
|
2024-01-08 12:00:09 +11:00
|
|
|
});
|
2024-01-06 09:33:56 +11:00
|
|
|
|
2024-01-09 11:50:37 +11:00
|
|
|
// spawn the api loop
|
2024-01-08 12:00:09 +11:00
|
|
|
tokio::task::spawn(async move {
|
2024-01-11 10:28:01 +11:00
|
|
|
let mut interval = tokio::time::interval(std::time::Duration::from_secs(
|
2024-01-12 09:27:38 +11:00
|
|
|
config.tesla_watch_interval_seconds,
|
2024-01-11 10:28:01 +11:00
|
|
|
));
|
2024-01-08 12:00:09 +11:00
|
|
|
loop {
|
2024-01-09 11:50:37 +11:00
|
|
|
// await either the next interval OR a message from the other thread
|
2024-01-08 12:16:25 +11:00
|
|
|
tokio::select! {
|
|
|
|
_ = interval.tick() => interface.refresh().await,
|
2024-01-11 08:57:41 +11:00
|
|
|
message = api_receiver.recv() => match message {
|
2024-01-08 12:16:25 +11:00
|
|
|
Ok(message) => interface.process_request(message).await,
|
2024-01-15 09:26:38 +11:00
|
|
|
Err(e) => panic!("Error on Tesla receive channel: {e:?}")
|
2024-01-08 12:16:25 +11:00
|
|
|
}
|
|
|
|
}
|
2024-01-06 09:33:56 +11:00
|
|
|
}
|
2024-01-08 12:00:09 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
server_handle.await;
|
2024-01-06 09:33:56 +11:00
|
|
|
}
|
2024-01-12 09:09:42 +11:00
|
|
|
}
|
2024-01-06 09:33:56 +11:00
|
|
|
}
|
2023-12-27 12:20:45 +11:00
|
|
|
}
|