From 94820194dbb08b2963ed4c5f3721f4990bfc11f9 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Thu, 17 Oct 2024 15:31:28 -0700 Subject: [PATCH] add bambu; (#132) * add bambu; Signed-off-by: Jess Frazelle * updates Signed-off-by: Jess Frazelle * fixes Signed-off-by: Jess Frazelle * updates Signed-off-by: Jess Frazelle --------- Signed-off-by: Jess Frazelle --- bambulabs/src/templates.rs | 3 -- src/bambu/mod.rs | 1 + src/bambu/temperature.rs | 66 ++++++++++++++++++++++++++++++++ src/bin/machine-api/cmd_serve.rs | 3 ++ src/tests.rs | 8 +++- 5 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 src/bambu/temperature.rs diff --git a/bambulabs/src/templates.rs b/bambulabs/src/templates.rs index b040276..a2c4981 100644 --- a/bambulabs/src/templates.rs +++ b/bambulabs/src/templates.rs @@ -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), @@ -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), @@ -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), diff --git a/src/bambu/mod.rs b/src/bambu/mod.rs index 6c70c0c..9b6fb60 100644 --- a/src/bambu/mod.rs +++ b/src/bambu/mod.rs @@ -2,6 +2,7 @@ mod control; mod discover; +mod temperature; use std::{net::IpAddr, sync::Arc}; diff --git a/src/bambu/temperature.rs b/src/bambu/temperature.rs new file mode 100644 index 0000000..e50ec63 --- /dev/null +++ b/src/bambu/temperature.rs @@ -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, +} + +impl TemperatureSensorsTrait for TemperatureSensors { + type Error = anyhow::Error; + + async fn sensors(&self) -> Result> { + 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> { + 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) + } +} diff --git a/src/bin/machine-api/cmd_serve.rs b/src/bin/machine-api/cmd_serve.rs index 55b6144..d29f803 100644 --- a/src/bin/machine-api/cmd_serve.rs +++ b/src/bin/machine-api/cmd_serve.rs @@ -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! */ } } } diff --git a/src/tests.rs b/src/tests.rs index 792d3ae..20de2d4 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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));