From 944f4807588754e110d73ff1cd12810b45aecd81 Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Sun, 4 Jan 2026 14:20:31 -0700 Subject: [PATCH] add status command to fetch printer status --- src/main.rs | 90 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7b0b4a0..e972374 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ use clap::{Parser, Subcommand}; use core::time::Duration; +use reqwest::blocking::Client; +use serde::{Deserialize, Serialize}; use std::fs; use thiserror::Error; -use serde::Deserialize; -use reqwest::blocking::Client; use uuid::Uuid; - #[derive(Deserialize, Debug)] struct Telemetry { #[serde(rename = "temp-bed")] @@ -25,7 +24,27 @@ struct Info { telemetry: Telemetry, // temperature: Temperature, state: State, +} +#[derive(Serialize, Deserialize, Debug)] +struct JobStatus { + progress: f32, + time_printing: usize, +} + +#[derive(Serialize, Deserialize, Debug)] +struct PrinterStatus { + state: String, + temp_bed: f32, + target_bed: f32, + temp_nozzle: f32, + target_nozzle: f32, +} + +#[derive(Serialize, Deserialize, Debug)] +struct Status { + job: Option, + printer: Option, } #[derive(Deserialize, Debug)] @@ -42,7 +61,7 @@ struct Storage { struct Prusa { api_key: String, ip_addr: String, - client: Client, + client: Client, } impl Prusa { @@ -56,26 +75,45 @@ impl Prusa { fn get_info(&self) -> Result { let api_target = "/api/printer"; - let resp = self.client.get(format!("http://{}{api_target}", self.ip_addr)) - .header("X-Api-Key", &self.api_key) - .send()?; + let resp = self + .client + .get(format!("http://{}{api_target}", self.ip_addr)) + .header("X-Api-Key", &self.api_key) + .send()?; let text = resp.text()?; - // println!("{text}"); + println!("{text}"); let info: Info = serde_json::from_str(&text)?; // println!("{info:?}"); Ok(info) } + fn get_status(&self) -> Result { + let api_target = "/api/v1/status"; + let resp = self + .client + .get(format!("http://{}{api_target}", self.ip_addr)) + .header("X-Api-Key", &self.api_key) + .send()?; + let text = resp.text()?; + // println!("{text}"); + let status: Status = serde_json::from_str(&text)?; + // println!("{info:?}"); + // Ok(info) + Ok(status) + } + // TODO return this into a specific type fn get_storage_info(&self) -> Result { let api_target = "/api/v1/storage"; - let resp = self.client.get(format!("http://{}{api_target}", self.ip_addr)) + let resp = self + .client + .get(format!("http://{}{api_target}", self.ip_addr)) .header("X-Api-Key", &self.api_key) .send()?; let text = resp.text()?; // println!("{text}"); let info: StorageInfo = serde_json::from_str(&text)?; - + Ok(info) } @@ -86,7 +124,6 @@ impl Prusa { // type: string // format: binary - // Need parameters /* parameters: @@ -127,7 +164,9 @@ impl Prusa { // TODO: Allow storage selection // For now, assume that we're using only USB: let api_target = format!("/api/v1/files/usb/{uuid}.bgcode"); - let resp = self.client.put(format!("http://{}{api_target}", self.ip_addr)) + let resp = self + .client + .put(format!("http://{}{api_target}", self.ip_addr)) .header("X-Api-Key", &self.api_key) .header("Content-Type", "application/octet-stream") .header("Content-Length", data.len()) @@ -183,17 +222,18 @@ impl Prusa { fn try_print_file(&self, id: &Uuid) -> Result<(), Error> { // /api/v1/files/{storage}/{path} let api_target = format!("/api/v1/files/usb/{id}.bgcode"); - let resp = self.client.post(format!("http://{}{api_target}", self.ip_addr)) + let resp = self + .client + .post(format!("http://{}{api_target}", self.ip_addr)) .header("X-Api-Key", &self.api_key) .header("Content-Type", "application/octet-stream") .send()?; let text = resp.text()?; println!("{text}"); - + Ok(()) } - // TODO: poorly named, should be something else so as not to confuse with print operations fn print_info(&self) -> Result<(), Error> { let info = self.get_info()?; @@ -230,6 +270,7 @@ struct Cli { #[derive(Subcommand)] enum Command { Info, + Status, Load { filepath: String, #[arg(long)] @@ -263,7 +304,6 @@ pub enum Error { JsonError(#[from] serde_json::Error), } - fn main() -> Result<(), Error> { let cli = Cli::parse(); let file_data = fs::read_to_string(cli.printer_config)?; @@ -277,19 +317,27 @@ fn main() -> Result<(), Error> { Command::Info => { prusa.print_info()?; prusa.print_storage_info()?; - }, + } + Command::Status => { + let status = prusa.get_status()?; + // println!("{status:?}"); + println!("{}", serde_json::to_string(&status)?); + } // Should generate UUID for the filename: - Command::Load {filepath, print_immediately} => { + Command::Load { + filepath, + print_immediately, + } => { let uuid = prusa.try_load_file(&filepath)?; if print_immediately { prusa.try_print_file(&uuid)?; } println!("Loaded as UUID:\n{uuid}"); - }, - Command::Print {file_id} => prusa.try_print_file(&file_id)?, + } + Command::Print { file_id } => prusa.try_print_file(&file_id)?, } } - _ => println!("Unrecognized printer type") + _ => println!("Unrecognized printer type"), } Ok(())