From 6dc56af3facb922f33dfebfeb7b5beb1c8a3708e Mon Sep 17 00:00:00 2001 From: Dattas Moonchaser Date: Sat, 8 Nov 2014 14:36:57 -0700 Subject: [PATCH 1/2] Add octoprint.comm.protocol.gcode hooks --- src/octoprint/comm/protocol/reprap/__init__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index ba01bd3fd..316b42dd7 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -19,6 +19,7 @@ from octoprint.filemanager import valid_file_type from octoprint.filemanager.destinations import FileDestinations from octoprint.settings import settings from octoprint.util import CountedEvent +import octoprint.plugin class RepRapProtocol(Protocol): @@ -163,6 +164,8 @@ class RepRapProtocol(Protocol): self._current_extruder = 0 self._state = State.OFFLINE + self._pluginManager = octoprint.plugin.plugin_manager() + self._preprocessors = dict() self._setup_preprocessors() @@ -223,6 +226,8 @@ class RepRapProtocol(Protocol): self._temperature_interval = protocol_options["timeout"]["temperature"] if "timeout" in protocol_options and "temperature" in protocol_options["timeout"] else 5.0 self._sdstatus_interval = protocol_options["timeout"]["sdstatus"] if "timeout" in protocol_options and "sdstatus" in protocol_options["timeout"] else 5.0 + self._gcode_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode") + self._reset() # connect @@ -570,6 +575,12 @@ class RepRapProtocol(Protocol): if entry.command is not None: entry.command.progress = with_progress else: + #handle our hooks, if any + for hook in self._gcode_hooks: + hook_cmd = self._gcode_hooks[hook](self, command) + if hook_cmd and isinstance(hook_cmd, basestring): + command = hook_cmd + if not isinstance(command, GcodeCommand): command = GcodeCommand.from_line(command) command.progress = with_progress From 8752f70ff325339ba86375c35e2d9e4924ebcc5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 10 Nov 2014 12:33:24 +0100 Subject: [PATCH 2/2] Added multiple hooks for the individual processing phases, gcode hooks now have to process the parsed command (might be unknown) --- src/octoprint/comm/protocol/reprap/__init__.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index b89922274..da06a87f9 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -166,6 +166,11 @@ class RepRapProtocol(Protocol): self._state = State.OFFLINE self._pluginManager = octoprint.plugin.plugin_manager() + self._gcode_hooks = dict( + queued=self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.queued").values(), + sent=self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.sent").values(), + acknowledged=self._pluginManager.get_hooks("octoprint.comm.protocol.gcode.acknowledged").values() + ) self._preprocessors = dict() self._setup_preprocessors() @@ -227,8 +232,6 @@ class RepRapProtocol(Protocol): self._temperature_interval = protocol_options["timeout"]["temperature"] if "timeout" in protocol_options and "temperature" in protocol_options["timeout"] else 5.0 self._sdstatus_interval = protocol_options["timeout"]["sdstatus"] if "timeout" in protocol_options and "sdstatus" in protocol_options["timeout"] else 5.0 - self._gcode_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode") - self._reset() # connect @@ -576,12 +579,6 @@ class RepRapProtocol(Protocol): if entry.command is not None: entry.command.progress = with_progress else: - #handle our hooks, if any - for hook in self._gcode_hooks: - hook_cmd = self._gcode_hooks[hook](self, command) - if hook_cmd and isinstance(hook_cmd, basestring): - command = hook_cmd - if not isinstance(command, GcodeCommand): command = GcodeCommand.from_line(command) command.progress = with_progress @@ -883,6 +880,10 @@ class RepRapProtocol(Protocol): if not phase in ("queued", "sent", "acknowledged"): return None + #handle our hooks, if any + for hook in self._gcode_hooks[phase]: + command, with_line_number = hook(self, command, with_line_number=with_line_number) + if phase in self._preprocessors and command.command in self._preprocessors[phase]: command, with_line_number = self._preprocessors[phase][command.command](command, with_line_number)