Add Printer trait + PrinterHandle enum domain model with polymorphism example

This commit is contained in:
2026-08-28 17:03:21 +00:00
parent 2807b2b067
commit 03bd07fbd7
8 changed files with 302 additions and 1 deletions
+37
View File
@@ -0,0 +1,37 @@
use tracing::info;
use super::{Printer, PrinterBase, PrinterError};
pub struct KlipperPrinter {
base: PrinterBase,
}
impl KlipperPrinter {
pub fn new(id: impl Into<String>, name: impl Into<String>, host: impl Into<String>) -> Self {
Self {
base: PrinterBase { id: id.into(), name: name.into(), host: host.into() },
}
}
}
impl Printer for KlipperPrinter {
fn base(&self) -> &PrinterBase {
&self.base
}
/// OVERRIDE: Moonraker's real-time feed is a WebSocket JSON-RPC channel
/// (see adapters::moonraker::run for that), but it also exposes a plain
/// REST endpoint. `connect()` here is a lightweight reachability probe
/// against that REST endpoint before the daemon opens the WS stream —
/// a third, again-completely-different notion of "connect".
async fn connect(&mut self) -> Result<(), PrinterError> {
info!(printer = %self.base.id, host = %self.base.host, "probing Moonraker over HTTP");
reqwest::get(format!("http://{}/printer/info", self.base.host))
.await
.and_then(|resp| resp.error_for_status())
.map_err(|err| PrinterError::Connection(self.base.id.clone(), err.to_string()))?;
Ok(())
}
}