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.
- flashforge/__init__.py +137 -56
- flashforge/_removed.py +65 -0
- flashforge/api/__init__.py +12 -18
- flashforge/api/constants/__init__.py +11 -11
- flashforge/api/constants/commands.py +18 -15
- flashforge/api/constants/endpoints.py +11 -11
- flashforge/api/misc/__init__.py +8 -8
- flashforge/api/network/__init__.py +11 -11
- flashforge/api/network/utils.py +71 -71
- flashforge/backends/__init__.py +80 -0
- flashforge/backends/connect.py +275 -0
- flashforge/backends/creator5_backend.py +191 -0
- flashforge/backends/dual_api_backend.py +170 -0
- flashforge/backends/dual_api_models.py +145 -0
- flashforge/backends/http_core.py +431 -0
- flashforge/backends/http_printer_backend.py +254 -0
- flashforge/backends/legacy_backend.py +436 -0
- flashforge/backends/printer_backend.py +217 -0
- flashforge/backends/types.py +125 -0
- flashforge/capabilities/__init__.py +98 -0
- flashforge/capabilities/models.py +67 -0
- flashforge/capabilities/palettes/__init__.py +28 -0
- flashforge/capabilities/palettes/ad5x_palette.py +70 -0
- flashforge/capabilities/palettes/creator5_palette.py +78 -0
- flashforge/capabilities/palettes/palette.py +54 -0
- flashforge/{api/controls/creator5_palette.py → capabilities/palettes/palette_snap.py} +226 -258
- flashforge/capabilities/printer_capabilities.py +145 -0
- flashforge/capabilities/registry.py +125 -0
- flashforge/capabilities/resolve_model.py +61 -0
- flashforge/discovery/__init__.py +39 -39
- flashforge/discovery/discovery.py +150 -77
- flashforge/exceptions.py +90 -51
- flashforge/models/__init__.py +52 -55
- flashforge/models/machine_info.py +540 -543
- flashforge/{api/controls/info.py → models/machine_info_parser.py} +35 -242
- flashforge/models/responses.py +119 -254
- flashforge/status/__init__.py +43 -0
- flashforge/status/printer_snapshot.py +396 -0
- flashforge/tcp/__init__.py +55 -54
- flashforge/tcp/a3_client.py +427 -427
- flashforge/tcp/a4_client.py +304 -304
- flashforge/tcp/ff_client.py +609 -590
- flashforge/tcp/gcode/__init__.py +15 -13
- flashforge/tcp/gcode/a3_gcode_controller.py +109 -109
- flashforge/tcp/gcode/gcode_controller.py +292 -308
- flashforge/tcp/gcode/gcodes.py +81 -99
- flashforge/tcp/gcode/legacy_gcode_controller.py +56 -0
- flashforge/tcp/parsers/__init__.py +27 -27
- flashforge/tcp/parsers/endstop_status.py +248 -248
- flashforge/tcp/parsers/location_info.py +83 -83
- flashforge/tcp/parsers/print_status.py +180 -180
- flashforge/tcp/parsers/printer_info.py +146 -146
- flashforge/tcp/parsers/temp_info.py +218 -218
- flashforge/tcp/parsers/thumbnail_info.py +274 -274
- flashforge/tcp/tcp_client.py +52 -12
- flashforge/threemf/__init__.py +40 -40
- flashforge/threemf/parser.py +560 -560
- flashforge/threemf/warnings.py +58 -58
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.1.0.dist-info}/METADATA +46 -47
- flashforge_python_api-3.1.0.dist-info/RECORD +69 -0
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.1.0.dist-info}/WHEEL +1 -1
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.1.0.dist-info}/licenses/LICENSE +21 -21
- flashforge/api/controls/__init__.py +0 -17
- flashforge/api/controls/control.py +0 -413
- flashforge/api/controls/files.py +0 -210
- flashforge/api/controls/job_control.py +0 -896
- flashforge/api/controls/temp_control.py +0 -309
- flashforge/client.py +0 -609
- flashforge_python_api-1.6.0.dist-info/RECORD +0 -52
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.1.0.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The legacy family: Adventurer 3, Adventurer 4 and other TCP-only printers.
|
|
3
|
+
|
|
4
|
+
These printers have no HTTP API. Everything goes over the TCP G-code channel (port 8899).
|
|
5
|
+
Each model uses its own TCP client: :class:`FlashForgeA3Client`,
|
|
6
|
+
:class:`FlashForgeA4Client`, or the generic :class:`FlashForgeClient` for an unknown
|
|
7
|
+
legacy printer (or a modern printer in force-legacy mode).
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import math
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from pathlib import PurePath
|
|
15
|
+
from typing import Protocol
|
|
16
|
+
|
|
17
|
+
from ..capabilities.models import BackendFamily, PrinterModelId
|
|
18
|
+
from ..capabilities.printer_capabilities import resolve_capabilities
|
|
19
|
+
from ..exceptions import InvalidParameterError
|
|
20
|
+
from ..status.printer_snapshot import LegacyStatusInput, PrinterSnapshot, snapshot_from_legacy
|
|
21
|
+
from ..tcp.a3_client import FlashForgeA3Client
|
|
22
|
+
from ..tcp.a4_client import FlashForgeA4Client
|
|
23
|
+
from ..tcp.ff_client import FlashForgeClient
|
|
24
|
+
from ..tcp.parsers.endstop_status import EndstopStatus
|
|
25
|
+
from ..tcp.parsers.print_status import PrintStatus
|
|
26
|
+
from ..tcp.parsers.temp_info import TempInfo
|
|
27
|
+
from ..tcp.tcp_client import FlashForgeTcpClientOptions
|
|
28
|
+
from .printer_backend import PrinterBackend, PrinterIdentity
|
|
29
|
+
from .types import LocalJobParams, UploadJobParams
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class LegacyPrinterInfo:
|
|
34
|
+
"""Printer facts read over M115."""
|
|
35
|
+
|
|
36
|
+
#: Firmware machine type, for example "Adventurer 4". Safe to match on.
|
|
37
|
+
machine_type: str
|
|
38
|
+
#: User-set printer name. Display only.
|
|
39
|
+
name: str
|
|
40
|
+
firmware_version: str
|
|
41
|
+
serial_number: str
|
|
42
|
+
mac_address: str
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class LegacyTransport(Protocol):
|
|
46
|
+
"""The operations a legacy backend needs. One adapter per TCP client type."""
|
|
47
|
+
|
|
48
|
+
async def init_control(self) -> bool: ...
|
|
49
|
+
async def dispose(self) -> None: ...
|
|
50
|
+
async def get_info(self) -> LegacyPrinterInfo | None: ...
|
|
51
|
+
async def get_temp_info(self) -> TempInfo | None: ...
|
|
52
|
+
async def get_endstop(self) -> EndstopStatus | None: ...
|
|
53
|
+
async def get_print_status(self) -> PrintStatus | None: ...
|
|
54
|
+
async def list_files(self) -> list[str]: ...
|
|
55
|
+
async def get_thumbnail(self, file_name: str) -> bytes | None: ...
|
|
56
|
+
async def start_job(self, file_name: str) -> bool: ...
|
|
57
|
+
async def pause_job(self) -> bool: ...
|
|
58
|
+
async def resume_job(self) -> bool: ...
|
|
59
|
+
async def stop_job(self) -> bool: ...
|
|
60
|
+
async def home(self) -> bool: ...
|
|
61
|
+
async def set_extruder_temp(self, temp: int) -> bool: ...
|
|
62
|
+
async def set_bed_temp(self, temp: int) -> bool: ...
|
|
63
|
+
async def set_led(self, on: bool) -> bool: ...
|
|
64
|
+
async def send_raw(self, command: str) -> str: ...
|
|
65
|
+
async def upload(self, file_path: str) -> bool: ...
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class GenericLegacyTransport:
|
|
69
|
+
"""Adapter for the generic :class:`FlashForgeClient`."""
|
|
70
|
+
|
|
71
|
+
def __init__(self, client: FlashForgeClient) -> None:
|
|
72
|
+
self.client = client
|
|
73
|
+
|
|
74
|
+
async def init_control(self) -> bool:
|
|
75
|
+
return await self.client.init_control()
|
|
76
|
+
|
|
77
|
+
async def dispose(self) -> None:
|
|
78
|
+
await self.client.dispose()
|
|
79
|
+
|
|
80
|
+
async def get_info(self) -> LegacyPrinterInfo | None:
|
|
81
|
+
info = await self.client.get_printer_info()
|
|
82
|
+
if info is None:
|
|
83
|
+
return None
|
|
84
|
+
return LegacyPrinterInfo(
|
|
85
|
+
info.type_name, info.name, info.firmware_version, info.serial_number, info.mac_address
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
async def get_temp_info(self) -> TempInfo | None:
|
|
89
|
+
return await self.client.get_temp_info()
|
|
90
|
+
|
|
91
|
+
async def get_endstop(self) -> EndstopStatus | None:
|
|
92
|
+
return await self.client.get_endstop_status()
|
|
93
|
+
|
|
94
|
+
async def get_print_status(self) -> PrintStatus | None:
|
|
95
|
+
return await self.client.get_print_status()
|
|
96
|
+
|
|
97
|
+
async def list_files(self) -> list[str]:
|
|
98
|
+
return await self.client.get_file_list_async()
|
|
99
|
+
|
|
100
|
+
async def get_thumbnail(self, file_name: str) -> bytes | None:
|
|
101
|
+
thumb = await self.client.get_thumbnail(file_name)
|
|
102
|
+
return thumb.get_image_bytes() if thumb else None
|
|
103
|
+
|
|
104
|
+
async def start_job(self, file_name: str) -> bool:
|
|
105
|
+
return await self.client.start_job(file_name)
|
|
106
|
+
|
|
107
|
+
async def pause_job(self) -> bool:
|
|
108
|
+
return await self.client.pause_job()
|
|
109
|
+
|
|
110
|
+
async def resume_job(self) -> bool:
|
|
111
|
+
return await self.client.resume_job()
|
|
112
|
+
|
|
113
|
+
async def stop_job(self) -> bool:
|
|
114
|
+
return await self.client.stop_job()
|
|
115
|
+
|
|
116
|
+
async def home(self) -> bool:
|
|
117
|
+
return await self.client.home_axes()
|
|
118
|
+
|
|
119
|
+
async def set_extruder_temp(self, temp: int) -> bool:
|
|
120
|
+
if temp > 0:
|
|
121
|
+
return await self.client.set_extruder_temp(temp)
|
|
122
|
+
return await self.client.cancel_extruder_temp()
|
|
123
|
+
|
|
124
|
+
async def set_bed_temp(self, temp: int) -> bool:
|
|
125
|
+
if temp > 0:
|
|
126
|
+
return await self.client.set_bed_temp(temp)
|
|
127
|
+
return await self.client.cancel_bed_temp()
|
|
128
|
+
|
|
129
|
+
async def set_led(self, on: bool) -> bool:
|
|
130
|
+
return await (self.client.led_on() if on else self.client.led_off())
|
|
131
|
+
|
|
132
|
+
async def send_raw(self, command: str) -> str:
|
|
133
|
+
return await self.client.send_raw_cmd(command)
|
|
134
|
+
|
|
135
|
+
async def upload(self, file_path: str) -> bool:
|
|
136
|
+
return await self.client.upload_file(file_path)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class A4Transport:
|
|
140
|
+
"""Adapter for :class:`FlashForgeA4Client`."""
|
|
141
|
+
|
|
142
|
+
def __init__(self, client: FlashForgeA4Client) -> None:
|
|
143
|
+
self.client = client
|
|
144
|
+
|
|
145
|
+
async def init_control(self) -> bool:
|
|
146
|
+
return await self.client.init_control()
|
|
147
|
+
|
|
148
|
+
async def dispose(self) -> None:
|
|
149
|
+
await self.client.dispose()
|
|
150
|
+
|
|
151
|
+
async def get_info(self) -> LegacyPrinterInfo | None:
|
|
152
|
+
info = await self.client.get_printer_info()
|
|
153
|
+
if info is None:
|
|
154
|
+
return None
|
|
155
|
+
return LegacyPrinterInfo(
|
|
156
|
+
info.machine_type,
|
|
157
|
+
info.machine_name,
|
|
158
|
+
info.firmware,
|
|
159
|
+
info.serial_number or "",
|
|
160
|
+
info.mac_address,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
async def get_temp_info(self) -> TempInfo | None:
|
|
164
|
+
return await self.client.get_temp_info()
|
|
165
|
+
|
|
166
|
+
async def get_endstop(self) -> EndstopStatus | None:
|
|
167
|
+
return await self.client.get_endstop_status()
|
|
168
|
+
|
|
169
|
+
async def get_print_status(self) -> PrintStatus | None:
|
|
170
|
+
return await self.client.get_print_status()
|
|
171
|
+
|
|
172
|
+
async def list_files(self) -> list[str]:
|
|
173
|
+
return [entry.name for entry in await self.client.list_files()]
|
|
174
|
+
|
|
175
|
+
async def get_thumbnail(self, file_name: str) -> bytes | None:
|
|
176
|
+
thumb = await self.client.get_thumbnail(file_name)
|
|
177
|
+
return thumb.get_image_bytes() if thumb else None
|
|
178
|
+
|
|
179
|
+
async def start_job(self, file_name: str) -> bool:
|
|
180
|
+
return await self.client.start_job(file_name)
|
|
181
|
+
|
|
182
|
+
async def pause_job(self) -> bool:
|
|
183
|
+
return await self.client.pause_job()
|
|
184
|
+
|
|
185
|
+
async def resume_job(self) -> bool:
|
|
186
|
+
return await self.client.resume_job()
|
|
187
|
+
|
|
188
|
+
async def stop_job(self) -> bool:
|
|
189
|
+
return await self.client.stop_job()
|
|
190
|
+
|
|
191
|
+
async def home(self) -> bool:
|
|
192
|
+
return await self.client.home_axes()
|
|
193
|
+
|
|
194
|
+
async def set_extruder_temp(self, temp: int) -> bool:
|
|
195
|
+
if temp > 0:
|
|
196
|
+
return await self.client.set_extruder_temp(temp)
|
|
197
|
+
return await self.client.cancel_extruder_temp()
|
|
198
|
+
|
|
199
|
+
async def set_bed_temp(self, temp: int) -> bool:
|
|
200
|
+
if temp > 0:
|
|
201
|
+
return await self.client.set_bed_temp(temp)
|
|
202
|
+
return await self.client.cancel_bed_temp()
|
|
203
|
+
|
|
204
|
+
async def set_led(self, on: bool) -> bool:
|
|
205
|
+
return await (self.client.led_on() if on else self.client.led_off())
|
|
206
|
+
|
|
207
|
+
async def send_raw(self, command: str) -> str:
|
|
208
|
+
return await self.client.send_raw_cmd(command)
|
|
209
|
+
|
|
210
|
+
async def upload(self, file_path: str) -> bool:
|
|
211
|
+
return await self.client.upload_file(file_path)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
class A3Transport:
|
|
215
|
+
"""Adapter for :class:`FlashForgeA3Client`."""
|
|
216
|
+
|
|
217
|
+
def __init__(self, client: FlashForgeA3Client) -> None:
|
|
218
|
+
self.client = client
|
|
219
|
+
|
|
220
|
+
async def init_control(self) -> bool:
|
|
221
|
+
return await self.client.init_control()
|
|
222
|
+
|
|
223
|
+
async def dispose(self) -> None:
|
|
224
|
+
await self.client.dispose()
|
|
225
|
+
|
|
226
|
+
async def get_info(self) -> LegacyPrinterInfo | None:
|
|
227
|
+
info = await self.client.get_printer_info()
|
|
228
|
+
if info is None:
|
|
229
|
+
return None
|
|
230
|
+
return LegacyPrinterInfo(
|
|
231
|
+
info.machine_type, info.machine_name, info.firmware, info.serial_number, ""
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
async def get_temp_info(self) -> TempInfo | None:
|
|
235
|
+
return await self.client.get_temp_info()
|
|
236
|
+
|
|
237
|
+
async def get_endstop(self) -> EndstopStatus | None:
|
|
238
|
+
return await self.client.get_endstop_status()
|
|
239
|
+
|
|
240
|
+
async def get_print_status(self) -> PrintStatus | None:
|
|
241
|
+
return await self.client.get_print_status()
|
|
242
|
+
|
|
243
|
+
async def list_files(self) -> list[str]:
|
|
244
|
+
return [entry.name for entry in await self.client.list_files()]
|
|
245
|
+
|
|
246
|
+
async def get_thumbnail(self, file_name: str) -> bytes | None:
|
|
247
|
+
thumb = await self.client.get_thumbnail(file_name)
|
|
248
|
+
return thumb.data if thumb else None
|
|
249
|
+
|
|
250
|
+
async def start_job(self, file_name: str) -> bool:
|
|
251
|
+
return await self.client.select_file(file_name) and await self.client.start_print()
|
|
252
|
+
|
|
253
|
+
async def pause_job(self) -> bool:
|
|
254
|
+
return await self.client.pause_print()
|
|
255
|
+
|
|
256
|
+
async def resume_job(self) -> bool:
|
|
257
|
+
return await self.client.start_print()
|
|
258
|
+
|
|
259
|
+
async def stop_job(self) -> bool:
|
|
260
|
+
return await self.client.stop_print()
|
|
261
|
+
|
|
262
|
+
async def home(self) -> bool:
|
|
263
|
+
return await self.client.home()
|
|
264
|
+
|
|
265
|
+
async def set_extruder_temp(self, temp: int) -> bool:
|
|
266
|
+
return await self.client.send_cmd_ok(f"~M104 S{max(0, round(temp))}")
|
|
267
|
+
|
|
268
|
+
async def set_bed_temp(self, temp: int) -> bool:
|
|
269
|
+
return await self.client.send_cmd_ok(f"~M140 S{max(0, round(temp))}")
|
|
270
|
+
|
|
271
|
+
async def set_led(self, on: bool) -> bool:
|
|
272
|
+
return await self.client.led_control("1" if on else "0")
|
|
273
|
+
|
|
274
|
+
async def send_raw(self, command: str) -> str:
|
|
275
|
+
return (await self.client.send_command_async(command)) or ""
|
|
276
|
+
|
|
277
|
+
async def upload(self, file_path: str) -> bool:
|
|
278
|
+
return await self.client.upload_file(file_path)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def create_legacy_transport(
|
|
282
|
+
model: PrinterModelId, ip_address: str, tcp_port: int | None = None
|
|
283
|
+
) -> LegacyTransport:
|
|
284
|
+
"""Creates the TCP transport that fits a legacy model."""
|
|
285
|
+
options = FlashForgeTcpClientOptions(port=tcp_port)
|
|
286
|
+
if model == PrinterModelId.ADVENTURER_3:
|
|
287
|
+
return A3Transport(FlashForgeA3Client(ip_address, options))
|
|
288
|
+
if model == PrinterModelId.ADVENTURER_4:
|
|
289
|
+
return A4Transport(FlashForgeA4Client(ip_address, options))
|
|
290
|
+
return GenericLegacyTransport(FlashForgeClient(ip_address, options))
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _to_int(text: str) -> int | None:
|
|
294
|
+
try:
|
|
295
|
+
return int(text)
|
|
296
|
+
except (TypeError, ValueError):
|
|
297
|
+
return None
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
class LegacyBackend(PrinterBackend):
|
|
301
|
+
"""Backend for TCP-only printers."""
|
|
302
|
+
|
|
303
|
+
family = BackendFamily.LEGACY
|
|
304
|
+
|
|
305
|
+
def __init__(
|
|
306
|
+
self,
|
|
307
|
+
model: PrinterModelId,
|
|
308
|
+
ip_address: str,
|
|
309
|
+
transport: LegacyTransport,
|
|
310
|
+
info: LegacyPrinterInfo | None = None,
|
|
311
|
+
) -> None:
|
|
312
|
+
if model not in (
|
|
313
|
+
PrinterModelId.ADVENTURER_3,
|
|
314
|
+
PrinterModelId.ADVENTURER_4,
|
|
315
|
+
PrinterModelId.GENERIC_LEGACY,
|
|
316
|
+
):
|
|
317
|
+
raise ValueError(f"{model} is not a legacy model")
|
|
318
|
+
self.transport = transport
|
|
319
|
+
super().__init__(
|
|
320
|
+
model,
|
|
321
|
+
resolve_capabilities(model),
|
|
322
|
+
PrinterIdentity(
|
|
323
|
+
ip_address=ip_address,
|
|
324
|
+
serial_number=info.serial_number if info else "",
|
|
325
|
+
name=info.name if info else "",
|
|
326
|
+
firmware_version=info.firmware_version if info else "",
|
|
327
|
+
mac_address=info.mac_address if info else "",
|
|
328
|
+
),
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
async def initialize(self) -> bool:
|
|
332
|
+
"""
|
|
333
|
+
Logs in to the TCP channel and reads M115.
|
|
334
|
+
|
|
335
|
+
Returns:
|
|
336
|
+
False when the login fails.
|
|
337
|
+
"""
|
|
338
|
+
if not await self.transport.init_control():
|
|
339
|
+
return False
|
|
340
|
+
info = await self.transport.get_info()
|
|
341
|
+
if info is not None:
|
|
342
|
+
self.identity = self.identity.with_updates(
|
|
343
|
+
serial_number=info.serial_number or self.identity.serial_number,
|
|
344
|
+
name=info.name or self.identity.name,
|
|
345
|
+
firmware_version=info.firmware_version,
|
|
346
|
+
mac_address=info.mac_address or self.identity.mac_address,
|
|
347
|
+
)
|
|
348
|
+
return True
|
|
349
|
+
|
|
350
|
+
async def dispose(self) -> None:
|
|
351
|
+
await self.transport.dispose()
|
|
352
|
+
|
|
353
|
+
async def get_status(self) -> PrinterSnapshot:
|
|
354
|
+
endstop = await self.transport.get_endstop()
|
|
355
|
+
temps = await self.transport.get_temp_info()
|
|
356
|
+
printing = endstop is not None and (endstop.is_printing() or endstop.is_paused())
|
|
357
|
+
print_status = await self.transport.get_print_status() if printing else None
|
|
358
|
+
percent: float | None = None
|
|
359
|
+
current_layer = total_layers = None
|
|
360
|
+
if print_status is not None:
|
|
361
|
+
value = print_status.get_print_percent()
|
|
362
|
+
percent = value if math.isfinite(value) else None
|
|
363
|
+
current_layer = _to_int(print_status.layer_current)
|
|
364
|
+
total_layers = _to_int(print_status.layer_total)
|
|
365
|
+
extruder = temps.get_extruder_temp() if temps else None
|
|
366
|
+
bed = temps.get_bed_temp() if temps else None
|
|
367
|
+
return snapshot_from_legacy(
|
|
368
|
+
LegacyStatusInput(
|
|
369
|
+
machine_status=endstop.machine_status if endstop else None,
|
|
370
|
+
move_mode=endstop.move_mode if endstop else None,
|
|
371
|
+
extruder=(extruder.get_current(), extruder.get_set()) if extruder else None,
|
|
372
|
+
bed=(bed.get_current(), bed.get_set()) if bed else None,
|
|
373
|
+
current_file=(endstop.current_file or "") if endstop else "",
|
|
374
|
+
percent=percent,
|
|
375
|
+
current_layer=current_layer,
|
|
376
|
+
total_layers=total_layers,
|
|
377
|
+
led_on=endstop.led_enabled if endstop else None,
|
|
378
|
+
)
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
async def pause_job(self) -> bool:
|
|
382
|
+
return await self.transport.pause_job()
|
|
383
|
+
|
|
384
|
+
async def resume_job(self) -> bool:
|
|
385
|
+
return await self.transport.resume_job()
|
|
386
|
+
|
|
387
|
+
async def cancel_job(self) -> bool:
|
|
388
|
+
return await self.transport.stop_job()
|
|
389
|
+
|
|
390
|
+
async def upload_file(self, params: UploadJobParams) -> bool:
|
|
391
|
+
"""Uploads over TCP (M28/M29). Starts the print when ``start_print`` is True."""
|
|
392
|
+
if params.material_mappings:
|
|
393
|
+
self._unsupported("upload_file", self.capabilities.material_station.reason)
|
|
394
|
+
if not await self.transport.upload(params.file_path):
|
|
395
|
+
return False
|
|
396
|
+
if not params.start_print:
|
|
397
|
+
return True
|
|
398
|
+
return await self.transport.start_job(PurePath(params.file_path.replace("\\", "/")).name)
|
|
399
|
+
|
|
400
|
+
async def start_local_job(self, params: LocalJobParams) -> bool:
|
|
401
|
+
if not params.file_name or not params.file_name.strip():
|
|
402
|
+
raise InvalidParameterError("file_name must not be empty.")
|
|
403
|
+
if params.material_mappings:
|
|
404
|
+
self._unsupported("start_local_job", self.capabilities.material_station.reason)
|
|
405
|
+
return await self.transport.start_job(params.file_name)
|
|
406
|
+
|
|
407
|
+
async def get_local_files(self) -> list[str]:
|
|
408
|
+
return await self.transport.list_files()
|
|
409
|
+
|
|
410
|
+
async def get_thumbnail(self, file_name: str) -> bytes | None:
|
|
411
|
+
return await self.transport.get_thumbnail(file_name)
|
|
412
|
+
|
|
413
|
+
async def home_axes(self) -> bool:
|
|
414
|
+
return await self.transport.home()
|
|
415
|
+
|
|
416
|
+
async def home_axes_rapid(self) -> bool:
|
|
417
|
+
return await self.transport.home()
|
|
418
|
+
|
|
419
|
+
async def send_gcode(self, command: str) -> str:
|
|
420
|
+
line = command.strip()
|
|
421
|
+
return await self.transport.send_raw(line if line.startswith("~") else f"~{line}")
|
|
422
|
+
|
|
423
|
+
async def set_extruder_temp(self, temp: int) -> bool:
|
|
424
|
+
return await self.transport.set_extruder_temp(temp)
|
|
425
|
+
|
|
426
|
+
async def cancel_extruder_temp(self) -> bool:
|
|
427
|
+
return await self.transport.set_extruder_temp(0)
|
|
428
|
+
|
|
429
|
+
async def set_bed_temp(self, temp: int) -> bool:
|
|
430
|
+
return await self.transport.set_bed_temp(temp)
|
|
431
|
+
|
|
432
|
+
async def cancel_bed_temp(self) -> bool:
|
|
433
|
+
return await self.transport.set_bed_temp(0)
|
|
434
|
+
|
|
435
|
+
async def set_led(self, on: bool) -> bool:
|
|
436
|
+
return await self.transport.set_led(on)
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The abstract printer backend. Every family backend extends it.
|
|
3
|
+
|
|
4
|
+
Each operation has a default that raises :class:`NotSupportedError`. A family overrides
|
|
5
|
+
only the operations its printers support, so a caller never gets a silent ``False`` for
|
|
6
|
+
an operation the printer cannot do. Read ``capabilities`` to decide what to offer.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from abc import ABC, abstractmethod
|
|
12
|
+
from dataclasses import dataclass, replace
|
|
13
|
+
from typing import TYPE_CHECKING, NoReturn
|
|
14
|
+
|
|
15
|
+
from ..capabilities.models import BackendFamily, PrinterModelId
|
|
16
|
+
from ..capabilities.printer_capabilities import PrinterCapabilities
|
|
17
|
+
from ..exceptions import NotSupportedError
|
|
18
|
+
from ..models.machine_info import FFGcodeFileEntry, FFMachineInfo
|
|
19
|
+
from ..status.printer_snapshot import PrinterSnapshot
|
|
20
|
+
from .types import FiltrationMode, LocalJobParams, PrintOverrides, SlotAction, UploadJobParams
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from ..api.filament.filament import Filament
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class PrinterIdentity:
|
|
28
|
+
"""Facts about the connected printer that do not change while connected."""
|
|
29
|
+
|
|
30
|
+
ip_address: str
|
|
31
|
+
#: Serial number. Empty for legacy printers that do not report it.
|
|
32
|
+
serial_number: str
|
|
33
|
+
#: The printer name as the user set it. Display only. Never use it to pick behavior.
|
|
34
|
+
name: str
|
|
35
|
+
#: Firmware version string, empty when unknown.
|
|
36
|
+
firmware_version: str
|
|
37
|
+
#: MAC address, empty when unknown.
|
|
38
|
+
mac_address: str
|
|
39
|
+
|
|
40
|
+
def with_updates(self, **changes: str) -> PrinterIdentity:
|
|
41
|
+
"""A copy with some fields changed."""
|
|
42
|
+
return replace(self, **changes)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class PrinterBackend(ABC):
|
|
46
|
+
"""Base class of every printer backend."""
|
|
47
|
+
|
|
48
|
+
family: BackendFamily
|
|
49
|
+
|
|
50
|
+
def __init__(
|
|
51
|
+
self,
|
|
52
|
+
model: PrinterModelId,
|
|
53
|
+
capabilities: PrinterCapabilities,
|
|
54
|
+
identity: PrinterIdentity,
|
|
55
|
+
) -> None:
|
|
56
|
+
self.model = PrinterModelId(model)
|
|
57
|
+
self.capabilities = capabilities
|
|
58
|
+
self.identity = identity
|
|
59
|
+
|
|
60
|
+
def _unsupported(self, operation: str, reason: str | None = None) -> NoReturn:
|
|
61
|
+
raise NotSupportedError(
|
|
62
|
+
operation,
|
|
63
|
+
self.model.value,
|
|
64
|
+
reason or f"The {self.capabilities.display_name} does not support this operation.",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
async def __aenter__(self) -> PrinterBackend:
|
|
68
|
+
return self
|
|
69
|
+
|
|
70
|
+
async def __aexit__(self, *_exc: object) -> None:
|
|
71
|
+
await self.dispose()
|
|
72
|
+
|
|
73
|
+
# ---- Lifecycle -----------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
@abstractmethod
|
|
76
|
+
async def dispose(self) -> None:
|
|
77
|
+
"""Closes every connection to the printer. Safe to call more than once."""
|
|
78
|
+
|
|
79
|
+
# ---- Status --------------------------------------------------------------------------
|
|
80
|
+
|
|
81
|
+
@abstractmethod
|
|
82
|
+
async def get_status(self) -> PrinterSnapshot:
|
|
83
|
+
"""Reads the printer and returns a normalized status snapshot."""
|
|
84
|
+
|
|
85
|
+
async def get_machine_info(self) -> FFMachineInfo:
|
|
86
|
+
"""The parsed ``/detail`` data. Modern printers only."""
|
|
87
|
+
self._unsupported("get_machine_info", "This printer has no HTTP API.")
|
|
88
|
+
|
|
89
|
+
# ---- Jobs ----------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
@abstractmethod
|
|
92
|
+
async def pause_job(self) -> bool: ...
|
|
93
|
+
|
|
94
|
+
@abstractmethod
|
|
95
|
+
async def resume_job(self) -> bool: ...
|
|
96
|
+
|
|
97
|
+
@abstractmethod
|
|
98
|
+
async def cancel_job(self) -> bool: ...
|
|
99
|
+
|
|
100
|
+
async def clear_platform(self) -> bool:
|
|
101
|
+
"""Tells the printer the build plate is clear after a print. Modern printers only."""
|
|
102
|
+
self._unsupported("clear_platform")
|
|
103
|
+
|
|
104
|
+
async def upload_file(self, params: UploadJobParams) -> bool:
|
|
105
|
+
"""Uploads a file and, as an option, starts it."""
|
|
106
|
+
self._unsupported("upload_file")
|
|
107
|
+
|
|
108
|
+
@abstractmethod
|
|
109
|
+
async def start_local_job(self, params: LocalJobParams) -> bool:
|
|
110
|
+
"""Starts a print of a file that is already on the printer."""
|
|
111
|
+
|
|
112
|
+
@abstractmethod
|
|
113
|
+
async def get_local_files(self) -> list[str]:
|
|
114
|
+
"""Names of the files stored on the printer."""
|
|
115
|
+
|
|
116
|
+
async def get_recent_files(self) -> list[FFGcodeFileEntry]:
|
|
117
|
+
"""The 10 most recent files. Only the AD5X sends per-tool material data."""
|
|
118
|
+
self._unsupported("get_recent_files")
|
|
119
|
+
|
|
120
|
+
@abstractmethod
|
|
121
|
+
async def get_thumbnail(self, file_name: str) -> bytes | None:
|
|
122
|
+
"""Thumbnail of a stored file as PNG bytes, or None when there is none."""
|
|
123
|
+
|
|
124
|
+
# ---- Motion and G-code ---------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
async def home_axes(self) -> bool:
|
|
127
|
+
self._unsupported("home_axes", self.capabilities.gcode.reason)
|
|
128
|
+
|
|
129
|
+
async def home_axes_rapid(self) -> bool:
|
|
130
|
+
self._unsupported("home_axes_rapid", self.capabilities.gcode.reason)
|
|
131
|
+
|
|
132
|
+
async def send_gcode(self, command: str) -> str:
|
|
133
|
+
"""Sends one G-code line over TCP and returns the raw reply. ``~`` is added if missing."""
|
|
134
|
+
self._unsupported("send_gcode", self.capabilities.gcode.reason)
|
|
135
|
+
|
|
136
|
+
# ---- Temperatures --------------------------------------------------------------------
|
|
137
|
+
|
|
138
|
+
@abstractmethod
|
|
139
|
+
async def set_extruder_temp(self, temp: int) -> bool:
|
|
140
|
+
"""Sets the primary extruder (tool 0)."""
|
|
141
|
+
|
|
142
|
+
@abstractmethod
|
|
143
|
+
async def cancel_extruder_temp(self) -> bool: ...
|
|
144
|
+
|
|
145
|
+
@abstractmethod
|
|
146
|
+
async def set_bed_temp(self, temp: int) -> bool: ...
|
|
147
|
+
|
|
148
|
+
@abstractmethod
|
|
149
|
+
async def cancel_bed_temp(self) -> bool: ...
|
|
150
|
+
|
|
151
|
+
async def set_tool_temp(self, tool_index: int, temp: int) -> bool:
|
|
152
|
+
"""Sets one tool (0-based). Creator 5 series only."""
|
|
153
|
+
self._unsupported("set_tool_temp", "This printer has one tool head.")
|
|
154
|
+
|
|
155
|
+
async def set_tool_temps(self, temps: list[int]) -> bool:
|
|
156
|
+
"""Sets every tool at once, ordered T0..Tn. Creator 5 series only."""
|
|
157
|
+
self._unsupported("set_tool_temps", "This printer has one tool head.")
|
|
158
|
+
|
|
159
|
+
async def cancel_tool_temp(self, tool_index: int) -> bool:
|
|
160
|
+
"""Turns one tool heater off (0-based). Creator 5 series only."""
|
|
161
|
+
self._unsupported("cancel_tool_temp", "This printer has one tool head.")
|
|
162
|
+
|
|
163
|
+
async def set_chamber_temp(self, temp: int) -> bool:
|
|
164
|
+
"""Sets the chamber heater. Creator 5 Pro only. The printer applies no upper limit."""
|
|
165
|
+
self._unsupported("set_chamber_temp", self.capabilities.chamber.reason)
|
|
166
|
+
|
|
167
|
+
async def cancel_chamber_temp(self) -> bool:
|
|
168
|
+
self._unsupported("cancel_chamber_temp", self.capabilities.chamber.reason)
|
|
169
|
+
|
|
170
|
+
# ---- Light, camera, air --------------------------------------------------------------
|
|
171
|
+
|
|
172
|
+
async def set_led(self, on: bool) -> bool:
|
|
173
|
+
self._unsupported("set_led", self.capabilities.led.reason)
|
|
174
|
+
|
|
175
|
+
async def set_camera(self, on: bool) -> bool:
|
|
176
|
+
"""Turns the built-in camera stream on or off. Adventurer 5M Pro only."""
|
|
177
|
+
self._unsupported(
|
|
178
|
+
"set_camera", "Only the Adventurer 5M Pro can turn its camera on or off over the API."
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
async def detect_camera_stream(self, timeout_ms: int = 3000) -> str:
|
|
182
|
+
"""Probes the built-in camera stream. Returns the URL or an empty string."""
|
|
183
|
+
return ""
|
|
184
|
+
|
|
185
|
+
async def set_filtration(self, mode: FiltrationMode) -> bool:
|
|
186
|
+
"""Sets the air filtration mode. Adventurer 5M Pro only."""
|
|
187
|
+
self._unsupported("set_filtration", self.capabilities.filtration.reason)
|
|
188
|
+
|
|
189
|
+
# ---- Active print --------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
async def set_print_overrides(self, overrides: PrintOverrides) -> bool:
|
|
192
|
+
"""Changes speed, Z offset or fans during a print. Needs an active print."""
|
|
193
|
+
self._unsupported("set_print_overrides")
|
|
194
|
+
|
|
195
|
+
# ---- Filament ------------------------------------------------------------------------
|
|
196
|
+
|
|
197
|
+
async def prepare_filament_load(self, filament: Filament) -> bool:
|
|
198
|
+
self._unsupported("prepare_filament_load", self.capabilities.gcode.reason)
|
|
199
|
+
|
|
200
|
+
async def load_filament(self) -> bool:
|
|
201
|
+
self._unsupported("load_filament", self.capabilities.gcode.reason)
|
|
202
|
+
|
|
203
|
+
async def finish_filament_load(self) -> bool:
|
|
204
|
+
self._unsupported("finish_filament_load", self.capabilities.gcode.reason)
|
|
205
|
+
|
|
206
|
+
# ---- Material station ----------------------------------------------------------------
|
|
207
|
+
|
|
208
|
+
async def configure_slot(self, slot_id: int, material_name: str, hex_color: str) -> bool:
|
|
209
|
+
"""
|
|
210
|
+
Sets the material name and color of a slot (native 1-based ID). The color snaps to
|
|
211
|
+
the model's firmware palette. AD5X and Creator 5 series.
|
|
212
|
+
"""
|
|
213
|
+
self._unsupported("configure_slot", self.capabilities.material_station.reason)
|
|
214
|
+
|
|
215
|
+
async def slot_action(self, slot_id: int, action: SlotAction) -> bool:
|
|
216
|
+
"""Loads, unloads or cancels on a slot (native 1-based ID). AD5X only."""
|
|
217
|
+
self._unsupported("slot_action", "Only the AD5X can load and unload filament over the API.")
|