From a468073d0d0582e5bbb8670cf8e1ba73227a3b9f Mon Sep 17 00:00:00 2001 From: Serguey Parkhomovsky Date: Tue, 17 Mar 2026 22:21:20 -0700 Subject: More cleanups --- src/main.rs | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 3c2c772..de6e27d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,13 +23,11 @@ async fn main() { match printer { Printer::Prusa { name, host, api_key } => { tracing::info!(name, host, "Found Prusa"); - let state_clone = state.clone(); - tokio::spawn(poll_prusa(name, host, api_key, state_clone)); + tokio::spawn(poll_prusa(name, host, api_key, state.clone())); } Printer::Bambu { name, host, serial_number, access_code } => { tracing::info!(name, host, "Found Bambu"); - let state_clone = state.clone(); - tokio::spawn(poll_bambu(name, host, serial_number, access_code, state_clone)); + tokio::spawn(poll_bambu(name, host, serial_number, access_code, state.clone())); } } } @@ -39,32 +37,33 @@ async fn main() { .with_state(state.clone()); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); - let _ = axum::serve(listener, app).await; + axum::serve(listener, app).await.unwrap(); } -async fn poll_prusa( - name: String, - host: String, - api_key: String, - state: StateMap, -) { +async fn fetch_prusa( + client: &Client, + name: &str, + host: &str, + api_key: &str, + state: &StateMap, +) -> Result<(), Box> { + let response = client + .get(format!("http://{}/api/v1/status", host)) + .header("X-Api-Key", api_key) + .send() + .await? + .json::() + .await?; + let mut lock = state.lock().await; + let entry = lock.entry(name.to_owned()).or_default(); + extract_status_from_prusa(&response, entry); + Ok(()) +} + +async fn poll_prusa(name: String, host: String, api_key: String, state: StateMap) { let client = Client::new(); loop { - let result: Result<(), Box> = async { - let response = client - .get(format!("http://{}/api/v1/status", host)) - .header("X-Api-Key", &api_key) - .send() - .await? - .json::() - .await?; - let mut lock = state.lock().await; - let entry = lock.entry(name.clone()).or_default(); - extract_status_from_prusa(&response, entry); - Ok(()) - } - .await; - if let Err(e) = result { + if let Err(e) = fetch_prusa(&client, &name, &host, &api_key, &state).await { tracing::error!(name, error = %e, "Error polling Prusa printer"); } tokio::time::sleep(Duration::from_secs(5)).await; -- cgit v1.2.3