Skip to content

Commit 0754565

Browse files
committed
Initial import of a module to talk to Moonraker
1 parent a67cd40 commit 0754565

File tree

7 files changed

+355
-0
lines changed

7 files changed

+355
-0
lines changed

Cargo.lock

Lines changed: 173 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ debug = ["dep:delouse", "dep:console-subscriber"]
6464
[workspace]
6565
members = [
6666
"bambulabs",
67+
"moonraker"
6768
]
6869

moonraker/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "moonraker"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1"
8+
bytes = "1.7.1"
9+
reqwest = { version = "0", features = ["json", "multipart"] }
10+
serde = { version = "1.0.204", features = ["derive"] }
11+
tokio = { version = "1", features = ["full"] }

moonraker/src/bin/print.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use anyhow::Result;
2+
use moonraker;
3+
use std::path::PathBuf;
4+
5+
#[tokio::main]
6+
async fn main() -> Result<()> {
7+
let args: Vec<String> = std::env::args().collect();
8+
9+
let printer = moonraker::PrintManager::new(&args[1])?;
10+
let path: PathBuf = args[2].parse().unwrap();
11+
let path: PathBuf = printer.upload_file(&path).await?.item.path.parse().unwrap();
12+
eprintln!("Uploaded {}", path.display());
13+
eprintln!("Requesting print");
14+
printer.print(&path).await?;
15+
eprintln!("OK");
16+
17+
Ok(())
18+
}

moonraker/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![deny(missing_docs)]
2+
#![deny(missing_copy_implementations)]
3+
#![deny(trivial_casts)]
4+
#![deny(trivial_numeric_casts)]
5+
#![deny(unused_import_braces)]
6+
#![deny(unused_qualifications)]
7+
#![deny(rustdoc::broken_intra_doc_links)]
8+
#![deny(rustdoc::private_intra_doc_links)]
9+
10+
//! This crate implements support for interfacing with the moonraker 3d printer
11+
//! api, proxying calls to klipper.
12+
13+
mod print;
14+
mod upload;
15+
16+
use anyhow::Result;
17+
18+
/// PrintManager is a moonraker instance which can accept gcode for printing.
19+
pub struct PrintManager {
20+
pub(crate) url_base: String,
21+
}
22+
23+
impl PrintManager {
24+
/// Create a new PrintManager handle to control the printer via the
25+
/// moonraker interface.
26+
pub fn new(url_base: &str) -> Result<Self> {
27+
Ok(Self {
28+
url_base: url_base.to_owned(),
29+
})
30+
}
31+
}

moonraker/src/print.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use anyhow::Result;
2+
use std::path::Path;
3+
4+
use super::PrintManager;
5+
6+
impl PrintManager {
7+
/// Print an uploaded file.
8+
pub async fn print(&self, file_name: &Path) -> Result<()> {
9+
let file_name = file_name.to_str().unwrap();
10+
let client = reqwest::Client::new();
11+
client
12+
.post(format!("{}/printer/print/start", self.url_base))
13+
.form(&[("filename", file_name)])
14+
.send()
15+
.await?;
16+
Ok(())
17+
}
18+
19+
/// Cancel a print job.
20+
pub async fn cancel_print(&self) -> Result<()> {
21+
let client = reqwest::Client::new();
22+
client
23+
.post(format!("{}/printer/print/cancel", self.url_base))
24+
.send()
25+
.await?;
26+
Ok(())
27+
}
28+
}

0 commit comments

Comments
 (0)