flashforge-python-api 1.6.0__py3-none-any.whl → 3.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. flashforge/__init__.py +137 -56
  2. flashforge/_removed.py +65 -0
  3. flashforge/api/__init__.py +12 -18
  4. flashforge/api/constants/__init__.py +11 -11
  5. flashforge/api/constants/commands.py +18 -15
  6. flashforge/api/constants/endpoints.py +11 -11
  7. flashforge/api/misc/__init__.py +8 -8
  8. flashforge/api/network/__init__.py +11 -11
  9. flashforge/api/network/utils.py +71 -71
  10. flashforge/backends/__init__.py +80 -0
  11. flashforge/backends/connect.py +275 -0
  12. flashforge/backends/creator5_backend.py +191 -0
  13. flashforge/backends/dual_api_backend.py +170 -0
  14. flashforge/backends/dual_api_models.py +145 -0
  15. flashforge/backends/http_core.py +431 -0
  16. flashforge/backends/http_printer_backend.py +254 -0
  17. flashforge/backends/legacy_backend.py +436 -0
  18. flashforge/backends/printer_backend.py +217 -0
  19. flashforge/backends/types.py +125 -0
  20. flashforge/capabilities/__init__.py +98 -0
  21. flashforge/capabilities/models.py +67 -0
  22. flashforge/capabilities/palettes/__init__.py +28 -0
  23. flashforge/capabilities/palettes/ad5x_palette.py +70 -0
  24. flashforge/capabilities/palettes/creator5_palette.py +78 -0
  25. flashforge/capabilities/palettes/palette.py +54 -0
  26. flashforge/{api/controls/creator5_palette.py → capabilities/palettes/palette_snap.py} +226 -258
  27. flashforge/capabilities/printer_capabilities.py +145 -0
  28. flashforge/capabilities/registry.py +125 -0
  29. flashforge/capabilities/resolve_model.py +61 -0
  30. flashforge/discovery/__init__.py +39 -39
  31. flashforge/discovery/discovery.py +150 -77
  32. flashforge/exceptions.py +90 -51
  33. flashforge/models/__init__.py +52 -55
  34. flashforge/models/machine_info.py +540 -543
  35. flashforge/{api/controls/info.py → models/machine_info_parser.py} +35 -242
  36. flashforge/models/responses.py +119 -254
  37. flashforge/status/__init__.py +43 -0
  38. flashforge/status/printer_snapshot.py +396 -0
  39. flashforge/tcp/__init__.py +55 -54
  40. flashforge/tcp/a3_client.py +427 -427
  41. flashforge/tcp/a4_client.py +304 -304
  42. flashforge/tcp/ff_client.py +609 -590
  43. flashforge/tcp/gcode/__init__.py +15 -13
  44. flashforge/tcp/gcode/a3_gcode_controller.py +109 -109
  45. flashforge/tcp/gcode/gcode_controller.py +292 -308
  46. flashforge/tcp/gcode/gcodes.py +81 -99
  47. flashforge/tcp/gcode/legacy_gcode_controller.py +56 -0
  48. flashforge/tcp/parsers/__init__.py +27 -27
  49. flashforge/tcp/parsers/endstop_status.py +248 -248
  50. flashforge/tcp/parsers/location_info.py +83 -83
  51. flashforge/tcp/parsers/print_status.py +180 -180
  52. flashforge/tcp/parsers/printer_info.py +146 -146
  53. flashforge/tcp/parsers/temp_info.py +218 -218
  54. flashforge/tcp/parsers/thumbnail_info.py +274 -274
  55. flashforge/tcp/tcp_client.py +52 -12
  56. flashforge/threemf/__init__.py +40 -40
  57. flashforge/threemf/parser.py +560 -560
  58. flashforge/threemf/warnings.py +58 -58
  59. {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.1.0.dist-info}/METADATA +46 -47
  60. flashforge_python_api-3.1.0.dist-info/RECORD +69 -0
  61. {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.1.0.dist-info}/WHEEL +1 -1
  62. {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.1.0.dist-info}/licenses/LICENSE +21 -21
  63. flashforge/api/controls/__init__.py +0 -17
  64. flashforge/api/controls/control.py +0 -413
  65. flashforge/api/controls/files.py +0 -210
  66. flashforge/api/controls/job_control.py +0 -896
  67. flashforge/api/controls/temp_control.py +0 -309
  68. flashforge/client.py +0 -609
  69. flashforge_python_api-1.6.0.dist-info/RECORD +0 -52
  70. {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.1.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,170 @@
1
+ """
2
+ The dual-API family: Adventurer 5M, Adventurer 5M Pro and AD5X.
3
+
4
+ These printers have the HTTP API (port 8898) and the TCP command channel (port 8899). TCP
5
+ carries homing, raw G-code, extruder and bed temperatures, the filament-load flow, the
6
+ full file list. The light goes over HTTP like on every modern printer. Everything else goes over HTTP.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from typing import TYPE_CHECKING, Any
12
+
13
+ from ..api.constants.commands import Commands
14
+ from ..capabilities.models import BackendFamily, PrinterModelId
15
+ from ..models.machine_info import FFMachineInfo
16
+ from ..tcp.ff_client import FlashForgeClient as TcpClient
17
+ from ..tcp.tcp_client import FlashForgeTcpClientOptions
18
+ from .http_core import HttpCore
19
+ from .http_printer_backend import HttpPrinterBackend
20
+ from .types import FiltrationMode, LocalJobParams, UploadJobParams
21
+
22
+ if TYPE_CHECKING:
23
+ from ..api.filament.filament import Filament
24
+
25
+
26
+ def uses_new_job_format(firmware_version: str) -> bool:
27
+ """
28
+ True when 5M-family firmware takes the extra upload headers and print-start fields
29
+ (firmware 3.1.3 and newer).
30
+
31
+ The printers report the version as "v3.1.3" or "3.2.0-1.0.1". A leading "v" and the
32
+ "-..." suffix are ignored. An unreadable or empty version reads as NEW: every current
33
+ firmware is new, and the old format drops fields that new firmware needs.
34
+ """
35
+ text = firmware_version.strip().split("-")[0].strip().lstrip("vV")
36
+ parts = text.split(".")
37
+ if not text or not all(p.isdigit() for p in parts[:3]):
38
+ return True
39
+ minimum = (3, 1, 3)
40
+ for i in range(3):
41
+ current = int(parts[i]) if i < len(parts) else 0
42
+ if current > minimum[i]:
43
+ return True
44
+ if current < minimum[i]:
45
+ return False
46
+ return True
47
+
48
+
49
+ class DualApiBackend(HttpPrinterBackend):
50
+ """Base class of the dual-API family."""
51
+
52
+ family = BackendFamily.DUAL_API
53
+
54
+ def __init__(
55
+ self,
56
+ model: PrinterModelId,
57
+ http: HttpCore,
58
+ info: FFMachineInfo,
59
+ *,
60
+ tcp_port: int | None = None,
61
+ tcp_client: TcpClient | None = None,
62
+ ) -> None:
63
+ super().__init__(model, http, info)
64
+ #: The TCP command channel. It opens its socket on the first command.
65
+ self.tcp: TcpClient = tcp_client or TcpClient(
66
+ http.ip_address, FlashForgeTcpClientOptions(port=tcp_port)
67
+ )
68
+
69
+ async def initialize(self) -> bool:
70
+ """Reads ``/product``, then logs in to the TCP channel."""
71
+ if not await super().initialize():
72
+ return False
73
+ return await self.tcp.init_control()
74
+
75
+ async def dispose(self) -> None:
76
+ await super().dispose()
77
+ # dispose() logs out and stops the keep-alive.
78
+ await self.tcp.dispose()
79
+
80
+ # ---- Upload and print start (5M family format; the AD5X overrides) -------------------
81
+
82
+ def _uses_new_job_format(self) -> bool:
83
+ return uses_new_job_format(self.identity.firmware_version)
84
+
85
+ async def upload_file(self, params: UploadJobParams) -> bool:
86
+ if params.material_mappings:
87
+ self._unsupported("upload_file", self.capabilities.material_station.reason)
88
+ headers = {
89
+ "printNow": str(params.start_print).lower(),
90
+ "levelingBeforePrint": str(params.leveling_before_print).lower(),
91
+ }
92
+ if self._uses_new_job_format():
93
+ headers["flowCalibration"] = "false"
94
+ headers["useMatlStation"] = "false"
95
+ headers["gcodeToolCnt"] = "0"
96
+ headers["materialMappings"] = "W10=" # base64 of "[]"
97
+ return await self.http.upload(params.file_path, headers)
98
+
99
+ async def start_local_job(self, params: LocalJobParams) -> bool:
100
+ if params.material_mappings:
101
+ self._unsupported("start_local_job", self.capabilities.material_station.reason)
102
+ body: dict[str, Any] = {
103
+ "fileName": params.file_name,
104
+ "levelingBeforePrint": params.leveling_before_print,
105
+ }
106
+ if self._uses_new_job_format():
107
+ body["flowCalibration"] = False
108
+ body["useMatlStation"] = False
109
+ body["gcodeToolCnt"] = 0
110
+ body["materialMappings"] = []
111
+ return await self.http.send_print_command(body)
112
+
113
+ # ---- Files ---------------------------------------------------------------------------
114
+
115
+ async def get_local_files(self) -> list[str]:
116
+ """The full file list over TCP (M661)."""
117
+ return await self.tcp.get_file_list_async()
118
+
119
+ # ---- Motion and G-code ---------------------------------------------------------------
120
+
121
+ async def home_axes(self) -> bool:
122
+ return await self.tcp.home_axes()
123
+
124
+ async def home_axes_rapid(self) -> bool:
125
+ return await self.tcp.rapid_home()
126
+
127
+ async def send_gcode(self, command: str) -> str:
128
+ line = command.strip()
129
+ return await self.tcp.send_raw_cmd(line if line.startswith("~") else f"~{line}")
130
+
131
+ # ---- Temperatures (TCP M-codes) ------------------------------------------------------
132
+
133
+ async def set_extruder_temp(self, temp: int) -> bool:
134
+ return await self.tcp.set_extruder_temp(temp)
135
+
136
+ async def cancel_extruder_temp(self) -> bool:
137
+ return await self.tcp.cancel_extruder_temp()
138
+
139
+ async def set_bed_temp(self, temp: int) -> bool:
140
+ return await self.tcp.set_bed_temp(temp)
141
+
142
+ async def cancel_bed_temp(self) -> bool:
143
+ return await self.tcp.cancel_bed_temp()
144
+
145
+ # ---- Filament (TCP) ------------------------------------------------------------------
146
+
147
+ async def prepare_filament_load(self, filament: Filament) -> bool:
148
+ return await self.tcp.prepare_filament_load(filament)
149
+
150
+ async def load_filament(self) -> bool:
151
+ return await self.tcp.load_filament()
152
+
153
+ async def finish_filament_load(self) -> bool:
154
+ return await self.tcp.finish_filament_load()
155
+
156
+ # ---- Helpers for subclasses ----------------------------------------------------------
157
+
158
+ async def _send_filtration(self, mode: FiltrationMode) -> bool:
159
+ return await self.http.send_control_command(
160
+ Commands.CIRCULATION_CONTROL_CMD,
161
+ {
162
+ "internal": "open" if mode == "internal" else "close",
163
+ "external": "open" if mode == "external" else "close",
164
+ },
165
+ )
166
+
167
+ async def _send_camera_control(self, on: bool) -> bool:
168
+ return await self.http.send_control_command(
169
+ Commands.CAMERA_CONTROL_CMD, {"action": "open" if on else "close"}
170
+ )
@@ -0,0 +1,145 @@
1
+ """
2
+ The dual-API model backends: Adventurer 5M, Adventurer 5M Pro and AD5X.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from ..api.constants.commands import Commands
8
+ from ..capabilities.models import PrinterModelId
9
+ from ..exceptions import InvalidParameterError
10
+ from ..models.machine_info import FFMachineInfo
11
+ from ..tcp.ff_client import FlashForgeClient as TcpClient
12
+ from .dual_api_backend import DualApiBackend
13
+ from .http_core import HttpCore
14
+ from .http_printer_backend import encode_material_mappings, validate_material_mappings
15
+ from .types import FiltrationMode, LocalJobParams, SlotAction, UploadJobParams
16
+
17
+
18
+ class Adventurer5MBackend(DualApiBackend):
19
+ """Adventurer 5M. The shared dual-API behavior with no model extras."""
20
+
21
+ def __init__(
22
+ self,
23
+ http: HttpCore,
24
+ info: FFMachineInfo,
25
+ *,
26
+ tcp_port: int | None = None,
27
+ tcp_client: TcpClient | None = None,
28
+ ) -> None:
29
+ super().__init__(
30
+ PrinterModelId.ADVENTURER_5M,
31
+ http,
32
+ info,
33
+ tcp_port=tcp_port,
34
+ tcp_client=tcp_client,
35
+ )
36
+
37
+
38
+ class Adventurer5MProBackend(DualApiBackend):
39
+ """Adventurer 5M Pro. The only model with API-controlled air filtration."""
40
+
41
+ def __init__(
42
+ self,
43
+ http: HttpCore,
44
+ info: FFMachineInfo,
45
+ *,
46
+ tcp_port: int | None = None,
47
+ tcp_client: TcpClient | None = None,
48
+ ) -> None:
49
+ super().__init__(
50
+ PrinterModelId.ADVENTURER_5M_PRO,
51
+ http,
52
+ info,
53
+ tcp_port=tcp_port,
54
+ tcp_client=tcp_client,
55
+ )
56
+
57
+ async def set_filtration(self, mode: FiltrationMode) -> bool:
58
+ """Sets the filtration mode (``circulateCtl_cmd``): internal, external or off."""
59
+ return await self._send_filtration(mode)
60
+
61
+ async def set_camera(self, on: bool) -> bool:
62
+ """Turns the built-in camera stream on or off (``streamCtrl_cmd``)."""
63
+ return await self._send_camera_control(on)
64
+
65
+
66
+ class AD5XBackend(DualApiBackend):
67
+ """AD5X (Adventurer 5X) with the 4-slot material station."""
68
+
69
+ def __init__(
70
+ self,
71
+ http: HttpCore,
72
+ info: FFMachineInfo,
73
+ *,
74
+ tcp_port: int | None = None,
75
+ tcp_client: TcpClient | None = None,
76
+ ) -> None:
77
+ super().__init__(PrinterModelId.AD5X, http, info, tcp_port=tcp_port, tcp_client=tcp_client)
78
+
79
+ def _uses_new_job_format(self) -> bool:
80
+ # The AD5X always uses the new format; its version line is not the 5M's 3.x line.
81
+ return True
82
+
83
+ def _supports_left_cooling_fan(self) -> bool:
84
+ return True
85
+
86
+ async def upload_file(self, params: UploadJobParams) -> bool:
87
+ """
88
+ Uploads a file. With material mappings the print uses the material station; the
89
+ mappings go in a base64 ``materialMappings`` header.
90
+ """
91
+ mappings = params.material_mappings
92
+ validate_material_mappings(mappings)
93
+ return await self.http.upload(
94
+ params.file_path,
95
+ {
96
+ "printNow": str(params.start_print).lower(),
97
+ "levelingBeforePrint": str(params.leveling_before_print).lower(),
98
+ "flowCalibration": str(params.flow_calibration).lower(),
99
+ "firstLayerInspection": str(params.first_layer_inspection).lower(),
100
+ "timeLapseVideo": str(params.time_lapse_video).lower(),
101
+ "useMatlStation": str(bool(mappings)).lower(),
102
+ "gcodeToolCnt": str(len(mappings)),
103
+ "materialMappings": encode_material_mappings(mappings),
104
+ },
105
+ )
106
+
107
+ async def start_local_job(self, params: LocalJobParams) -> bool:
108
+ """
109
+ Starts a stored file. With mappings it is a multi-color print from the material
110
+ station; without mappings it is a single-color print.
111
+ """
112
+ if not params.file_name or not params.file_name.strip():
113
+ raise InvalidParameterError("file_name must not be empty.")
114
+ mappings = params.material_mappings
115
+ validate_material_mappings(mappings)
116
+ return await self.http.send_print_command(
117
+ {
118
+ "fileName": params.file_name,
119
+ "levelingBeforePrint": params.leveling_before_print,
120
+ "firstLayerInspection": False,
121
+ "flowCalibration": params.flow_calibration,
122
+ "timeLapseVideo": params.time_lapse_video,
123
+ "useMatlStation": bool(mappings),
124
+ "gcodeToolCnt": len(mappings),
125
+ "materialMappings": [m.to_wire() for m in mappings],
126
+ }
127
+ )
128
+
129
+ async def configure_slot(self, slot_id: int, material_name: str, hex_color: str) -> bool:
130
+ return await self._send_slot_config(slot_id, material_name, hex_color)
131
+
132
+ async def slot_action(self, slot_id: int, action: SlotAction) -> bool:
133
+ """
134
+ Loads, unloads or cancels on a slot (``ms_cmd``). The operation runs on the printer
135
+ after the reply; poll ``get_status().material_station.state_action`` to follow it.
136
+
137
+ Args:
138
+ slot_id: Native slot ID 1-4. The printer ignores it for CANCEL.
139
+ action: The slot action.
140
+ """
141
+ if not isinstance(slot_id, int) or not 1 <= slot_id <= 4:
142
+ raise InvalidParameterError(f"slot_id must be 1-4, got {slot_id}.")
143
+ return await self.http.send_control_command(
144
+ Commands.MATERIAL_STATION_CMD, {"slot": slot_id, "action": int(action)}
145
+ )