From 46f51b2bb0dadfff0382af32c22356571bb46d17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Mon, 14 Jun 2021 21:09:55 +0200 Subject: [PATCH] print_stats: add `cancelled` when `CANCEL_PRINT` used (#4366) Before this change, a `CANCEL_PRINT` set a `print_stats` to `paused` that would later be workaround-ed with `fluidd`/`mainsail` to re-define `CANCEL_PRINT`. This sets a proper canceled state, but additionally closes a file from a `virtual_sdcard` context for `canceled`/`error`, as this is no longer resumable from this point. Signed-off-by: Kamil Trzcinski --- klippy/extras/pause_resume.py | 9 ++++++--- klippy/extras/print_stats.py | 12 ++++++++---- klippy/extras/virtual_sdcard.py | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/klippy/extras/pause_resume.py b/klippy/extras/pause_resume.py index 229bd6f4..4d9684f2 100644 --- a/klippy/extras/pause_resume.py +++ b/klippy/extras/pause_resume.py @@ -42,12 +42,14 @@ class PauseResume: return { 'is_paused': self.is_paused } + def is_sd_active(self): + return self.v_sd is not None and self.v_sd.is_active() def send_pause_command(self): # This sends the appropriate pause command from an event. Note # the difference between pause_command_sent and is_paused, the # module isn't officially paused until the PAUSE gcode executes. if not self.pause_command_sent: - if self.v_sd is not None and self.v_sd.is_active(): + if self.is_sd_active(): # Printing from virtual sd, run pause command self.sd_paused = True self.v_sd.do_pause() @@ -88,8 +90,9 @@ class PauseResume: self.is_paused = self.pause_command_sent = False cmd_CANCEL_PRINT_help = ("Cancel the current print") def cmd_CANCEL_PRINT(self, gcmd): - self.cmd_PAUSE(gcmd) - if not self.sd_paused: + if self.is_sd_active() or self.sd_paused: + self.v_sd.do_cancel() + else: gcmd.respond_info("action:cancel") self.cmd_CLEAR_PAUSE(gcmd) diff --git a/klippy/extras/print_stats.py b/klippy/extras/print_stats.py index 7fc816d9..ab300f27 100644 --- a/klippy/extras/print_stats.py +++ b/klippy/extras/print_stats.py @@ -41,11 +41,15 @@ class PrintStats: self._update_filament_usage(curtime) if self.state != "error": self.state = "paused" - def note_error(self, message): - self.state = "error" - self.error_message = message def note_complete(self): - self.state = "complete" + self._note_finish("complete") + def note_error(self, message): + self._note_finish("error", message) + def note_cancel(self): + self._note_finish("cancelled") + def _note_finish(self, state, error_message = ""): + self.state = state + self.error_message = error_message eventtime = self.reactor.monotonic() self.total_duration = eventtime - self.print_start_time if self.filament_used < 0.0000001: diff --git a/klippy/extras/virtual_sdcard.py b/klippy/extras/virtual_sdcard.py index 66fc5ade..3cd77612 100644 --- a/klippy/extras/virtual_sdcard.py +++ b/klippy/extras/virtual_sdcard.py @@ -98,6 +98,13 @@ class VirtualSD: self.must_pause_work = False self.work_timer = self.reactor.register_timer( self.work_handler, self.reactor.NOW) + def do_cancel(self): + if self.current_file is not None: + self.do_pause() + self.current_file.close() + self.current_file = None + self.print_stats.note_cancel() + self.file_position = self.file_size = 0. # G-Code commands def cmd_error(self, gcmd): raise gcmd.error("SD write not supported") @@ -212,6 +219,7 @@ class VirtualSD: gcode_mutex = self.gcode.get_mutex() partial_input = "" lines = [] + error_message = None while not self.must_pause_work: if not lines: # Read more data @@ -245,7 +253,7 @@ class VirtualSD: try: self.gcode.run_script(line) except self.gcode.error as e: - self.print_stats.note_error(str(e)) + error_message = str(e) break except: logging.exception("virtual_sdcard dispatch") @@ -265,7 +273,9 @@ class VirtualSD: logging.info("Exiting SD card print (position %d)", self.file_position) self.work_timer = None self.cmd_from_sd = False - if self.current_file is not None: + if error_message is not None: + self.print_stats.note_error(error_message) + elif self.current_file is not None: self.print_stats.note_pause() else: self.print_stats.note_complete()