Skip to content

Commit

Permalink
More work on Issue #516
Browse files Browse the repository at this point in the history
  • Loading branch information
FormerLurker committed May 8, 2020
1 parent f42145a commit 18b96c5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 33 deletions.
60 changes: 32 additions & 28 deletions octoprint_octolapse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2339,20 +2339,6 @@ def on_event(self, event, payload):
# If we haven't loaded our settings yet, return.
if self._octolapse_settings is None or self._timelapse is None or self._timelapse.get_current_state() == TimelapseState.Idle:
return

if event == Events.PRINT_STARTED:
# warn and cancel print if not printing locally
if not self._octolapse_settings.main_settings.is_octolapse_enabled:
return

if (
payload["origin"] != "local"
):
self._timelapse.end_timelapse("FAILED")
error = error_messages.get_error(["init", "cant_print_from_sd"])
logger.info(error["description"])
self.on_print_start_failed([error])
return
if event == Events.PRINTER_STATE_CHANGED:
self.send_state_changed_message({"status": self.get_status_dict()})
elif event == Events.CONNECTIVITY_CHANGED:
Expand Down Expand Up @@ -2396,15 +2382,14 @@ def on_print_paused(self):
self._timelapse.on_print_paused()

def on_print_start(self, parsed_command):
"""Return True in order to release the printer job lock, False keeps it locked."""
logger.info(
"Print start detected, attempting to start timelapse."
)
# check for problems starting the timelapse
try:
results = self.test_timelapse_config()
if not results["success"]:
self.on_print_start_failed(results["error"])
self.on_print_start_failed(results["error"], parsed_command=parsed_command)
return

# get all of the settings we need
Expand All @@ -2413,11 +2398,11 @@ def on_print_start(self, parsed_command):
errors = timelapse_settings["errors"]
if len(errors) == 0:
errors = timelapse_settings["warnings"]
self.on_print_start_failed(errors)
self.on_print_start_failed(errors, parsed_command=parsed_command)
return

if len(timelapse_settings["warnings"]) > 0:
self.send_plugin_errors("warning", timelapse_settings["warnings"])
self.send_plugin_errors("warning", timelapse_settings["warnings"], parsed_command=parsed_command)

settings_clone = timelapse_settings["settings"]
current_trigger_clone = settings_clone.profiles.current_trigger()
Expand All @@ -2428,13 +2413,15 @@ def on_print_start(self, parsed_command):
timelapse_settings, parsed_command
)
# exit and allow the timelapse pre-processing routine to complete.
return
if self.start_timelapse(timelapse_settings):
self._timelapse.release_job_on_hold_lock()
elif self.start_timelapse(timelapse_settings):
self._timelapse.release_job_on_hold_lock(parsed_command=parsed_command)

except Exception as e:
error = error_messages.get_error(["init", "timelapse_start_exception"])
logger.exception("Unable to start the timelapse.")
self.on_print_start_failed([error])
self.on_print_start_failed([error], parsed_command=parsed_command)



def test_timelapse_config(self):
if not self._octolapse_settings.main_settings.is_octolapse_enabled:
Expand Down Expand Up @@ -2792,18 +2779,34 @@ def start_timelapse(self, timelapse_settings, snapshot_plans=None):
self.on_timelapse_start()
return True

def on_print_start_failed(self, errors):
def on_print_start_failed(self, errors, parsed_command=None):
if not isinstance(errors, list):
errors = [errors]

if self._octolapse_settings.main_settings.cancel_print_on_startup_error:
cancel_print = self._octolapse_settings.main_settings.cancel_print_on_startup_error
is_warning = not cancel_print

if len(errors) == 1:
error = errors[0]
if "options" in error:
options = error["options"]
if "is_warning" in options:
is_warning = options["is_warning"]
if "cancel_print" in options:
cancel_print = options["cancel_print"]

if cancel_print:
parsed_command = None # don't send any commands, we've cancelled.
self._printer.cancel_print(tags={'octolapse-startup-failed'})
self.send_plugin_errors("print-start-error", errors=errors)
else:

if is_warning:
self.send_plugin_errors("print-start-warning", errors=errors)
else:
self.send_plugin_errors("print-start-error", errors=errors)

# see if there is a job lock, if you find one release it, and don't wait for signals.
self._timelapse.release_job_on_hold_lock(True)
self._timelapse.release_job_on_hold_lock(force=True, reset=True, parsed_command=parsed_command)


def on_preprocessing_failed(self, errors):
message_type = "gcode-preprocessing-failed"
Expand Down Expand Up @@ -2856,6 +2859,7 @@ def cancel_preprocessing(self):
self.snapshot_plan_preview_close_time = 0
self.saved_preprocessing_quality_issues = ""
self.saved_missed_snapshots = 0
self._timelapse.release_job_on_hold_lock(reset=True)

def pre_processing_cancelled(self):
# signal complete to the UI (will close the progress popup
Expand All @@ -2870,7 +2874,7 @@ def pre_processing_failed(self, preprocessing_issues):
errors=preprocessing_issues
)
# cancel the print
self._printer.cancel_print(tags={'octolapse-preprocessing-cancelled'})
self._printer.cancel_print(tags={'preprocessing-cancelled'})
# inform the timelapse object that preprocessing has failed
self._timelapse.preprocessing_finished(None)
# close the UI progress popup
Expand Down
10 changes: 7 additions & 3 deletions octoprint_octolapse/error_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@
},
'cant_print_from_sd': {
"name": "Can't start from SD",
"description": "Octolapse cannot be used when printing from the SD card. Disable Octolapse to print from "
"SD.",
'help_link': "error_help_init_cant_print_from_sd.md"
"description": "Octolapse cannot be used when printing from the SD card. Your print will continue. "
"Disable the plugin from within the Octolapse tab to prevent this message from displaying.",
'help_link': "error_help_init_cant_print_from_sd.md",
'options': {
'is_warning': True,
'cancel_print': False
}
},
'octolapse_is_disabled': {
"name": "Octolapse is Disabled",
Expand Down
19 changes: 17 additions & 2 deletions octoprint_octolapse/timelapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,19 @@ def stop_snapshots(self, message=None, error=False):
timelapse_stopped_callback_thread.start()
return True

def release_job_on_hold_lock(self, force=False):
def release_job_on_hold_lock(self, force=False, reset=False, parsed_command=None):
if parsed_command is not None:
self._octoprint_printer.commands([parsed_command.gcode], tags={"before_release_job_lock"})

if self.job_on_hold:
if force or ( self._stabilization_signal.is_set() and self._position_signal.is_set()):
logger.debug("Releasing job-on-hold lock.")
self._octoprint_printer.set_job_on_hold(False)
self.job_on_hold = False
if reset:
self._reset()



def on_print_failed(self):
if self._state != TimelapseState.Idle:
Expand Down Expand Up @@ -657,7 +664,7 @@ def get_is_taking_snapshot(self):
return self._snapshot_task_queue.qsize() > 0

def on_print_start(self, parsed_command):
return self._print_start_callback(parsed_command)
self._print_start_callback(parsed_command)

def on_print_start_failed(self, message):
self._print_start_failed_callback(message)
Expand Down Expand Up @@ -896,6 +903,14 @@ def detect_timelapse_start(self, command_string, tags):
) or {'script:beforePrintStarted', 'trigger:comm.send_gcode_script'} <= tags
) and self._octoprint_printer.is_printing()
):
if command_string.startswith("M23"):
# SD print, can't do anything about it. Send a warning
error = error_messages.get_error(["init", "cant_print_from_sd"])
logger.info(error["description"])
self.on_print_start_failed([error])
# continue with the print since we can't stop it
return None

if self._octoprint_printer.set_job_on_hold(True):
logger.debug("Setting job-on-hold lock.")
self.job_on_hold = True
Expand Down

0 comments on commit 18b96c5

Please sign in to comment.