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 <ayufan@ayufan.eu>
This commit is contained in:
Kamil Trzciński 2021-06-14 21:09:55 +02:00 committed by GitHub
parent f7279a037d
commit 46f51b2bb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 9 deletions

View File

@ -42,12 +42,14 @@ class PauseResume:
return { return {
'is_paused': self.is_paused '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): def send_pause_command(self):
# This sends the appropriate pause command from an event. Note # This sends the appropriate pause command from an event. Note
# the difference between pause_command_sent and is_paused, the # the difference between pause_command_sent and is_paused, the
# module isn't officially paused until the PAUSE gcode executes. # module isn't officially paused until the PAUSE gcode executes.
if not self.pause_command_sent: 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 # Printing from virtual sd, run pause command
self.sd_paused = True self.sd_paused = True
self.v_sd.do_pause() self.v_sd.do_pause()
@ -88,8 +90,9 @@ class PauseResume:
self.is_paused = self.pause_command_sent = False self.is_paused = self.pause_command_sent = False
cmd_CANCEL_PRINT_help = ("Cancel the current print") cmd_CANCEL_PRINT_help = ("Cancel the current print")
def cmd_CANCEL_PRINT(self, gcmd): def cmd_CANCEL_PRINT(self, gcmd):
self.cmd_PAUSE(gcmd) if self.is_sd_active() or self.sd_paused:
if not self.sd_paused: self.v_sd.do_cancel()
else:
gcmd.respond_info("action:cancel") gcmd.respond_info("action:cancel")
self.cmd_CLEAR_PAUSE(gcmd) self.cmd_CLEAR_PAUSE(gcmd)

View File

@ -41,11 +41,15 @@ class PrintStats:
self._update_filament_usage(curtime) self._update_filament_usage(curtime)
if self.state != "error": if self.state != "error":
self.state = "paused" self.state = "paused"
def note_error(self, message):
self.state = "error"
self.error_message = message
def note_complete(self): 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() eventtime = self.reactor.monotonic()
self.total_duration = eventtime - self.print_start_time self.total_duration = eventtime - self.print_start_time
if self.filament_used < 0.0000001: if self.filament_used < 0.0000001:

View File

@ -98,6 +98,13 @@ class VirtualSD:
self.must_pause_work = False self.must_pause_work = False
self.work_timer = self.reactor.register_timer( self.work_timer = self.reactor.register_timer(
self.work_handler, self.reactor.NOW) 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 # G-Code commands
def cmd_error(self, gcmd): def cmd_error(self, gcmd):
raise gcmd.error("SD write not supported") raise gcmd.error("SD write not supported")
@ -212,6 +219,7 @@ class VirtualSD:
gcode_mutex = self.gcode.get_mutex() gcode_mutex = self.gcode.get_mutex()
partial_input = "" partial_input = ""
lines = [] lines = []
error_message = None
while not self.must_pause_work: while not self.must_pause_work:
if not lines: if not lines:
# Read more data # Read more data
@ -245,7 +253,7 @@ class VirtualSD:
try: try:
self.gcode.run_script(line) self.gcode.run_script(line)
except self.gcode.error as e: except self.gcode.error as e:
self.print_stats.note_error(str(e)) error_message = str(e)
break break
except: except:
logging.exception("virtual_sdcard dispatch") logging.exception("virtual_sdcard dispatch")
@ -265,7 +273,9 @@ class VirtualSD:
logging.info("Exiting SD card print (position %d)", self.file_position) logging.info("Exiting SD card print (position %d)", self.file_position)
self.work_timer = None self.work_timer = None
self.cmd_from_sd = False 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() self.print_stats.note_pause()
else: else:
self.print_stats.note_complete() self.print_stats.note_complete()