From d1612e426a897bb4a6d3c200f692d56a00d0659d Mon Sep 17 00:00:00 2001 From: Nicanor Romero Venier Date: Thu, 3 Sep 2015 17:07:33 +0200 Subject: [PATCH 1/4] Added hook for recieved lines --- src/octoprint/util/comm.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 44eae0ba..21f0b7ff 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -269,6 +269,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._recieved_message_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.recieved") self._printer_action_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.action") self._gcodescript_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.scripts") @@ -1399,9 +1400,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)) @@ -1409,6 +1407,14 @@ class MachineCom(object): self._log("WARN: While reading last line: %s" % e) self._log("Recv: %r" % ret) + for name, hook in self._recieved_message_hooks.items(): + try: + ret = hook(self, ret) + except: + self._logger.exception("Error while processing hook {name}:".format(**locals())) + if ret is None: + return "" + return ret def _getNext(self): From ced18c86e0ecc2a33afa69722611220c4db80bda Mon Sep 17 00:00:00 2001 From: Nicanor Romero Venier Date: Thu, 3 Sep 2015 18:43:07 +0200 Subject: [PATCH 2/4] Added docs for the gcode.recieved hook --- docs/plugins/hooks.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index 36a6e2c3..c208babe 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -159,6 +159,30 @@ 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-recieved: + +octoprint.comm.protocol.gcode.recieved +------------------------------- + +.. py:function:: hook(comm_instance, line, *args, **kwargs) + + Get the returned lines sent by the printer. Handlers should return the recieved line or in any case, the modified + version of it. + + **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 recieved from the printer. + :return: The recieved line or in any case, a modified version of it. + :rtype: str + .. _sec-plugins-hook-comm-protocol-scripts: octoprint.comm.protocol.scripts From fa95507177e5054725d0c20b0ff4ddd6f5103322 Mon Sep 17 00:00:00 2001 From: Nicanor Romero Venier Date: Mon, 7 Sep 2015 10:04:06 +0200 Subject: [PATCH 3/4] Included an early return if ret is None Also corrected typos, received instead of recieved. --- docs/plugins/hooks.rst | 12 ++++++------ src/octoprint/util/comm.py | 9 +++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index c208babe..303a5e12 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -159,14 +159,14 @@ 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-recieved: +.. _sec-plugins-hook-comm-protocol-gcode-received: -octoprint.comm.protocol.gcode.recieved -------------------------------- +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 recieved line or in any case, the modified + Get the returned lines sent by the printer. Handlers should return the received line or in any case, the modified version of it. **Example:** @@ -179,8 +179,8 @@ octoprint.comm.protocol.gcode.recieved :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 recieved from the printer. - :return: The recieved line or in any case, a modified version of it. + :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: diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 21f0b7ff..645be0d8 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -269,7 +269,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._recieved_message_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.recieved") + 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") @@ -1407,13 +1407,14 @@ class MachineCom(object): self._log("WARN: While reading last line: %s" % e) self._log("Recv: %r" % ret) - for name, hook in self._recieved_message_hooks.items(): + 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())) - if ret is None: - return "" + else: + if ret is None: + return "" return ret From 74b0056095c7a4d0bfa9409f537a9458514dc3cf Mon Sep 17 00:00:00 2001 From: Nicanor Romero Venier Date: Mon, 7 Sep 2015 10:52:03 +0200 Subject: [PATCH 4/4] Added info to the hook's docs --- docs/plugins/hooks.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index 303a5e12..add3d78b 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -167,7 +167,9 @@ 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. + 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:**