buddy_client 0.0.1

A client for the Prusa Buddy Firmware http api.
Documentation
use semver::Version as Semver;
use serde::{Deserialize, Serialize};

/// The version information return from `get_version()` which wraps the `/api/version` endpoint.
#[derive(Debug, Serialize, Deserialize)]
pub struct Version {
    /// The version of the Buddy API.
    pub api: Semver,
    /// A potentially unreliable server version. Their source code comments detail:
    /// >  ___FIXME: The server version is probably bogus. But it's unclear what the version is supposed to mean anyway. Waiting for the new PrusaLink API to replace this?___
    pub server: Semver,
    /// Reports "PrusaLink"
    pub text: String,
    /// The hotend nozzle diameter in mm.
    pub nozzle_diameter: f32,
    /// The name of the host.
    pub hostname: String,
    /// Capabilities of the api version.
    pub capabilities: VersionCapabilities,
}

/// Capabilities of the API.
#[derive(Debug, Serialize, Deserialize)]
pub struct VersionCapabilities {
    /// Should always return `true` as it
    #[serde(rename = "upload-by-put")]
    pub upload_by_put: Option<bool>,
}

/// The information returned from `get_v1_info()` which wraps the `/api/v1/info` endpoint.
#[derive(Serialize, Deserialize, Debug)]
pub struct V1Info {
    /// The hotend nozzle diameter in mm.
    pub nozzle_diameter: f32,
    /// Indicates of the Multi-Material Unit (MMU) has been equipped to the printer.
    pub mmu: bool,
    /// The serial number of the printer
    pub serial: String,
    /// The name of the host.
    pub hostname: String,
    /// The minimum extrusion temperature of the printer.
    pub min_extrusion_temp: u32,
}

/// The status of the printer returned from `get_v1_status()` which wraps the `/api/v1/status` endpoint.
#[derive(Serialize, Deserialize, Debug)]
pub struct V1Status {
    pub storage: V1StorageStatus,
    pub printer: V1PrinterStatus,
}

/// The status of the printer storage
#[derive(Debug, Serialize, Deserialize)]
pub struct V1StorageStatus {
    // Path to the storage media. Hard-coded as "/usb/"
    pub path: String,
    // Name of the storage media. Hard-coded as "usb".
    pub name: String,
    // Reports if the media is read-only.
    pub read_only: bool,
}

/// The different states reported by the V1 API.
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub enum V1State {
    Idle,
    Busy,
    Printing,
    Paused,
    Finished,
    Stopped,
    Error,
    Attention,
    Ready,
}

/// The status information reported by the V1 API.
#[derive(Debug, Serialize, Deserialize)]
pub struct V1PrinterStatus {
    /// The printer state
    pub state: V1State,
    /// The bed temperature in Celsius.
    pub temp_bed: f32,
    /// The target bed temperature in Celsius. 0.0 if none is set.
    pub target_bed: f32,
    /// The nozzle temperature in Celsius.
    pub temp_nozzle: f32,
    /// The target nozzle temperature in Celsius. 0.0 if none is set.
    pub target_nozzle: f32,
    /// Position of the printhead in the x-axis in mm.
    pub axis_x: f32,
    /// Position of the printhead in the y-axis in mm.
    pub axis_y: f32,
    /// Position of the printhead in the z-axis in mm.
    pub axis_z: f32,
    /// Flowrate in mm/s.
    pub flow: u32,
    /// Travel speed in mm/s.
    pub speed: u32,
    /// Hot end fan speed in rpm.
    pub fan_hotend: u32,
    /// Printer fan speed in rpm.
    pub fan_print: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct V1Storage {
    // A list of storage items that is only one at the moment.
    pub storage_list: Vec<StorageItem>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum StorageKind {
    Usb,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct StorageItem {
    // Path to the storage media
    pub path: String,
    // Name of the storage media
    pub name: String,
    // Returns the kind of storage which is just USB at this time. Renamed to kind in Rust but has the key of type in the json response.
    #[serde(rename = "type")]
    pub kind: StorageKind,
    // Reports if the media is read-only.
    pub read_only: bool,
    // Reports if the media is available to use.
    pub available: bool,
}

/// Printer information response by `get_octo_printer()` which wraps `/api/printer`.
#[derive(Debug, Clone, Deserialize)]
pub struct OctoPrinter {
    /// Some telemetry for the printer.
    pub telemetry: OctoTelemetry,
    /// The temperatures of the different probes.
    pub temperature: OctoTemperatures,
    /// The state of the printer.
    pub state: OctoState,
}

/// Telemetry information.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct OctoTelemetry {
    /// The temperature of the print bed probe in Celsius.
    pub temp_bed: f32,
    /// The temperature of the hotend probe in Celsius.
    pub temp_nozzle: f32,
    /// The travel speed (mm/s).
    pub print_speed: u32,
    /// The current height of the z axis (mm).
    pub z_height: f32,
    /// Material being used by the printer.
    pub material: String,
}

/// Information on the temperature probes and their targets.
#[derive(Debug, Clone, Deserialize)]
pub struct OctoTemperatures {
    /// Hotend temperature
    pub tool0: OctoTemperature,
    /// Bed temperature
    pub bed: OctoTemperature,
}

#[derive(Debug, Clone, Deserialize)]
pub struct OctoTemperature {
    /// The temperature reported by the probe.
    pub actual: f32,
    /// The target temperature that has been set.
    pub target: f32,
    /// The offset between the temperatures.
    pub offset: f32,
    /// The temperature displayed to the user. As noted in their source code:
    /// > ___Our own extension, because our printers sometimes display different "target" temperature than what they heat towards.___
    pub display: Option<f32>,
}

/// The octo state text can be one of two strings.
#[derive(Debug, Clone, Deserialize)]
pub enum OctoStateText {
    Printing,
    Operational,
}

/// State information provided within `OctoPrinter`
#[derive(Debug, Clone, Deserialize)]
pub struct OctoState {
    /// The state text which can be one of two strings.
    pub text: OctoStateText,
    /// A set of flags that can be set.
    pub flags: OctoFlags,
}

/// The link state can be one of four strings or `None`.
#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum LinkState {
    Busy,
    Printing,
    Paused,
    Attention,
}

/// The various status flags that Octoprint might be interested in.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OctoFlags {
    pub link_state: Option<LinkState>,
    pub operational: bool,
    pub paused: bool,
    pub printing: bool,
    pub cancelling: bool,
    pub pausing: bool,
    pub error: bool,
    pub sd_ready: bool,
    pub closed_on_error: bool,
    pub ready: bool,
    pub busy: bool,
}

/// The job statuses used by Octoprint.
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub enum OctoJobState {
    Unknown,
    Printing,
    CrashRecovery,
    Pausing,
    Resuming,
    Cancelling,
    Operational,
    Error,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OctoJob {
    /// The printer state
    pub state: OctoJobState,
    /// The details of the job that is being processed if a job is being processed.
    pub job: Option<OctoJobDetails>,
    /// The progress of the current print if there is one.
    pub progress: Option<OctoProgress>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OctoJobDetails {
    /// The estimated print time of the job.
    pub estimated_print_time: String,
    /// Details of the file being printed.
    pub file: OctoFileMetadata,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OctoFileMetadata {
    /// Name of the file.
    pub name: String,
    /// Path of the file.
    pub path: String,
    /// Name displayed on the screen.
    pub display: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OctoProgress {
    /// The time left until the job is completed (secs).
    pub print_time_left: Option<u32>,
    /// The level of completion.
    pub completion: f32,
    /// How long has been printing for.
    pub print_time: u32,
}

/// The job status reported by the V1 API.
#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub enum V1JobState {
    Printing,
    Paused,
    Finished,
    Stopped,
    Error,
}

/// The response format to v1 job.
#[derive(Debug, Clone, Deserialize)]
pub struct V1Job {
    /// The id of the job.
    pub id: u32,
    /// State of the job
    pub state: V1JobState,
    /// Job progress.
    pub progress: f32,
    /// The time remaining on the job.
    pub time_remaining: Option<u32>,
    /// The time spent printing the job.
    pub time_printing: u32,
    /// File details.
    pub file: V1JobFile,
}

/// Details of the file the job originated from.
#[derive(Debug, Clone, Deserialize)]
pub struct V1JobFile {
    /// References to the file
    pub refs: V1JobFileRefs,
    /// Name of the file.
    pub name: String,
    /// Display name of the file. This may be a concatenated version if the file name is too long to display.
    pub display_name: String,
    /// The file path.
    pub path: String,
    /// The size of the file in bytes.
    pub size: Option<u32>,
    /// The last modified timestamp associated with the file.
    pub m_timestamp: Option<u32>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct V1JobFileRefs {
    /// The file icon.
    pub icon: String,
    /// The file's thumbnail.
    pub thumbnail: String,
    /// The file's download path.
    pub download: String,
}