Pointed at a real H2C that was 20% into a job, `print_status()` returned
nothing at all. The printer was in fact pushing a full status object every
second; every one was arriving, failing to deserialize, and being dropped
invisibly by an `if let Ok(report) = ..` with no `else`.
That silence produced two confidently-wrong claims in our own comments,
both corrected here:
- `pushall`'s doc said whether v2 emits `push_status` unprompted was "an
open question". It is not: v2 pushes a full 97-field object roughly once
a second from the moment you subscribe, no trigger needed. There is no
missing "other trigger" to hunt for.
- The "sequence gap" warning blamed the network for dropped v1 updates.
Those updates were not dropped in transit - they failed to parse, so
they never reached the counter, making the next one that did look like a
skip. Confirmed: seq 57, 62, 64 and 69 all arrived. After the fix, zero
gaps and zero parse failures across a 25s run against both printers.
Two concrete causes, both found with captured payloads:
- v1 trays carry `tray_temp`/`tray_time`; v2 trays carry
`drying_temp`/`drying_time`. All four were required, so *every* v1 push
carrying AMS data failed.
- A third tray shape existed that nothing modelled: an empty v2 slot sends
`{"id": "2", "state": 0}`, which matched neither `EmptyTray` (it denies
unknown fields, deliberately) nor `LoadedTray` (16 fields short).
A parse failure now reports itself, naming the printer, the sequence id and
the serde error. A report this process cannot parse is a defect in
`PrintReport`, and the only way to fix it is to be told which field.
`PrintReport` itself modelled 9 of 97 top-level fields, and none of them
were progress or temperature - it was built from a truncated example. It
now carries gcode_state, percent, layer counts, remaining time, nozzle/bed
temperatures, fan gears, wifi and HMS entries. `merge_from`'s two dozen
four-line blocks collapse to `newer.x.or(self.x.take())` one-liners: the
old shape made adding a field a three-place edit, and forgetting the merge
half produced a field that silently never survived a partial update.
Two scale traps documented where they bite: reported fan speeds are 0-15
gears, not percent and not the 0-255 PWM we send (`set_fan_speed(50)`
reads back as "7"); and v2 pins `sequence_id` to one value for minutes
while contents change, so it orders and identifies nothing there.
Two examples, and the distinction between them is the point. `live_reports`
is read-only and safe against a printer mid-job. `bambu_idle_checks`
writes, so it requires an explicit printer id and refuses to run unless
that printer reports `gcode_state: "IDLE"`.
Regression fixtures are verbatim captures off both printers, hardware
identifiers scrubbed - the only way to keep this from regressing without a
printer plugged in.
89 lines
3.6 KiB
Rust
89 lines
3.6 KiB
Rust
//! Does `PrintReport` actually parse what real printers really send?
|
|
//!
|
|
//! The fixtures in `tests/fixtures/` are verbatim `print.push_status`
|
|
//! payloads captured off real hardware on 2026-10-03 via
|
|
//! `cargo run --example live_reports` (hardware identifiers scrubbed):
|
|
//!
|
|
//! - `push_status_v1_p1p_idle.json` - a P1P on BambuMQTT v1, idle.
|
|
//! - `push_status_v2_h2c_printing.json` - an H2C on BambuMQTT v2, 13% into
|
|
//! a real job.
|
|
//!
|
|
//! These exist because the struct was originally built from a *truncated*
|
|
//! example payload, and the gap between that and reality turned out to be
|
|
//! the cause of two live misbehaviours (silently empty `print_status()`, and
|
|
//! bogus "sequence gap" warnings). A fixture is the only way to keep that
|
|
//! from regressing without a printer plugged in.
|
|
|
|
use continuum_proxy::printer::PrintReport;
|
|
|
|
fn load(name: &str) -> serde_json::Value {
|
|
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/");
|
|
let text = std::fs::read_to_string(format!("{path}{name}")).expect("fixture should exist");
|
|
serde_json::from_str(&text).expect("fixture should be valid JSON")
|
|
}
|
|
|
|
/// The failure mode that mattered: `handle_incoming_report` does
|
|
/// `if let Ok(report) = serde_json::from_value::<PrintReport>(..)`, so a
|
|
/// parse error doesn't just lose that one update - it's invisible. Assert on
|
|
/// the error text, not just `is_ok()`, so a regression says *what* broke.
|
|
fn assert_parses(name: &str) {
|
|
let value = load(name);
|
|
match serde_json::from_value::<PrintReport>(value) {
|
|
Ok(_) => {}
|
|
Err(err) => panic!("{name} failed to parse as PrintReport: {err}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn parses_v1_idle_push_status() {
|
|
assert_parses("push_status_v1_p1p_idle.json");
|
|
}
|
|
|
|
#[test]
|
|
fn parses_v2_printing_push_status() {
|
|
assert_parses("push_status_v2_h2c_printing.json");
|
|
}
|
|
|
|
/// The whole point of consuming these at all: a print farm needs progress
|
|
/// and temperatures. If the struct parses but drops these, it's no more
|
|
/// useful than not parsing.
|
|
#[test]
|
|
fn v2_printing_report_carries_progress_and_temperatures() {
|
|
let value = load("push_status_v2_h2c_printing.json");
|
|
let report: PrintReport = serde_json::from_value(value).expect("should parse");
|
|
|
|
assert_eq!(report.gcode_state.as_deref(), Some("RUNNING"));
|
|
assert_eq!(report.mc_percent, Some(13));
|
|
assert_eq!(report.layer_num, Some(2));
|
|
assert_eq!(report.total_layer_num, Some(50));
|
|
assert_eq!(report.mc_remaining_time, Some(102));
|
|
assert_eq!(report.nozzle_temper, Some(230.0));
|
|
assert_eq!(report.subtask_name.as_deref(), Some("\u{15b}wieczka-24.stl"));
|
|
}
|
|
|
|
/// An idle printer still has to produce a coherent report - just with
|
|
/// nothing printing.
|
|
#[test]
|
|
fn v1_idle_report_reads_as_idle() {
|
|
let value = load("push_status_v1_p1p_idle.json");
|
|
let report: PrintReport = serde_json::from_value(value).expect("should parse");
|
|
|
|
assert_eq!(report.gcode_state.as_deref(), Some("IDLE"));
|
|
assert_eq!(report.mc_percent, Some(0));
|
|
}
|
|
|
|
/// v1 and v2 disagree about tray field names (`tray_temp`/`tray_time` vs
|
|
/// `drying_temp`/`drying_time`) - this is the specific mismatch that made
|
|
/// every v1 push carrying AMS data fail. Both spellings have to work.
|
|
#[test]
|
|
fn both_protocol_generations_parse_their_ams_trays() {
|
|
for name in ["push_status_v1_p1p_idle.json", "push_status_v2_h2c_printing.json"] {
|
|
let value = load(name);
|
|
let report: PrintReport = serde_json::from_value(value).unwrap_or_else(|err| {
|
|
panic!("{name}: {err}");
|
|
});
|
|
let ams = report.ams.expect("fixture should carry AMS state");
|
|
assert!(!ams.ams.is_empty(), "{name}: expected at least one AMS unit");
|
|
}
|
|
}
|