flashforge-python-api 1.6.0__py3-none-any.whl → 3.0.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 +337 -256
- 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 +730 -763
- 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.0.0.dist-info}/METADATA +46 -47
- flashforge_python_api-3.0.0.dist-info/RECORD +69 -0
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/WHEEL +1 -1
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.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.0.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,413 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
FlashForge Python API - Control Module
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
import logging
|
|
6
|
-
from typing import TYPE_CHECKING, Any
|
|
7
|
-
|
|
8
|
-
from ...models.responses import FilamentArgs
|
|
9
|
-
from ..constants.commands import Commands
|
|
10
|
-
from ..constants.endpoints import Endpoints
|
|
11
|
-
from ..network.utils import NetworkUtils, json_from_response
|
|
12
|
-
from .creator5_palette import snap_to_creator5_palette
|
|
13
|
-
|
|
14
|
-
if TYPE_CHECKING:
|
|
15
|
-
from ...client import FlashForgeClient
|
|
16
|
-
from ...tcp.ff_client import FlashForgeClient as TcpClient
|
|
17
|
-
|
|
18
|
-
logger = logging.getLogger(__name__)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class Control:
|
|
22
|
-
"""
|
|
23
|
-
Provides methods for controlling various aspects of the FlashForge 3D printer.
|
|
24
|
-
This includes homing axes, controlling filtration, camera, speed, Z-axis offset,
|
|
25
|
-
fans, LEDs, and filament operations.
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
def __init__(self, client: "FlashForgeClient"):
|
|
29
|
-
"""
|
|
30
|
-
Creates an instance of the Control class.
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
client: The FlashForgeClient instance used for communication with the printer.
|
|
34
|
-
"""
|
|
35
|
-
self.client = client
|
|
36
|
-
self._tcp_client: TcpClient | None = None
|
|
37
|
-
|
|
38
|
-
@property
|
|
39
|
-
def tcp_client(self) -> "TcpClient":
|
|
40
|
-
"""Get the TCP client instance."""
|
|
41
|
-
if self._tcp_client is None:
|
|
42
|
-
self._tcp_client = self.client.tcp_client
|
|
43
|
-
return self._tcp_client
|
|
44
|
-
|
|
45
|
-
async def home_axes(self) -> bool:
|
|
46
|
-
"""
|
|
47
|
-
Homes the X, Y, and Z axes of the printer.
|
|
48
|
-
|
|
49
|
-
Returns:
|
|
50
|
-
True if the command is successful, False otherwise (including when the
|
|
51
|
-
printer has no TCP control channel, e.g. Creator 5).
|
|
52
|
-
"""
|
|
53
|
-
if not self.client.can_use_tcp("home_axes"):
|
|
54
|
-
return False
|
|
55
|
-
return await self.tcp_client.home_axes()
|
|
56
|
-
|
|
57
|
-
async def home_axes_rapid(self) -> bool:
|
|
58
|
-
"""
|
|
59
|
-
Performs a rapid homing of the X, Y, and Z axes.
|
|
60
|
-
|
|
61
|
-
Returns:
|
|
62
|
-
True if the command is successful, False otherwise (including when the
|
|
63
|
-
printer has no TCP control channel, e.g. Creator 5).
|
|
64
|
-
"""
|
|
65
|
-
if not self.client.can_use_tcp("home_axes_rapid"):
|
|
66
|
-
return False
|
|
67
|
-
return await self.tcp_client.rapid_home()
|
|
68
|
-
|
|
69
|
-
async def set_external_filtration_on(self) -> bool:
|
|
70
|
-
"""
|
|
71
|
-
Turns on the external filtration system.
|
|
72
|
-
Requires the printer to have filtration control.
|
|
73
|
-
|
|
74
|
-
Returns:
|
|
75
|
-
True if the command is successful, False otherwise.
|
|
76
|
-
"""
|
|
77
|
-
if self.client.filtration_control:
|
|
78
|
-
return await self._send_filtration_command(
|
|
79
|
-
FilamentArgs(internal="close", external="open")
|
|
80
|
-
)
|
|
81
|
-
logger.warning("set_external_filtration_on: filtration is not equipped on this printer.")
|
|
82
|
-
return False
|
|
83
|
-
|
|
84
|
-
async def set_internal_filtration_on(self) -> bool:
|
|
85
|
-
"""
|
|
86
|
-
Turns on the internal filtration system.
|
|
87
|
-
Requires the printer to have filtration control.
|
|
88
|
-
|
|
89
|
-
Returns:
|
|
90
|
-
True if the command is successful, False otherwise.
|
|
91
|
-
"""
|
|
92
|
-
if self.client.filtration_control:
|
|
93
|
-
return await self._send_filtration_command(
|
|
94
|
-
FilamentArgs(internal="open", external="close")
|
|
95
|
-
)
|
|
96
|
-
logger.warning("set_internal_filtration_on: filtration is not equipped on this printer.")
|
|
97
|
-
return False
|
|
98
|
-
|
|
99
|
-
async def set_filtration_off(self) -> bool:
|
|
100
|
-
"""
|
|
101
|
-
Turns off both internal and external filtration systems.
|
|
102
|
-
Requires the printer to have filtration control.
|
|
103
|
-
|
|
104
|
-
Returns:
|
|
105
|
-
True if the command is successful, False otherwise.
|
|
106
|
-
"""
|
|
107
|
-
if self.client.filtration_control:
|
|
108
|
-
return await self._send_filtration_command(
|
|
109
|
-
FilamentArgs(internal="close", external="close")
|
|
110
|
-
)
|
|
111
|
-
logger.warning("set_filtration_off: filtration is not equipped on this printer.")
|
|
112
|
-
return False
|
|
113
|
-
|
|
114
|
-
async def turn_camera_on(self) -> bool:
|
|
115
|
-
"""
|
|
116
|
-
Turns on the printer's camera.
|
|
117
|
-
Only applicable for Pro models.
|
|
118
|
-
|
|
119
|
-
Returns:
|
|
120
|
-
True if the command is successful, False otherwise.
|
|
121
|
-
"""
|
|
122
|
-
if not self.client.is_pro:
|
|
123
|
-
return False
|
|
124
|
-
return await self._send_camera_command(True)
|
|
125
|
-
|
|
126
|
-
async def turn_camera_off(self) -> bool:
|
|
127
|
-
"""
|
|
128
|
-
Turns off the printer's camera.
|
|
129
|
-
Only applicable for Pro models.
|
|
130
|
-
|
|
131
|
-
Returns:
|
|
132
|
-
True if the command is successful, False otherwise.
|
|
133
|
-
"""
|
|
134
|
-
if not self.client.is_pro:
|
|
135
|
-
return False
|
|
136
|
-
return await self._send_camera_command(False)
|
|
137
|
-
|
|
138
|
-
async def set_speed_override(self, speed: int) -> bool:
|
|
139
|
-
"""
|
|
140
|
-
Sets the print speed override.
|
|
141
|
-
|
|
142
|
-
Args:
|
|
143
|
-
speed: The desired print speed percentage (e.g., 100 for normal speed).
|
|
144
|
-
|
|
145
|
-
Returns:
|
|
146
|
-
True if the command is successful, False otherwise.
|
|
147
|
-
"""
|
|
148
|
-
return await self._send_printer_control_cmd(print_speed=speed)
|
|
149
|
-
|
|
150
|
-
async def set_z_axis_override(self, offset: float) -> bool:
|
|
151
|
-
"""
|
|
152
|
-
Sets the Z-axis offset override.
|
|
153
|
-
|
|
154
|
-
Args:
|
|
155
|
-
offset: The Z-axis offset value.
|
|
156
|
-
|
|
157
|
-
Returns:
|
|
158
|
-
True if the command is successful, False otherwise.
|
|
159
|
-
"""
|
|
160
|
-
return await self._send_printer_control_cmd(z_offset=offset)
|
|
161
|
-
|
|
162
|
-
async def set_chamber_fan_speed(self, speed: int) -> bool:
|
|
163
|
-
"""
|
|
164
|
-
Sets the chamber fan speed.
|
|
165
|
-
|
|
166
|
-
Args:
|
|
167
|
-
speed: The desired chamber fan speed percentage.
|
|
168
|
-
|
|
169
|
-
Returns:
|
|
170
|
-
True if the command is successful, False otherwise.
|
|
171
|
-
"""
|
|
172
|
-
return await self._send_printer_control_cmd(chamber_fan_speed=speed)
|
|
173
|
-
|
|
174
|
-
async def set_cooling_fan_speed(self, speed: int) -> bool:
|
|
175
|
-
"""
|
|
176
|
-
Sets the cooling fan speed.
|
|
177
|
-
|
|
178
|
-
Args:
|
|
179
|
-
speed: The desired cooling fan speed percentage.
|
|
180
|
-
|
|
181
|
-
Returns:
|
|
182
|
-
True if the command is successful, False otherwise.
|
|
183
|
-
"""
|
|
184
|
-
return await self._send_printer_control_cmd(cooling_fan_speed=speed)
|
|
185
|
-
|
|
186
|
-
async def set_led_on(self) -> bool:
|
|
187
|
-
"""
|
|
188
|
-
Turns on the printer's LED lights.
|
|
189
|
-
|
|
190
|
-
Returns:
|
|
191
|
-
True if the command is successful, False otherwise.
|
|
192
|
-
"""
|
|
193
|
-
if not self.client.led_control:
|
|
194
|
-
logger.warning("set_led_on: LED control is not equipped on this printer.")
|
|
195
|
-
return False
|
|
196
|
-
return await self.send_control_command(Commands.LIGHT_CONTROL_CMD, {"status": "open"})
|
|
197
|
-
|
|
198
|
-
async def set_led_off(self) -> bool:
|
|
199
|
-
"""
|
|
200
|
-
Turns off the printer's LED lights.
|
|
201
|
-
|
|
202
|
-
Returns:
|
|
203
|
-
True if the command is successful, False otherwise.
|
|
204
|
-
"""
|
|
205
|
-
if not self.client.led_control:
|
|
206
|
-
logger.warning("set_led_off: LED control is not equipped on this printer.")
|
|
207
|
-
return False
|
|
208
|
-
return await self.send_control_command(Commands.LIGHT_CONTROL_CMD, {"status": "close"})
|
|
209
|
-
|
|
210
|
-
async def turn_runout_sensor_on(self) -> bool:
|
|
211
|
-
"""
|
|
212
|
-
Turns on the filament runout sensor.
|
|
213
|
-
|
|
214
|
-
Returns:
|
|
215
|
-
True if the command is successful, False otherwise (including when the
|
|
216
|
-
printer has no TCP control channel, e.g. Creator 5).
|
|
217
|
-
"""
|
|
218
|
-
if not self.client.can_use_tcp("turn_runout_sensor_on"):
|
|
219
|
-
return False
|
|
220
|
-
return await self.tcp_client.turn_runout_sensor_on()
|
|
221
|
-
|
|
222
|
-
async def turn_runout_sensor_off(self) -> bool:
|
|
223
|
-
"""
|
|
224
|
-
Turns off the filament runout sensor.
|
|
225
|
-
|
|
226
|
-
Returns:
|
|
227
|
-
True if the command is successful, False otherwise (including when the
|
|
228
|
-
printer has no TCP control channel, e.g. Creator 5).
|
|
229
|
-
"""
|
|
230
|
-
if not self.client.can_use_tcp("turn_runout_sensor_off"):
|
|
231
|
-
return False
|
|
232
|
-
return await self.tcp_client.turn_runout_sensor_off()
|
|
233
|
-
|
|
234
|
-
async def configure_slot(self, slot: int, material_name: str, hex_rgb: str) -> bool:
|
|
235
|
-
"""
|
|
236
|
-
Configures the material name and color metadata for a material-station slot.
|
|
237
|
-
This information is shown on the printer UI and used for print validation; it
|
|
238
|
-
does not move any filament. Available on the AD5X and the Creator 5 / Creator 5 Pro.
|
|
239
|
-
|
|
240
|
-
The ``msConfig_cmd`` handler is confirmed present on the Creator 5
|
|
241
|
-
(verified in firmware 1.9.2). The Creator 5 has no removable IFS; it
|
|
242
|
-
surfaces its 4 tool heads as the 4 "slots", so this sets per-tool material
|
|
243
|
-
metadata. Both models share the same ``OrcaServer`` command path and wire
|
|
244
|
-
format. Note that **filament load/unload (``slotAction`` / ``ms_cmd``)
|
|
245
|
-
remains AD5X-only** — the Creator 5 firmware has no ``ms_cmd``.
|
|
246
|
-
|
|
247
|
-
The firmware accepts arbitrary material strings, but the two models render
|
|
248
|
-
the slot color icon differently:
|
|
249
|
-
|
|
250
|
-
- AD5X: accepts freeform hex (the leading "#" is stripped before sending).
|
|
251
|
-
- Creator 5 / 5 Pro: renders an icon ONLY when ``rgb`` is a byte-for-byte,
|
|
252
|
-
case-sensitive match against the firmware's 24-entry palette (WITH the
|
|
253
|
-
"#"); any other value falls back to White. This method snaps the
|
|
254
|
-
caller's color to the nearest palette entry automatically for the Creator 5.
|
|
255
|
-
|
|
256
|
-
Args:
|
|
257
|
-
slot: The slot number (1-4).
|
|
258
|
-
material_name: The material type (e.g., "PLA", "PETG").
|
|
259
|
-
hex_rgb: The color as a hex string.
|
|
260
|
-
|
|
261
|
-
Returns:
|
|
262
|
-
True if the command is successful, False otherwise.
|
|
263
|
-
"""
|
|
264
|
-
if not self.client.is_ad5x and not self.client.is_creator5:
|
|
265
|
-
logger.warning(
|
|
266
|
-
"configure_slot: the material station is only available on the AD5X / Creator 5."
|
|
267
|
-
)
|
|
268
|
-
return False
|
|
269
|
-
# The AD5X and Creator 5 use MUTUALLY EXCLUSIVE color wire formats (see
|
|
270
|
-
# creator5_palette for the firmware match rules), so model-gate here:
|
|
271
|
-
# - AD5X: freeform hex, the leading "#" stripped ("RRGGBB"). Unchanged.
|
|
272
|
-
# - Creator 5 / 5 Pro: the firmware renders an icon ONLY on a byte-for-byte
|
|
273
|
-
# match against its 24-entry palette (case-sensitive, WITH the "#"). Snap
|
|
274
|
-
# the caller's color to the nearest palette entry in uppercase "#RRGGBB".
|
|
275
|
-
if self.client.is_creator5:
|
|
276
|
-
rgb = snap_to_creator5_palette(hex_rgb).hex
|
|
277
|
-
else:
|
|
278
|
-
rgb = hex_rgb[1:] if hex_rgb.startswith("#") else hex_rgb
|
|
279
|
-
return await self.send_control_command(
|
|
280
|
-
Commands.MATERIAL_STATION_CONFIG_CMD,
|
|
281
|
-
{"slot": slot, "mt": material_name, "rgb": rgb},
|
|
282
|
-
)
|
|
283
|
-
|
|
284
|
-
async def send_control_command(self, command: str, args: dict[str, Any]) -> bool:
|
|
285
|
-
"""
|
|
286
|
-
Sends a generic control command to the printer via HTTP POST.
|
|
287
|
-
|
|
288
|
-
Args:
|
|
289
|
-
command: The specific command string to send.
|
|
290
|
-
args: The arguments or payload specific to the command.
|
|
291
|
-
|
|
292
|
-
Returns:
|
|
293
|
-
True if the command is acknowledged with a success code, False otherwise.
|
|
294
|
-
"""
|
|
295
|
-
payload = {
|
|
296
|
-
"serialNumber": self.client.serial_number,
|
|
297
|
-
"checkCode": self.client.check_code,
|
|
298
|
-
"payload": {"cmd": command, "args": args},
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
# Log the command, never `payload`: it carries the serial number and
|
|
302
|
-
# check code, and these lines end up pasted into bug reports.
|
|
303
|
-
logger.debug("Sending control command %s with args %s", command, args)
|
|
304
|
-
|
|
305
|
-
try:
|
|
306
|
-
session = await self.client.get_http_session()
|
|
307
|
-
async with self.client.command_lock:
|
|
308
|
-
async with session.post(
|
|
309
|
-
self.client.get_endpoint(Endpoints.CONTROL),
|
|
310
|
-
json=payload,
|
|
311
|
-
headers={"Content-Type": "application/json"},
|
|
312
|
-
) as response:
|
|
313
|
-
data = await json_from_response(response)
|
|
314
|
-
logger.debug("Control command %s reply: %s", command, data)
|
|
315
|
-
|
|
316
|
-
return NetworkUtils.is_ok(data)
|
|
317
|
-
|
|
318
|
-
except Exception as e:
|
|
319
|
-
logger.warning("Error in send_control_command (%s): %s", command, e)
|
|
320
|
-
return False
|
|
321
|
-
|
|
322
|
-
async def send_job_control_cmd(self, command: str) -> bool:
|
|
323
|
-
"""
|
|
324
|
-
Sends a job control command.
|
|
325
|
-
|
|
326
|
-
Args:
|
|
327
|
-
command: The job control command to send.
|
|
328
|
-
|
|
329
|
-
Returns:
|
|
330
|
-
True if the command is successful, False otherwise.
|
|
331
|
-
"""
|
|
332
|
-
payload = {
|
|
333
|
-
"jobID": "", # jobID seems to be optional or not strictly enforced
|
|
334
|
-
"action": command,
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
return await self.send_control_command(Commands.JOB_CONTROL_CMD, payload)
|
|
338
|
-
|
|
339
|
-
async def _send_printer_control_cmd(
|
|
340
|
-
self,
|
|
341
|
-
z_offset: float = 0.0,
|
|
342
|
-
print_speed: int = 100,
|
|
343
|
-
chamber_fan_speed: int = 100,
|
|
344
|
-
cooling_fan_speed: int = 100,
|
|
345
|
-
) -> bool:
|
|
346
|
-
"""
|
|
347
|
-
Sends a command to control various printer settings during a print.
|
|
348
|
-
|
|
349
|
-
Args:
|
|
350
|
-
z_offset: The Z-axis compensation offset.
|
|
351
|
-
print_speed: The print speed percentage.
|
|
352
|
-
chamber_fan_speed: The chamber fan speed percentage.
|
|
353
|
-
cooling_fan_speed: The cooling fan speed percentage.
|
|
354
|
-
|
|
355
|
-
Returns:
|
|
356
|
-
True if the command is successful, False otherwise.
|
|
357
|
-
"""
|
|
358
|
-
info = await self.client.info.get()
|
|
359
|
-
|
|
360
|
-
if info and info.current_print_layer < 2:
|
|
361
|
-
# Don't accidentally turn on the fans in the initial layers
|
|
362
|
-
chamber_fan_speed = 0
|
|
363
|
-
cooling_fan_speed = 0
|
|
364
|
-
|
|
365
|
-
if not self._is_printing(info):
|
|
366
|
-
raise Exception("Attempted to send printerCtl_cmd with no active job")
|
|
367
|
-
|
|
368
|
-
payload = {
|
|
369
|
-
"zAxisCompensation": z_offset,
|
|
370
|
-
"speed": print_speed,
|
|
371
|
-
"chamberFan": chamber_fan_speed,
|
|
372
|
-
"coolingFan": cooling_fan_speed,
|
|
373
|
-
"coolingLeftFan": 0, # This is unused
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
return await self.send_control_command(Commands.PRINTER_CONTROL_CMD, payload)
|
|
377
|
-
|
|
378
|
-
async def _send_filtration_command(self, args: FilamentArgs) -> bool:
|
|
379
|
-
"""
|
|
380
|
-
Sends a command to control the printer's filtration system.
|
|
381
|
-
|
|
382
|
-
Args:
|
|
383
|
-
args: The filtration arguments specifying internal and external fan states.
|
|
384
|
-
|
|
385
|
-
Returns:
|
|
386
|
-
True if the command is successful, False otherwise.
|
|
387
|
-
"""
|
|
388
|
-
return await self.send_control_command(Commands.CIRCULATION_CONTROL_CMD, args.model_dump())
|
|
389
|
-
|
|
390
|
-
async def _send_camera_command(self, enabled: bool) -> bool:
|
|
391
|
-
"""
|
|
392
|
-
Sends a command to control the printer's camera.
|
|
393
|
-
|
|
394
|
-
Args:
|
|
395
|
-
enabled: True to turn the camera on ("open"), false to turn it off ("close").
|
|
396
|
-
|
|
397
|
-
Returns:
|
|
398
|
-
True if the command is successful, False otherwise.
|
|
399
|
-
"""
|
|
400
|
-
payload = {"action": "open" if enabled else "close"}
|
|
401
|
-
return await self.send_control_command(Commands.CAMERA_CONTROL_CMD, payload)
|
|
402
|
-
|
|
403
|
-
def _is_printing(self, info: Any) -> bool:
|
|
404
|
-
"""
|
|
405
|
-
Checks if the printer is currently printing based on its status information.
|
|
406
|
-
|
|
407
|
-
Args:
|
|
408
|
-
info: The printer information object.
|
|
409
|
-
|
|
410
|
-
Returns:
|
|
411
|
-
True if the printer status is "printing", False otherwise.
|
|
412
|
-
"""
|
|
413
|
-
return info and getattr(info, "status", "") == "printing"
|
flashforge/api/controls/files.py
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
FlashForge Python API - Files Module
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
import base64
|
|
6
|
-
import logging
|
|
7
|
-
from typing import TYPE_CHECKING
|
|
8
|
-
|
|
9
|
-
from pydantic import ValidationError
|
|
10
|
-
|
|
11
|
-
from ...models.machine_info import FFGcodeFileEntry
|
|
12
|
-
from ...models.responses import GCodeListResponse, ThumbnailResponse
|
|
13
|
-
from ..constants.endpoints import Endpoints
|
|
14
|
-
from ..network.utils import NetworkUtils, json_from_response
|
|
15
|
-
|
|
16
|
-
if TYPE_CHECKING:
|
|
17
|
-
from ...client import FlashForgeClient
|
|
18
|
-
|
|
19
|
-
logger = logging.getLogger(__name__)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
class Files:
|
|
23
|
-
"""
|
|
24
|
-
Provides methods for managing files on the FlashForge 3D printer.
|
|
25
|
-
This includes retrieving file lists and thumbnails.
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
def __init__(self, client: "FlashForgeClient"):
|
|
29
|
-
"""
|
|
30
|
-
Creates an instance of the Files class.
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
client: The FlashForgeClient instance used for communication with the printer.
|
|
34
|
-
"""
|
|
35
|
-
self.client = client
|
|
36
|
-
|
|
37
|
-
async def get_file_list(self) -> list[str]:
|
|
38
|
-
"""
|
|
39
|
-
Retrieves a list of files stored locally on the printer.
|
|
40
|
-
|
|
41
|
-
HTTP-only printers (Creator 5 / 5 Pro) have no TCP/8899 file-listing
|
|
42
|
-
channel, so this falls back to the HTTP ``/gcodeList`` recent-file list
|
|
43
|
-
(mirroring ``Files.ts``) instead of hanging on a dead TCP socket.
|
|
44
|
-
|
|
45
|
-
Returns:
|
|
46
|
-
A list of file names, or empty list if retrieval fails.
|
|
47
|
-
"""
|
|
48
|
-
if self.client.http_only:
|
|
49
|
-
entries = await self.get_recent_file_list()
|
|
50
|
-
return [e.gcode_file_name for e in entries if e.gcode_file_name]
|
|
51
|
-
|
|
52
|
-
# Legacy / 5M-family printers expose the file list over TCP.
|
|
53
|
-
if hasattr(self.client, "tcp_client") and self.client.tcp_client:
|
|
54
|
-
return await self.client.tcp_client.get_file_list_async()
|
|
55
|
-
return []
|
|
56
|
-
|
|
57
|
-
async def get_local_file_list(self) -> list[str]:
|
|
58
|
-
"""
|
|
59
|
-
Retrieves a list of files stored locally on the printer.
|
|
60
|
-
|
|
61
|
-
HTTP-only printers (Creator 5 / 5 Pro) have no TCP/8899 file-listing
|
|
62
|
-
channel; see :meth:`get_file_list` for the HTTP fallback.
|
|
63
|
-
|
|
64
|
-
Returns:
|
|
65
|
-
A list of file names, or empty list if retrieval fails.
|
|
66
|
-
"""
|
|
67
|
-
return await self.get_file_list()
|
|
68
|
-
|
|
69
|
-
async def get_recent_file_list(self) -> list[FFGcodeFileEntry]:
|
|
70
|
-
"""
|
|
71
|
-
Retrieves a list of the 10 most recently printed files from the printer's API.
|
|
72
|
-
|
|
73
|
-
Only the **AD5X** answers with `gcodeListDetail`, the per-file block carrying
|
|
74
|
-
print time, filament weight, and the per-tool material data that material
|
|
75
|
-
matching is built from. Every other model - the 5M, the 5M Pro, **and the
|
|
76
|
-
Creator 5 / Creator 5 Pro** - returns bare file names, and their entries come
|
|
77
|
-
back with `gcode_tool_datas=None` and `printing_time=0`.
|
|
78
|
-
|
|
79
|
-
The Creator 5 is the surprise there, and it is firmware, not a parsing gap:
|
|
80
|
-
it is a newer printer than the AD5X but reports less (confirmed against a
|
|
81
|
-
live Creator 5 Pro, 2026-08-05). Do not describe this method as "AD5X and
|
|
82
|
-
newer" - only the AD5X answers with `gcodeListDetail`. Callers that need
|
|
83
|
-
per-tool data on a Creator 5 must parse the 3mf themselves at upload time.
|
|
84
|
-
|
|
85
|
-
Returns:
|
|
86
|
-
A list of FFGcodeFileEntry objects. Returns an empty list if the request fails or an error occurs.
|
|
87
|
-
"""
|
|
88
|
-
payload = {"serialNumber": self.client.serial_number, "checkCode": self.client.check_code}
|
|
89
|
-
|
|
90
|
-
try:
|
|
91
|
-
session = await self.client.get_http_session()
|
|
92
|
-
async with session.post(
|
|
93
|
-
self.client.get_endpoint(Endpoints.GCODE_LIST),
|
|
94
|
-
json=payload,
|
|
95
|
-
headers={"Content-Type": "application/json"},
|
|
96
|
-
) as response:
|
|
97
|
-
if response.status != 200:
|
|
98
|
-
return []
|
|
99
|
-
|
|
100
|
-
data = await json_from_response(response)
|
|
101
|
-
|
|
102
|
-
if not NetworkUtils.is_ok(data):
|
|
103
|
-
logger.warning(
|
|
104
|
-
"Error retrieving the file list: %s",
|
|
105
|
-
NetworkUtils.get_error_message(data),
|
|
106
|
-
)
|
|
107
|
-
return []
|
|
108
|
-
|
|
109
|
-
# Parse the response using GCodeListResponse
|
|
110
|
-
try:
|
|
111
|
-
result = GCodeListResponse(**data)
|
|
112
|
-
except ValidationError as err:
|
|
113
|
-
# The names-only fallback below silently costs the caller
|
|
114
|
-
# every per-file field, which is indistinguishable from a
|
|
115
|
-
# printer that only reports names. Say so, loudly enough to
|
|
116
|
-
# reach an integration's log, or the next model that changes
|
|
117
|
-
# this payload looks like it reports no metadata at all.
|
|
118
|
-
logger.warning(
|
|
119
|
-
"Could not parse the /gcodeList response; falling back to file "
|
|
120
|
-
"names only, so print time, filament weight, and per-tool "
|
|
121
|
-
"material data are unavailable for every file. %s",
|
|
122
|
-
err,
|
|
123
|
-
)
|
|
124
|
-
raw_list = data.get("gcodeList", [])
|
|
125
|
-
if isinstance(raw_list, list):
|
|
126
|
-
entries: list[FFGcodeFileEntry] = []
|
|
127
|
-
for file_name in raw_list:
|
|
128
|
-
if isinstance(file_name, str):
|
|
129
|
-
entries.append(
|
|
130
|
-
FFGcodeFileEntry(
|
|
131
|
-
gcodeFileName=file_name,
|
|
132
|
-
printingTime=0,
|
|
133
|
-
)
|
|
134
|
-
)
|
|
135
|
-
return entries
|
|
136
|
-
return []
|
|
137
|
-
|
|
138
|
-
# Only the AD5X provides detailed info in gcodeListDetail. The
|
|
139
|
-
# Creator 5 series does not, despite being the newer hardware.
|
|
140
|
-
if result.gcode_list_detail and len(result.gcode_list_detail) > 0:
|
|
141
|
-
return result.gcode_list_detail
|
|
142
|
-
|
|
143
|
-
# Fallback for older printers using gcodeList
|
|
144
|
-
if result.gcode_list and len(result.gcode_list) > 0:
|
|
145
|
-
# Check if it's a list of strings or already FFGcodeFileEntry objects
|
|
146
|
-
first_item = result.gcode_list[0]
|
|
147
|
-
|
|
148
|
-
if isinstance(first_item, str):
|
|
149
|
-
# Convert string array to FFGcodeFileEntry objects
|
|
150
|
-
return [
|
|
151
|
-
FFGcodeFileEntry(gcodeFileName=file_name, printingTime=0)
|
|
152
|
-
for file_name in result.gcode_list
|
|
153
|
-
if isinstance(file_name, str)
|
|
154
|
-
]
|
|
155
|
-
elif isinstance(first_item, FFGcodeFileEntry):
|
|
156
|
-
# Already FFGcodeFileEntry objects - need explicit type narrowing
|
|
157
|
-
return [
|
|
158
|
-
item for item in result.gcode_list if isinstance(item, FFGcodeFileEntry)
|
|
159
|
-
]
|
|
160
|
-
|
|
161
|
-
return []
|
|
162
|
-
|
|
163
|
-
except Exception as err:
|
|
164
|
-
logger.warning("get_recent_file_list error: %s", err)
|
|
165
|
-
return []
|
|
166
|
-
|
|
167
|
-
async def get_gcode_thumbnail(self, file_name: str) -> bytes | None:
|
|
168
|
-
"""
|
|
169
|
-
Retrieves the thumbnail image for a specified G-code file.
|
|
170
|
-
The image data is returned as bytes.
|
|
171
|
-
|
|
172
|
-
Args:
|
|
173
|
-
file_name: The name of the G-code file (e.g., "my_print.gcode") for which to retrieve the thumbnail.
|
|
174
|
-
|
|
175
|
-
Returns:
|
|
176
|
-
Bytes containing the thumbnail image data (decoded from base64),
|
|
177
|
-
or None if the request fails, the file has no thumbnail, or an error occurs.
|
|
178
|
-
"""
|
|
179
|
-
payload = {
|
|
180
|
-
"serialNumber": self.client.serial_number,
|
|
181
|
-
"checkCode": self.client.check_code,
|
|
182
|
-
"fileName": file_name,
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
try:
|
|
186
|
-
session = await self.client.get_http_session()
|
|
187
|
-
async with session.post(
|
|
188
|
-
self.client.get_endpoint(Endpoints.GCODE_THUMB),
|
|
189
|
-
json=payload,
|
|
190
|
-
headers={"Content-Type": "application/json"},
|
|
191
|
-
) as response:
|
|
192
|
-
if response.status != 200:
|
|
193
|
-
return None
|
|
194
|
-
|
|
195
|
-
data = await json_from_response(response)
|
|
196
|
-
|
|
197
|
-
if NetworkUtils.is_ok(data):
|
|
198
|
-
# Parse response and return decoded image bytes
|
|
199
|
-
result = ThumbnailResponse(**data)
|
|
200
|
-
return base64.b64decode(result.image_data)
|
|
201
|
-
else:
|
|
202
|
-
logger.warning(
|
|
203
|
-
"Error retrieving the thumbnail: %s",
|
|
204
|
-
NetworkUtils.get_error_message(data),
|
|
205
|
-
)
|
|
206
|
-
return None
|
|
207
|
-
|
|
208
|
-
except Exception as err:
|
|
209
|
-
logger.warning("get_gcode_thumbnail error: %s", err)
|
|
210
|
-
return None
|