diff --git a/docs/features/atcommands.rst b/docs/features/atcommands.rst new file mode 100644 index 000000000..a8ead05d2 --- /dev/null +++ b/docs/features/atcommands.rst @@ -0,0 +1,30 @@ +.. _sec-features-atcommands: + +@ Commands +========== + +@ commands (also known as host commands elsewhere) are special commands you may include in GCODE files streamed +through OctoPrint to your printer or send as part of GCODE scripts, through the Terminal Tab, the API or plugins. +Contrary to other commands they will never be sent to the printer but instead trigger functions inside OctoPrint. + +They are always of the form ``@[ ]``, e.g. ``@pause`` or ``@custom_command with some parameters``. + +Out of the box OctoPrint supports handling of these commands starting with version 1.3.7: + +@cancel + OctoPrint will cancel the current print job like if the "Cancel" button had been clicked. This command doesn't + take any parameters. + +@abort + Same as ``cancel``. + +@pause + OctoPrint will pause the current print job just like if the "Pause" button had been clicked. This command doesn't + take any parameters. + +@resume + OctoPrint will resume the current print job just like if the "Resume" button had been clicked. This command doesn't + take any parameters. + +Support for additional commands may be added by plugins by implementing a handler for one of the +:ref:`octoprint.comm.protocol.atcommand ` hooks. diff --git a/docs/features/index.rst b/docs/features/index.rst index ebbf48821..37c056f2f 100644 --- a/docs/features/index.rst +++ b/docs/features/index.rst @@ -15,5 +15,6 @@ Features custom_controls.rst gcode_scripts.rst action_commands.rst + atcommands.rst plugins.rst safemode.rst diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index 1d7e4ed61..6843e1d01 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -408,6 +408,45 @@ octoprint.comm.protocol.action :param str action: The parsed out action command, so for a ``line`` like ``// action:some_command`` this will be ``some_command`` +.. _sec-plugins-hook-comm-protocol-atcommand-phase: + +octoprint.comm.protocol.atcommand. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This describes actually two hooks: + + * ``octoprint.comm.protocol.atcommand.queuing`` + * ``octoprint.comm.protocol.atcommand.sending`` + +.. py:function:: protocol_atcommandphase_hook(comm_instance, phase, command, parameters, tags=None, *args, **kwargs) + + Trigger on :ref:`@ commands ` as they progress through the ``queuing`` and ``sending`` + phases of the comm layer. See :ref:`the gcode phase hook ` for a + detailed description of each of these phases. + + Hook handlers may use this to react to arbitrary :ref:`@ commands ` included in GCODE files + streamed to the printer or sent as part of GCODE scripts, through the API or plugins. + + Please note that these hooks do not allow to rewrite, suppress or expand @ commands, they are merely callbacks to + trigger the *actual execution* of whatever functionality lies behind a given @ command, similar to + :ref:`the action command hook `. + + **Example** + + Pause the print on ``@wait`` (this mirrors the implementation of the built-in ``@pause`` command, just with a + different name). + + .. onlineinclude:: https://raw.githubusercontent.com/OctoPrint/Plugin-Examples/master/custom_atcommand.py + :linenos: + :tab-width: 4 + :caption: `custom_action_command.py `__ + + :param object comm_instance: The :class:`~octoprint.util.comm.MachineCom` instance which triggered the hook. + :param str phase: The current phase in the command progression, either ``queuing`` or ``sending``. Will always + match the ```` of the hook. + :param str cmd: The @ command without the leading @ + :param str parameters: Any parameters provided to the @ command. If none were provided this will be an empty string. + .. _sec-plugins-hook-comm-protocol-gcode-phase: octoprint.comm.protocol.gcode. diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 12afc9f63..2a7137f13 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -532,6 +532,10 @@ class MachineCom(object): ) self._received_message_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.received") self._error_message_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.error") + self._atcommand_hooks = dict( + queuing=self._pluginManager.get_hooks("octoprint.comm.protocol.atcommand.queuing"), + sending=self._pluginManager.get_hooks("octoprint.comm.protocol.atcommand.sending") + ) self._printer_action_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.action") self._gcodescript_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.scripts") @@ -2942,16 +2946,24 @@ class MachineCom(object): parameters = split[1] else: atcommand = split[0] - parameters = None + parameters = "" atcommand = atcommand[1:] + # send it through the phase specific handlers provided by plugins + for name, hook in self._atcommand_hooks[phase].items(): + try: + hook(self, phase, atcommand, parameters, tags=tags) + except: + self._logger.exception("Error while processing hook {} for phase {} and command {}:".format(name, phase, atcommand)) + + # trigger built-in handler if available 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)) + self._logger.exception("Error in handler for phase {} and command {}".format(phase, atcommand)) ##~~ actual sending via serial @@ -3264,19 +3276,22 @@ class MachineCom(object): ## atcommands - def _atcommand_pause_queuing(self, *args, **kwargs): - tags = kwargs.get("tags", set()) + def _atcommand_pause_queuing(self, command, parameters, tags=None, *args, **kwargs): + if tags is None: + 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()) + def _atcommand_cancel_queuing(self, command, parameters, tags=None, *args, **kwargs): + if tags is None: + tags = set() if not "script:afterPrintCancelled" in tags: self.cancelPrint(tags={"trigger:atcommand_cancel"}) _atcommand_abort_queuing = _atcommand_cancel_queuing - def _atcommand_resume_queuing(self, *args, **kwargs): - tags = kwargs.get("tags", set()) + def _atcommand_resume_queuing(self, command, parameters, tags=None, *args, **kwargs): + if tags is None: + tags = set() if not "script:beforePrintResumed" in tags: self.setPause(False, tags={"trigger:atcommand_resume"})