diff options
| author | Serguey Parkhomovsky <xindigo@gmail.com> | 2026-03-17 22:21:20 -0700 |
|---|---|---|
| committer | Serguey Parkhomovsky <xindigo@gmail.com> | 2026-03-17 22:21:20 -0700 |
| commit | a468073d0d0582e5bbb8670cf8e1ba73227a3b9f (patch) | |
| tree | 0efb7c386b5b83dd7fa6df8d62a2243885ca9b17 /src/main.rs | |
| parent | ede6af9d1084d35d8458158673583841894b63cd (diff) | |
More cleanups
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 51 |
1 files changed, 25 insertions, 26 deletions
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<dyn std::error::Error + Send + Sync>> { + let response = client + .get(format!("http://{}/api/v1/status", host)) + .header("X-Api-Key", api_key) + .send() + .await? + .json::<PrusaStatus>() + .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<dyn std::error::Error + Send + Sync>> = async { - let response = client - .get(format!("http://{}/api/v1/status", host)) - .header("X-Api-Key", &api_key) - .send() - .await? - .json::<PrusaStatus>() - .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; |
