Compare commits
27 Commits
0.1.0
...
feature/re
Author | SHA1 | Date | |
---|---|---|---|
16dc138e9f | |||
f42d3167c5 | |||
4ea98036e5 | |||
0d16732561 | |||
ef305ee6ce | |||
1f7eed6b23 | |||
55b78cea05 | |||
f35f456eb2 | |||
42ba306e4f | |||
19cac21db6 | |||
4faa240b06 | |||
38a6f58306 | |||
ed33fd8fb1 | |||
53e1f88e1a | |||
8178dea15a | |||
73f77ed659 | |||
a13a5a1e2a | |||
06c9d68390 | |||
07f601694d | |||
98a1f59169 | |||
ba2eadb064 | |||
f5017b5631 | |||
956a261a45 | |||
155f3d2bd3 | |||
75b0a11fef | |||
4da769da49 | |||
527ec9ef3c |
@ -37,12 +37,7 @@ from .printer.bambu_virtual_printer import BambuVirtualPrinter
|
||||
@contextmanager
|
||||
def measure_elapsed():
|
||||
start = perf_counter()
|
||||
|
||||
def _get_elapsed():
|
||||
return perf_counter() - start
|
||||
|
||||
yield _get_elapsed
|
||||
print(f"Total elapsed: {_get_elapsed()}")
|
||||
yield lambda: perf_counter() - start
|
||||
|
||||
|
||||
class BambuPrintPlugin(
|
||||
|
@ -71,7 +71,6 @@ class BambuVirtualPrinter:
|
||||
self._current_state = self._state_idle
|
||||
|
||||
self._running = True
|
||||
self._print_status_reporter = None
|
||||
self._printer_thread = threading.Thread(
|
||||
target=self._printer_worker,
|
||||
name="octoprint.plugins.bambu_printer.printer_state",
|
||||
@ -185,7 +184,6 @@ class BambuVirtualPrinter:
|
||||
self._telemetry.bedTargetTemp = temperatures.target_bed_temp
|
||||
self._telemetry.chamberTemp = temperatures.chamber_temp
|
||||
|
||||
self._log.debug(f"Received printer state update: {print_job_state}")
|
||||
if (
|
||||
print_job_state == "IDLE"
|
||||
or print_job_state == "FINISH"
|
||||
@ -276,9 +274,9 @@ class BambuVirtualPrinter:
|
||||
self.lastN = 0
|
||||
self._running = False
|
||||
|
||||
if self._print_status_reporter is not None:
|
||||
self._print_status_reporter.cancel()
|
||||
self._print_status_reporter = None
|
||||
if self._sdstatus_reporter is not None:
|
||||
self._sdstatus_reporter.cancel()
|
||||
self._sdstatus_reporter = None
|
||||
|
||||
if self._settings.get_boolean(["simulateReset"]):
|
||||
for item in self._settings.get(["resetLines"]):
|
||||
@ -312,16 +310,7 @@ class BambuVirtualPrinter:
|
||||
|
||||
def select_project_file(self, file_path: str) -> bool:
|
||||
self._log.debug(f"Select project file: {file_path}")
|
||||
file_info = self._project_files_view.get_file_by_stem(
|
||||
file_path, [".gcode", ".3mf"]
|
||||
)
|
||||
if (
|
||||
self._selected_project_file is not None
|
||||
and file_info is not None
|
||||
and self._selected_project_file.path == file_info.path
|
||||
):
|
||||
return True
|
||||
|
||||
file_info = self._project_files_view.get_cached_file_data(file_path)
|
||||
if file_info is None:
|
||||
self._log.error(f"Cannot select not existing file: {file_path}")
|
||||
return False
|
||||
@ -339,6 +328,7 @@ class BambuVirtualPrinter:
|
||||
@gcode_executor.register("M23")
|
||||
def _select_sd_file(self, data: str) -> bool:
|
||||
filename = data.split(maxsplit=1)[1].strip()
|
||||
self._list_project_files()
|
||||
return self.select_project_file(filename)
|
||||
|
||||
def _send_file_selected_message(self):
|
||||
@ -365,43 +355,30 @@ class BambuVirtualPrinter:
|
||||
matchS = re.search(r"S([0-9]+)", data)
|
||||
if matchS:
|
||||
interval = int(matchS.group(1))
|
||||
if self._sdstatus_reporter is not None:
|
||||
self._sdstatus_reporter.cancel()
|
||||
|
||||
if interval > 0:
|
||||
self.start_continuous_status_report(interval)
|
||||
self._sdstatus_reporter = RepeatedTimer(
|
||||
interval, self.report_print_job_status
|
||||
)
|
||||
self._sdstatus_reporter.start()
|
||||
else:
|
||||
self.stop_continuous_status_report()
|
||||
self._sdstatus_reporter = None
|
||||
|
||||
self.report_print_job_status()
|
||||
return True
|
||||
|
||||
def start_continuous_status_report(self, interval: int):
|
||||
if self._print_status_reporter is not None:
|
||||
self._print_status_reporter.cancel()
|
||||
|
||||
self._print_status_reporter = RepeatedTimer(
|
||||
interval, self.report_print_job_status
|
||||
)
|
||||
self._print_status_reporter.start()
|
||||
|
||||
def stop_continuous_status_report(self):
|
||||
if self._print_status_reporter is not None:
|
||||
self._print_status_reporter.cancel()
|
||||
self._print_status_reporter = None
|
||||
|
||||
@gcode_executor.register("M30")
|
||||
def _delete_project_file(self, data: str) -> bool:
|
||||
file_path = data.split(maxsplit=1)[1].strip()
|
||||
file_info = self.project_files.get_file_data(file_path)
|
||||
if file_info is not None:
|
||||
self.file_system.delete_file(file_info.path)
|
||||
self._update_project_file_list()
|
||||
else:
|
||||
self._log.error(f"File not found to delete {file_path}")
|
||||
def _delete_sd_file(self, data: str) -> bool:
|
||||
file_path = data.split(None, 1)[1].strip()
|
||||
self._list_project_files()
|
||||
self.file_system.delete_file(Path(file_path))
|
||||
return True
|
||||
|
||||
@gcode_executor.register("M105")
|
||||
def _report_temperatures(self, data: str) -> bool:
|
||||
self._processTemperatureQuery()
|
||||
return True
|
||||
return self._processTemperatureQuery()
|
||||
|
||||
# noinspection PyUnusedLocal
|
||||
@gcode_executor.register_no_data("M115")
|
||||
@ -409,6 +386,7 @@ class BambuVirtualPrinter:
|
||||
self.sendIO("Bambu Printer Integration")
|
||||
self.sendIO("Cap:EXTENDED_M20:1")
|
||||
self.sendIO("Cap:LFN_WRITE:1")
|
||||
self.sendIO("Cap:LFN_WRITE:1")
|
||||
return True
|
||||
|
||||
@gcode_executor.register("M117")
|
||||
@ -486,8 +464,8 @@ class BambuVirtualPrinter:
|
||||
return True
|
||||
|
||||
@gcode_executor.register("M20")
|
||||
def _update_project_file_list(self, data: str = ""):
|
||||
self._project_files_view.update() # internally sends list to serial io
|
||||
def _list_project_files(self, data: str = ""):
|
||||
self._project_files_view.update()
|
||||
return True
|
||||
|
||||
def _list_cached_project_files(self):
|
||||
@ -497,11 +475,10 @@ class BambuVirtualPrinter:
|
||||
):
|
||||
self.sendIO(item)
|
||||
self.sendIO("End file list")
|
||||
self.sendOk()
|
||||
|
||||
@gcode_executor.register_no_data("M24")
|
||||
def _start_resume_sd_print(self):
|
||||
self._current_state.start_new_print()
|
||||
def _start_print(self):
|
||||
self._current_state.start_resume_print()
|
||||
return True
|
||||
|
||||
@gcode_executor.register_no_data("M25")
|
||||
@ -515,30 +492,14 @@ class BambuVirtualPrinter:
|
||||
return True
|
||||
|
||||
def report_print_job_status(self):
|
||||
if self.current_print_job is not None:
|
||||
print_job = self.current_print_job
|
||||
if print_job is not None:
|
||||
self.sendIO(
|
||||
f"SD printing byte {self.current_print_job.file_position}"
|
||||
f"/{self.current_print_job.file_info.size}"
|
||||
f"SD printing byte {print_job.file_position}/{print_job.file_info.size}"
|
||||
)
|
||||
else:
|
||||
self.sendIO("Not SD printing")
|
||||
|
||||
def report_print_finished(self):
|
||||
if self.current_print_job is None:
|
||||
return
|
||||
self._log.debug(
|
||||
f"SD File Print finishing: {self.current_print_job.file_info.file_name}"
|
||||
)
|
||||
self.sendIO("Done printing file")
|
||||
|
||||
def finalize_print_job(self):
|
||||
if self.current_print_job is not None:
|
||||
self.report_print_job_status()
|
||||
self.report_print_finished()
|
||||
self.current_print_job = None
|
||||
self.report_print_job_status()
|
||||
self.change_state(self._state_idle)
|
||||
|
||||
def _create_temperature_message(self) -> str:
|
||||
template = "{heater}:{actual:.2f}/ {target:.2f}"
|
||||
temps = collections.OrderedDict()
|
||||
|
@ -15,9 +15,7 @@ from octoprint_bambu_printer.printer.file_system.file_info import FileInfo
|
||||
@dataclass
|
||||
class CachedFileView:
|
||||
file_system: RemoteSDCardFileList
|
||||
folder_view: dict[tuple[str, str | list[str] | None], None] = field(
|
||||
default_factory=dict
|
||||
) # dict preserves order, but set does not. We use only dict keys as storage
|
||||
folder_view: set[tuple[str, str | list[str] | None]] = field(default_factory=set)
|
||||
on_update: Callable[[], None] | None = None
|
||||
|
||||
def __post_init__(self):
|
||||
@ -27,7 +25,7 @@ class CachedFileView:
|
||||
def with_filter(
|
||||
self, folder: str, extensions: str | list[str] | None = None
|
||||
) -> "CachedFileView":
|
||||
self.folder_view[(folder, extensions)] = None
|
||||
self.folder_view.add((folder, extensions))
|
||||
return self
|
||||
|
||||
def list_all_views(self):
|
||||
@ -35,7 +33,7 @@ class CachedFileView:
|
||||
result: list[FileInfo] = []
|
||||
|
||||
with self.file_system.get_ftps_client() as ftp:
|
||||
for filter in self.folder_view.keys():
|
||||
for filter in self.folder_view:
|
||||
result.extend(self.file_system.list_files(*filter, ftp, existing_files))
|
||||
return result
|
||||
|
||||
@ -46,8 +44,8 @@ class CachedFileView:
|
||||
self.on_update()
|
||||
|
||||
def _update_file_list_cache(self, files: list[FileInfo]):
|
||||
self._file_alias_cache = {info.dosname: info.path.as_posix() for info in files}
|
||||
self._file_data_cache = {info.path.as_posix(): info for info in files}
|
||||
self._file_alias_cache = {info.dosname: info.file_name for info in files}
|
||||
self._file_data_cache = {info.file_name: info for info in files}
|
||||
|
||||
def get_all_info(self):
|
||||
self.update()
|
||||
@ -56,39 +54,26 @@ class CachedFileView:
|
||||
def get_all_cached_info(self):
|
||||
return list(self._file_data_cache.values())
|
||||
|
||||
def get_file_data(self, file_path: str | Path) -> FileInfo | None:
|
||||
file_data = self.get_file_data_cached(file_path)
|
||||
if file_data is None:
|
||||
self.update()
|
||||
file_data = self.get_file_data_cached(file_path)
|
||||
return file_data
|
||||
|
||||
def get_file_data_cached(self, file_path: str | Path) -> FileInfo | None:
|
||||
if isinstance(file_path, str):
|
||||
file_path = Path(file_path).as_posix().strip("/")
|
||||
else:
|
||||
file_path = file_path.as_posix().strip("/")
|
||||
|
||||
if file_path not in self._file_data_cache:
|
||||
file_path = self._file_alias_cache.get(file_path, file_path)
|
||||
return self._file_data_cache.get(file_path, None)
|
||||
|
||||
def get_file_by_stem(self, file_stem: str, allowed_suffixes: list[str]):
|
||||
def get_file_by_suffix(self, file_stem: str, allowed_suffixes: list[str]):
|
||||
if file_stem == "":
|
||||
return None
|
||||
|
||||
file_stem = Path(file_stem).with_suffix("").stem
|
||||
file_data = self._get_file_by_stem_cached(file_stem, allowed_suffixes)
|
||||
file_data = self._get_file_by_suffix_cached(file_stem, allowed_suffixes)
|
||||
if file_data is None:
|
||||
self.update()
|
||||
file_data = self._get_file_by_stem_cached(file_stem, allowed_suffixes)
|
||||
file_data = self._get_file_by_suffix_cached(file_stem, allowed_suffixes)
|
||||
return file_data
|
||||
|
||||
def _get_file_by_stem_cached(self, file_stem: str, allowed_suffixes: list[str]):
|
||||
for file_path_str in self._file_data_cache.keys():
|
||||
file_path = Path(file_path_str)
|
||||
if file_stem == file_path.with_suffix("").stem and all(
|
||||
suffix in allowed_suffixes for suffix in file_path.suffixes
|
||||
):
|
||||
return self.get_file_data_cached(file_path)
|
||||
def get_cached_file_data(self, file_name: str) -> FileInfo | None:
|
||||
file_name = Path(file_name).name
|
||||
file_name = self._file_alias_cache.get(file_name, file_name)
|
||||
return self._file_data_cache.get(file_name, None)
|
||||
|
||||
def _get_file_by_suffix_cached(self, file_stem: str, allowed_suffixes: list[str]):
|
||||
for suffix in allowed_suffixes:
|
||||
file_data = self.get_cached_file_data(
|
||||
Path(file_stem).with_suffix(suffix).as_posix()
|
||||
)
|
||||
if file_data is not None:
|
||||
return file_data
|
||||
return None
|
||||
|
@ -7,6 +7,8 @@ import logging.handlers
|
||||
|
||||
from octoprint.util import get_dos_filename
|
||||
|
||||
from octoprint_bambu_printer.printer.file_system.cached_file_view import CachedFileView
|
||||
|
||||
from .ftps_client import IoTFTPSClient, IoTFTPSConnection
|
||||
from .file_info import FileInfo
|
||||
|
||||
@ -21,7 +23,7 @@ class RemoteSDCardFileList:
|
||||
def delete_file(self, file_path: Path) -> None:
|
||||
try:
|
||||
with self.get_ftps_client() as ftp:
|
||||
if ftp.delete_file(file_path.as_posix()):
|
||||
if ftp.delete_file(str(file_path)):
|
||||
self._logger.debug(f"{file_path} deleted")
|
||||
else:
|
||||
raise RuntimeError(f"Deleting file {file_path} failed")
|
||||
|
@ -1,35 +1,26 @@
|
||||
from __future__ import annotations
|
||||
from pathlib import Path
|
||||
|
||||
from octoprint_bambu_printer.printer.file_system.file_info import FileInfo
|
||||
from octoprint_bambu_printer.printer.print_job import PrintJob
|
||||
from octoprint_bambu_printer.printer.states.a_printer_state import APrinterState
|
||||
|
||||
|
||||
class IdleState(APrinterState):
|
||||
|
||||
def start_new_print(self):
|
||||
def start_resume_print(self):
|
||||
selected_file = self._printer.selected_file
|
||||
if selected_file is None:
|
||||
self._log.warn("Cannot start print job if file was not selected")
|
||||
return
|
||||
|
||||
print_command = self._get_print_command_for_file(selected_file)
|
||||
self._log.debug(f"Sending print command: {print_command}")
|
||||
print_command = self._get_print_command_for_file(selected_file.file_name)
|
||||
if self._printer.bambu_client.publish(print_command):
|
||||
self._log.info(f"Started print for {selected_file.file_name}")
|
||||
self._printer.change_state(self._printer._state_printing)
|
||||
else:
|
||||
self._log.warn(f"Failed to start print for {selected_file.file_name}")
|
||||
self._printer.change_state(self._printer._state_idle)
|
||||
|
||||
def _get_print_command_for_file(self, selected_file: FileInfo):
|
||||
|
||||
# URL to print. Root path, protocol can vary. E.g., if sd card, "ftp:///myfile.3mf", "ftp:///cache/myotherfile.3mf"
|
||||
filesystem_root = (
|
||||
"file:///mnt/sdcard/"
|
||||
if self._printer._settings.get_boolean(["device_type"]) in ["X1", "X1C"]
|
||||
else "file:///"
|
||||
)
|
||||
|
||||
def _get_print_command_for_file(self, selected_file):
|
||||
print_command = {
|
||||
"print": {
|
||||
"sequence_id": 0,
|
||||
@ -40,9 +31,14 @@ class IdleState(APrinterState):
|
||||
"project_id": "0",
|
||||
"subtask_id": "0",
|
||||
"task_id": "0",
|
||||
"subtask_name": selected_file.file_name,
|
||||
"url": f"{filesystem_root}{selected_file.path.as_posix()}",
|
||||
"bed_type": "auto",
|
||||
"subtask_name": f"{selected_file}",
|
||||
"file": f"{selected_file}",
|
||||
"url": (
|
||||
f"file:///mnt/sdcard/{selected_file}"
|
||||
if self._printer._settings.get_boolean(["device_type"])
|
||||
in ["X1", "X1C"]
|
||||
else f"file:///sdcard/{selected_file}"
|
||||
),
|
||||
"timelapse": self._printer._settings.get_boolean(["timelapse"]),
|
||||
"bed_leveling": self._printer._settings.get_boolean(["bed_leveling"]),
|
||||
"flow_cali": self._printer._settings.get_boolean(["flow_cali"]),
|
||||
@ -51,7 +47,6 @@ class IdleState(APrinterState):
|
||||
),
|
||||
"layer_inspect": self._printer._settings.get_boolean(["layer_inspect"]),
|
||||
"use_ams": self._printer._settings.get_boolean(["use_ams"]),
|
||||
"ams_mapping": "",
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,33 +19,35 @@ class PausedState(APrinterState):
|
||||
def __init__(self, printer: BambuVirtualPrinter) -> None:
|
||||
super().__init__(printer)
|
||||
self._pausedLock = threading.Event()
|
||||
self._paused_repeated_report = None
|
||||
|
||||
def init(self):
|
||||
if not self._pausedLock.is_set():
|
||||
self._pausedLock.set()
|
||||
|
||||
self._printer.sendIO("// action:paused")
|
||||
self._printer.start_continuous_status_report(3)
|
||||
self._sendPaused()
|
||||
|
||||
def finalize(self):
|
||||
if self._pausedLock.is_set():
|
||||
self._pausedLock.clear()
|
||||
if self._paused_repeated_report is not None:
|
||||
self._paused_repeated_report.join()
|
||||
self._paused_repeated_report = None
|
||||
|
||||
def start_new_print(self):
|
||||
def _sendPaused(self):
|
||||
if self._printer.current_print_job is None:
|
||||
self._log.warn("job paused, but no print job available?")
|
||||
return
|
||||
paused_timer = RepeatedTimer(
|
||||
interval=3.0,
|
||||
function=self._printer.report_print_job_status,
|
||||
daemon=True,
|
||||
run_first=True,
|
||||
condition=self._pausedLock.is_set,
|
||||
)
|
||||
paused_timer.start()
|
||||
|
||||
def start_resume_print(self):
|
||||
if self._printer.bambu_client.connected:
|
||||
if self._printer.bambu_client.publish(pybambu.commands.RESUME):
|
||||
self._log.info("print resumed")
|
||||
self._printer.change_state(self._printer._state_printing)
|
||||
else:
|
||||
self._log.info("print resume failed")
|
||||
|
||||
def cancel_print(self):
|
||||
if self._printer.bambu_client.connected:
|
||||
if self._printer.bambu_client.publish(pybambu.commands.STOP):
|
||||
self._log.info("print cancelled")
|
||||
self._printer.finalize_print_job()
|
||||
else:
|
||||
self._log.info("print cancel failed")
|
||||
|
@ -53,33 +53,34 @@ class PrintingState(APrinterState):
|
||||
self._printer.report_print_job_status()
|
||||
time.sleep(3)
|
||||
|
||||
self.update_print_job_info()
|
||||
if (
|
||||
self._printer.current_print_job is not None
|
||||
and self._printer.current_print_job.progress >= 100
|
||||
):
|
||||
self._printer.finalize_print_job()
|
||||
if self._printer.current_print_job is None:
|
||||
|
||||
self._log.warn("Printing state was triggered with empty print job")
|
||||
return
|
||||
|
||||
if self._printer.current_print_job.progress >= 100:
|
||||
self._finish_print()
|
||||
|
||||
def update_print_job_info(self):
|
||||
print_job_info = self._printer.bambu_client.get_device().print_job
|
||||
task_name: str = print_job_info.subtask_name
|
||||
project_file_info = self._printer.project_files.get_file_by_stem(
|
||||
task_name, [".gcode", ".3mf"]
|
||||
project_file_info = self._printer.project_files.get_file_by_suffix(
|
||||
task_name, [".3mf", ".gcode.3mf"]
|
||||
)
|
||||
if project_file_info is None:
|
||||
self._log.debug(f"No 3mf file found for {print_job_info}")
|
||||
self._current_print_job = None
|
||||
self._printer.change_state(self._printer._state_idle)
|
||||
return
|
||||
|
||||
progress = print_job_info.print_percentage
|
||||
self._printer.current_print_job = PrintJob(project_file_info, progress)
|
||||
self._printer.select_project_file(project_file_info.path.as_posix())
|
||||
self._printer.select_project_file(project_file_info.file_name)
|
||||
|
||||
def pause_print(self):
|
||||
if self._printer.bambu_client.connected:
|
||||
if self._printer.bambu_client.publish(pybambu.commands.PAUSE):
|
||||
self._log.info("print paused")
|
||||
self._printer.change_state(self._printer._state_paused)
|
||||
else:
|
||||
self._log.info("print pause failed")
|
||||
|
||||
@ -87,6 +88,17 @@ class PrintingState(APrinterState):
|
||||
if self._printer.bambu_client.connected:
|
||||
if self._printer.bambu_client.publish(pybambu.commands.STOP):
|
||||
self._log.info("print cancelled")
|
||||
self._printer.finalize_print_job()
|
||||
self._finish_print()
|
||||
self._printer.change_state(self._printer._state_idle)
|
||||
else:
|
||||
self._log.info("print cancel failed")
|
||||
|
||||
def _finish_print(self):
|
||||
if self._printer.current_print_job is not None:
|
||||
self._log.debug(
|
||||
f"SD File Print finishing: {self._printer.current_print_job.file_info.file_name}"
|
||||
)
|
||||
self._printer.sendIO("Done printing file")
|
||||
self._printer.current_print_job = None
|
||||
|
||||
self._printer.change_state(self._printer._state_idle)
|
||||
|
2
setup.py
2
setup.py
@ -14,7 +14,7 @@ plugin_package = "octoprint_bambu_printer"
|
||||
plugin_name = "OctoPrint-BambuPrinter"
|
||||
|
||||
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
|
||||
plugin_version = "0.1.0"
|
||||
plugin_version = "0.0.23"
|
||||
|
||||
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
|
||||
# module
|
||||
|
@ -2,9 +2,8 @@ from __future__ import annotations
|
||||
from datetime import datetime, timezone
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from octoprint_bambu_printer.printer.file_system.cached_file_view import CachedFileView
|
||||
import pybambu
|
||||
@ -30,9 +29,7 @@ def output_test_folder(output_folder: Path):
|
||||
|
||||
@fixture
|
||||
def log_test():
|
||||
log = logging.getLogger("gcode_unittest")
|
||||
log.setLevel(logging.DEBUG)
|
||||
return log
|
||||
return logging.getLogger("gcode_unittest")
|
||||
|
||||
|
||||
class DictGetter:
|
||||
@ -92,11 +89,7 @@ def project_files_info_ftp():
|
||||
def cache_files_info_ftp():
|
||||
return {
|
||||
"cache/print.3mf": (1200, _ftp_date_format(datetime(2024, 5, 7))),
|
||||
"cache/print3.gcode.3mf": (1200, _ftp_date_format(datetime(2024, 5, 7))),
|
||||
"cache/long file path with spaces.gcode.3mf": (
|
||||
1200,
|
||||
_ftp_date_format(datetime(2024, 5, 7)),
|
||||
),
|
||||
"cache/print2.3mf": (1200, _ftp_date_format(datetime(2024, 5, 7))),
|
||||
}
|
||||
|
||||
|
||||
@ -194,11 +187,8 @@ def test_list_sd_card(printer: BambuVirtualPrinter):
|
||||
assert result[0] == b"Begin file list"
|
||||
assert result[1].endswith(b'"print.3mf"')
|
||||
assert result[2].endswith(b'"print2.3mf"')
|
||||
assert result[3].endswith(b'"print.3mf"')
|
||||
assert result[4].endswith(b'"print3.gcode.3mf"')
|
||||
assert result[-3] == b"End file list"
|
||||
assert result[-2] == b"ok"
|
||||
assert result[-1] == b"ok"
|
||||
assert result[3] == b"End file list"
|
||||
assert result[4] == b"ok"
|
||||
|
||||
|
||||
def test_list_ftp_paths_p1s(settings, ftps_session_mock):
|
||||
@ -249,67 +239,6 @@ def test_list_ftp_paths_x1(settings, ftps_session_mock):
|
||||
)
|
||||
|
||||
|
||||
def test_delete_sd_file_gcode(printer: BambuVirtualPrinter):
|
||||
with patch(
|
||||
"octoprint_bambu_printer.printer.file_system.ftps_client.IoTFTPSConnection.delete_file"
|
||||
) as delete_function:
|
||||
printer.write(b"M30 print.3mf\n")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert result[-1] == b"ok"
|
||||
delete_function.assert_called_with("print.3mf")
|
||||
|
||||
printer.write(b"M30 cache/print.3mf\n")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert result[-1] == b"ok"
|
||||
delete_function.assert_called_with("cache/print.3mf")
|
||||
|
||||
|
||||
def test_delete_sd_file_by_dosname(printer: BambuVirtualPrinter):
|
||||
with patch(
|
||||
"octoprint_bambu_printer.printer.file_system.ftps_client.IoTFTPSConnection.delete_file"
|
||||
) as delete_function:
|
||||
file_info = printer.project_files.get_file_data("cache/print.3mf")
|
||||
assert file_info is not None
|
||||
|
||||
printer.write(b"M30 " + file_info.dosname.encode() + b"\n")
|
||||
printer.flush()
|
||||
assert printer.readlines()[-1] == b"ok"
|
||||
assert delete_function.call_count == 1
|
||||
delete_function.assert_called_with("cache/print.3mf")
|
||||
|
||||
printer.write(b"M30 cache/print.3mf\n")
|
||||
printer.flush()
|
||||
assert printer.readlines()[-1] == b"ok"
|
||||
assert delete_function.call_count == 2
|
||||
delete_function.assert_called_with("cache/print.3mf")
|
||||
|
||||
|
||||
def test_select_project_file_by_stem(printer: BambuVirtualPrinter):
|
||||
printer.write(b"M23 print3\n")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert printer.selected_file is not None
|
||||
assert printer.selected_file.path == Path("cache/print3.gcode.3mf")
|
||||
assert result[-2] == b"File selected"
|
||||
assert result[-1] == b"ok"
|
||||
|
||||
|
||||
def test_select_project_long_name_file_with_multiple_extensions(
|
||||
printer: BambuVirtualPrinter,
|
||||
):
|
||||
printer.write(b"M23 long file path with spaces.gcode.3mf\n")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert printer.selected_file is not None
|
||||
assert printer.selected_file.path == Path(
|
||||
"cache/long file path with spaces.gcode.3mf"
|
||||
)
|
||||
assert result[-2] == b"File selected"
|
||||
assert result[-1] == b"ok"
|
||||
|
||||
|
||||
def test_cannot_start_print_without_file(printer: BambuVirtualPrinter):
|
||||
printer.write(b"M24\n")
|
||||
printer.flush()
|
||||
@ -349,13 +278,9 @@ def test_print_started_with_selected_file(printer: BambuVirtualPrinter, print_jo
|
||||
|
||||
printer.write(b"M24\n")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert result[-1] == b"ok"
|
||||
|
||||
# emulate printer reporting it's status
|
||||
print_job_mock.gcode_state = "RUNNING"
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert result[0] == b"ok"
|
||||
assert isinstance(printer.current_state, PrintingState)
|
||||
|
||||
|
||||
@ -366,26 +291,18 @@ def test_pause_print(printer: BambuVirtualPrinter, bambu_client_mock, print_job_
|
||||
printer.write(b"M23 print.3mf\n")
|
||||
printer.write(b"M24\n")
|
||||
printer.flush()
|
||||
|
||||
print_job_mock.gcode_state = "RUNNING"
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
printer.readlines()
|
||||
assert isinstance(printer.current_state, PrintingState)
|
||||
|
||||
printer.write(b"M25\n") # pausing the print
|
||||
bambu_client_mock.publish.return_value = True
|
||||
printer.write(b"M25\n") # GCode for pausing the print
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert result[-1] == b"ok"
|
||||
|
||||
print_job_mock.gcode_state = "PAUSE"
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
assert result[0] == b"ok"
|
||||
assert isinstance(printer.current_state, PausedState)
|
||||
bambu_client_mock.publish.assert_called_with(pybambu.commands.PAUSE)
|
||||
|
||||
|
||||
def test_events_update_printer_state(printer: BambuVirtualPrinter, print_job_mock):
|
||||
print_job_mock.subtask_name = "print.3mf"
|
||||
print_job_mock.gcode_state = "RUNNING"
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
@ -421,45 +338,10 @@ def test_printer_info_check(printer: BambuVirtualPrinter):
|
||||
assert isinstance(printer.current_state, IdleState)
|
||||
|
||||
|
||||
def test_abort_print_during_printing(printer: BambuVirtualPrinter, print_job_mock):
|
||||
print_job_mock.subtask_name = "print.3mf"
|
||||
|
||||
printer.write(b"M20\nM23 print.3mf\nM24\n")
|
||||
printer.flush()
|
||||
print_job_mock.gcode_state = "RUNNING"
|
||||
print_job_mock.print_percentage = 50
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
printer.readlines()
|
||||
assert isinstance(printer.current_state, PrintingState)
|
||||
|
||||
printer.write(b"M26 S0\n")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert result[-1] == b"ok"
|
||||
assert isinstance(printer.current_state, IdleState)
|
||||
|
||||
|
||||
def test_abort_print_during_pause(printer: BambuVirtualPrinter, print_job_mock):
|
||||
print_job_mock.subtask_name = "print.3mf"
|
||||
|
||||
printer.write(b"M20\nM23 print.3mf\nM24\n")
|
||||
printer.flush()
|
||||
print_job_mock.gcode_state = "RUNNING"
|
||||
printer.new_update("event_printer_data_update")
|
||||
def test_abort_print(printer: BambuVirtualPrinter):
|
||||
printer.write(b"M26\n") # GCode for aborting the print
|
||||
printer.flush()
|
||||
|
||||
printer.write(b"M25\n")
|
||||
printer.flush()
|
||||
print_job_mock.gcode_state = "PAUSE"
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
|
||||
printer.readlines()
|
||||
assert isinstance(printer.current_state, PausedState)
|
||||
|
||||
printer.write(b"M26 S0\n")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert result[-1] == b"ok"
|
||||
assert isinstance(printer.current_state, IdleState)
|
||||
@ -487,9 +369,7 @@ def test_file_selection_does_not_affect_current_print(
|
||||
|
||||
printer.write(b"M23 print.3mf\nM24\n")
|
||||
printer.flush()
|
||||
print_job_mock.gcode_state = "RUNNING"
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
printer.readlines()
|
||||
assert isinstance(printer.current_state, PrintingState)
|
||||
assert printer.current_print_job is not None
|
||||
assert printer.current_print_job.file_info.file_name == "print.3mf"
|
||||
@ -509,9 +389,7 @@ def test_finished_print_job_reset_after_new_file_selected(
|
||||
|
||||
printer.write(b"M23 print.3mf\nM24\n")
|
||||
printer.flush()
|
||||
print_job_mock.gcode_state = "RUNNING"
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
printer.readlines()
|
||||
assert isinstance(printer.current_state, PrintingState)
|
||||
assert printer.current_print_job is not None
|
||||
assert printer.current_print_job.file_info.file_name == "print.3mf"
|
||||
@ -535,28 +413,3 @@ def test_finished_print_job_reset_after_new_file_selected(
|
||||
assert printer.current_print_job is None
|
||||
assert printer.selected_file is not None
|
||||
assert printer.selected_file.file_name == "print2.3mf"
|
||||
|
||||
|
||||
def test_finish_detected_correctly(printer: BambuVirtualPrinter, print_job_mock):
|
||||
print_job_mock.subtask_name = "print.3mf"
|
||||
print_job_mock.gcode_state = "RUNNING"
|
||||
print_job_mock.print_percentage = 99
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
assert isinstance(printer.current_state, PrintingState)
|
||||
assert printer.current_print_job is not None
|
||||
assert printer.current_print_job.file_info.file_name == "print.3mf"
|
||||
assert printer.current_print_job.progress == 99
|
||||
|
||||
print_job_mock.print_percentage = 100
|
||||
print_job_mock.gcode_state = "FINISH"
|
||||
printer.new_update("event_printer_data_update")
|
||||
printer.flush()
|
||||
result = printer.readlines()
|
||||
assert result[-3].endswith(b"1000/1000")
|
||||
assert result[-2] == b"Done printing file"
|
||||
assert result[-1] == b"Not SD printing"
|
||||
assert isinstance(printer.current_state, IdleState)
|
||||
assert printer.current_print_job is None
|
||||
assert printer.selected_file is not None
|
||||
assert printer.selected_file.file_name == "print.3mf"
|
||||
|
Reference in New Issue
Block a user