diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index 36a6e2c3..add3d78b 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -159,6 +159,32 @@ This describes actually four hooks: :param str gcode: Parsed GCODE command, e.g. ``G0`` or ``M110``, may also be None if no known command could be parsed :return: None, 1-tuple, 2-tuple or string, see the description above for details. +.. _sec-plugins-hook-comm-protocol-gcode-received: + +octoprint.comm.protocol.gcode.received +-------------------------------------- + +.. py:function:: hook(comm_instance, line, *args, **kwargs) + + Get the returned lines sent by the printer. Handlers should return the received line or in any case, the modified + version of it. If the the handler returns None, processing will be aborted and the communication layer will get an + empty string as the received line. Note that Python functions will also automatically return ``None`` if an empty + ``return`` statement is used or just nothing is returned explicitely from the handler. + + **Example:** + + Looks for the response of a M115, which contains information about the MACHINE_TYPE, among other things. + + .. onlineinclude:: https://raw.githubusercontent.com/OctoPrint/Plugin-Examples/master/read_m115_response.py + :linenos: + :tab-width: 4 + :caption: `read_m115_response.py `_ + + :param MachineCom comm_instance: The :class:`~octoprint.util.comm.MachineCom` instance which triggered the hook. + :param str line: The line received from the printer. + :return: The received line or in any case, a modified version of it. + :rtype: str + .. _sec-plugins-hook-comm-protocol-scripts: octoprint.comm.protocol.scripts diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index fd2042d2..5b7ca346 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -270,6 +270,7 @@ class MachineCom(object): sending=self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.sending"), sent=self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.sent") ) + self._received_message_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.received") self._printer_action_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.action") self._gcodescript_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.scripts") @@ -1416,9 +1417,6 @@ class MachineCom(object): self._errorValue = get_exception_string() self.close(is_error=True) return None - if ret == '': - #self._log("Recv: TIMEOUT") - return '' try: self._log("Recv: %s" % sanitize_ascii(ret)) @@ -1426,6 +1424,15 @@ class MachineCom(object): self._log("WARN: While reading last line: %s" % e) self._log("Recv: %r" % ret) + for name, hook in self._received_message_hooks.items(): + try: + ret = hook(self, ret) + except: + self._logger.exception("Error while processing hook {name}:".format(**locals())) + else: + if ret is None: + return "" + return ret def _getNext(self):