Bladeren bron

MQTT: the debug log shows what a reported command contains (#3531)

- The printer reports back every command it receives, also those
  sent from its own touchscreen; the debug log now records their
  content, not only their name, capped at 4000 characters.
- push_status and project_file stay name-only.
maziggy 3 dagen geleden
bovenliggende
commit
5380550222

+ 1 - 0
CHANGELOG.md

@@ -10,6 +10,7 @@ All notable changes to Bambuddy will be documented in this file.
 - **The slicer sidecars can be published on one address only (suggested by @pleasantone)** — The sidecar compose file published the slicer ports on every network interface, and the sidecars have no login. The new `SIDECAR_BIND` setting in `slicer-api/.env` limits them to one address: `127.0.0.1` when Bambuddy runs on the same host with its default `network_mode: host`. Left unset, nothing changes, so Bambuddy in bridge mode or on another host still reaches the sidecars
 
 ### Changed
+- **The debug log shows what a command the printer reports back contains (#3531)** — The printer reports every command it receives back to Bambuddy, also the ones sent from its own touchscreen, and the debug log kept only their names. It now records their content too, so a support package captures exactly what a touchscreen action sends, such as the H3C's Unload All. Status updates and print starts stay name-only: the first arrives every second, the second may carry a signed download link.
 - **A slice sent again from the slicer adds a print to its archive (#1634, requested by @d3nn3s08, contributed by @Keybored02 in #3508)** — A print from the File Manager or the queue already joined the archive of its file, but a print sent from Bambu Studio or OrcaSlicer, or started on the printer, always got a new archive: the slicer writes a new 3MF each time it sends, so the file never matched, and the archive's print count stayed at one. Bambuddy now also keeps the hash of each archive's G-code, and a print whose G-code matches an archive nobody owns, in no project and not from a File Manager file adds a run to that archive. The archive takes the new 3MF in place of its old one and keeps its photos, timelapses, notes, tags and earlier runs; a name set by hand stays, and a name taken from the file follows the new file. While another print of it is running, the archive keeps its file and only gets the run. A model sliced again with other settings gets an archive of its own. OrcaSlicer writes the slicing time into the G-code, so with it only the same slice sent again matches. Archives made before are read once in the background after the update. Wiki updated.
 - **Choose which printer an "Any model" job takes (#2915, requested by @bryanmahin, contributed by @Keybored02 in #3547)** — Several printers of a model can take an "Any model" job, and the one with the fewest print hours always got it. **Settings > Workflow > Printer Order for Any-Model Jobs** now chooses: the fewest print hours (the default, as before), the most (use up the machines near replacement first), the printer idle the longest (by its last finished print; one that never printed goes first), or the printers' own priority, a number in **Edit Printer** (lowest first, none last). The order only ranks printers that can take the job: a busy, offline or filament-short printer is still passed over, and preferred colours still come first. Powering a printer on with an Auto Power On plug follows the same order.
 

+ 13 - 0
backend/app/services/bambu_mqtt.py

@@ -1039,6 +1039,11 @@ _HMS_USER_ACTION_CODES: frozenset[str] = frozenset(
 # "0500_0007", which matches nothing in any catalog.
 HMS_MQTT_VERIFY_FAILED: str = "0500050000010007"
 
+# Command responses whose content the debug log leaves out (#3531). push_status
+# is the printer's status, arriving every second and logged field by field;
+# project_file carries the download URL of a print, which may be a signed one.
+_COMMANDS_LOGGED_BY_NAME_ONLY: frozenset[str] = frozenset({"push_status", "project_file"})
+
 
 @dataclass
 class KProfile:
@@ -2867,6 +2872,14 @@ class BambuMQTTClient:
             if "command" in print_data:
                 cmd = print_data.get("command")
                 logger.debug("[%s] Received command response: %s", self.serial_number, cmd)
+                if cmd not in _COMMANDS_LOGGED_BY_NAME_ONLY:
+                    # The printer reports back commands sent from its own
+                    # screen too, and this is the only place their content can
+                    # be seen: H3C Unload All is one command whose fields are
+                    # not documented anywhere (#3531).
+                    logger.debug(
+                        "[%s] %s content: %s", self.serial_number, cmd, json.dumps(print_data, default=str)[:4000]
+                    )
                 if cmd in ("extrusion_cali_set", "extrusion_cali_del"):
                     # INFO, not debug: this is the printer's verdict on a write
                     # the user just made, and it was invisible in support

+ 53 - 0
backend/tests/unit/services/test_command_response_content_3531.py

@@ -0,0 +1,53 @@
+"""#3531: the debug log records the content of commands the printer reports back.
+
+The H3C touchscreen's Unload All is one command, ``multi_slot_ams_change_filament``,
+whose fields are not documented anywhere. The printer reports it back like every
+command, but the debug log kept only its name. push_status (the status itself)
+and project_file (which may carry a signed download URL) stay name-only.
+"""
+
+import logging
+
+import pytest
+
+from backend.app.services.bambu_mqtt import BambuMQTTClient
+
+
+@pytest.fixture
+def mqtt_client():
+    return BambuMQTTClient(ip_address="192.168.1.100", serial_number="TEST123", access_code="12345678")
+
+
+def _content_lines(caplog):
+    return [r.getMessage() for r in caplog.records if " content: " in r.getMessage()]
+
+
+def test_a_reported_command_is_logged_with_its_fields(mqtt_client, caplog):
+    caplog.set_level(logging.DEBUG, logger="backend.app.services.bambu_mqtt")
+
+    mqtt_client._process_message(
+        {"print": {"command": "multi_slot_ams_change_filament", "sequence_id": "7", "slots": [{"ams_id": 128}]}}
+    )
+
+    lines = _content_lines(caplog)
+    assert len(lines) == 1
+    assert "multi_slot_ams_change_filament content:" in lines[0]
+    assert '"slots": [{"ams_id": 128}]' in lines[0]
+
+
+@pytest.mark.parametrize("command", ["push_status", "project_file"])
+def test_status_and_print_start_stay_name_only(mqtt_client, caplog, command):
+    caplog.set_level(logging.DEBUG, logger="backend.app.services.bambu_mqtt")
+
+    mqtt_client._process_message({"print": {"command": command, "url": "https://example.invalid/signed?token=x"}})
+
+    assert _content_lines(caplog) == []
+
+
+def test_long_content_is_capped(mqtt_client, caplog):
+    caplog.set_level(logging.DEBUG, logger="backend.app.services.bambu_mqtt")
+
+    mqtt_client._process_message({"print": {"command": "gcode_line", "param": "G1 X1\n" * 2000}})
+
+    (line,) = _content_lines(caplog)
+    assert len(line.split(" content: ", 1)[1]) == 4000