Make the uplink match the written protocol, and test that it does

Adds tests/uplink_protocol.rs, the gateway half of a contract test whose
other half is continuum-backend/tests/edge_protocol.test.ts. It checks that
every message the backend sends is one this gateway can read, and that what
this gateway writes is what the schema says it writes.

It found two things immediately.

Telemetry was serializing empty fields as explicit nulls. serde emits
`"job_name": null` for an empty Option unless told otherwise, and the
backend's validator treats a missing key and an explicit null as different
things - so every batch containing an idle printer, which by definition has
no job name, would have been rejected whole. Every optional field now has
skip_serializing_if, confirmed against a live capture: an idle P1P's
snapshot has no nulls in it at all.

ServerMessage had no variant for the handshake being rejected. A bad or
expired token would have arrived as "unparseable message from cloud,
ignoring" and then been retried every five seconds forever, which in the
logs is indistinguishable from a network fault. HelloError is now a real
variant, logged at error level naming the env var to go fix, and it stops
the connection attempt instead of looping on it.

Also exposes config and uplink from lib.rs so the test can reach them, same
reason fleet and printer are already there.

Verified live against a P1P and an H2C for the whole run, including the H2C
finishing its print: RUNNING 98% -> FINISH 100% layer 50/50 with the nozzle
cooling 84C -> 76C, all of it arriving through the uplink, no warnings.
This commit is contained in:
2026-10-03 10:37:24 +00:00
parent 91fbcaf665
commit ca3846b7c7
4 changed files with 170 additions and 1 deletions
+34 -1
View File
@@ -15,7 +15,12 @@ pub enum ClientMessage {
/// which the *cloud* generates - unlike Bambu's `sequence_id`, we're
/// both ends of this protocol, so the side that needs the correlation
/// gets to choose it.
CommandResult { command_id: String, ok: bool, error: Option<String> },
CommandResult {
command_id: String,
ok: bool,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
},
}
/// A flattened snapshot of one printer, in the terms an operator console
@@ -30,6 +35,14 @@ pub enum ClientMessage {
/// Every field is optional because an unreachable or just-connected printer
/// genuinely has nothing to report, and "no data yet" is different from
/// "zero".
///
/// Absent fields are *omitted* rather than serialized as `null`, which is
/// what `skip_serializing_if` below is for. Caught by the contract test
/// against real captures: serde's default is to emit `"job_name": null`, and
/// the backend's validator reads a missing key and an explicit null as
/// different things, so an idle printer's entire telemetry batch was being
/// rejected. The backend now accepts either, but this end sends only the one
/// the schema describes.
#[derive(Debug, Clone, Serialize)]
pub struct PrinterTelemetry {
/// Our own id from printers.toml, not the vendor serial - this is what
@@ -40,21 +53,33 @@ pub struct PrinterTelemetry {
/// through as the printer's own string rather than narrowed to an enum,
/// so an unrecognised state reaches the cloud as itself instead of being
/// dropped on the floor here.
#[serde(skip_serializing_if = "Option::is_none")]
pub state: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub percent: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub layer: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub total_layers: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub remaining_minutes: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub job_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub nozzle_temp: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub nozzle_target: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bed_temp: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bed_target: Option<f32>,
/// Non-zero means the printer is reporting a fault.
#[serde(skip_serializing_if = "Option::is_none")]
pub print_error: Option<i32>,
/// How many Health-Management-System entries the printer currently has
/// active. Just the count for now - decoding the `{attr, code}` pairs
/// into messages needs Bambu's HMS tables, which we don't have yet.
#[serde(skip_serializing_if = "Option::is_none")]
pub active_alerts: Option<usize>,
}
@@ -63,6 +88,14 @@ pub struct PrinterTelemetry {
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ServerMessage {
HelloAck { session_id: String },
/// The handshake was rejected - a bad, expired or wrong-farm token. The
/// backend closes the connection right after sending this.
///
/// Worth a variant of its own rather than letting it fall into the
/// unparseable-message branch: retrying a rejected token every few seconds
/// forever looks exactly like a network problem in the logs otherwise, and
/// it is the one uplink failure a human has to go fix by hand.
HelloError { message: String },
HeartbeatAck { seq: u64 },
/// Do something to one printer. `printer_id` is our own id from
/// printers.toml.