summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs51
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;