Skip to content

Commit a54cffe

Browse files
committed
add more moonraker endpoints
1 parent 6341860 commit a54cffe

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

moonraker/src/print.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
use anyhow::Result;
2+
use serde::{Deserialize, Serialize};
23
use std::path::Path;
34

45
use super::PrintManager;
56

7+
#[derive(Clone, Debug, Deserialize, Serialize)]
8+
pub struct InfoResponse {
9+
pub state: String,
10+
pub state_message: String,
11+
pub hostname: String,
12+
pub software_version: String,
13+
pub cpu_info: String,
14+
}
15+
16+
#[derive(Clone, Debug, Deserialize, Serialize)]
17+
struct InfoResponseWrapper {
18+
pub result: InfoResponse,
19+
}
20+
621
impl PrintManager {
722
/// Print an uploaded file.
823
pub async fn print(&self, file_name: &Path) -> Result<()> {
@@ -16,6 +31,38 @@ impl PrintManager {
1631
Ok(())
1732
}
1833

34+
/// This endpoint will immediately halt the printer and put it in a
35+
/// "shutdown" state. It should be used to implement an "emergency stop"
36+
/// button and also used if a user enters M112(emergency stop) via a
37+
/// console.
38+
pub async fn emergency_stop(&self) -> Result<()> {
39+
let client = reqwest::Client::new();
40+
client
41+
.post(format!("{}/printer/emergency_stop", self.url_base))
42+
.send()
43+
.await?;
44+
Ok(())
45+
}
46+
47+
/// Get information regarding the processor and its state.
48+
pub async fn info(&self) -> Result<InfoResponse> {
49+
let client = reqwest::Client::new();
50+
let resp: InfoResponseWrapper = client
51+
.post(format!("{}/printer/info", self.url_base))
52+
.send()
53+
.await?
54+
.json()
55+
.await?;
56+
Ok(resp.result)
57+
}
58+
59+
/// Restart the printer (shut down and reboot).
60+
pub async fn restart(&self) -> Result<()> {
61+
let client = reqwest::Client::new();
62+
client.post(format!("{}/printer/restart", self.url_base)).send().await?;
63+
Ok(())
64+
}
65+
1966
/// Cancel a print job.
2067
pub async fn cancel_print(&self) -> Result<()> {
2168
let client = reqwest::Client::new();
@@ -25,4 +72,24 @@ impl PrintManager {
2572
.await?;
2673
Ok(())
2774
}
75+
76+
/// Pause a print job.
77+
pub async fn pause_print(&self) -> Result<()> {
78+
let client = reqwest::Client::new();
79+
client
80+
.post(format!("{}/printer/print/pause", self.url_base))
81+
.send()
82+
.await?;
83+
Ok(())
84+
}
85+
86+
/// Resume a print job.
87+
pub async fn resume_print(&self) -> Result<()> {
88+
let client = reqwest::Client::new();
89+
client
90+
.post(format!("{}/printer/print/resume", self.url_base))
91+
.send()
92+
.await?;
93+
Ok(())
94+
}
2895
}

moonraker/src/upload.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ pub struct DeleteResponseItem {
2727
}
2828

2929
#[derive(Clone, Debug, Deserialize, Serialize)]
30-
pub struct DeleteResponseInner {
30+
pub struct DeleteResponse {
3131
pub item: DeleteResponseItem,
3232
pub action: String,
3333
}
3434

3535
#[derive(Clone, Debug, Deserialize, Serialize)]
36-
pub struct DeleteResponse {
37-
result: DeleteResponseInner,
36+
struct DeleteResponseWrapper {
37+
result: DeleteResponse,
3838
}
3939

4040
impl PrintManager {
@@ -83,11 +83,12 @@ impl PrintManager {
8383
pub async fn delete(&self, file_name: &Path) -> Result<DeleteResponse> {
8484
let file_name = file_name.to_str().unwrap();
8585
let client = reqwest::Client::new();
86-
Ok(client
86+
let resp: DeleteResponseWrapper = client
8787
.delete(format!("{}/server/files/gcodes/{}", self.url_base, file_name))
8888
.send()
8989
.await?
9090
.json()
91-
.await?)
91+
.await?;
92+
Ok(resp.result)
9293
}
9394
}

0 commit comments

Comments
 (0)