diff --git a/src/api_interface.rs b/src/api_interface.rs index 53e5183..2507e2f 100644 --- a/src/api_interface.rs +++ b/src/api_interface.rs @@ -59,11 +59,12 @@ struct AuthInfo { } #[derive(Clone, Copy, Debug)] +// these are the messages that the webserver can send the api thread pub enum InterfaceRequest { FlashLights, } -const REFRESH_INTERVAL: Duration = Duration::from_secs(12 * 60 * 60); +const KEY_REFRESH_INTERVAL: Duration = Duration::from_secs(12 * 60 * 60); impl TeslaInterface { pub async fn load(auth_path: PathBuf) -> Result { @@ -150,7 +151,8 @@ impl TeslaInterface { } async fn refresh_keys(&mut self) { - if Instant::now().duration_since(self.last_refresh) >= REFRESH_INTERVAL { + // refresh our Tesla (the company's web servers, not the car) access token + if Instant::now().duration_since(self.last_refresh) >= KEY_REFRESH_INTERVAL { match self.api.refresh().await { Ok(_) => { let now = Instant::now(); @@ -166,6 +168,9 @@ impl TeslaInterface { } async fn get_state(api: &FleetApi, vehicle_id: VehicleId) -> Result { + // Endpoint::VehicleDataCombo or multiple Endpoints in one request + // doesn't seem to reliably get them all, + // so for each endpoint we do a new request let charge_state = api .vehicle_data(&GetVehicleData { vehicle_id, diff --git a/src/main.rs b/src/main.rs index 9c8072e..9f259b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,7 @@ async fn main() { 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, receiver) = async_channel::unbounded(); let server_handle = server::launch_server(server::ServerState { @@ -60,9 +61,11 @@ async fn main() { api_requests, }); + // spawn the api loop tokio::task::spawn(async move { let mut interval = tokio::time::interval(std::time::Duration::from_secs(120)); loop { + // await either the next interval OR a message from the other thread tokio::select! { _ = interval.tick() => interface.refresh().await, message = receiver.recv() => match message { diff --git a/src/server/mod.rs b/src/server/mod.rs index 793c893..fd644d5 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -29,6 +29,9 @@ pub async fn launch_server(state: ServerState) { } fn rocket(state: ServerState) -> rocket::Rocket { + // serve the html from disk if running in a debug build + // this allows editing the webpage without having to rebuild the executable + // but in release builds, bundle the entire webapp folder into the exe let fileserver: Vec = if cfg!(debug_assertions) { rocket::fs::FileServer::from(format!( "{}/webapp",