Skip to content

Commit 1e65ee7

Browse files
committed
Add metrics endpoint(s), expose return values.
1 parent a54cffe commit 1e65ee7

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

Cargo.lock

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

moonraker/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ anyhow = "1"
88
bytes = "1.7.1"
99
reqwest = { version = "0", features = ["json", "multipart"] }
1010
serde = { version = "1.0.204", features = ["derive"] }
11+
serde_json = "1"
1112
tokio = { version = "1", features = ["full"] }

moonraker/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
//! This crate implements support for interfacing with the moonraker 3d printer
1111
//! api, proxying calls to klipper.
1212
13+
mod metrics;
1314
mod print;
1415
mod upload;
1516

1617
use anyhow::Result;
1718

19+
pub use metrics::{ControlledTemperatureReadings, TemperatureReadings};
20+
pub use print::InfoResponse;
21+
pub use upload::{DeleteResponse, DeleteResponseItem, UploadResponse, UploadResponseItem};
22+
1823
/// PrintManager is a moonraker instance which can accept gcode for printing.
1924
pub struct PrintManager {
2025
pub(crate) url_base: String,

moonraker/src/metrics.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use anyhow::Result;
2+
use serde::{Deserialize, Serialize};
3+
4+
use super::PrintManager;
5+
6+
/// Temperature readings from a heated element controlled by klipper.
7+
#[derive(Clone, Debug, Deserialize, Serialize)]
8+
pub struct ControlledTemperatureReadings {
9+
/// Observed temperatures, from oldest (0th) to latest (last)
10+
pub temperatures: Vec<f64>,
11+
12+
/// Target temperatures, from oldest (0th) to latest (last)
13+
pub targets: Vec<f64>,
14+
15+
/// Controlled power level, from oldest (0th) to latest (last)
16+
pub powers: Vec<f64>,
17+
}
18+
19+
/// TemperatureReadings as reported by klipper.
20+
#[derive(Clone, Debug, Deserialize, Serialize)]
21+
pub struct TemperatureReadings {
22+
/// Information about the 3D printer extruder head.
23+
pub extruder: ControlledTemperatureReadings,
24+
25+
/// Information about a heated bed, if present
26+
pub heater_bed: Option<ControlledTemperatureReadings>,
27+
}
28+
29+
#[derive(Clone, Debug, Deserialize, Serialize)]
30+
struct TemperatureReadingsWrapper {
31+
result: TemperatureReadings,
32+
}
33+
34+
impl PrintManager {
35+
/// Print an uploaded file.
36+
pub async fn temperatures(&self) -> Result<TemperatureReadings> {
37+
let client = reqwest::Client::new();
38+
39+
let resp: TemperatureReadingsWrapper = client
40+
.get(format!("{}/server/temperature_store", self.url_base))
41+
.send()
42+
.await?
43+
.json()
44+
.await?;
45+
46+
Ok(resp.result)
47+
}
48+
}

moonraker/src/print.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@ use std::path::Path;
44

55
use super::PrintManager;
66

7+
/// Information about the underlying Klipper runtime and host computer.
78
#[derive(Clone, Debug, Deserialize, Serialize)]
89
pub struct InfoResponse {
10+
/// slug defining what state the printer is currently in, such as
11+
/// `ready`.
912
pub state: String,
13+
14+
/// Human readable description of the state above.
1015
pub state_message: String,
16+
17+
/// Hostname of the host computer.
1118
pub hostname: String,
19+
20+
/// Version of Klipper running on the host computer.
1221
pub software_version: String,
22+
23+
/// CPU of the host running Klipper.
1324
pub cpu_info: String,
1425
}
1526

moonraker/src/upload.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,48 @@ use std::path::{Path, PathBuf};
66

77
use super::PrintManager;
88

9+
/// File that has been uploaded to Moonraker.
910
#[derive(Clone, Debug, Deserialize, Serialize)]
1011
pub struct UploadResponseItem {
12+
/// Path of the file relative to the root directory.
1113
pub path: String,
14+
15+
/// Root folder. Currently only a limted set are supported,
16+
/// check the moonraker docs for more information. This code
17+
/// assumes everything is `gcodes` for now.
1218
pub root: String,
1319
}
1420

21+
/// Response to an upload request.
1522
#[derive(Clone, Debug, Deserialize, Serialize)]
1623
pub struct UploadResponse {
24+
/// `gcode` file uploaded to the printer.
1725
pub item: UploadResponseItem,
26+
27+
/// Has this print been started?
1828
pub print_started: bool,
29+
30+
/// Has this print been enqueued?
1931
pub print_queued: bool,
20-
pub action: String,
2132
}
2233

34+
/// File that has been deleted from Moonraker.
2335
#[derive(Clone, Debug, Deserialize, Serialize)]
2436
pub struct DeleteResponseItem {
37+
/// Path of the file relative to the root directory.
2538
pub path: String,
39+
40+
/// Root folder. Currently only a limted set are supported,
41+
/// check the moonraker docs for more information. This code
42+
/// assumes everything is `gcodes` for now.
2643
pub root: String,
2744
}
2845

46+
/// Response to a delete request.
2947
#[derive(Clone, Debug, Deserialize, Serialize)]
3048
pub struct DeleteResponse {
49+
/// `gcode` file that has been deleted.
3150
pub item: DeleteResponseItem,
32-
pub action: String,
3351
}
3452

3553
#[derive(Clone, Debug, Deserialize, Serialize)]

0 commit comments

Comments
 (0)