From b25ae185b0c3952be5b24cd47c44bfd8bd5c53c9 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Tue, 30 Jul 2024 18:19:57 -0700 Subject: [PATCH 1/4] u[pdates Signed-off-by: Jess Frazelle --- src/main.rs | 22 ++++++++++++---------- src/server/context.rs | 4 +--- src/server/mod.rs | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index fea000e..02a02e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,8 +79,8 @@ pub enum SubCommand { /// Run the server. Server(Server), - /// List all available USB devices. - ListUsbDevices, + /// List all available machines on the network or over USB. + ListMachines, /// Slice the given `file` with config from `config_file` SliceFile { @@ -105,7 +105,7 @@ pub enum SubCommand { } /// A subcommand for running the server. -#[derive(Parser, Clone, Debug, Default)] +#[derive(Parser, Clone, Debug)] pub struct Server { /// IP address and port that the server should listen #[clap(short, long, default_value = "0.0.0.0:8080")] @@ -192,11 +192,14 @@ async fn run_cmd(opts: &Opts) -> Result<()> { SubCommand::Server(s) => { crate::server::server(s, opts).await?; } - SubCommand::ListUsbDevices => { - let printers = crate::usb_printer::UsbPrinter::list_all(); - println!("Printers:"); - for printer in printers { - println!("{:?}", printer); + SubCommand::ListMachines => { + // Now connect to first printer we find over serial port + // + let api_context = Arc::new(Context::new(Default::default(), opts.create_logger("print")).await?); + + let machines = api_context.list_machines()?; + for (id, machine) in machines.iter() { + println!("{}: {:?}", id, machine); } } SubCommand::SliceFile { config_file, file } => { @@ -217,8 +220,7 @@ async fn run_cmd(opts: &Opts) -> Result<()> { // Now connect to first printer we find over serial port // - let api_context = - Arc::new(Context::new(Default::default(), opts.create_logger("print"), Default::default()).await?); + let api_context = Arc::new(Context::new(Default::default(), opts.create_logger("print")).await?); let machine = api_context .find_machine_by_id(printer_id)? .expect("Printer not found by given ID"); diff --git a/src/server/context.rs b/src/server/context.rs index 488506e..19aaf1d 100644 --- a/src/server/context.rs +++ b/src/server/context.rs @@ -11,7 +11,6 @@ use crate::network_printer::{NetworkPrinter, NetworkPrinterManufacturer}; pub struct Context { pub schema: serde_json::Value, pub logger: slog::Logger, - pub settings: crate::Server, pub usb_printers: Arc>, pub network_printers: Arc>>, pub active_jobs: Mutex>>>, @@ -21,7 +20,7 @@ impl Context { /** * Return a new Context. */ - pub async fn new(schema: serde_json::Value, logger: slog::Logger, settings: crate::Server) -> Result { + pub async fn new(schema: serde_json::Value, logger: slog::Logger) -> Result { let mut network_printers: HashMap> = HashMap::new(); // Add formlabs backend. @@ -40,7 +39,6 @@ impl Context { Ok(Context { schema, logger, - settings, network_printers: Arc::new(network_printers), usb_printers: Arc::new(crate::usb_printer::UsbPrinter::list_all()), active_jobs: Mutex::new(HashMap::new()), diff --git a/src/server/mod.rs b/src/server/mod.rs index ff4c7ec..dcd6dea 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -52,7 +52,7 @@ pub async fn create_server( let logger = opts.create_logger("server"); let dropshot_logger = logger.new(slog::o!("component" => "dropshot")); - let api_context = Arc::new(Context::new(schema, logger, s.clone()).await?); + let api_context = Arc::new(Context::new(schema, logger).await?); let server = HttpServerStarter::new(&config_dropshot, api, api_context.clone(), &dropshot_logger) .map_err(|error| anyhow!("failed to create server: {}", error))? From bfddc53c0bad70c6c56360cbb2cd71a49192679f Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Tue, 30 Jul 2024 19:45:18 -0700 Subject: [PATCH 2/4] updates Signed-off-by: Jess Frazelle --- src/main.rs | 37 ++++++++++++++++++++++++++ src/network_printer/bambu_x1_carbon.rs | 2 -- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 02a02e1..963b9b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ use anyhow::{bail, Result}; use clap::Parser; use gcode::GcodeSequence; use machine::Machine; +use network_printer::NetworkPrinterManufacturer; use opentelemetry::KeyValue; use opentelemetry_otlp::WithExportConfig; use opentelemetry_sdk::Resource; @@ -197,6 +198,24 @@ async fn run_cmd(opts: &Opts) -> Result<()> { // let api_context = Arc::new(Context::new(Default::default(), opts.create_logger("print")).await?); + println!("Discovering printers..."); + let cloned_api_context = api_context.clone(); + // We don't care if it times out, we just want to wait for the discovery tasks to + // finish. + let _ = tokio::time::timeout(tokio::time::Duration::from_secs(5), async move { + let form_labs = cloned_api_context + .network_printers + .get(&NetworkPrinterManufacturer::Formlabs) + .expect("No formlabs discover task registered"); + let bambu = cloned_api_context + .network_printers + .get(&NetworkPrinterManufacturer::Bambu) + .expect("No Bambu discover task registered"); + + tokio::join!(form_labs.discover(), bambu.discover()) + }) + .await; + let machines = api_context.list_machines()?; for (id, machine) in machines.iter() { println!("{}: {:?}", id, machine); @@ -221,6 +240,24 @@ async fn run_cmd(opts: &Opts) -> Result<()> { // Now connect to first printer we find over serial port // let api_context = Arc::new(Context::new(Default::default(), opts.create_logger("print")).await?); + + println!("Discovering printers..."); + // Start all the discovery tasks. + let cloned_api_context = api_context.clone(); + let _ = tokio::time::timeout(tokio::time::Duration::from_secs(5), async move { + let form_labs = cloned_api_context + .network_printers + .get(&NetworkPrinterManufacturer::Formlabs) + .expect("No formlabs discover task registered"); + let bambu = cloned_api_context + .network_printers + .get(&NetworkPrinterManufacturer::Bambu) + .expect("No Bambu discover task registered"); + + tokio::join!(form_labs.discover(), bambu.discover()) + }) + .await; + let machine = api_context .find_machine_by_id(printer_id)? .expect("Printer not found by given ID"); diff --git a/src/network_printer/bambu_x1_carbon.rs b/src/network_printer/bambu_x1_carbon.rs index 9c2640d..857945a 100644 --- a/src/network_printer/bambu_x1_carbon.rs +++ b/src/network_printer/bambu_x1_carbon.rs @@ -30,8 +30,6 @@ impl NetworkPrinter for BambuX1Carbon { let mut socket_buf = [0u8; 1536]; while let Ok(n) = socket.recv(&mut socket_buf).await { - println!("Frame"); - // The SSDP/UPnP frames we're looking for from Bambu printers are pure ASCII, so we don't // mind if we end up with garbage in the resulting string. Note that other SSDP packets from // e.g. macOS Bonjour(?) do contain binary data which means this conversion isn't suitable From 1803030cd201961bd0bdbf6a18f998f96daec5cf Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Tue, 30 Jul 2024 19:46:42 -0700 Subject: [PATCH 3/4] updates Signed-off-by: Jess Frazelle --- src/main.rs | 4 ++-- src/network_printer/formlabs.rs | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 963b9b6..c174c0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,7 +202,7 @@ async fn run_cmd(opts: &Opts) -> Result<()> { let cloned_api_context = api_context.clone(); // We don't care if it times out, we just want to wait for the discovery tasks to // finish. - let _ = tokio::time::timeout(tokio::time::Duration::from_secs(5), async move { + let _ = tokio::time::timeout(tokio::time::Duration::from_secs(10), async move { let form_labs = cloned_api_context .network_printers .get(&NetworkPrinterManufacturer::Formlabs) @@ -244,7 +244,7 @@ async fn run_cmd(opts: &Opts) -> Result<()> { println!("Discovering printers..."); // Start all the discovery tasks. let cloned_api_context = api_context.clone(); - let _ = tokio::time::timeout(tokio::time::Duration::from_secs(5), async move { + let _ = tokio::time::timeout(tokio::time::Duration::from_secs(10), async move { let form_labs = cloned_api_context .network_printers .get(&NetworkPrinterManufacturer::Formlabs) diff --git a/src/network_printer/formlabs.rs b/src/network_printer/formlabs.rs index c5e658d..18014d8 100644 --- a/src/network_printer/formlabs.rs +++ b/src/network_printer/formlabs.rs @@ -39,7 +39,6 @@ impl NetworkPrinter for Formlabs { manufacturer: NetworkPrinterManufacturer::Formlabs, model: None, }; - println!("formlabs printer: {:#?}", printer); self.printers.insert(addr.to_string(), printer); } else { println!("formlabs printer does not advertise address: {:#?}", response); From 65872418b108b73ad895a6df08783b7b8b9bafaf Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Tue, 30 Jul 2024 19:49:22 -0700 Subject: [PATCH 4/4] update openapi spec Signed-off-by: Jess Frazelle --- openapi/api.json | 20 ++++++++++++++++++-- src/server/endpoints.rs | 6 +++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/openapi/api.json b/openapi/api.json index 1e4224d..287b80f 100644 --- a/openapi/api.json +++ b/openapi/api.json @@ -154,13 +154,29 @@ "description": "The job id used for this print.", "type": "string" }, + "parameters": { + "allOf": [ + { + "$ref": "#/components/schemas/PrintParameters" + } + ], + "description": "The parameters used for this print." + } + }, + "required": [ + "job_id", + "parameters" + ], + "type": "object" + }, + "PrintParameters": { + "description": "Parameters for printing.", + "properties": { "machine_id": { - "description": "The machine id used for this print.", "type": "string" } }, "required": [ - "job_id", "machine_id" ], "type": "object" diff --git a/src/server/endpoints.rs b/src/server/endpoints.rs index eb7108c..a05113c 100644 --- a/src/server/endpoints.rs +++ b/src/server/endpoints.rs @@ -62,8 +62,8 @@ pub struct PrintJobResponse { /// The job id used for this print. pub job_id: String, - /// The machine id used for this print. - pub machine_id: String, + /// The parameters used for this print. + pub parameters: PrintParameters, } /** Print a given file. File must be a sliceable 3D model. */ @@ -118,7 +118,7 @@ pub(crate) async fn print_file( Ok(HttpResponseOk(PrintJobResponse { job_id: job_id.to_string(), - machine_id: machine.id(), + parameters: params, })) }