flashforge-python-api 1.5.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 -233
- 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 -0
- flashforge/threemf/parser.py +560 -0
- flashforge/threemf/warnings.py +58 -0
- flashforge_python_api-3.0.0.dist-info/METADATA +184 -0
- flashforge_python_api-3.0.0.dist-info/RECORD +69 -0
- {flashforge_python_api-1.5.0.dist-info → flashforge_python_api-3.0.0.dist-info}/WHEEL +1 -1
- {flashforge_python_api-1.5.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 -880
- flashforge/api/controls/temp_control.py +0 -309
- flashforge/client.py +0 -609
- flashforge_python_api-1.5.0.dist-info/METADATA +0 -152
- flashforge_python_api-1.5.0.dist-info/RECORD +0 -49
- {flashforge_python_api-1.5.0.dist-info → flashforge_python_api-3.0.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"""
|
|
2
|
-
FlashForge Python API - Constants Package
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from .commands import Commands
|
|
6
|
-
from .endpoints import Endpoints
|
|
7
|
-
|
|
8
|
-
__all__ = [
|
|
9
|
-
"Commands",
|
|
10
|
-
"Endpoints",
|
|
11
|
-
]
|
|
1
|
+
"""
|
|
2
|
+
FlashForge Python API - Constants Package
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .commands import Commands
|
|
6
|
+
from .endpoints import Endpoints
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"Commands",
|
|
10
|
+
"Endpoints",
|
|
11
|
+
]
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
class Commands:
|
|
2
|
-
"""
|
|
3
|
-
Commands used in the "new" HTTP API
|
|
4
|
-
"""
|
|
5
|
-
|
|
6
|
-
LIGHT_CONTROL_CMD = "lightControl_cmd"
|
|
7
|
-
PRINTER_CONTROL_CMD = "printerCtl_cmd"
|
|
8
|
-
JOB_CONTROL_CMD = "jobCtl_cmd"
|
|
9
|
-
CIRCULATION_CONTROL_CMD = "circulateCtl_cmd"
|
|
10
|
-
CAMERA_CONTROL_CMD = "streamCtrl_cmd"
|
|
11
|
-
TEMP_CONTROL_CMD = "temperatureCtl_cmd"
|
|
12
|
-
# AD5X + Creator 5 material-station slot metadata (name + color). Filament
|
|
13
|
-
# load/unload (`ms_cmd`) stays AD5X-only — the Creator 5 firmware has no
|
|
14
|
-
# `ms_cmd` (verified in the firmware).
|
|
15
|
-
MATERIAL_STATION_CONFIG_CMD = "msConfig_cmd"
|
|
1
|
+
class Commands:
|
|
2
|
+
"""
|
|
3
|
+
Commands used in the "new" HTTP API
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
LIGHT_CONTROL_CMD = "lightControl_cmd"
|
|
7
|
+
PRINTER_CONTROL_CMD = "printerCtl_cmd"
|
|
8
|
+
JOB_CONTROL_CMD = "jobCtl_cmd"
|
|
9
|
+
CIRCULATION_CONTROL_CMD = "circulateCtl_cmd"
|
|
10
|
+
CAMERA_CONTROL_CMD = "streamCtrl_cmd"
|
|
11
|
+
TEMP_CONTROL_CMD = "temperatureCtl_cmd"
|
|
12
|
+
# AD5X + Creator 5 material-station slot metadata (name + color). Filament
|
|
13
|
+
# load/unload (`ms_cmd`) stays AD5X-only — the Creator 5 firmware has no
|
|
14
|
+
# `ms_cmd` (verified in the firmware).
|
|
15
|
+
MATERIAL_STATION_CONFIG_CMD = "msConfig_cmd"
|
|
16
|
+
# AD5X filament load / unload / cancel on a material-station slot.
|
|
17
|
+
MATERIAL_STATION_CMD = "ms_cmd"
|
|
18
|
+
STATE_CONTROL_CMD = "stateCtrl_cmd"
|
|
@@ -4,14 +4,14 @@ CAMERA_STREAM_PORT: Final[int] = 8080
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
class Endpoints:
|
|
7
|
-
"""
|
|
8
|
-
Endpoints for the "new" HTTP API
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
CONTROL = "/control"
|
|
12
|
-
DETAIL = "/detail"
|
|
13
|
-
GCODE_LIST = "/gcodeList"
|
|
14
|
-
GCODE_PRINT = "/printGcode"
|
|
15
|
-
GCODE_THUMB = "/gcodeThumb"
|
|
16
|
-
PRODUCT = "/product"
|
|
17
|
-
UPLOAD_FILE = "/uploadGcode"
|
|
7
|
+
"""
|
|
8
|
+
Endpoints for the "new" HTTP API
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
CONTROL = "/control"
|
|
12
|
+
DETAIL = "/detail"
|
|
13
|
+
GCODE_LIST = "/gcodeList"
|
|
14
|
+
GCODE_PRINT = "/printGcode"
|
|
15
|
+
GCODE_THUMB = "/gcodeThumb"
|
|
16
|
+
PRODUCT = "/product"
|
|
17
|
+
UPLOAD_FILE = "/uploadGcode"
|
flashforge/api/misc/__init__.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"""
|
|
2
|
-
FlashForge Python API - Miscellaneous Utilities
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from .scientific_notation import format_scientific_notation
|
|
6
|
-
from .temperature import Temperature
|
|
7
|
-
|
|
8
|
-
__all__ = ["Temperature", "format_scientific_notation"]
|
|
1
|
+
"""
|
|
2
|
+
FlashForge Python API - Miscellaneous Utilities
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .scientific_notation import format_scientific_notation
|
|
6
|
+
from .temperature import Temperature
|
|
7
|
+
|
|
8
|
+
__all__ = ["Temperature", "format_scientific_notation"]
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"""
|
|
2
|
-
FlashForge Python API - Network Package
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from .fnet_code import FNetCode
|
|
6
|
-
from .utils import NetworkUtils
|
|
7
|
-
|
|
8
|
-
__all__ = [
|
|
9
|
-
"NetworkUtils",
|
|
10
|
-
"FNetCode",
|
|
11
|
-
]
|
|
1
|
+
"""
|
|
2
|
+
FlashForge Python API - Network Package
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .fnet_code import FNetCode
|
|
6
|
+
from .utils import NetworkUtils
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"NetworkUtils",
|
|
10
|
+
"FNetCode",
|
|
11
|
+
]
|
flashforge/api/network/utils.py
CHANGED
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
"""
|
|
2
|
-
FlashForge Python API - Network Utilities
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
import json
|
|
6
|
-
|
|
7
|
-
import aiohttp
|
|
8
|
-
|
|
9
|
-
from ...models.responses import GenericResponse
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
async def json_from_response(response: aiohttp.ClientResponse) -> dict:
|
|
13
|
-
"""Parse JSON from a response, with a fallback for malformed Content-Type headers.
|
|
14
|
-
|
|
15
|
-
Some FlashForge printers return "appliation/json" instead of "application/json",
|
|
16
|
-
causing aiohttp to raise ContentTypeError. This helper handles both cases.
|
|
17
|
-
"""
|
|
18
|
-
try:
|
|
19
|
-
return await response.json() # type: ignore[no-any-return]
|
|
20
|
-
except aiohttp.ContentTypeError:
|
|
21
|
-
text = await response.text()
|
|
22
|
-
return json.loads(text) # type: ignore[no-any-return]
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class NetworkUtils:
|
|
26
|
-
"""Utility class for handling network responses and status codes."""
|
|
27
|
-
|
|
28
|
-
@staticmethod
|
|
29
|
-
def is_ok(response: GenericResponse | dict | None) -> bool:
|
|
30
|
-
"""
|
|
31
|
-
Checks if a response indicates success.
|
|
32
|
-
|
|
33
|
-
Args:
|
|
34
|
-
response: The response object to check
|
|
35
|
-
|
|
36
|
-
Returns:
|
|
37
|
-
True if the response indicates success, False otherwise
|
|
38
|
-
"""
|
|
39
|
-
if response is None:
|
|
40
|
-
return False
|
|
41
|
-
|
|
42
|
-
# Handle dictionary responses
|
|
43
|
-
if isinstance(response, dict):
|
|
44
|
-
code = response.get("code", -1)
|
|
45
|
-
else:
|
|
46
|
-
# Handle GenericResponse objects
|
|
47
|
-
code = getattr(response, "code", -1)
|
|
48
|
-
|
|
49
|
-
# Success codes: 0 or 200
|
|
50
|
-
return code in (0, 200)
|
|
51
|
-
|
|
52
|
-
@staticmethod
|
|
53
|
-
def get_error_message(response: GenericResponse | dict | None) -> str:
|
|
54
|
-
"""
|
|
55
|
-
Extracts error message from a response.
|
|
56
|
-
|
|
57
|
-
Args:
|
|
58
|
-
response: The response object to extract message from
|
|
59
|
-
|
|
60
|
-
Returns:
|
|
61
|
-
Error message string, or empty string if none found
|
|
62
|
-
"""
|
|
63
|
-
if response is None:
|
|
64
|
-
return "No response received"
|
|
65
|
-
|
|
66
|
-
# Handle dictionary responses
|
|
67
|
-
if isinstance(response, dict):
|
|
68
|
-
return str(response.get("message", "Unknown error"))
|
|
69
|
-
else:
|
|
70
|
-
# Handle GenericResponse objects
|
|
71
|
-
return getattr(response, "message", "Unknown error")
|
|
1
|
+
"""
|
|
2
|
+
FlashForge Python API - Network Utilities
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
import aiohttp
|
|
8
|
+
|
|
9
|
+
from ...models.responses import GenericResponse
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def json_from_response(response: aiohttp.ClientResponse) -> dict:
|
|
13
|
+
"""Parse JSON from a response, with a fallback for malformed Content-Type headers.
|
|
14
|
+
|
|
15
|
+
Some FlashForge printers return "appliation/json" instead of "application/json",
|
|
16
|
+
causing aiohttp to raise ContentTypeError. This helper handles both cases.
|
|
17
|
+
"""
|
|
18
|
+
try:
|
|
19
|
+
return await response.json() # type: ignore[no-any-return]
|
|
20
|
+
except aiohttp.ContentTypeError:
|
|
21
|
+
text = await response.text()
|
|
22
|
+
return json.loads(text) # type: ignore[no-any-return]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class NetworkUtils:
|
|
26
|
+
"""Utility class for handling network responses and status codes."""
|
|
27
|
+
|
|
28
|
+
@staticmethod
|
|
29
|
+
def is_ok(response: GenericResponse | dict | None) -> bool:
|
|
30
|
+
"""
|
|
31
|
+
Checks if a response indicates success.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
response: The response object to check
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
True if the response indicates success, False otherwise
|
|
38
|
+
"""
|
|
39
|
+
if response is None:
|
|
40
|
+
return False
|
|
41
|
+
|
|
42
|
+
# Handle dictionary responses
|
|
43
|
+
if isinstance(response, dict):
|
|
44
|
+
code = response.get("code", -1)
|
|
45
|
+
else:
|
|
46
|
+
# Handle GenericResponse objects
|
|
47
|
+
code = getattr(response, "code", -1)
|
|
48
|
+
|
|
49
|
+
# Success codes: 0 or 200
|
|
50
|
+
return code in (0, 200)
|
|
51
|
+
|
|
52
|
+
@staticmethod
|
|
53
|
+
def get_error_message(response: GenericResponse | dict | None) -> str:
|
|
54
|
+
"""
|
|
55
|
+
Extracts error message from a response.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
response: The response object to extract message from
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
Error message string, or empty string if none found
|
|
62
|
+
"""
|
|
63
|
+
if response is None:
|
|
64
|
+
return "No response received"
|
|
65
|
+
|
|
66
|
+
# Handle dictionary responses
|
|
67
|
+
if isinstance(response, dict):
|
|
68
|
+
return str(response.get("message", "Unknown error"))
|
|
69
|
+
else:
|
|
70
|
+
# Handle GenericResponse objects
|
|
71
|
+
return getattr(response, "message", "Unknown error")
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Printer backends, one per family, and the connect factory.
|
|
3
|
+
|
|
4
|
+
- Dual-API (HTTP + TCP): :class:`Adventurer5MBackend`, :class:`Adventurer5MProBackend`,
|
|
5
|
+
:class:`AD5XBackend`.
|
|
6
|
+
- HTTP-only: :class:`Creator5Backend` (Creator 5 and Creator 5 Pro). No TCP code.
|
|
7
|
+
- Legacy (TCP only): :class:`LegacyBackend` (Adventurer 3, Adventurer 4, generic).
|
|
8
|
+
|
|
9
|
+
Use :func:`connect_printer` to get the right one.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .connect import ConnectOptions, ConnectTarget, connect_printer, create_http_backend
|
|
13
|
+
from .creator5_backend import NOZZLE_COUNT, NOZZLE_OFF, TEMP_NO_CHANGE, TEMP_OFF, Creator5Backend
|
|
14
|
+
from .dual_api_backend import DualApiBackend, uses_new_job_format
|
|
15
|
+
from .dual_api_models import AD5XBackend, Adventurer5MBackend, Adventurer5MProBackend
|
|
16
|
+
from .http_core import UPLOAD_TIMEOUT, DetailResult, HttpCore, describe_detail_rejection
|
|
17
|
+
from .http_printer_backend import (
|
|
18
|
+
PRINTER_CTL_NO_CHANGE,
|
|
19
|
+
HttpPrinterBackend,
|
|
20
|
+
encode_material_mappings,
|
|
21
|
+
validate_material_mappings,
|
|
22
|
+
)
|
|
23
|
+
from .legacy_backend import (
|
|
24
|
+
A3Transport,
|
|
25
|
+
A4Transport,
|
|
26
|
+
GenericLegacyTransport,
|
|
27
|
+
LegacyBackend,
|
|
28
|
+
LegacyPrinterInfo,
|
|
29
|
+
LegacyTransport,
|
|
30
|
+
create_legacy_transport,
|
|
31
|
+
)
|
|
32
|
+
from .printer_backend import PrinterBackend, PrinterIdentity
|
|
33
|
+
from .types import (
|
|
34
|
+
FiltrationMode,
|
|
35
|
+
LocalJobParams,
|
|
36
|
+
MaterialMapping,
|
|
37
|
+
PrintOverrides,
|
|
38
|
+
SlotAction,
|
|
39
|
+
UploadJobParams,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
__all__ = [
|
|
43
|
+
"A3Transport",
|
|
44
|
+
"A4Transport",
|
|
45
|
+
"AD5XBackend",
|
|
46
|
+
"Adventurer5MBackend",
|
|
47
|
+
"Adventurer5MProBackend",
|
|
48
|
+
"ConnectOptions",
|
|
49
|
+
"ConnectTarget",
|
|
50
|
+
"Creator5Backend",
|
|
51
|
+
"DetailResult",
|
|
52
|
+
"DualApiBackend",
|
|
53
|
+
"FiltrationMode",
|
|
54
|
+
"GenericLegacyTransport",
|
|
55
|
+
"HttpCore",
|
|
56
|
+
"HttpPrinterBackend",
|
|
57
|
+
"LegacyBackend",
|
|
58
|
+
"LegacyPrinterInfo",
|
|
59
|
+
"LegacyTransport",
|
|
60
|
+
"LocalJobParams",
|
|
61
|
+
"MaterialMapping",
|
|
62
|
+
"NOZZLE_COUNT",
|
|
63
|
+
"NOZZLE_OFF",
|
|
64
|
+
"PRINTER_CTL_NO_CHANGE",
|
|
65
|
+
"PrintOverrides",
|
|
66
|
+
"PrinterBackend",
|
|
67
|
+
"PrinterIdentity",
|
|
68
|
+
"SlotAction",
|
|
69
|
+
"TEMP_NO_CHANGE",
|
|
70
|
+
"TEMP_OFF",
|
|
71
|
+
"UPLOAD_TIMEOUT",
|
|
72
|
+
"UploadJobParams",
|
|
73
|
+
"connect_printer",
|
|
74
|
+
"create_http_backend",
|
|
75
|
+
"create_legacy_transport",
|
|
76
|
+
"describe_detail_rejection",
|
|
77
|
+
"encode_material_mappings",
|
|
78
|
+
"uses_new_job_format",
|
|
79
|
+
"validate_material_mappings",
|
|
80
|
+
]
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The connect factory. It picks the family backend and returns it ready to use.
|
|
3
|
+
|
|
4
|
+
Model resolution:
|
|
5
|
+
|
|
6
|
+
- Modern printer found by discovery: the model comes from the discovery product ID, and
|
|
7
|
+
the connection only verifies the serial number and check code over ``/detail``.
|
|
8
|
+
- No discovery result (manual IP, saved printer): the model comes from the ``/detail``
|
|
9
|
+
response of that check (PID first, then the factory model name).
|
|
10
|
+
- Legacy printer: discovery already names AD3/AD4. Without a discovery result (and with no
|
|
11
|
+
check code), the backend probes the TCP channel with M115.
|
|
12
|
+
- ``force_legacy``: always the legacy family over TCP, even for a modern printer.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import asyncio
|
|
18
|
+
from collections.abc import Callable
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
|
|
21
|
+
from ..capabilities.models import BackendFamily, PrinterModelId
|
|
22
|
+
from ..capabilities.registry import get_model_spec
|
|
23
|
+
from ..capabilities.resolve_model import model_from_machine_type
|
|
24
|
+
from ..discovery.discovery import DiscoveredPrinter
|
|
25
|
+
from ..exceptions import PrinterConnectionError
|
|
26
|
+
from ..models.machine_info import FFMachineInfo
|
|
27
|
+
from ..models.machine_info_parser import MachineInfoParser
|
|
28
|
+
from .creator5_backend import Creator5Backend
|
|
29
|
+
from .dual_api_backend import DualApiBackend
|
|
30
|
+
from .dual_api_models import AD5XBackend, Adventurer5MBackend, Adventurer5MProBackend
|
|
31
|
+
from .http_core import HttpCore
|
|
32
|
+
from .http_printer_backend import HttpPrinterBackend
|
|
33
|
+
from .legacy_backend import LegacyBackend, LegacyTransport, create_legacy_transport
|
|
34
|
+
from .printer_backend import PrinterBackend
|
|
35
|
+
|
|
36
|
+
LegacyTransportFactory = Callable[[PrinterModelId, str, "int | None"], LegacyTransport]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class ConnectTarget:
|
|
41
|
+
"""Where to connect. A :class:`DiscoveredPrinter` also works."""
|
|
42
|
+
|
|
43
|
+
ip_address: str
|
|
44
|
+
#: Serial number. Required for modern printers.
|
|
45
|
+
serial_number: str | None = None
|
|
46
|
+
#: Model from discovery or a saved printer. None when unknown.
|
|
47
|
+
model: PrinterModelId | None = None
|
|
48
|
+
#: HTTP API port from discovery (``event_port``). Default 8898.
|
|
49
|
+
event_port: int | None = None
|
|
50
|
+
#: TCP command port from discovery (``command_port``). Default 8899.
|
|
51
|
+
command_port: int | None = None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True)
|
|
55
|
+
class ConnectOptions:
|
|
56
|
+
"""Options for :func:`connect_printer`."""
|
|
57
|
+
|
|
58
|
+
#: Check code (LAN access code). Required for modern printers.
|
|
59
|
+
check_code: str | None = None
|
|
60
|
+
#: Use the legacy TCP backend even for a modern printer.
|
|
61
|
+
force_legacy: bool = False
|
|
62
|
+
#: Override the HTTP API port.
|
|
63
|
+
http_port: int | None = None
|
|
64
|
+
#: Override the TCP command port.
|
|
65
|
+
tcp_port: int | None = None
|
|
66
|
+
#: HTTP request timeout in seconds.
|
|
67
|
+
timeout_s: float = 15.0
|
|
68
|
+
#: Creates the legacy TCP transport. For tests and custom transports.
|
|
69
|
+
legacy_transport: LegacyTransportFactory | None = None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class _HttpUnreachableError(PrinterConnectionError):
|
|
73
|
+
"""The HTTP API did not answer at all. Internal: lets the factory try the legacy probe."""
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
async def _initialize_or_dispose(backend: HttpPrinterBackend | LegacyBackend) -> bool:
|
|
77
|
+
"""
|
|
78
|
+
Runs ``backend.initialize()``. If the caller cancels or times out the connect while the TCP
|
|
79
|
+
login (``~M601``) is in flight, the backend is disposed before the cancel propagates:
|
|
80
|
+
an abandoned login would keep the socket open and hold the printer's control lock.
|
|
81
|
+
"""
|
|
82
|
+
try:
|
|
83
|
+
return await backend.initialize()
|
|
84
|
+
except BaseException:
|
|
85
|
+
await asyncio.shield(backend.dispose())
|
|
86
|
+
raise
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _is_modern(model: PrinterModelId | None) -> bool:
|
|
90
|
+
if model is None:
|
|
91
|
+
return False
|
|
92
|
+
return get_model_spec(model).family in (BackendFamily.DUAL_API, BackendFamily.HTTP_ONLY)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _legacy_model(model: PrinterModelId | None) -> PrinterModelId:
|
|
96
|
+
if model in (PrinterModelId.ADVENTURER_3, PrinterModelId.ADVENTURER_4):
|
|
97
|
+
return model
|
|
98
|
+
return PrinterModelId.GENERIC_LEGACY
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _as_target(target: ConnectTarget | DiscoveredPrinter) -> ConnectTarget:
|
|
102
|
+
if isinstance(target, ConnectTarget):
|
|
103
|
+
return target
|
|
104
|
+
model = PrinterModelId(str(target.model)) if target.model else None
|
|
105
|
+
return ConnectTarget(
|
|
106
|
+
ip_address=target.ip_address,
|
|
107
|
+
serial_number=target.serial_number,
|
|
108
|
+
model=model,
|
|
109
|
+
event_port=target.event_port,
|
|
110
|
+
command_port=target.command_port,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
async def connect_printer(
|
|
115
|
+
target: ConnectTarget | DiscoveredPrinter,
|
|
116
|
+
options: ConnectOptions | None = None,
|
|
117
|
+
) -> PrinterBackend:
|
|
118
|
+
"""
|
|
119
|
+
Connects to a printer and returns its family backend.
|
|
120
|
+
|
|
121
|
+
Raises:
|
|
122
|
+
PrinterConnectionError: with a plain-language reason when the connection fails.
|
|
123
|
+
FlashForgeResponseError: the printer answered ``/detail`` with a payload that did
|
|
124
|
+
not parse (a library bug report, not a connection problem).
|
|
125
|
+
"""
|
|
126
|
+
opts = options or ConnectOptions()
|
|
127
|
+
tgt = _as_target(target)
|
|
128
|
+
known = tgt.model if tgt.model and tgt.model != PrinterModelId.UNKNOWN else None
|
|
129
|
+
tcp_port = opts.tcp_port if opts.tcp_port is not None else tgt.command_port
|
|
130
|
+
factory = opts.legacy_transport or create_legacy_transport
|
|
131
|
+
|
|
132
|
+
if opts.force_legacy:
|
|
133
|
+
return await _connect_legacy(
|
|
134
|
+
tgt.ip_address,
|
|
135
|
+
PrinterModelId.GENERIC_LEGACY if _is_modern(known) else known,
|
|
136
|
+
tcp_port,
|
|
137
|
+
factory,
|
|
138
|
+
)
|
|
139
|
+
if known is not None and get_model_spec(known).family == BackendFamily.LEGACY:
|
|
140
|
+
return await _connect_legacy(tgt.ip_address, known, tcp_port, factory)
|
|
141
|
+
if not opts.check_code or not tgt.serial_number:
|
|
142
|
+
if _is_modern(known):
|
|
143
|
+
raise PrinterConnectionError(
|
|
144
|
+
"This printer needs its serial number and check code. Enter the check code "
|
|
145
|
+
"shown on the printer."
|
|
146
|
+
)
|
|
147
|
+
# Manual IP with no credentials: only a legacy printer can answer.
|
|
148
|
+
return await _connect_legacy(tgt.ip_address, None, tcp_port, factory)
|
|
149
|
+
try:
|
|
150
|
+
return await _connect_modern(tgt, known, opts, tcp_port)
|
|
151
|
+
except _HttpUnreachableError as error:
|
|
152
|
+
# Manual IP with a check code, but the HTTP API did not answer at all: the printer can
|
|
153
|
+
# be a legacy model, which has no HTTP API. Try the TCP probe; keep the HTTP error if
|
|
154
|
+
# that fails too. A known modern model never gets here as a legacy printer.
|
|
155
|
+
if _is_modern(known):
|
|
156
|
+
raise PrinterConnectionError(str(error)) from None
|
|
157
|
+
try:
|
|
158
|
+
return await _connect_legacy(tgt.ip_address, None, tcp_port, factory)
|
|
159
|
+
except PrinterConnectionError:
|
|
160
|
+
raise PrinterConnectionError(str(error)) from None
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
async def _connect_modern(
|
|
164
|
+
target: ConnectTarget,
|
|
165
|
+
known: PrinterModelId | None,
|
|
166
|
+
opts: ConnectOptions,
|
|
167
|
+
tcp_port: int | None,
|
|
168
|
+
) -> HttpPrinterBackend:
|
|
169
|
+
http = HttpCore(
|
|
170
|
+
target.ip_address,
|
|
171
|
+
target.serial_number or "",
|
|
172
|
+
opts.check_code or "",
|
|
173
|
+
http_port=opts.http_port if opts.http_port is not None else target.event_port,
|
|
174
|
+
timeout_s=opts.timeout_s,
|
|
175
|
+
)
|
|
176
|
+
try:
|
|
177
|
+
detail = await http.fetch_detail()
|
|
178
|
+
if detail.response is None:
|
|
179
|
+
if not detail.reachable:
|
|
180
|
+
raise _HttpUnreachableError(detail.error or "No /detail response.")
|
|
181
|
+
raise PrinterConnectionError(detail.error or "No /detail response.", detail.code)
|
|
182
|
+
info = MachineInfoParser.from_detail(detail.response.detail)
|
|
183
|
+
if info is None:
|
|
184
|
+
raise PrinterConnectionError("The printer returned an invalid /detail response.")
|
|
185
|
+
# The /detail PID and the discovery PID are the same firmware value. Prefer /detail
|
|
186
|
+
# when it names a model; otherwise keep the discovery model.
|
|
187
|
+
model = info.model_id if info.model_id != PrinterModelId.UNKNOWN else known
|
|
188
|
+
if model is None or not _is_modern(model):
|
|
189
|
+
raise PrinterConnectionError(
|
|
190
|
+
f'The printer model is not supported (pid {info.pid}, model "{info.model}").'
|
|
191
|
+
)
|
|
192
|
+
backend = create_http_backend(model, http, info, opts, tcp_port)
|
|
193
|
+
except BaseException:
|
|
194
|
+
await http.close()
|
|
195
|
+
raise
|
|
196
|
+
if not await _initialize_or_dispose(backend):
|
|
197
|
+
await backend.dispose()
|
|
198
|
+
raise PrinterConnectionError(
|
|
199
|
+
"The printer accepted the check code but the control channel did not open."
|
|
200
|
+
if isinstance(backend, DualApiBackend)
|
|
201
|
+
else "The printer accepted the check code but /product failed."
|
|
202
|
+
)
|
|
203
|
+
return backend
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def create_http_backend(
|
|
207
|
+
model: PrinterModelId,
|
|
208
|
+
http: HttpCore,
|
|
209
|
+
info: FFMachineInfo,
|
|
210
|
+
options: ConnectOptions | None = None,
|
|
211
|
+
tcp_port: int | None = None,
|
|
212
|
+
) -> HttpPrinterBackend:
|
|
213
|
+
"""Creates the HTTP backend for a modern model. Exported for tests and custom flows."""
|
|
214
|
+
opts = options or ConnectOptions()
|
|
215
|
+
port = tcp_port if tcp_port is not None else opts.tcp_port
|
|
216
|
+
if model == PrinterModelId.ADVENTURER_5M:
|
|
217
|
+
return Adventurer5MBackend(http, info, tcp_port=port)
|
|
218
|
+
if model == PrinterModelId.ADVENTURER_5M_PRO:
|
|
219
|
+
return Adventurer5MProBackend(http, info, tcp_port=port)
|
|
220
|
+
if model == PrinterModelId.AD5X:
|
|
221
|
+
return AD5XBackend(http, info, tcp_port=port)
|
|
222
|
+
if model in (PrinterModelId.CREATOR_5, PrinterModelId.CREATOR_5_PRO):
|
|
223
|
+
return Creator5Backend(model, http, info)
|
|
224
|
+
raise PrinterConnectionError(f"{model} is not a modern HTTP printer.")
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
async def _probe_legacy_model(
|
|
228
|
+
probe_as: PrinterModelId,
|
|
229
|
+
ip_address: str,
|
|
230
|
+
tcp_port: int | None,
|
|
231
|
+
factory: LegacyTransportFactory,
|
|
232
|
+
) -> PrinterModelId | None:
|
|
233
|
+
"""Logs in with one TCP client type and reads M115. None when it got no valid answer."""
|
|
234
|
+
probe = factory(probe_as, ip_address, tcp_port)
|
|
235
|
+
try:
|
|
236
|
+
if not await probe.init_control():
|
|
237
|
+
return None
|
|
238
|
+
info = await probe.get_info()
|
|
239
|
+
if info is None:
|
|
240
|
+
return None
|
|
241
|
+
return _legacy_model(model_from_machine_type(info.machine_type))
|
|
242
|
+
except Exception: # noqa: BLE001 - a failed probe only means "not this client type"
|
|
243
|
+
return None
|
|
244
|
+
finally:
|
|
245
|
+
# Also runs on cancel/timeout, so an abandoned probe login never keeps the socket.
|
|
246
|
+
await asyncio.shield(probe.dispose())
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
async def _connect_legacy(
|
|
250
|
+
ip_address: str,
|
|
251
|
+
known: PrinterModelId | None,
|
|
252
|
+
tcp_port: int | None,
|
|
253
|
+
factory: LegacyTransportFactory,
|
|
254
|
+
) -> LegacyBackend:
|
|
255
|
+
"""Connects over TCP. Without a known model, M115 decides between AD3, AD4 and generic."""
|
|
256
|
+
model: PrinterModelId | None = _legacy_model(known) if known else None
|
|
257
|
+
if model is None:
|
|
258
|
+
# The Adventurer 3 answers M115 in its own format, which the generic parser cannot
|
|
259
|
+
# read, so the A3 client is the second probe.
|
|
260
|
+
model = await _probe_legacy_model(
|
|
261
|
+
PrinterModelId.GENERIC_LEGACY, ip_address, tcp_port, factory
|
|
262
|
+
) or await _probe_legacy_model(PrinterModelId.ADVENTURER_3, ip_address, tcp_port, factory)
|
|
263
|
+
if model is None:
|
|
264
|
+
raise PrinterConnectionError(
|
|
265
|
+
f"No answer on the TCP command channel at {ip_address}. Check the IP address "
|
|
266
|
+
"and that the printer is on."
|
|
267
|
+
)
|
|
268
|
+
backend = LegacyBackend(model, ip_address, factory(model, ip_address, tcp_port))
|
|
269
|
+
if not await _initialize_or_dispose(backend):
|
|
270
|
+
await backend.dispose()
|
|
271
|
+
raise PrinterConnectionError(
|
|
272
|
+
f"The printer at {ip_address} did not accept the TCP login. Close other apps that "
|
|
273
|
+
"use the printer and try again."
|
|
274
|
+
)
|
|
275
|
+
return backend
|