Skip to content

Commit

Permalink
add bambu; (#132)
Browse files Browse the repository at this point in the history
* add bambu;

Signed-off-by: Jess Frazelle <[email protected]>

* updates

Signed-off-by: Jess Frazelle <[email protected]>

* fixes

Signed-off-by: Jess Frazelle <[email protected]>

* updates

Signed-off-by: Jess Frazelle <[email protected]>

---------

Signed-off-by: Jess Frazelle <[email protected]>
  • Loading branch information
jessfraz authored Oct 17, 2024
1 parent 6bf8ec4 commit 9482019
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
3 changes: 0 additions & 3 deletions bambulabs/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,6 @@ mod tests {
if path.is_dir() {
continue;
}
println!("Deserializing file: {}", path.display());
let contents = match std::fs::read_to_string(path) {
Ok(contents) => contents,
Err(err) => panic!("Error reading file `{}`: {:?}", path.display(), err),
Expand Down Expand Up @@ -1055,7 +1054,6 @@ mod tests {
if path.is_dir() {
continue;
}
println!("Deserializing file: {}", path.display());
let contents = match std::fs::read_to_string(path) {
Ok(contents) => contents,
Err(err) => panic!("Error reading file `{}`: {:?}", path.display(), err),
Expand Down Expand Up @@ -1093,7 +1091,6 @@ mod tests {
if path.is_dir() {
continue;
}
println!("Deserializing file: {}", path.display());
let contents = match std::fs::read_to_string(path) {
Ok(contents) => contents,
Err(err) => panic!("Error reading file `{}`: {:?}", path.display(), err),
Expand Down
1 change: 1 addition & 0 deletions src/bambu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod control;
mod discover;
mod temperature;

use std::{net::IpAddr, sync::Arc};

Expand Down
66 changes: 66 additions & 0 deletions src/bambu/temperature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::{collections::HashMap, sync::Arc};

use anyhow::Result;

use super::X1Carbon;
use crate::{TemperatureSensor, TemperatureSensorReading, TemperatureSensors as TemperatureSensorsTrait};

impl X1Carbon {
/// Return a handle to read the temperature information from the
/// Moonraker printer.
pub fn get_temperature_sensors(&self) -> TemperatureSensors {
TemperatureSensors {
client: self.client.clone(),
}
}
}

/// Struct to read Temperature values from the 3d printer.
#[derive(Clone)]
pub struct TemperatureSensors {
client: Arc<bambulabs::client::Client>,
}

impl TemperatureSensorsTrait for TemperatureSensors {
type Error = anyhow::Error;

async fn sensors(&self) -> Result<HashMap<String, TemperatureSensor>> {
Ok(HashMap::from([
("extruder".to_owned(), TemperatureSensor::Extruder),
("bed".to_owned(), TemperatureSensor::Bed),
("chamber".to_owned(), TemperatureSensor::Chamber),
]))
}

async fn poll_sensors(&mut self) -> Result<HashMap<String, TemperatureSensorReading>> {
let Some(status) = self.client.get_status()? else {
return Ok(HashMap::new());
};

let mut sensor_readings = HashMap::from([(
"extruder".to_owned(),
TemperatureSensorReading {
temperature_celsius: status.nozzle_temper.unwrap_or(0.0),
target_temperature_celsius: status.nozzle_target_temper,
},
)]);

sensor_readings.insert(
"bed".to_owned(),
TemperatureSensorReading {
temperature_celsius: status.bed_temper.unwrap_or(0.0),
target_temperature_celsius: status.bed_target_temper,
},
);

sensor_readings.insert(
"chamber".to_owned(),
TemperatureSensorReading {
temperature_celsius: status.chamber_temper.unwrap_or(0.0),
target_temperature_celsius: None,
},
);

Ok(sensor_readings)
}
}
3 changes: 3 additions & 0 deletions src/bin/machine-api/cmd_serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ pub async fn main(_cli: &Cli, cfg: &Config, bind: &str) -> Result<()> {
AnyMachine::Moonraker(moonraker) => {
let _ = spawn_metrics(registry.clone(), &machine_id, moonraker.get_temperature_sensors()).await;
}
AnyMachine::BambuX1Carbon(bambu) => {
let _ = spawn_metrics(registry.clone(), &machine_id, bambu.get_temperature_sensors()).await;
}
_ => { /* Nothing to do here! */ }
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ impl ServerContext {
let registry = Registry::default();

// Create the server in debug mode.
let (server, _context) =
crate::server::create_server(&bind, Arc::new(RwLock::new(HashMap::new())), registry).await?;
let (server, _context) = crate::server::create_server(
&bind,
Arc::new(RwLock::new(HashMap::new())),
Arc::new(RwLock::new(registry)),
)
.await?;

// Sleep for 5 seconds while the server is comes up.
std::thread::sleep(std::time::Duration::from_secs(5));
Expand Down

0 comments on commit 9482019

Please sign in to comment.