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,83 +1,83 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Location information parser for FlashForge 3D printers.
|
|
3
|
-
|
|
4
|
-
This module parses the response from M114 command to extract current
|
|
5
|
-
X, Y, Z coordinates of the printer's print head.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
import logging
|
|
9
|
-
import re
|
|
10
|
-
from typing import Optional
|
|
11
|
-
|
|
12
|
-
logger = logging.getLogger(__name__)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class LocationInfo:
|
|
16
|
-
"""
|
|
17
|
-
Represents the current X, Y, and Z coordinates of the printer's print head.
|
|
18
|
-
|
|
19
|
-
This information is typically parsed from the response of an M114 G-code command,
|
|
20
|
-
which reports the current position.
|
|
21
|
-
"""
|
|
22
|
-
|
|
23
|
-
def __init__(self) -> None:
|
|
24
|
-
"""Initialize empty location info."""
|
|
25
|
-
self.x: str = ""
|
|
26
|
-
"""The current X-axis coordinate as a string (e.g., "10.00")."""
|
|
27
|
-
|
|
28
|
-
self.y: str = ""
|
|
29
|
-
"""The current Y-axis coordinate as a string (e.g., "20.50")."""
|
|
30
|
-
|
|
31
|
-
self.z: str = ""
|
|
32
|
-
"""The current Z-axis coordinate as a string (e.g., "5.25")."""
|
|
33
|
-
|
|
34
|
-
def from_replay(self, replay: str) -> Optional["LocationInfo"]:
|
|
35
|
-
"""
|
|
36
|
-
Parse a raw string replay from M114 command to populate coordinate info.
|
|
37
|
-
|
|
38
|
-
The parsing logic assumes the replay is a multi-line string where the second line
|
|
39
|
-
(data[1]) contains the coordinate data in a format like "X:10.00 Y:20.50 Z:5.25 ...".
|
|
40
|
-
It splits this line by spaces and then extracts the values for X, Y, and Z by
|
|
41
|
-
removing the prefixes "X:", "Y:", and "Z:".
|
|
42
|
-
|
|
43
|
-
Args:
|
|
44
|
-
replay: The raw multi-line string response from the printer
|
|
45
|
-
|
|
46
|
-
Returns:
|
|
47
|
-
The populated LocationInfo instance, or None if parsing fails
|
|
48
|
-
"""
|
|
49
|
-
try:
|
|
50
|
-
lines = [
|
|
51
|
-
line.strip() for line in replay.replace("\r", "\n").split("\n") if line.strip()
|
|
52
|
-
]
|
|
53
|
-
coordinate_line = next(
|
|
54
|
-
(line for line in lines if "X:" in line and "Y:" in line and "Z:" in line),
|
|
55
|
-
"",
|
|
56
|
-
)
|
|
57
|
-
if not coordinate_line:
|
|
58
|
-
logger.error("LocationInfo replay has bad/null data")
|
|
59
|
-
return None
|
|
60
|
-
|
|
61
|
-
x_match = re.search(r"X:\s*([^\s]+)", coordinate_line)
|
|
62
|
-
y_match = re.search(r"Y:\s*([^\s]+)", coordinate_line)
|
|
63
|
-
z_match = re.search(r"Z:\s*([^\s]+)", coordinate_line)
|
|
64
|
-
if not x_match or not y_match or not z_match:
|
|
65
|
-
logger.error("LocationInfo replay has bad/null data")
|
|
66
|
-
return None
|
|
67
|
-
|
|
68
|
-
self.x = x_match.group(1)
|
|
69
|
-
self.y = y_match.group(1)
|
|
70
|
-
self.z = z_match.group(1)
|
|
71
|
-
return self
|
|
72
|
-
except Exception:
|
|
73
|
-
logger.error("LocationInfo replay has bad/null data")
|
|
74
|
-
return None
|
|
75
|
-
|
|
76
|
-
def __str__(self) -> str:
|
|
77
|
-
"""
|
|
78
|
-
Return a string representation of the location information.
|
|
79
|
-
|
|
80
|
-
Returns:
|
|
81
|
-
A string in the format "X: [X_value] Y: [Y_value] Z: [Z_value]"
|
|
82
|
-
"""
|
|
83
|
-
return f"X: {self.x} Y: {self.y} Z: {self.z}"
|
|
1
|
+
"""
|
|
2
|
+
Location information parser for FlashForge 3D printers.
|
|
3
|
+
|
|
4
|
+
This module parses the response from M114 command to extract current
|
|
5
|
+
X, Y, Z coordinates of the printer's print head.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import logging
|
|
9
|
+
import re
|
|
10
|
+
from typing import Optional
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class LocationInfo:
|
|
16
|
+
"""
|
|
17
|
+
Represents the current X, Y, and Z coordinates of the printer's print head.
|
|
18
|
+
|
|
19
|
+
This information is typically parsed from the response of an M114 G-code command,
|
|
20
|
+
which reports the current position.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self) -> None:
|
|
24
|
+
"""Initialize empty location info."""
|
|
25
|
+
self.x: str = ""
|
|
26
|
+
"""The current X-axis coordinate as a string (e.g., "10.00")."""
|
|
27
|
+
|
|
28
|
+
self.y: str = ""
|
|
29
|
+
"""The current Y-axis coordinate as a string (e.g., "20.50")."""
|
|
30
|
+
|
|
31
|
+
self.z: str = ""
|
|
32
|
+
"""The current Z-axis coordinate as a string (e.g., "5.25")."""
|
|
33
|
+
|
|
34
|
+
def from_replay(self, replay: str) -> Optional["LocationInfo"]:
|
|
35
|
+
"""
|
|
36
|
+
Parse a raw string replay from M114 command to populate coordinate info.
|
|
37
|
+
|
|
38
|
+
The parsing logic assumes the replay is a multi-line string where the second line
|
|
39
|
+
(data[1]) contains the coordinate data in a format like "X:10.00 Y:20.50 Z:5.25 ...".
|
|
40
|
+
It splits this line by spaces and then extracts the values for X, Y, and Z by
|
|
41
|
+
removing the prefixes "X:", "Y:", and "Z:".
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
replay: The raw multi-line string response from the printer
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
The populated LocationInfo instance, or None if parsing fails
|
|
48
|
+
"""
|
|
49
|
+
try:
|
|
50
|
+
lines = [
|
|
51
|
+
line.strip() for line in replay.replace("\r", "\n").split("\n") if line.strip()
|
|
52
|
+
]
|
|
53
|
+
coordinate_line = next(
|
|
54
|
+
(line for line in lines if "X:" in line and "Y:" in line and "Z:" in line),
|
|
55
|
+
"",
|
|
56
|
+
)
|
|
57
|
+
if not coordinate_line:
|
|
58
|
+
logger.error("LocationInfo replay has bad/null data")
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
x_match = re.search(r"X:\s*([^\s]+)", coordinate_line)
|
|
62
|
+
y_match = re.search(r"Y:\s*([^\s]+)", coordinate_line)
|
|
63
|
+
z_match = re.search(r"Z:\s*([^\s]+)", coordinate_line)
|
|
64
|
+
if not x_match or not y_match or not z_match:
|
|
65
|
+
logger.error("LocationInfo replay has bad/null data")
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
self.x = x_match.group(1)
|
|
69
|
+
self.y = y_match.group(1)
|
|
70
|
+
self.z = z_match.group(1)
|
|
71
|
+
return self
|
|
72
|
+
except Exception:
|
|
73
|
+
logger.error("LocationInfo replay has bad/null data")
|
|
74
|
+
return None
|
|
75
|
+
|
|
76
|
+
def __str__(self) -> str:
|
|
77
|
+
"""
|
|
78
|
+
Return a string representation of the location information.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
A string in the format "X: [X_value] Y: [Y_value] Z: [Z_value]"
|
|
82
|
+
"""
|
|
83
|
+
return f"X: {self.x} Y: {self.y} Z: {self.z}"
|
|
@@ -1,180 +1,180 @@
|
|
|
1
|
-
"""
|
|
2
|
-
FlashForge Python API - Print Status Parser
|
|
3
|
-
|
|
4
|
-
Parses print progress information from M27 command responses.
|
|
5
|
-
"""
|
|
6
|
-
|
|
7
|
-
import logging
|
|
8
|
-
from typing import Optional
|
|
9
|
-
|
|
10
|
-
logger = logging.getLogger(__name__)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class PrintStatus:
|
|
14
|
-
"""
|
|
15
|
-
Represents the status of an ongoing print job, including SD card byte progress and layer progress.
|
|
16
|
-
|
|
17
|
-
This information is typically parsed from the response of an M27 G-code command,
|
|
18
|
-
which reports the print progress from the SD card.
|
|
19
|
-
"""
|
|
20
|
-
|
|
21
|
-
def __init__(self) -> None:
|
|
22
|
-
"""Initialize a new PrintStatus instance."""
|
|
23
|
-
self.sd_current: str = ""
|
|
24
|
-
self.sd_total: str = ""
|
|
25
|
-
self.layer_current: str = ""
|
|
26
|
-
self.layer_total: str = ""
|
|
27
|
-
|
|
28
|
-
def from_replay(self, replay: str) -> Optional["PrintStatus"]:
|
|
29
|
-
"""
|
|
30
|
-
Parses a raw string replay (typically from an M27 command) to populate
|
|
31
|
-
the print status properties of this instance.
|
|
32
|
-
|
|
33
|
-
The parsing logic expects a multi-line string:
|
|
34
|
-
- Line 1 (data[0]): Usually a command echo, ignored
|
|
35
|
-
- Line 2 (data[1]): Contains SD card progress, e.g., "SD printing byte 12345/67890"
|
|
36
|
-
It extracts the current and total bytes
|
|
37
|
-
- Line 3 (data[2]): Contains layer progress, e.g., "Layer: 10/250"
|
|
38
|
-
It extracts the current and total layers
|
|
39
|
-
|
|
40
|
-
Args:
|
|
41
|
-
replay: The raw multi-line string response from the printer
|
|
42
|
-
|
|
43
|
-
Returns:
|
|
44
|
-
The populated PrintStatus instance, or None if parsing fails
|
|
45
|
-
"""
|
|
46
|
-
if not replay:
|
|
47
|
-
return None
|
|
48
|
-
|
|
49
|
-
try:
|
|
50
|
-
self.sd_current = ""
|
|
51
|
-
self.sd_total = ""
|
|
52
|
-
self.layer_current = ""
|
|
53
|
-
self.layer_total = ""
|
|
54
|
-
lines = [
|
|
55
|
-
line.strip() for line in replay.replace("\r", "\n").split("\n") if line.strip()
|
|
56
|
-
]
|
|
57
|
-
|
|
58
|
-
for line in lines:
|
|
59
|
-
if "SD printing byte " in line:
|
|
60
|
-
sd_progress = line.split("SD printing byte ", 1)[1].strip().strip('"')
|
|
61
|
-
sd_progress_data = sd_progress.split("/")
|
|
62
|
-
if len(sd_progress_data) >= 2:
|
|
63
|
-
self.sd_current = sd_progress_data[0].strip()
|
|
64
|
-
self.sd_total = sd_progress_data[1].strip()
|
|
65
|
-
elif line.startswith("Layer:"):
|
|
66
|
-
layer_progress = line.replace("Layer:", "", 1).strip()
|
|
67
|
-
lp_data = layer_progress.split("/")
|
|
68
|
-
if len(lp_data) >= 2:
|
|
69
|
-
self.layer_current = lp_data[0].strip()
|
|
70
|
-
self.layer_total = lp_data[1].strip()
|
|
71
|
-
else:
|
|
72
|
-
return None
|
|
73
|
-
|
|
74
|
-
if not self.sd_current or not self.sd_total:
|
|
75
|
-
logger.warning("PrintStatus: invalid SD progress format.")
|
|
76
|
-
return None
|
|
77
|
-
|
|
78
|
-
return self
|
|
79
|
-
|
|
80
|
-
except Exception as e:
|
|
81
|
-
logger.warning("Error parsing the print status: %s", e)
|
|
82
|
-
return None
|
|
83
|
-
|
|
84
|
-
def get_print_percent(self) -> float:
|
|
85
|
-
"""
|
|
86
|
-
Calculates the print progress percentage based on the current and total layers.
|
|
87
|
-
The result is clamped between 0 and 100.
|
|
88
|
-
|
|
89
|
-
Returns:
|
|
90
|
-
The print progress percentage (0-100), rounded to the nearest integer.
|
|
91
|
-
Returns NaN if layer information is not available or invalid.
|
|
92
|
-
"""
|
|
93
|
-
try:
|
|
94
|
-
current_layer = int(self.layer_current)
|
|
95
|
-
total_layers = int(self.layer_total)
|
|
96
|
-
|
|
97
|
-
if total_layers == 0:
|
|
98
|
-
return float("nan")
|
|
99
|
-
|
|
100
|
-
perc = (current_layer / total_layers) * 100
|
|
101
|
-
return round(min(100, max(0, perc))) # Clamp between 0 and 100
|
|
102
|
-
|
|
103
|
-
except (ValueError, TypeError):
|
|
104
|
-
return self.get_sd_percent()
|
|
105
|
-
|
|
106
|
-
def get_layer_progress(self) -> str:
|
|
107
|
-
"""
|
|
108
|
-
Gets the layer progress as a string.
|
|
109
|
-
|
|
110
|
-
Returns:
|
|
111
|
-
A string in the format "currentLayer/totalLayers"
|
|
112
|
-
"""
|
|
113
|
-
return f"{self.layer_current}/{self.layer_total}"
|
|
114
|
-
|
|
115
|
-
def get_sd_progress(self) -> str:
|
|
116
|
-
"""
|
|
117
|
-
Gets the SD card byte progress as a string.
|
|
118
|
-
|
|
119
|
-
Returns:
|
|
120
|
-
A string in the format "currentBytes/totalBytes"
|
|
121
|
-
"""
|
|
122
|
-
return f"{self.sd_current}/{self.sd_total}"
|
|
123
|
-
|
|
124
|
-
def get_sd_percent(self) -> float:
|
|
125
|
-
"""
|
|
126
|
-
Calculates the SD card progress percentage based on current and total bytes.
|
|
127
|
-
|
|
128
|
-
Returns:
|
|
129
|
-
The SD progress percentage (0-100), or NaN if data is invalid
|
|
130
|
-
"""
|
|
131
|
-
try:
|
|
132
|
-
current_bytes = int(self.sd_current)
|
|
133
|
-
total_bytes = int(self.sd_total)
|
|
134
|
-
|
|
135
|
-
if total_bytes == 0:
|
|
136
|
-
return float("nan")
|
|
137
|
-
|
|
138
|
-
perc = (current_bytes / total_bytes) * 100
|
|
139
|
-
return round(min(100, max(0, perc))) # Clamp between 0 and 100
|
|
140
|
-
|
|
141
|
-
except (ValueError, TypeError):
|
|
142
|
-
return float("nan")
|
|
143
|
-
|
|
144
|
-
def is_complete(self) -> bool:
|
|
145
|
-
"""
|
|
146
|
-
Checks if the print is complete based on layer progress.
|
|
147
|
-
|
|
148
|
-
Returns:
|
|
149
|
-
True if current layer equals total layers, False otherwise
|
|
150
|
-
"""
|
|
151
|
-
try:
|
|
152
|
-
current = int(self.layer_current)
|
|
153
|
-
total = int(self.layer_total)
|
|
154
|
-
return current >= total and total > 0
|
|
155
|
-
except (ValueError, TypeError):
|
|
156
|
-
sd_percent = self.get_sd_percent()
|
|
157
|
-
return sd_percent == 100 if sd_percent == sd_percent else False
|
|
158
|
-
|
|
159
|
-
def __str__(self) -> str:
|
|
160
|
-
"""String representation of the print status."""
|
|
161
|
-
layer_perc = self.get_print_percent()
|
|
162
|
-
sd_perc = self.get_sd_percent()
|
|
163
|
-
|
|
164
|
-
layer_str = "nan%" if layer_perc != layer_perc else f"{layer_perc}%" # Check for NaN
|
|
165
|
-
sd_str = "nan%" if sd_perc != sd_perc else f"{sd_perc}%" # Check for NaN
|
|
166
|
-
|
|
167
|
-
return (
|
|
168
|
-
f"PrintStatus(layer={self.get_layer_progress()} [{layer_str}], "
|
|
169
|
-
f"sd={self.get_sd_progress()} [{sd_str}])"
|
|
170
|
-
)
|
|
171
|
-
|
|
172
|
-
def __repr__(self) -> str:
|
|
173
|
-
"""Detailed string representation for debugging."""
|
|
174
|
-
return (
|
|
175
|
-
f"PrintStatus("
|
|
176
|
-
f"sd_current='{self.sd_current}', "
|
|
177
|
-
f"sd_total='{self.sd_total}', "
|
|
178
|
-
f"layer_current='{self.layer_current}', "
|
|
179
|
-
f"layer_total='{self.layer_total}')"
|
|
180
|
-
)
|
|
1
|
+
"""
|
|
2
|
+
FlashForge Python API - Print Status Parser
|
|
3
|
+
|
|
4
|
+
Parses print progress information from M27 command responses.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
from typing import Optional
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PrintStatus:
|
|
14
|
+
"""
|
|
15
|
+
Represents the status of an ongoing print job, including SD card byte progress and layer progress.
|
|
16
|
+
|
|
17
|
+
This information is typically parsed from the response of an M27 G-code command,
|
|
18
|
+
which reports the print progress from the SD card.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self) -> None:
|
|
22
|
+
"""Initialize a new PrintStatus instance."""
|
|
23
|
+
self.sd_current: str = ""
|
|
24
|
+
self.sd_total: str = ""
|
|
25
|
+
self.layer_current: str = ""
|
|
26
|
+
self.layer_total: str = ""
|
|
27
|
+
|
|
28
|
+
def from_replay(self, replay: str) -> Optional["PrintStatus"]:
|
|
29
|
+
"""
|
|
30
|
+
Parses a raw string replay (typically from an M27 command) to populate
|
|
31
|
+
the print status properties of this instance.
|
|
32
|
+
|
|
33
|
+
The parsing logic expects a multi-line string:
|
|
34
|
+
- Line 1 (data[0]): Usually a command echo, ignored
|
|
35
|
+
- Line 2 (data[1]): Contains SD card progress, e.g., "SD printing byte 12345/67890"
|
|
36
|
+
It extracts the current and total bytes
|
|
37
|
+
- Line 3 (data[2]): Contains layer progress, e.g., "Layer: 10/250"
|
|
38
|
+
It extracts the current and total layers
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
replay: The raw multi-line string response from the printer
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
The populated PrintStatus instance, or None if parsing fails
|
|
45
|
+
"""
|
|
46
|
+
if not replay:
|
|
47
|
+
return None
|
|
48
|
+
|
|
49
|
+
try:
|
|
50
|
+
self.sd_current = ""
|
|
51
|
+
self.sd_total = ""
|
|
52
|
+
self.layer_current = ""
|
|
53
|
+
self.layer_total = ""
|
|
54
|
+
lines = [
|
|
55
|
+
line.strip() for line in replay.replace("\r", "\n").split("\n") if line.strip()
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
for line in lines:
|
|
59
|
+
if "SD printing byte " in line:
|
|
60
|
+
sd_progress = line.split("SD printing byte ", 1)[1].strip().strip('"')
|
|
61
|
+
sd_progress_data = sd_progress.split("/")
|
|
62
|
+
if len(sd_progress_data) >= 2:
|
|
63
|
+
self.sd_current = sd_progress_data[0].strip()
|
|
64
|
+
self.sd_total = sd_progress_data[1].strip()
|
|
65
|
+
elif line.startswith("Layer:"):
|
|
66
|
+
layer_progress = line.replace("Layer:", "", 1).strip()
|
|
67
|
+
lp_data = layer_progress.split("/")
|
|
68
|
+
if len(lp_data) >= 2:
|
|
69
|
+
self.layer_current = lp_data[0].strip()
|
|
70
|
+
self.layer_total = lp_data[1].strip()
|
|
71
|
+
else:
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
if not self.sd_current or not self.sd_total:
|
|
75
|
+
logger.warning("PrintStatus: invalid SD progress format.")
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
return self
|
|
79
|
+
|
|
80
|
+
except Exception as e:
|
|
81
|
+
logger.warning("Error parsing the print status: %s", e)
|
|
82
|
+
return None
|
|
83
|
+
|
|
84
|
+
def get_print_percent(self) -> float:
|
|
85
|
+
"""
|
|
86
|
+
Calculates the print progress percentage based on the current and total layers.
|
|
87
|
+
The result is clamped between 0 and 100.
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
The print progress percentage (0-100), rounded to the nearest integer.
|
|
91
|
+
Returns NaN if layer information is not available or invalid.
|
|
92
|
+
"""
|
|
93
|
+
try:
|
|
94
|
+
current_layer = int(self.layer_current)
|
|
95
|
+
total_layers = int(self.layer_total)
|
|
96
|
+
|
|
97
|
+
if total_layers == 0:
|
|
98
|
+
return float("nan")
|
|
99
|
+
|
|
100
|
+
perc = (current_layer / total_layers) * 100
|
|
101
|
+
return round(min(100, max(0, perc))) # Clamp between 0 and 100
|
|
102
|
+
|
|
103
|
+
except (ValueError, TypeError):
|
|
104
|
+
return self.get_sd_percent()
|
|
105
|
+
|
|
106
|
+
def get_layer_progress(self) -> str:
|
|
107
|
+
"""
|
|
108
|
+
Gets the layer progress as a string.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
A string in the format "currentLayer/totalLayers"
|
|
112
|
+
"""
|
|
113
|
+
return f"{self.layer_current}/{self.layer_total}"
|
|
114
|
+
|
|
115
|
+
def get_sd_progress(self) -> str:
|
|
116
|
+
"""
|
|
117
|
+
Gets the SD card byte progress as a string.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
A string in the format "currentBytes/totalBytes"
|
|
121
|
+
"""
|
|
122
|
+
return f"{self.sd_current}/{self.sd_total}"
|
|
123
|
+
|
|
124
|
+
def get_sd_percent(self) -> float:
|
|
125
|
+
"""
|
|
126
|
+
Calculates the SD card progress percentage based on current and total bytes.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
The SD progress percentage (0-100), or NaN if data is invalid
|
|
130
|
+
"""
|
|
131
|
+
try:
|
|
132
|
+
current_bytes = int(self.sd_current)
|
|
133
|
+
total_bytes = int(self.sd_total)
|
|
134
|
+
|
|
135
|
+
if total_bytes == 0:
|
|
136
|
+
return float("nan")
|
|
137
|
+
|
|
138
|
+
perc = (current_bytes / total_bytes) * 100
|
|
139
|
+
return round(min(100, max(0, perc))) # Clamp between 0 and 100
|
|
140
|
+
|
|
141
|
+
except (ValueError, TypeError):
|
|
142
|
+
return float("nan")
|
|
143
|
+
|
|
144
|
+
def is_complete(self) -> bool:
|
|
145
|
+
"""
|
|
146
|
+
Checks if the print is complete based on layer progress.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
True if current layer equals total layers, False otherwise
|
|
150
|
+
"""
|
|
151
|
+
try:
|
|
152
|
+
current = int(self.layer_current)
|
|
153
|
+
total = int(self.layer_total)
|
|
154
|
+
return current >= total and total > 0
|
|
155
|
+
except (ValueError, TypeError):
|
|
156
|
+
sd_percent = self.get_sd_percent()
|
|
157
|
+
return sd_percent == 100 if sd_percent == sd_percent else False
|
|
158
|
+
|
|
159
|
+
def __str__(self) -> str:
|
|
160
|
+
"""String representation of the print status."""
|
|
161
|
+
layer_perc = self.get_print_percent()
|
|
162
|
+
sd_perc = self.get_sd_percent()
|
|
163
|
+
|
|
164
|
+
layer_str = "nan%" if layer_perc != layer_perc else f"{layer_perc}%" # Check for NaN
|
|
165
|
+
sd_str = "nan%" if sd_perc != sd_perc else f"{sd_perc}%" # Check for NaN
|
|
166
|
+
|
|
167
|
+
return (
|
|
168
|
+
f"PrintStatus(layer={self.get_layer_progress()} [{layer_str}], "
|
|
169
|
+
f"sd={self.get_sd_progress()} [{sd_str}])"
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
def __repr__(self) -> str:
|
|
173
|
+
"""Detailed string representation for debugging."""
|
|
174
|
+
return (
|
|
175
|
+
f"PrintStatus("
|
|
176
|
+
f"sd_current='{self.sd_current}', "
|
|
177
|
+
f"sd_total='{self.sd_total}', "
|
|
178
|
+
f"layer_current='{self.layer_current}', "
|
|
179
|
+
f"layer_total='{self.layer_total}')"
|
|
180
|
+
)
|