Files
continuum-proxy/examples/bambu_commands_demo.rs
T
octoturge 1583f531fa Add print.rs (AmsState/AmsUnit/AmsTray/XcamSettings), fix print vs pushing
Mirrors continuum-schemas' schemas/bambulab/ restructuring: PushallResponse
removed (it never existed on the wire — pushall's request goes to
"pushing", but the state-push stream it triggers arrives under "print",
a different root key, not a direct reply). BambuReport::Pushing(...) ->
BambuReport::Print(PrintReport).

AmsTray is an untagged enum (Empty/Loaded) rather than one struct full of
Option<T> fields, matching the JSON Schema's oneOf for the same slot -
#[serde(deny_unknown_fields)] on EmptyTray is load-bearing: untagged enums
try variants in order, and a loaded tray (superset of Empty's one field)
would otherwise incorrectly match Empty first.

Verified against the full real ams/xcam example from the conversation via
examples/bambu_commands_demo.rs - both the empty and loaded tray in that
payload deserialize to the correct enum variant.
2026-08-28 23:19:20 +00:00

56 lines
3.1 KiB
Rust

//! Run with: cargo run --example bambu_commands_demo
//!
//! Round-trips both commands against the exact JSON from Bambu's real
//! protocol — proves the plain hand-written struct + serde approach
//! matches byte for byte, same as the proto-based version did, with none
//! of the build.rs ceremony.
use continuum_proxy::printer::{CommandEnvelope, GetVersionRequest, GetVersionResponse, PrintReport, PushallRequest};
fn main() {
// --- get_version request ---
let req = GetVersionRequest {
envelope: CommandEnvelope { sequence_id: "0".to_string(), command: "get_version".to_string() },
};
let actual = serde_json::to_string(&req).unwrap();
let expected = r#"{"sequence_id":"0","command":"get_version"}"#;
println!("get_version request: {actual}");
println!("matches Bambu's shape: {}\n", actual == expected);
// --- get_version response, deserialized from Bambu's real payload ---
let real_response = r#"{"command":"get_version","module":[{"hw_ver":"","name":"ota","sn":"","sw_ver":"01.01.01.00"},{"hw_ver":"","name":"xm","sn":"","sw_ver":"00.00.00.00"}],"sequence_id":"0"}"#;
let parsed: GetVersionResponse = serde_json::from_str(real_response).unwrap();
println!("get_version response parsed fine, no result/reason present: {parsed:#?}\n");
// --- pushall request ---
let pushall = PushallRequest {
envelope: CommandEnvelope { sequence_id: "0".to_string(), command: "pushall".to_string() },
version: 1,
push_target: 1,
};
let actual = serde_json::to_string(&pushall).unwrap();
let expected = r#"{"sequence_id":"0","command":"pushall","version":1,"push_target":1}"#;
println!("pushall request: {actual}");
println!("matches Bambu's shape: {}\n", actual == expected);
// --- print report: arrives under "print", NOT "pushing" (pushall
// triggers this stream, it isn't a direct reply to it) — includes the
// full ams/xcam example, with one empty tray and one loaded tray, to
// exercise the untagged Empty/Loaded split.
let real_print_report = r#"{
"ams": {"ams": [{"humidity":"4","id":"0","temp":"22.7","tray":[
{"id":"0"},
{"bed_temp":"0","bed_temp_type":"0","cols":["000000FF"],"drying_temp":"0","drying_time":"0","id":"1","nozzle_temp_max":"240","nozzle_temp_min":"190","remain":0,"tag_uid":"0000000000000000","tray_color":"000000FF","tray_diameter":"0.00","tray_id_name":"","tray_info_idx":"GFA00","tray_sub_brands":"","tray_type":"PLA","tray_uuid":"00000000000000000000000000000000","tray_weight":"0","xcam_info":"000000000000000000000000"}
]}]},
"ams_exist_bits": "1",
"insert_flag": true,
"power_on_flag": false,
"tray_exist_bits": "e",
"tray_is_bbl_bits": "e",
"xcam": {"allow_skip_parts":false,"buildplate_marker_detector":false,"first_layer_inspector":true,"halt_print_sensitivity":"medium","print_halt":true,"printing_monitor":true,"spaghetti_detector":true},
"xcam_status": "0"
}"#;
let parsed: PrintReport = serde_json::from_str(real_print_report).unwrap();
println!("print report parsed fine, empty + loaded tray both matched correctly:\n{parsed:#?}");
}