Hook for defining custom atcommands

This commit is contained in:
Gina Häußge committed 2018-03-13 11:06:52 +01:00
1 parent 01bae0c95c
commit 32a31a7d4a
4 files changed
+93 -8

No files matched your search

+30
View File
@@ -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 ``@<command>[ <parameters>]``, 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 <sec-plugins-hook-comm-protocol-atcommand-phase>` hooks.
+1
View File
@@ -15,5 +15,6 @@ Features
custom_controls.rst
gcode_scripts.rst
action_commands.rst
atcommands.rst
plugins.rst
safemode.rst
+39
View File
@@ -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.<phase>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 <sec-features-atcommands>` as they progress through the ``queuing`` and ``sending``
phases of the comm layer. See :ref:`the gcode phase hook <sec-plugins-hook-comm-protocol-gcode-phase>` for a
detailed description of each of these phases.
Hook handlers may use this to react to arbitrary :ref:`@ commands <sec-features-atcommands>` 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 <sec-plugins-hook-comm-protocol-action>`.
**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 <https://github.com/OctoPrint/Plugin-Examples/blob/master/custom_atcommand.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 ``<phase>`` 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.<phase>
+23 -8
View File
@@ -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"})