test_a2l_ams_lite_2619.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. """A2L "AMS Lite" unit-id normalisation (memory a2l-am-unit-16).
  2. The A2L reports its 4-slot AMS Lite as physical unit id 16, but its tray
  3. bitmasks sit at bit base 24 (= id 6) and it reports tray_now as a local 0-3
  4. slot. We normalise 16 -> 6 at the MQTT ingest boundary so global tray ids land
  5. at 24-27 and every ams_id*4+slot consumer works unchanged, and translate back to
  6. the physical id 16 only on the outbound wire.
  7. Field values here mirror the confirmed capture (2026-07-20): physical slots 1
  8. empty, 2 & 3 loaded, 4 empty; tray_exist_bits "6000000"; tray_now "2" while
  9. printing physical slot 3.
  10. """
  11. import json
  12. from unittest.mock import MagicMock
  13. import pytest
  14. from backend.app.services.bambu_mqtt import (
  15. A2L_LITE_GLOBAL_BASE,
  16. A2L_LITE_NORMALIZED_AMS_ID,
  17. A2L_LITE_PHYSICAL_AMS_ID,
  18. BambuMQTTClient,
  19. a2l_lite_wire_ids,
  20. apply_tray_exist_bits,
  21. normalize_am_unit_id,
  22. )
  23. def _client(model: str = "A2L") -> BambuMQTTClient:
  24. return BambuMQTTClient(ip_address="10.0.0.1", serial_number="A2L", access_code="c", model=model)
  25. def _wired(client: BambuMQTTClient) -> BambuMQTTClient:
  26. client._client = MagicMock()
  27. client.state.connected = True
  28. return client
  29. def _capture_frame() -> dict:
  30. """One push_status frame matching Mike's 2026-07-20 capture."""
  31. return {
  32. "ams": [
  33. {
  34. "id": 16,
  35. "tray": [
  36. {"id": 0},
  37. {
  38. "id": 1,
  39. "state": 3,
  40. "remain": 100,
  41. "tray_type": "",
  42. "tray_info_idx": "",
  43. "tray_color": "FFFFFF00",
  44. },
  45. {
  46. "id": 2,
  47. "state": 3,
  48. "remain": 100,
  49. "tray_type": "",
  50. "tray_info_idx": "",
  51. "tray_color": "FFFFFF00",
  52. },
  53. {"id": 3},
  54. ],
  55. }
  56. ],
  57. "ams_exist_bits": "1000",
  58. "tray_exist_bits": "6000000",
  59. "tray_now": "2",
  60. "tray_pre": "2",
  61. "tray_tar": "2",
  62. }
  63. def _last_payload(client: BambuMQTTClient) -> dict:
  64. return json.loads(client._client.publish.call_args[0][1])["print"]
  65. class TestHelpers:
  66. def test_normalize_touches_only_16(self):
  67. assert normalize_am_unit_id(A2L_LITE_PHYSICAL_AMS_ID) == A2L_LITE_NORMALIZED_AMS_ID
  68. for other in (0, 1, 2, 3, 6, 15, 128, 135, 254, 255):
  69. assert normalize_am_unit_id(other) == other
  70. def test_wire_ids_only_for_normalised_6(self):
  71. # (physical ams id, local slot, physical global tray)
  72. assert a2l_lite_wire_ids(6, 2) == (16, 2, 66)
  73. assert a2l_lite_wire_ids(6, 0) == (16, 0, 64)
  74. # tray_id is taken modulo 4, so a global tray works too.
  75. assert a2l_lite_wire_ids(6, 26) == (16, 2, 66)
  76. # Any other unit id is left alone (returns None).
  77. for ams in (0, 3, 16, 128, 255):
  78. assert a2l_lite_wire_ids(ams, 2) is None
  79. class TestIngestNormalisation:
  80. def test_unit_id_16_normalised_to_6(self):
  81. client = _client()
  82. client._handle_ams_data(_capture_frame())
  83. assert client.state.raw_data["ams"][0]["id"] == A2L_LITE_NORMALIZED_AMS_ID
  84. assert client._has_a2l_am_unit is True
  85. def test_exists_annotation_uses_bit_base_24(self):
  86. # tray_exist_bits "6000000" = bits 25,26 -> global_bit 24+slot -> slots 1,2.
  87. client = _client()
  88. client._handle_ams_data(_capture_frame())
  89. trays = {t["id"]: t for t in client.state.raw_data["ams"][0]["tray"]}
  90. assert trays[1]["exists"] is True
  91. assert trays[2]["exists"] is True
  92. assert trays[0]["exists"] is False
  93. assert trays[3]["exists"] is False
  94. def test_regular_ams_untouched(self):
  95. client = _client(model="X1C")
  96. frame = {
  97. "ams": [{"id": 0, "tray": [{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}]}],
  98. "tray_exist_bits": "3",
  99. "tray_now": "1",
  100. }
  101. client._handle_ams_data(frame)
  102. assert client.state.raw_data["ams"][0]["id"] == 0
  103. assert client._has_a2l_am_unit is False
  104. assert client.state.tray_now == 1 # regular AMS 0 slot 1 == global 1
  105. def test_bare_list_ams_shape_is_also_normalised(self):
  106. # Some firmware/shapes deliver the unit list directly (no dict wrapper).
  107. client = _client()
  108. client._handle_ams_data([{"id": 16, "tray": [{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}]}])
  109. assert client.state.raw_data["ams"][0]["id"] == A2L_LITE_NORMALIZED_AMS_ID
  110. assert client._has_a2l_am_unit is True
  111. class TestTrayNowGlobalisation:
  112. def test_local_tray_now_globalised_to_24_plus_slot(self):
  113. client = _client()
  114. client._handle_ams_data(_capture_frame())
  115. # local slot 2 -> global 26 (24 + 2)
  116. assert client.state.tray_now == A2L_LITE_GLOBAL_BASE + 2 == 26
  117. def test_globalised_tray_passes_last_valid_guard(self):
  118. # last_loaded_tray is only written when the valid-tray guard accepts tn.
  119. client = _client()
  120. client._handle_ams_data(_capture_frame())
  121. assert client.state.last_loaded_tray == 26
  122. class TestTrayExistBitsBitBase:
  123. """#2697: ``apply_tray_exist_bits`` is reached with BOTH ids.
  124. ``_handle_ams_data`` normalises 16 -> 6 before calling it, but the VP
  125. bridge parses the raw printer payload itself and still holds the physical
  126. 16. Reading 16 as ``16 * 4`` lands on bits 64-67, where nothing is ever
  127. set, so every A2L slot was wiped in the slicer-facing cache. Both ids must
  128. resolve to bit base 24.
  129. """
  130. # Reporter's capture: bits 24, 25, 26 set -> slots 0/1/2 loaded, slot 3 empty.
  131. BITS = "7000000"
  132. def _units(self, ams_id):
  133. return [
  134. {
  135. "id": ams_id,
  136. "tray": [
  137. {
  138. "id": str(i),
  139. "state": 3,
  140. "tray_type": "PLA",
  141. "tray_color": "C12E1FFF",
  142. "tray_info_idx": "GFA00",
  143. "remain": 100,
  144. }
  145. for i in range(4)
  146. ],
  147. }
  148. ]
  149. def test_physical_id_16_uses_bit_base_24(self):
  150. units = self._units(A2L_LITE_PHYSICAL_AMS_ID)
  151. cleared = apply_tray_exist_bits(units, self.BITS)
  152. trays = units[0]["tray"]
  153. # Slots 0-2 are loaded and must survive untouched.
  154. for slot in range(3):
  155. assert trays[slot]["tray_type"] == "PLA", f"slot {slot} wrongly cleared"
  156. assert trays[slot]["state"] == 3
  157. # Only the genuinely empty slot 3 is cleared.
  158. assert cleared == 1
  159. assert trays[3]["state"] == 9
  160. assert trays[3]["tray_type"] == ""
  161. def test_normalised_id_6_matches_physical_id_16(self):
  162. physical = self._units(A2L_LITE_PHYSICAL_AMS_ID)
  163. normalised = self._units(A2L_LITE_NORMALIZED_AMS_ID)
  164. apply_tray_exist_bits(physical, self.BITS)
  165. apply_tray_exist_bits(normalised, self.BITS)
  166. assert physical[0]["tray"] == normalised[0]["tray"]
  167. def test_exists_annotation_matches_physical_slots(self):
  168. units = self._units(A2L_LITE_PHYSICAL_AMS_ID)
  169. apply_tray_exist_bits(units, self.BITS, annotate_exists=True)
  170. assert [t["exists"] for t in units[0]["tray"]] == [True, True, True, False]
  171. def test_regular_ams_unchanged(self):
  172. # id 0 still reads bits 0-3 — the fold must not touch any other unit.
  173. units = self._units(0)
  174. apply_tray_exist_bits(units, "e") # bits 1,2,3
  175. trays = units[0]["tray"]
  176. assert trays[0]["state"] == 9
  177. assert [t["tray_type"] for t in trays] == ["", "PLA", "PLA", "PLA"]
  178. class TestOutboundTranslation:
  179. def test_set_filament_setting_uses_physical_16_local_slot(self):
  180. client = _wired(_client())
  181. assert client.ams_set_filament_setting(
  182. ams_id=6,
  183. tray_id=2,
  184. tray_info_idx="GFL05",
  185. tray_type="PLA",
  186. tray_sub_brands="PLA Basic",
  187. tray_color="FF0000FF",
  188. nozzle_temp_min=190,
  189. nozzle_temp_max=230,
  190. )
  191. p = _last_payload(client)
  192. assert p["ams_id"] == A2L_LITE_PHYSICAL_AMS_ID
  193. assert p["tray_id"] == 2
  194. assert p["slot_id"] == 2
  195. def test_reset_slot_uses_physical_16_local_slot(self):
  196. client = _wired(_client())
  197. assert client.reset_ams_slot(ams_id=6, tray_id=3)
  198. p = _last_payload(client)
  199. assert p["ams_id"] == A2L_LITE_PHYSICAL_AMS_ID
  200. assert p["tray_id"] == 3
  201. assert p["slot_id"] == 3
  202. def test_cali_sel_uses_physical_global_tray(self):
  203. client = _wired(_client())
  204. assert client.extrusion_cali_sel(ams_id=6, tray_id=2, cali_idx=1, filament_id="GFL05")
  205. p = _last_payload(client)
  206. assert p["ams_id"] == A2L_LITE_PHYSICAL_AMS_ID
  207. assert p["tray_id"] == 66 # 16*4 + 2 (extrapolated physical global)
  208. assert p["slot_id"] == 2
  209. def test_cali_set_remaps_global_tray(self):
  210. client = _wired(_client())
  211. assert client.extrusion_cali_set(tray_id=26, k_value=0.02, filament_id="GFL05")
  212. p = _last_payload(client)
  213. assert p["filaments"][0]["tray_id"] == 66 # 26 (normalised) -> 66 (physical)
  214. def test_load_filament_target_and_ams(self):
  215. client = _wired(_client())
  216. assert client.ams_load_filament(tray_id=26)
  217. p = _last_payload(client)
  218. assert p["ams_id"] == A2L_LITE_PHYSICAL_AMS_ID
  219. assert p["slot_id"] == 2
  220. assert p["target"] == 66
  221. def test_unload_uses_physical_ams(self):
  222. client = _wired(_client())
  223. client.state.tray_now = 26
  224. assert client.ams_unload_filament()
  225. assert _last_payload(client)["ams_id"] == A2L_LITE_PHYSICAL_AMS_ID
  226. @pytest.mark.asyncio
  227. async def test_refresh_tray_uses_physical_16(self):
  228. client = _wired(_client())
  229. client.state.tray_now = 255 # nothing loaded, so refresh is allowed
  230. def accept(_topic, body, **_kw):
  231. sent = json.loads(body)["print"]
  232. client._process_message({"print": {**sent, "result": "SUCCESS", "reason": ""}})
  233. client._client.publish.side_effect = accept
  234. ok, _ = await client.ams_refresh_tray(ams_id=6, tray_id=2)
  235. assert ok
  236. p = _last_payload(client)
  237. assert p["ams_id"] == A2L_LITE_PHYSICAL_AMS_ID
  238. assert p["slot_id"] == 2
  239. def test_drying_uses_physical_16(self):
  240. client = _wired(_client())
  241. assert client.send_drying_command(ams_id=6, temp=55, duration=4, mode=1, filament="PLA")
  242. assert _last_payload(client)["ams_id"] == A2L_LITE_PHYSICAL_AMS_ID
  243. def test_regular_ams_command_unchanged(self):
  244. client = _wired(_client(model="X1C"))
  245. assert client.ams_set_filament_setting(
  246. ams_id=0,
  247. tray_id=2,
  248. tray_info_idx="GFL05",
  249. tray_type="PLA",
  250. tray_sub_brands="PLA Basic",
  251. tray_color="FF0000FF",
  252. nozzle_temp_min=190,
  253. nozzle_temp_max=230,
  254. )
  255. p = _last_payload(client)
  256. assert p["ams_id"] == 0
  257. assert p["tray_id"] == 2