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.
This commit is contained in:
Gina Häußge committed 2018-02-16 22:03:45 +01:00
1 parent 00e6a3856f
commit d166bafa8d
3 files changed
+53 -2

No files matched your search

+27
View File
@@ -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
+5
View File
@@ -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
+21 -2
View File
@@ -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