Implement job_on_hold functionality
This commit is contained in:
1 parent
b79618d752
commit
5282db6871
4 files changed
+106
-35
No files matched your search
@@ -8,8 +8,9 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms
|
||||
from octoprint.util.listener import ListenerAware
|
||||
from octoprint.comm.transport import TransportListener, TransportState
|
||||
|
||||
from octoprint.util import to_unicode
|
||||
from octoprint.util import to_unicode, CountedEvent
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
|
||||
class Protocol(ListenerAware, TransportListener):
|
||||
@@ -26,6 +27,46 @@ class Protocol(ListenerAware, TransportListener):
|
||||
self._job = None
|
||||
self._transport = None
|
||||
|
||||
self._job_on_hold = CountedEvent()
|
||||
|
||||
@contextlib.contextmanager
|
||||
def job_put_on_hold(self, blocking=True):
|
||||
if not self._job_on_hold.acquire(blocking=blocking):
|
||||
raise RuntimeError("Could not acquire job_on_hold lock")
|
||||
|
||||
self._job_on_hold.set()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self._job_on_hold.clear()
|
||||
if self._job_on_hold.counter == 0:
|
||||
self._job_on_hold_cleared()
|
||||
self._job_on_hold.release()
|
||||
|
||||
@property
|
||||
def job_on_hold(self):
|
||||
return self._job_on_hold.counter > 0
|
||||
|
||||
def set_job_on_hold(self, value, blocking=True):
|
||||
if not self._job_on_hold.acquire(blocking=blocking):
|
||||
return False
|
||||
|
||||
try:
|
||||
if value:
|
||||
self._job_on_hold.set()
|
||||
else:
|
||||
self._job_on_hold.clear()
|
||||
if self._job_on_hold.counter == 0:
|
||||
self._job_on_hold_cleared()
|
||||
finally:
|
||||
self._job_on_hold.release()
|
||||
|
||||
return True
|
||||
|
||||
@property
|
||||
def transport(self):
|
||||
return self._transport
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
return self._state
|
||||
@@ -167,6 +208,10 @@ class Protocol(ListenerAware, TransportListener):
|
||||
self._protocol_logger.info(message)
|
||||
self.notify_listeners("on_protocol_log", self, message)
|
||||
|
||||
def _job_on_hold_cleared(self):
|
||||
pass
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.__class__.__name__
|
||||
|
||||
@@ -369,3 +414,12 @@ class PositionAwareProtocolListener(object):
|
||||
|
||||
def on_protocol_position_z_update(self, protocol, z, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
class FirmwareDataAwareProtocolListener(object):
|
||||
|
||||
def on_protocol_firmware_info(self, protocol, info, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def on_protocol_firmware_capability(self, protocol, capability, enabled, capabilities, *args, **kwargs):
|
||||
pass
|
||||
@@ -87,16 +87,20 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
def get_attributes_starting_with(cls, prefix):
|
||||
return dict((x, getattr(cls, x)) for x in dir(cls) if x.startswith(prefix))
|
||||
|
||||
def __init__(self, flavor=None,
|
||||
def __init__(self,
|
||||
flavor=None,
|
||||
connection_timeout=30.0,
|
||||
communication_timeout=10.0,
|
||||
communication_busy_timeout=2.0,
|
||||
temperature_interval_idle=5.0,
|
||||
temperature_interval_printing=5.0,
|
||||
temperature_interval_autoreport=2.0,
|
||||
sd_status_interval_autoreport=1.0):
|
||||
sd_status_interval_autoreport=1.0,
|
||||
plugin_manager=None):
|
||||
super(ReprapGcodeProtocol, self).__init__()
|
||||
|
||||
self._plugin_manager = plugin_manager
|
||||
|
||||
self._logger = logging.getLogger(__name__)
|
||||
self._commdebug_logger = logging.getLogger("COMMDEBUG")
|
||||
self._phase_logger = logging.getLogger(__name__ + ".command_phases")
|
||||
@@ -165,6 +169,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
trigger_events=True,
|
||||
expect_continous_comms=False,
|
||||
ignore_ok=0,
|
||||
job_on_hold=property(lambda: self.job_on_hold),
|
||||
|
||||
# timeout
|
||||
timeout = None,
|
||||
@@ -207,18 +212,27 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
self._record_cancel_data = False
|
||||
|
||||
# hooks
|
||||
# TODO GCODE Hooks
|
||||
self._gcode_hooks = dict(queuing=dict(),
|
||||
queued=dict(),
|
||||
sending=dict(),
|
||||
sent=dict())
|
||||
self._gcode_hooks = dict(queuing=self._plugin_manager.get_hooks("octoprint.comm.protocol.gcode.queuing"),
|
||||
queued=self._plugin_manager.get_hooks("octoprint.comm.protocol.gcode.queued"),
|
||||
sending=self._plugin_manager.get_hooks("octoprint.comm.protocol.gcode.sending"),
|
||||
sent=self._plugin_manager.get_hooks("octoprint.comm.protocol.gcode.sent"),
|
||||
receiving=self._plugin_manager.get_hooks("octoprint.comm.protocol.gcode.receiving")) # TODO receiving hook
|
||||
|
||||
# TODO atcommand hooks
|
||||
self._atcommand_hooks = dict(queuing=dict(),
|
||||
sending=dict())
|
||||
self._error_hooks = self._plugin_manager.get_hooks("octoprint.comm.protocol.gcode.error") # TODO error hook
|
||||
|
||||
self._atcommand_hooks = dict(queuing=self._plugin_manager.get_hooks("octoprint.comm.protocol.atcommand.queuing"),
|
||||
sending=self._plugin_manager.get_hooks("octoprint.comm.protocol.atcommand.sending")),
|
||||
|
||||
self._action_hooks = self._plugin_manager.get_hooks("octoprint.comm.protocol.action")
|
||||
|
||||
self._script_hooks = self._plugin_manager.get_hooks("octoprint.comm.protocol.scripts") # TODO script hook
|
||||
|
||||
self._temperature_hooks = self._plugin_manager.get_hooks("octoprint.comm.protocol.temperatures.received") # TODO temperature hook
|
||||
|
||||
self._firmware_info_hooks = self._plugin_manager.get_hooks("octoprint.comm.firmware.info") # TODO firmware info hook
|
||||
|
||||
self._firmware_cap_hooks = self._plugin_manager.get_hooks("octoprint.comm.firmware.capabilities") # TODO firmware cap hook
|
||||
|
||||
# TODO action hooks
|
||||
self._action_hooks = dict()
|
||||
|
||||
# polling
|
||||
self._temperature_poller = None
|
||||
@@ -527,8 +541,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
else:
|
||||
return to_command(c, type=ct, tags=t)
|
||||
|
||||
# TODO job on hold
|
||||
if self.state in ProtocolState.PROCESSING_STATES and not self._job.parallel and not force:
|
||||
if self.state in ProtocolState.PROCESSING_STATES and not self._job.parallel and not self.job_on_hold and not force:
|
||||
if len(commands) > 1:
|
||||
command_type = None
|
||||
for command in commands:
|
||||
@@ -661,6 +674,9 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
self._internal_flags["trigger_events"] = True
|
||||
self._job.unregister_listener(self)
|
||||
|
||||
def _job_on_hold_cleared(self):
|
||||
self._continue_sending()
|
||||
|
||||
##~~ Transport logging
|
||||
|
||||
def on_transport_log_message(self, transport, data):
|
||||
@@ -864,7 +880,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
self._logger.info("Ignoring next ok, counter is now at {}".format(self._internal_flags["ignore_ok"]))
|
||||
|
||||
def _on_comm_wait(self):
|
||||
if self.state == ProtocolState.PROCESSING:
|
||||
if self.state == ProtocolState.PROCESSING and not self.job_on_hold:
|
||||
self._on_comm_ok(wait=True)
|
||||
|
||||
def _on_comm_resend(self, linenumber):
|
||||
@@ -1147,9 +1163,8 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
# we found something in the queue to send
|
||||
return True
|
||||
|
||||
# TODO job on hold
|
||||
#elif self.job_on_hold:
|
||||
# return False
|
||||
elif self.job_on_hold:
|
||||
return False
|
||||
|
||||
elif self._send_next_from_job():
|
||||
# we sent the next line from the file
|
||||
@@ -1208,10 +1223,9 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
# we are no longer printing, return False
|
||||
return False
|
||||
|
||||
# TODO job on hold
|
||||
#elif self.job_on_hold:
|
||||
# # job is on hold, return false
|
||||
# return False
|
||||
elif self.job_on_hold:
|
||||
# job is on hold, return false
|
||||
return False
|
||||
|
||||
line = self._job.get_next()
|
||||
if line is None:
|
||||
@@ -1519,7 +1533,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt
|
||||
|
||||
self._log_command_phase(phase, command)
|
||||
|
||||
if phase not in ("queueing", "queued", "sending", "sent"):
|
||||
if phase not in ("queuing", "queued", "sending", "sent"):
|
||||
return results
|
||||
|
||||
# send it through the phase specific handlers provided by plugins
|
||||
|
||||
@@ -110,7 +110,7 @@ class GenericFlavor(object):
|
||||
def comm_timeout(cls, line, lower_line, state, flags):
|
||||
now = monotonic_time()
|
||||
return ((line == "" and now > flags["timeout"]) or (flags["expect_continous_comms"]
|
||||
#and not flags["job_on_hold"] # TODO job on hold
|
||||
and not flags["job_on_hold"]
|
||||
and not flags["long_running_command"]
|
||||
and not flags["heating"]
|
||||
and now > flags["ok_timeout"])) and \
|
||||
|
||||
@@ -302,7 +302,7 @@ class Printer(PrinterInterface,
|
||||
|
||||
# TODO make this depend on the printer profile
|
||||
from octoprint.comm.protocol.reprap import ReprapGcodeProtocol
|
||||
protocol = ReprapGcodeProtocol()
|
||||
protocol = ReprapGcodeProtocol(plugin_manager=plugin_manager())
|
||||
protocol.register_listener(self)
|
||||
|
||||
self._transport = transport
|
||||
@@ -327,22 +327,20 @@ class Printer(PrinterInterface,
|
||||
eventManager().fire(Events.DISCONNECTED)
|
||||
|
||||
def get_transport(self, *args, **kwargs):
|
||||
|
||||
if self._comm is None:
|
||||
if self._protocol is None:
|
||||
return None
|
||||
|
||||
return self._comm.getTransport()
|
||||
return self._protocol.transport
|
||||
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=True, *args, **kwargs):
|
||||
if self._comm is None:
|
||||
if self._protocol is None:
|
||||
raise RuntimeError("No connection to the printer")
|
||||
return self._comm.job_put_on_hold(blocking=blocking)
|
||||
return self._protocol.job_put_on_hold(blocking=blocking)
|
||||
|
||||
def set_job_on_hold(self, value, blocking=True, *args, **kwargs):
|
||||
if self._comm is None:
|
||||
if self._protocol is None:
|
||||
raise RuntimeError("No connection to the printer")
|
||||
return self._comm.set_job_on_hold(value, blocking=blocking)
|
||||
return self._protocol.set_job_on_hold(value, blocking=blocking)
|
||||
|
||||
def fake_ack(self, *args, **kwargs):
|
||||
if self._protocol is None:
|
||||
@@ -360,7 +358,7 @@ class Printer(PrinterInterface,
|
||||
if not isinstance(commands, (list, tuple)):
|
||||
commands = [commands]
|
||||
|
||||
self._protocol.send_commands(*commands)
|
||||
self._protocol.send_commands(*commands, **kwargs)
|
||||
|
||||
def script(self, name, context=None, must_be_set=True, *args, **kwargs):
|
||||
if self._protocol is None:
|
||||
@@ -1134,6 +1132,11 @@ class Printer(PrinterInterface,
|
||||
|
||||
self._set_current_z(z)
|
||||
|
||||
def on_protocol_position_all_update(self, protocol, position, *args, **kwargs):
|
||||
payload = dict(reason=kwargs.get("reason", None))
|
||||
payload.update(position)
|
||||
eventManager().fire(Events.POSITION_UPDATE, payload)
|
||||
|
||||
#~~ octoprint.comm.protocol.JobAwareProtocolListener implementation
|
||||
|
||||
def on_protocol_job_processing(self, protocol, job, suppress_script=False):
|
||||
|
||||
Reference in new issue
Block a user