Просмотр исходного кода

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 4 дней назад
Родитель
Сommit
ed0742c781

+ 1 - 0
CHANGELOG.md

@@ -21,6 +21,7 @@ All notable changes to Bambuddy will be documented in this file.
 - **Choose which notifications get the camera photo (#2877, requested by @HappyPuppy21)** — **Attach Photo** on a notification provider sent the snapshot with every event that has one, so a channel that should show the finished print also got a photo at the start and at 25, 50 and 75 percent. With Attach Photo on, the provider's settings now list the photo events it sends, and unticking one sends that event without the photo. Every event keeps its photo until it is unticked, and the provider card names the events that go without one.
 - **BIQU Panda Breath on the printer card (#1466, requested by @embed-3d)** — A Panda Breath chamber heater with firmware 1.0.4 or later can connect to an MQTT broker, and Bambuddy now follows it there. Under **Settings > Network > MQTT Publishing**, turn on **BIQU Panda Breath**; it uses the broker set there, and lists each Panda Breath that reports with the printer it is bound to. The printer card of that printer gets a **Panda Breath** tile with the chamber temperature it reads and its target; clicking it turns the heater on or off, picks Auto, Always on or Drying, and sets a target from 0 to 60 °C. The tile stays on the card while the printer is switched off, since the heater keeps running. The device is matched by the printer serial number it reports, so it needs to be bound to its printer in its own web interface. Changes need the printer control permission for that printer. Not tested on a real device yet. Wiki updated.
 ### 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.
 - **SpoolBuddy writes OpenTag3D 2.004 on NTAG215 and NTAG216 tags (#3500, suggested by @pleasantone)** — SpoolBuddy wrote the first version of the OpenTag3D layout. The current version moved every field, so a reader that only knows it read the brand, colour, weight and temperatures from the wrong places. Tags with room for it now get version 2.004, with material, brand, colour name, up to four colours, minimum and maximum print temperature, net weight and empty spool weight. Version 2 no longer fits on an NTAG213, so those keep the first version. SpoolBuddy picks by the size the tag reports, and falls back to the first version if it can't read it; a SpoolBuddy that hasn't been updated keeps writing the first version. Bambuddy does not track density or bed temperature, so a version 2 tag leaves them unset. Wiki updated.
 - **Virtual printer cards start collapsed** — Settings > Virtual Printer opened every virtual printer's card with all its settings, so a few of them filled the page. Each card now shows only its header (name, mode, model, status) until it is clicked.

+ 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