From ede2e8c593e016344e946203fe23163fd7ab1dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 5 May 2015 17:30:35 +0200 Subject: [PATCH] Virtual printer now allows sending of custom action commands Sending "!!DEBUG:action_custom foo 1 2 3" will result in the virtual printer sending "// action:foo 1 2 3" back. Sending "!!DEBUG:action_custom bar" will result in the virtual printer sending "// action:bar" back. --- src/octoprint/plugins/virtual_printer/virtual.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/octoprint/plugins/virtual_printer/virtual.py b/src/octoprint/plugins/virtual_printer/virtual.py index 7c8b44cc..e1366314 100644 --- a/src/octoprint/plugins/virtual_printer/virtual.py +++ b/src/octoprint/plugins/virtual_printer/virtual.py @@ -14,12 +14,14 @@ import Queue from serial import SerialTimeoutException from octoprint.settings import settings +from octoprint.plugin import plugin_manager class VirtualPrinter(): command_regex = re.compile("[GM]\d+") sleep_regex = re.compile("sleep (\d+)") sleep_after_regex = re.compile("sleep_after ([GM]\d+) (\d+)") sleep_after_next_regex = re.compile("sleep_after_next ([GM]\d+) (\d+)") + custom_action_regex = re.compile("action_custom ([a-zA-Z0-9_]+)(\s+.*)?") def __init__(self, read_timeout=5.0, write_timeout=10.0): self._read_timeout = read_timeout @@ -71,6 +73,8 @@ class VirtualPrinter(): self._dont_answer = False + self._action_hooks = plugin_manager().get_hooks("octoprint.plugin.virtual_printer.custom_action") + waitThread = threading.Thread(target=self._sendWaitAfterTimeout) waitThread.start() @@ -294,8 +298,6 @@ class VirtualPrinter(): self.outgoing.put("// action:resume") elif data == "action_disconnect": self.outgoing.put("// action:disconnect") - elif data == "action_custom": - self.outgoing.put("// action:custom") elif data == "dont_answer": self._dont_answer = True elif data == "trigger_resend_lineno": @@ -307,6 +309,7 @@ class VirtualPrinter(): sleep_match = VirtualPrinter.sleep_regex.match(data) sleep_after_match = VirtualPrinter.sleep_after_regex.match(data) sleep_after_next_match = VirtualPrinter.sleep_after_next_regex.match(data) + custom_action_match = VirtualPrinter.custom_action_regex.match(data) if sleep_match is not None: interval = int(sleep_match.group(1)) @@ -322,6 +325,11 @@ class VirtualPrinter(): interval = int(sleep_after_next_match.group(2)) self._sleepAfterNext[command] = interval self.outgoing.put("// going to sleep {interval} seconds after next {command}".format(**locals())) + elif custom_action_match is not None: + action = custom_action_match.group(1) + params = custom_action_match.group(2) + params = params.strip() if params is not None else "" + self.outgoing.put("// action:{action} {params}".format(**locals()).strip()) except: pass