initial commit
NOTE: Info command works
This commit is contained in:
commit
64e9a2eced
3 changed files with 137 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
|
|
@ -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"
|
||||||
122
src/main.rs
Normal file
122
src/main.rs
Normal file
|
|
@ -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<Info, Error> {
|
||||||
|
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(())
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue