use serde::{Deserialize, Serialize}; #[derive(Debug, Default, Serialize, Clone)] pub enum PrintStatus { #[default] Unknown, Idle, Printing, Paused, Finished, } #[derive(Debug, Default, Serialize, Clone)] pub struct PrinterState { pub bed_temp: f32, pub status: PrintStatus, } #[derive(Debug, Deserialize)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] enum PrusaState { Idle, Printing, Paused, Finished, Stopped, Error, Attention, Busy, } #[derive(Debug, Deserialize)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] enum BambuState { Idle, Running, Pause, Finish, Failed, } #[derive(Debug, Deserialize)] struct PrusaJob { id: u32, progress: f32, time_remaining: u32, time_printing: u32, } #[derive(Debug, Deserialize)] struct PrusaPrinterInfo { state: PrusaState, temp_nozzle: f32, target_nozzle: f32, temp_bed: f32, target_bed: f32, axis_x: f32, axis_y: f32, axis_z: f32, flow: u32, speed: u32, fan_hotend: u32, fan_print: u32, } #[derive(Debug, Deserialize)] pub struct PrusaStatus { job: Option, printer: PrusaPrinterInfo, } #[derive(Deserialize)] struct BambuPrintMessage { bed_target_temper: Option, bed_temper: Option, gcode_state: Option, layer_num: Option, mc_percent: Option, mc_remaining_time: Option, nozzle_target_temper: Option, nozzle_temper: Option, print_speed: Option, total_layer_num: Option, } #[derive(Deserialize)] pub struct BambuStatus { print: Option, } #[derive(Debug, Deserialize)] pub struct Config { // This allows you to have a list of different printer types pub printers: Vec, } impl Config { pub fn load(toml: &str) -> Config { toml::from_str(toml).expect("Couldn't parse config.toml") } } #[derive(Debug, Deserialize)] #[serde(tag = "type", rename_all = "lowercase")] pub enum Printer { Prusa { name: String, host: String, api_key: String, }, Bambu { name: String, host: String, access_code: String, serial_number: String, }, } pub fn extract_status_from_prusa(status: &PrusaStatus, state: &mut PrinterState) { state.status = match status.printer.state { PrusaState::Idle => PrintStatus::Idle, PrusaState::Printing => PrintStatus::Printing, PrusaState::Paused => PrintStatus::Paused, PrusaState::Finished => PrintStatus::Finished, _ => PrintStatus::Unknown, }; state.bed_temp = status.printer.temp_bed; } pub fn extract_status_from_bambu(status: &BambuStatus, state: &mut PrinterState) { if let Some(print) = &status.print { if let Some(bed_temper) = print.bed_temper { state.bed_temp = bed_temper; } if let Some(gcode_state) = &print.gcode_state { state.status = match gcode_state { BambuState::Idle => PrintStatus::Idle, BambuState::Running => PrintStatus::Printing, BambuState::Pause => PrintStatus::Paused, BambuState::Finish => PrintStatus::Finished, _ => PrintStatus::Unknown, }; } } }