launch server even with no cars

This commit is contained in:
Alex Janka 2024-02-13 09:03:35 +11:00
parent 942d3cc3cf
commit 1cde09ff58

View file

@ -64,10 +64,10 @@ async fn main() {
let config_path = args.config_dir.join("config.json"); let config_path = args.config_dir.join("config.json");
let (config, _config_watcher) = config::init_config(config_path); let (config, _config_watcher) = config::init_config(config_path);
config::CONFIG.get_or_init(|| RwLock::new(config)); config::CONFIG.get_or_init(|| RwLock::new(config));
if let Some(mut interface) = TeslaInterface::load(auth_path) let interface = TeslaInterface::load(auth_path)
.await .await
.some_or_print_with("loading tesla interface") .some_or_print_with("loading tesla interface");
{
// build the channel that takes messages from the webserver thread to the api thread // build the channel that takes messages from the webserver thread to the api thread
let (api_requests, mut api_receiver) = tokio::sync::mpsc::unbounded_channel(); let (api_requests, mut api_receiver) = tokio::sync::mpsc::unbounded_channel();
// and to the pli thread // and to the pli thread
@ -87,8 +87,7 @@ async fn main() {
Ok(mut pli) => { Ok(mut pli) => {
let pl_state = pli.state.clone(); let pl_state = pli.state.clone();
tokio::task::spawn(async move { tokio::task::spawn(async move {
let mut interval = let mut interval = tokio::time::interval(std::time::Duration::from_secs(
tokio::time::interval(std::time::Duration::from_secs(
access_config().pl_watch_interval_seconds, access_config().pl_watch_interval_seconds,
)); ));
loop { loop {
@ -154,18 +153,30 @@ async fn main() {
}) })
.collect(); .collect();
let mut tesla_charge_rate_controller = let interface_and_tcrc = interface.map(|interface| {
let tcrc =
TeslaChargeRateController::new(interface.state.clone(), pl_state.clone()); TeslaChargeRateController::new(interface.state.clone(), pl_state.clone());
(interface, tcrc)
});
let car_state = interface_and_tcrc
.as_ref()
.map(|(interface, _)| interface.state.clone())
.unwrap_or_default();
let tcrc_state = interface_and_tcrc
.as_ref()
.map(|(_, tcrc)| tcrc.tcrc_state.clone())
.unwrap_or_default();
let server_handle = server::launch_server(server::ServerState { let server_handle = server::launch_server(server::ServerState {
car_state: interface.state.clone(), car_state,
pl_state, pl_state,
tcrc_state: tesla_charge_rate_controller.tcrc_state.clone(), tcrc_state,
api_requests, api_requests,
pli_requests, pli_requests,
tcrc_requests, tcrc_requests,
}); });
if let Some((mut interface, mut tesla_charge_rate_controller)) = interface_and_tcrc {
// spawn the api / charge rate control loop // spawn the api / charge rate control loop
tokio::task::spawn(async move { tokio::task::spawn(async move {
let mut normal_data_update_interval = let mut normal_data_update_interval =
@ -219,8 +230,8 @@ async fn main() {
} }
} }
}); });
}
tokio::join!(server_handle, local); tokio::join!(server_handle, local);
} }
} }
} }
}