From d166bafa8df4defd4f6da3f172a0aa5a1137dc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 16 Feb 2018 22:03:45 +0100 Subject: [PATCH] New context "job_on_hold" on printer object Can be used to do some stuff while printing while ensuring that the file won't continue to be streamed. Should be used sparingly and only for very limited reasons since it can severely affect print results. --- src/octoprint/printer/__init__.py | 27 +++++++++++++++++++++++++++ src/octoprint/printer/standard.py | 5 +++++ src/octoprint/util/comm.py | 23 +++++++++++++++++++++-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/octoprint/printer/__init__.py b/src/octoprint/printer/__init__.py index 84a892afb..038aa1547 100644 --- a/src/octoprint/printer/__init__.py +++ b/src/octoprint/printer/__init__.py @@ -107,6 +107,33 @@ class PrinterInterface(object): """ raise NotImplementedError() + def job_on_hold(self, blocking=True, *args, **kwargs): + """ + Contextmanager that allows executing code while printing while making sure that no commands from the file + being printed are continued to be sent to the printer. Note that this will only work for local files, + NOT SD files. + + Example: + + .. code-block:: python + + with printer.job_on_hold(): + park_printhead() + take_snapshot() + send_printhead_back() + + It should be used sparingly and only for very specific situations (such as parking the print head somewhere, + taking a snapshot from the webcam, then continuing). If you abuse this, you WILL cause print quality issues! + + A lock is in place that ensures that the context can only actually be held by one thread at a time. If you + don't want to block on acquire, be sure to set ``blocking`` to ``False`` and catch the ``RuntimeException`` thrown + if the lock can't be acquired. + + Args: + blocking (bool): Whether to block while attempting to acquire the lock (default) or not + """ + raise NotImplementedError() + def fake_ack(self, *args, **kwargs): """ Fakes an acknowledgment for the communication layer. If the communication between OctoPrint and the printer diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 620fde17e..1284ac2be 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -233,6 +233,11 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): return self._comm.getTransport() getTransport = util.deprecated("getTransport has been renamed to get_transport", since="1.2.0-dev-590", includedoc="Replaced by :func:`get_transport`") + def job_on_hold(self, blocking=None, *args, **kwargs): + if self._comm is None: + raise RuntimeError("No connection to the printer") + return self._comm.job_on_hold(blocking=blocking) + def fake_ack(self, *args, **kwargs): if self._comm is None: return diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 234efd228..372ed7fac 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -10,6 +10,7 @@ import glob import time import re import threading +import contextlib try: import queue @@ -502,6 +503,8 @@ class MachineCom(object): # print job self._currentFile = None + self._job_on_hold = False + self._job_on_hold_mutex = threading.RLock() # multithreading locks self._jobLock = threading.RLock() @@ -696,6 +699,19 @@ class MachineCom(object): ##~~ external interface + @contextlib.contextmanager + def job_on_hold(self, blocking=True): + if not self._job_on_hold_mutex.acquire(blocking=blocking): + raise RuntimeError("Could not acquire job_on_hold lock") + + self._job_on_hold = True + try: + yield + finally: + self._job_on_hold = False + self._job_on_hold_mutex.release() + self._continue_sending() + def close(self, is_error=False, wait=True, timeout=10.0, *args, **kwargs): """ Closes the connection to the printer. @@ -795,7 +811,7 @@ class MachineCom(object): if not cmd: return False - if self.isPrinting() and not self.isSdFileSelected(): + if self.isPrinting() and not self.isSdFileSelected() and not self._job_on_hold: try: self._command_queue.put((cmd, cmd_type, on_sent, tags), item_type=cmd_type) return True @@ -1421,7 +1437,7 @@ class MachineCom(object): handled = True # process timeouts - elif ((line == "" and now > self._timeout) or (self.isPrinting() and now > self._ok_timeout)) \ + elif ((line == "" and now > self._timeout) or (self.isPrinting() and not self._job_on_hold and now > self._ok_timeout)) \ and (not self._blockWhileDwelling or not self._dwelling_until or now > self._dwelling_until): # We have two timeout variants: # @@ -1869,6 +1885,9 @@ class MachineCom(object): # we found something in the queue to send return True + elif self._job_on_hold: + return False + elif self._sendNext(): # we sent the next line from the file return True