Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list machines uses the network devices #23

Merged
merged 4 commits into from
Jul 31, 2024
Merged
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
20 changes: 18 additions & 2 deletions openapi/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
59 changes: 49 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use clap::Parser;
use gcode::GcodeSequence;
use machine::Machine;
use network_printer::NetworkPrinterManufacturer;
use opentelemetry::KeyValue;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::Resource;
Expand Down Expand Up @@ -79,8 +80,8 @@
/// 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 {
Expand All @@ -105,7 +106,7 @@
}

/// 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")]
Expand Down Expand Up @@ -192,11 +193,32 @@
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?);

Check warning on line 199 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L199

Added line #L199 was not covered by tests

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(10), 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");

Check warning on line 213 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L201-L213

Added lines #L201 - L213 were not covered by tests

tokio::join!(form_labs.discover(), bambu.discover())
})
.await;

Check warning on line 217 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L216-L217

Added lines #L216 - L217 were not covered by tests

let machines = api_context.list_machines()?;
for (id, machine) in machines.iter() {
println!("{}: {:?}", id, machine);

Check warning on line 221 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L219-L221

Added lines #L219 - L221 were not covered by tests
}
}
SubCommand::SliceFile { config_file, file } => {
Expand All @@ -217,8 +239,25 @@

// 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?);

Check warning on line 242 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L242

Added line #L242 was not covered by tests

println!("Discovering printers...");
// Start all the discovery tasks.
let cloned_api_context = api_context.clone();
let _ = tokio::time::timeout(tokio::time::Duration::from_secs(10), 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");

Check warning on line 255 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L244-L255

Added lines #L244 - L255 were not covered by tests

tokio::join!(form_labs.discover(), bambu.discover())
})
.await;

Check warning on line 259 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L258-L259

Added lines #L258 - L259 were not covered by tests

let machine = api_context
.find_machine_by_id(printer_id)?
.expect("Printer not found by given ID");
Expand Down
2 changes: 0 additions & 2 deletions src/network_printer/bambu_x1_carbon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/network_printer/formlabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<String, crate::usb_printer::UsbPrinterInfo>>,
pub network_printers: Arc<HashMap<NetworkPrinterManufacturer, Box<dyn NetworkPrinter>>>,
pub active_jobs: Mutex<HashMap<String, tokio::task::JoinHandle<anyhow::Result<()>>>>,
Expand All @@ -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<Context> {
pub async fn new(schema: serde_json::Value, logger: slog::Logger) -> Result<Context> {
let mut network_printers: HashMap<NetworkPrinterManufacturer, Box<dyn NetworkPrinter>> = HashMap::new();

// Add formlabs backend.
Expand All @@ -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()),
Expand Down
6 changes: 3 additions & 3 deletions src/server/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
/// 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. */
Expand Down Expand Up @@ -118,7 +118,7 @@

Ok(HttpResponseOk(PrintJobResponse {
job_id: job_id.to_string(),
machine_id: machine.id(),
parameters: params,

Check warning on line 121 in src/server/endpoints.rs

View check run for this annotation

Codecov / codecov/patch

src/server/endpoints.rs#L121

Added line #L121 was not covered by tests
}))
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?
Expand Down
Loading