diff --git a/bambulabs/src/client.rs b/bambulabs/src/client.rs index ad5e5a1..b10d916 100644 --- a/bambulabs/src/client.rs +++ b/bambulabs/src/client.rs @@ -91,20 +91,26 @@ impl Client { let msg_opt = match ep.poll().await { Ok(msg_opt) => msg_opt, Err(err) => { - if let rumqttc::ConnectionError::MqttState(rumqttc::StateError::Io(err)) = err { - tracing::error!("Error polling for message: {:?}", err); - tracing::warn!("Reconnecting..."); - // We are in a bad state and should reconnect. - let opts = Self::get_config(&self.ip, &self.access_code)?; - let (client, event_loop) = rumqttc::AsyncClient::new(opts, 25); - drop(ep); - self.client = Arc::new(client); - self.event_loop = Arc::new(Mutex::new(event_loop)); - tracing::warn!("Reconnected."); - return Ok(()); + match err { + rumqttc::ConnectionError::MqttState(rumqttc::StateError::Io(err)) => { + tracing::error!("Error polling for message: {:?}", err); + } + rumqttc::ConnectionError::MqttState(rumqttc::StateError::AwaitPingResp) => { + tracing::error!("Error polling for message: AwaitPingResp"); + } + _ => { + tracing::error!("Error polling for message: {:?}; aborting", err); + return Ok(()); + } } - - tracing::error!("Error polling for message: {:?}", err); + tracing::warn!("Reconnecting..."); + // We are in a bad state and should reconnect. + let opts = Self::get_config(&self.ip, &self.access_code)?; + let (client, event_loop) = rumqttc::AsyncClient::new(opts, 25); + drop(ep); + self.client = Arc::new(client); + self.event_loop = Arc::new(Mutex::new(event_loop)); + tracing::warn!("Reconnected."); return Ok(()); } }; diff --git a/bambulabs/src/message.rs b/bambulabs/src/message.rs index d10d47e..1ff80df 100644 --- a/bambulabs/src/message.rs +++ b/bambulabs/src/message.rs @@ -422,7 +422,7 @@ pub struct PushStatus { /// The upload. pub upload: Option, /// The nozzle diameter. - pub nozzle_diameter: NozzleDiameter, + pub nozzle_diameter: Option, /// The nozzle temperature. pub nozzle_temper: Option, /// The nozzle type. @@ -558,7 +558,7 @@ pub enum GcodeState { #[derive(Debug, Clone, PartialEq, Eq, Deserialize_repr, JsonSchema, Copy, FromStr, Display)] #[display(style = "snake_case")] #[serde(rename_all = "snake_case")] -#[repr(i8)] +#[repr(i16)] pub enum Stage { /// Nothing. Nothing = -1, diff --git a/bambulabs/src/parser.rs b/bambulabs/src/parser.rs index 7cc01f0..5e77071 100644 --- a/bambulabs/src/parser.rs +++ b/bambulabs/src/parser.rs @@ -8,6 +8,7 @@ pub(crate) fn parse_message(message: &rumqttc::Event) -> Message { let payload = publish.payload.clone(); if let Ok(payload) = std::str::from_utf8(&payload) { + tracing::warn!("{}", payload); match serde_json::from_str::(payload) .map_err(|err| format_serde_error::SerdeError::new(payload.to_string(), err)) { diff --git a/openapi/api.json b/openapi/api.json index b2f1513..55b96fe 100644 --- a/openapi/api.json +++ b/openapi/api.json @@ -74,14 +74,6 @@ "description": "The current stage of the machine as defined by Bambu which can include errors, etc.", "nullable": true }, - "nozzle_diameter": { - "allOf": [ - { - "$ref": "#/components/schemas/NozzleDiameter" - } - ], - "description": "The nozzle diameter of the machine." - }, "type": { "enum": [ "bambu" @@ -90,7 +82,6 @@ } }, "required": [ - "nozzle_diameter", "type" ], "type": "object" @@ -589,39 +580,6 @@ } ] }, - "NozzleDiameter": { - "description": "A nozzle diameter.", - "oneOf": [ - { - "description": "0.2mm.", - "enum": [ - "0.2" - ], - "type": "string" - }, - { - "description": "0.4mm.", - "enum": [ - "0.4" - ], - "type": "string" - }, - { - "description": "0.6mm.", - "enum": [ - "0.6" - ], - "type": "string" - }, - { - "description": "0.8mm.", - "enum": [ - "0.8" - ], - "type": "string" - } - ] - }, "Pong": { "description": "The response from the `/ping` endpoint.", "properties": { diff --git a/src/bambu/control.rs b/src/bambu/control.rs index 2114d9d..be61767 100644 --- a/src/bambu/control.rs +++ b/src/bambu/control.rs @@ -112,9 +112,19 @@ impl ControlTrait for Bambu { anyhow::bail!("Failed to get status"); }; + let nozzle_diameter: f64 = match self.config.nozzle_diameter { + Some(nozzle_diameter) => nozzle_diameter, + None => status + .nozzle_diameter + .ok_or(anyhow::anyhow!( + "no nozzle in printer api response, and none set in the config" + ))? + .into(), + }; + let default = HardwareConfiguration::Fdm { config: FdmHardwareConfiguration { - nozzle_diameter: status.nozzle_diameter.into(), + nozzle_diameter, filaments: vec![Filament { material: FilamentMaterial::Pla, ..Default::default() @@ -160,7 +170,7 @@ impl ControlTrait for Bambu { Ok(HardwareConfiguration::Fdm { config: FdmHardwareConfiguration { - nozzle_diameter: status.nozzle_diameter.into(), + nozzle_diameter, filaments, loaded_filament_idx: nams.tray_now.map(|v| v.parse().unwrap_or(0)), }, diff --git a/src/bambu/discover.rs b/src/bambu/discover.rs index 9034e8a..cf5a185 100644 --- a/src/bambu/discover.rs +++ b/src/bambu/discover.rs @@ -69,6 +69,10 @@ pub struct Config { /// The access code for the printer. pub access_code: String, + + /// If set, override returned Nozzle Diameter (or if the printer + /// does not return one). + pub nozzle_diameter: Option, } const BAMBU_URN: &str = "urn:bambulab-com:device:3dprinter:1"; @@ -258,6 +262,7 @@ impl DiscoverTrait for BambuDiscover { machine_api_id.clone(), RwLock::new(Machine::new( Bambu { + config: config.clone(), info, client: Arc::new(client), }, diff --git a/src/bambu/mod.rs b/src/bambu/mod.rs index eb4216e..8ef1a29 100644 --- a/src/bambu/mod.rs +++ b/src/bambu/mod.rs @@ -16,6 +16,7 @@ use crate::MachineMakeModel; pub struct Bambu { client: Arc, info: PrinterInfo, + config: Config, } /// Information regarding a discovered Bambu Labs printer. diff --git a/src/server/endpoints.rs b/src/server/endpoints.rs index 59d1dec..cbac86b 100644 --- a/src/server/endpoints.rs +++ b/src/server/endpoints.rs @@ -50,8 +50,7 @@ pub enum ExtraMachineInfoResponse { Bambu { /// The current stage of the machine as defined by Bambu which can include errors, etc. current_stage: Option, - /// The nozzle diameter of the machine. - nozzle_diameter: bambulabs::message::NozzleDiameter, + // Only run in debug mode. This is just to help us know what information we have. #[cfg(debug_assertions)] #[cfg(not(test))] @@ -122,7 +121,6 @@ impl MachineInfoResponse { .ok_or_else(|| anyhow::anyhow!("no status for bambu"))?; Some(ExtraMachineInfoResponse::Bambu { current_stage: status.stg_cur, - nozzle_diameter: status.nozzle_diameter, #[cfg(debug_assertions)] #[cfg(not(test))] raw_status: status,