commit 64e9a2eced952775cada712dee91402e925608ad Author: sigil-03 Date: Thu Mar 14 19:16:45 2024 -0600 initial commit NOTE: Info command works diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d6a136c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "prusatool-rs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = { version = "4.5.2", features = ["derive"] } +reqwest = { version = "0.11.26", features = ["blocking"] } +serde = { version = "1.0.197", features = ["derive"] } +serde_json = "1.0.114" +thiserror = "1.0.58" +toml = "0.8.11" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0fab3d2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,122 @@ +use clap::{Parser, Subcommand}; +use std::fs; +use thiserror::Error; +use serde::Deserialize; +use reqwest::blocking::Client; + + +#[derive(Deserialize, Debug)] +struct Telemetry { + #[serde(rename = "temp-bed")] + temp_bed: f32, + #[serde(rename = "temp-nozzle")] + temp_nozzle: f32, +} + +#[derive(Deserialize, Debug)] +struct State { + text: String, +} + +#[derive(Deserialize, Debug)] +struct Info { + telemetry: Telemetry, + // temperature: Temperature, + state: State, + +} + +struct Prusa { + api_key: String, + ip_addr: String, + client: Client, +} + +impl Prusa { + fn new(api_key: &str, ip_addr: &str) -> Self { + Self { + api_key: String::from(api_key), + ip_addr: String::from(ip_addr), + client: Client::new(), + } + } + + 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 text = resp.text()?; + // println!("{text}"); + let info: Info = serde_json::from_str(&text)?; + // println!("{info:?}"); + Ok(info) + } + + fn print_info(&self) -> Result<(), Error> { + let info = self.get_info()?; + println!("\n--------------------"); + println!("STATE:\t{}", info.state.text); + println!("NOZZLE:\t{}", info.telemetry.temp_nozzle); + println!("BED:\t{}", info.telemetry.temp_bed); + println!("--------------------\n"); + + Ok(()) + } +} + +#[derive(Parser)] +struct Cli { + #[arg(short, long)] + printer_config: String, + #[command(subcommand)] + command: Command, +} + +#[derive(Subcommand)] +enum Command { + Info, +} + +#[derive(Deserialize, Debug)] +struct Config { + printer: Printer, +} +#[derive(Deserialize, Debug)] +struct Printer { + model: String, + api_key: String, + ip_addr: String, +} + +#[derive(Error, Debug)] +pub enum Error { + #[error("File error")] + FileError(#[from] std::io::Error), + #[error("Toml Parse Error")] + TomlError(#[from] toml::de::Error), + #[error("HTTP(S) Error")] + HttpError(#[from] reqwest::Error), + #[error("Serde JSON Error")] + JsonError(#[from] serde_json::Error), +} + + +fn main() -> Result<(), Error> { + let cli = Cli::parse(); + let file_data = fs::read_to_string(cli.printer_config)?; + let config: Config = toml::from_str(&file_data)?; + // println!("{:?}", config); + + match config.printer.model.as_str() { + "prusa-mk4" => { + let prusa = Prusa::new(&config.printer.api_key, &config.printer.ip_addr); + match cli.command { + Command::Info => prusa.print_info()?, + } + } + _ => println!("Unrecognized printer type") + } + + Ok(()) +}