Skip to content

Commit

Permalink
add progress (#138)
Browse files Browse the repository at this point in the history
* add progress

* add progress to the output

* progress
  • Loading branch information
paultag authored Oct 18, 2024
1 parent f739387 commit 400ea32
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions openapi/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@
"description": "Maximum part size that can be manufactured by this device. This may be some sort of theoretical upper bound, getting close to this limit seems like maybe a bad idea.\n\nThis may be `None` if the maximum size is not knowable by the Machine API.\n\nWhat \"close\" means is up to you!",
"nullable": true
},
"progress": {
"description": "Progress of the current print, if printing.",
"format": "double",
"nullable": true,
"type": "number"
},
"state": {
"allOf": [
{
Expand Down
4 changes: 4 additions & 0 deletions src/any_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ impl ControlTrait for AnyMachine {
for_all!(|self, machine| { machine.healthy().await })
}

async fn progress(&self) -> Result<Option<f64>> {
for_all!(|self, machine| { machine.progress().await })
}

async fn state(&self) -> Result<MachineState> {
for_all!(|self, machine| { machine.state().await })
}
Expand Down
4 changes: 4 additions & 0 deletions src/bambu/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl ControlTrait for Bambu {
Ok(())
}

async fn progress(&self) -> Result<Option<f64>> {
Ok(None)
}

async fn healthy(&self) -> bool {
let Ok(Some(status)) = self.client.get_status() else {
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/moonraker/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl ControlTrait for Client {
self.client.info().await.is_ok()
}

async fn progress(&self) -> Result<Option<f64>> {
Ok(None)
}

async fn state(&self) -> Result<MachineState> {
let status = self.client.status().await?;

Expand Down
5 changes: 5 additions & 0 deletions src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl ControlTrait for Noop {
true
}

async fn progress(&self) -> Result<Option<f64>> {
Ok(None)
}

async fn state(&self) -> Result<MachineState> {
Ok(MachineState::Unknown)
}
Expand All @@ -84,6 +88,7 @@ impl SuspendControlTrait for Noop {
async fn pause(&mut self) -> Result<()> {
Ok(())
}

async fn resume(&mut self) -> Result<()> {
Ok(())
}
Expand Down
5 changes: 5 additions & 0 deletions src/server/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ pub struct MachineInfoResponse {
/// Information about how the Machine is currently configured.
pub hardware_configuration: HardwareConfiguration,

/// Progress of the current print, if printing.
pub progress: Option<f64>,

/// Status of the printer -- be it printing, idle, or unreachable. This
/// may dictate if a machine is capable of taking a new job.
pub state: MachineState,
Expand All @@ -100,13 +103,15 @@ impl MachineInfoResponse {
pub(crate) async fn from_machine(id: &str, machine: &AnyMachine) -> anyhow::Result<Self> {
let machine_info = machine.machine_info().await?;
let hardware_configuration = machine.hardware_configuration().await?;
let progress = machine.progress().await?;

Ok(MachineInfoResponse {
id: id.to_owned(),
make_model: machine_info.make_model(),
machine_type: machine_info.machine_type(),
max_part_volume: machine_info.max_part_volume(),
hardware_configuration,
progress,
state: machine.state().await?,
extra: match machine {
AnyMachine::Moonraker(_) => Some(ExtraMachineInfoResponse::Moonraker {}),
Expand Down
3 changes: 3 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ where
async fn state(&self) -> Result<MachineState, Self::Error> {
self.0.lock().await.state().await
}
async fn progress(&self) -> Result<Option<f64>, Self::Error> {
self.0.lock().await.progress().await
}
async fn hardware_configuration(&self) -> Result<HardwareConfiguration, Self::Error> {
self.0.lock().await.hardware_configuration().await
}
Expand Down
5 changes: 5 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ pub trait Control {
/// Return the state of the printer.
fn state(&self) -> impl Future<Output = Result<MachineState, Self::Error>>;

/// Return the percentage of completion of the job. This may return an
/// error when connecting to the machine, and it may return None if
/// there is no job running, or if there's no way to know the progress.
fn progress(&self) -> impl Future<Output = Result<Option<f64>, Self::Error>>;

// TODO: look at merging MachineType and HardwareConfiguration; they
// communicate VERY similar things conceptually.

Expand Down
4 changes: 4 additions & 0 deletions src/usb/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ impl ControlTrait for Usb {
Ok(MachineState::Unknown)
}

async fn progress(&self) -> Result<Option<f64>> {
Ok(None)
}

async fn healthy(&self) -> bool {
// TODO: fix this, do a gcode ping or something?
true
Expand Down

0 comments on commit 400ea32

Please sign in to comment.