add get_storage_info and some try_load_file

This commit is contained in:
sigil-03 2024-03-18 21:11:11 -06:00
parent c55715fd3a
commit 4e09e5e4ab

View file

@ -27,6 +27,17 @@ struct Info {
}
#[derive(Deserialize, Debug)]
struct StorageInfo {
storage_list: Vec<Storage>,
}
#[derive(Deserialize, Debug)]
struct Storage {
path: String,
name: String,
}
struct Prusa {
api_key: String,
ip_addr: String,
@ -54,7 +65,37 @@ impl Prusa {
Ok(info)
}
fn try_load_file(&self, _filepath: &str) -> Result<Uuid, Error> {
// TODO return this into a specific type
fn get_storage_info(&self) -> Result<(StorageInfo), Error> {
let api_target = "/api/v1/storage";
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)
}
// Prusalink load file schema:
// content:
// application/octet-stream:
// schema:
// type: string
// format: binary
fn try_load_file(&self, filepath: &str) -> Result<Uuid, Error> {
let data = fs::read(filepath)?;
// TODO: Allow storage selection
// For now, assume that we're using only USB:
let api_target = "/api/v1/files/usb/{path}";
let resp = self.client.put(format!("http://{}{api_target}", self.ip_addr))
.header("X-Api-Key", &self.api_key)
.send()?;
let text = resp.text()?;
println!("{text}");
todo!("implement try_load_file")
}
@ -74,6 +115,18 @@ impl Prusa {
Ok(())
}
fn print_storage_info(&self) -> Result<(), Error> {
let info = self.get_storage_info()?;
for (index, storage) in info.storage_list.iter().enumerate() {
println!("\n------------------");
println!("STORAGE: {index}");
println!(" PATH:\t{}", storage.path);
println!(" NAME:\t{}", storage.name);
println!("--------------------\n");
}
Ok(())
}
}
#[derive(Parser)]
@ -130,7 +183,10 @@ fn main() -> Result<(), Error> {
"prusa-mk4" => {
let prusa = Prusa::new(&config.printer.api_key, &config.printer.ip_addr);
match cli.command {
Command::Info => prusa.print_info()?,
Command::Info => {
prusa.print_info()?;
prusa.print_storage_info()?;
},
// Should generate UUID for the filename:
Command::Load {filepath} => {
let uuid = prusa.try_load_file(&filepath)?;