Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions bambulabs/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
};
Expand Down
4 changes: 2 additions & 2 deletions bambulabs/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub struct PushStatus {
/// The upload.
pub upload: Option<PrintUpload>,
/// The nozzle diameter.
pub nozzle_diameter: NozzleDiameter,
pub nozzle_diameter: Option<NozzleDiameter>,
/// The nozzle temperature.
pub nozzle_temper: Option<f64>,
/// The nozzle type.
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions bambulabs/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Message>(payload)
.map_err(|err| format_serde_error::SerdeError::new(payload.to_string(), err))
{
Expand Down
42 changes: 0 additions & 42 deletions openapi/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -90,7 +82,6 @@
}
},
"required": [
"nozzle_diameter",
"type"
],
"type": "object"
Expand Down Expand Up @@ -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": {
Expand Down
14 changes: 12 additions & 2 deletions src/bambu/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)),
},
Expand Down
5 changes: 5 additions & 0 deletions src/bambu/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>,
}

const BAMBU_URN: &str = "urn:bambulab-com:device:3dprinter:1";
Expand Down Expand Up @@ -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),
},
Expand Down
1 change: 1 addition & 0 deletions src/bambu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::MachineMakeModel;
pub struct Bambu {
client: Arc<Client>,
info: PrinterInfo,
config: Config,
}

/// Information regarding a discovered Bambu Labs printer.
Expand Down
4 changes: 1 addition & 3 deletions src/server/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bambulabs::message::Stage>,
/// 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))]
Expand Down Expand Up @@ -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,
Expand Down