test_a2l_ams_lite_2619.py 11 KB

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