From ada873be1d7c054bc324b81cb3e8e95bd3ce7a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 12 Mar 2018 18:33:40 +0100 Subject: [PATCH] First throw at native @ command support For now the following commands are supported: * @pause: Pauses the print * @resume: Resumes the print * @cancel or @abort: Cancels the print As discussed in #2414. Also implements #324. --- src/octoprint/util/comm.py | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 1e4d9f9e4..d9927d3c3 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -2643,6 +2643,10 @@ class MachineCom(object): # if this is a gcode bound to an event, trigger that now eventManager().fire(gcodeToEvent[gcode]) + # process @ commands + if gcode is None and cmd.startswith("@"): + self._process_atcommand_phase("queuing", cmd, tags=tags) + # actually enqueue the command for sending if self._enqueue_for_sending(cmd, command_type=cmd_type, on_sent=on_sent, tags=tags): if not self.isStreaming(): @@ -2780,6 +2784,16 @@ class MachineCom(object): # and fetch the next item continue + # handle @ commands + if gcode is None and command.startswith("@"): + self._process_atcommand_phase("sending", command, tags=tags) + + # tickle... + self._continue_sending() + + # ... and fetch the next item + continue + # now comes the part where we increase line numbers and send stuff - no turning back now command_requiring_checksum = gcode is not None and gcode in self._checksum_requiring_commands command_allowing_checksum = gcode is not None or self._sendChecksumWithUnknownCommands @@ -2918,6 +2932,27 @@ class MachineCom(object): # finally return whatever we resulted on return results + def _process_atcommand_phase(self, phase, command, tags=None): + if (self.isStreaming() and self.isPrinting()) or phase not in ("queuing", "sending"): + return + + split = command.split(None, 1) + if len(split) == 2: + atcommand = split[0] + parameters = split[1] + else: + atcommand = split[0] + parameters = None + + atcommand = atcommand[1:] + + handler = getattr(self, "_atcommand_{}_{}".format(atcommand, phase), None) + if callable(handler): + try: + handler(atcommand, parameters, tags=tags) + except: + self._logger.exception("Error in handler for phase {} and command {} atcommand".format(phase, atcommand)) + ##~~ actual sending via serial def _do_increment_and_send_with_checksum(self, cmd): @@ -2998,6 +3033,8 @@ class MachineCom(object): ##~~ command handlers + ## gcode + def _gcode_T_queuing(self, cmd, cmd_type=None, gcode=None, subcode=None, *args, **kwargs): toolMatch = regexes_parameters["intT"].search(cmd) if toolMatch: @@ -3225,6 +3262,24 @@ class MachineCom(object): def _validate_tool(self, tool): return tool < self._printerProfileManager.get_current_or_default()["extruder"]["count"] and not tool in self._knownInvalidTools + ## atcommands + + def _atcommand_pause_queuing(self, *args, **kwargs): + tags = kwargs.get("tags", set()) + if not "script:afterPrintPaused" in tags: + self.setPause(True, tags={"trigger:atcommand_pause"}) + + def _atcommand_cancel_queuing(self, *args, **kwargs): + tags = kwargs.get("tags", set()) + if not "script:afterPrintCancelled" in tags: + self.cancelPrint(tags={"trigger:atcommand_cancel"}) + _atcommand_abort_sending = _atcommand_cancel_queuing + + def _atcommand_resume_queuing(self, *args, **kwargs): + tags = kwargs.get("tags", set()) + if not "script:beforePrintResumed" in tags: + self.setPause(False, tags={"trigger:atcommand_resume"}) + ##~~ command phase handlers def _command_phase_sending(self, cmd, cmd_type=None, gcode=None, subcode=None, *args, **kwargs):