From 23f157d07a41d7015073dc7d49ed50359086ce37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iwo=20Strzebo=C5=84ski?= Date: Mon, 7 Sep 2026 15:03:14 +0000 Subject: [PATCH] Prefix generated sequence_id instead of offsetting it A printer's own free-running counter for unsolicited messages is always a plain numeric string, so a non-numeric prefix ("cxproxy-") rules out collision with it outright, rather than a large numeric starting offset merely making collision improbable. Confirmed sequence_id need not be numeric (live test, ISO-timestamp-prefixed value round-tripped fine) and re-verified the new prefixed IDs round-trip through get_version on both P1P (v1) and H2C (v2). --- src/printer/bambu_mqtt.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/printer/bambu_mqtt.rs b/src/printer/bambu_mqtt.rs index 6635d46..30b16f3 100644 --- a/src/printer/bambu_mqtt.rs +++ b/src/printer/bambu_mqtt.rs @@ -86,6 +86,20 @@ const REQUEST_TIMEOUT: Duration = Duration::from_secs(10); /// 1MB has real headroom for that. const MAX_PACKET_SIZE: usize = 1024 * 1024; +/// Every `sequence_id` this crate generates gets this prefix - a printer +/// runs its own independent counter for unsolicited messages (confirmed +/// live: `print.push_status`'s was already in the 500s at the start of a +/// session, climbing steadily), and that counter has only ever been +/// observed as a plain unprefixed integer string. An earlier version of +/// this just started our own counter from a large offset instead of +/// prefixing it, to dodge the same range a printer's counter climbs +/// through - confirmed live (with an ISO-timestamp-prefixed sequence_id) +/// that `sequence_id` isn't required to be numeric at all, so a +/// non-numeric prefix rules out collision outright instead of merely +/// making it improbable, and the counter itself can go back to starting +/// at 1 each session. +const SEQUENCE_ID_PREFIX: &str = "cxproxy-"; + enum BambuMqttClient { V1(rumqttc::AsyncClient), V2(rumqttc::v5::AsyncClient), @@ -292,7 +306,10 @@ impl BambuMqttSession { /// `PrintStatusCache`. Not for a command with no reply at all either, /// like `gcode_line` - use `publish` (fire-and-forget) for those. pub async fn request(&self, category: &str, mut body: Value) -> Result { - let sequence_id = self.next_sequence_id.fetch_add(1, Ordering::Relaxed).to_string(); + let sequence_id = format!( + "{SEQUENCE_ID_PREFIX}{}", + self.next_sequence_id.fetch_add(1, Ordering::Relaxed) + ); body["sequence_id"] = Value::String(sequence_id.clone()); let (tx, rx) = oneshot::channel();