diff --git a/src/octoprint/comm/protocol/__init__.py b/src/octoprint/comm/protocol/__init__.py index 0214aaa68..91ad44f4e 100644 --- a/src/octoprint/comm/protocol/__init__.py +++ b/src/octoprint/comm/protocol/__init__.py @@ -185,7 +185,7 @@ class Protocol(ListenerAware, TransportListener): self._transport.connect(*transport_args, **transport_kwargs) self.state = ProtocolState.CONNECTING - def disconnect(self, error=False): + def disconnect(self, error=False, wait=True, timeout=10.0): if self.state in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR, ProtocolState.DISCONNECTING, ProtocolState.DISCONNECTING_WITH_ERROR): raise ProtocolNotConnectedError("Already disconnecting or disconnected") @@ -197,10 +197,13 @@ class Protocol(ListenerAware, TransportListener): self.process_protocol_log("--- Protocol {} disconnecting from transport {}...".format(self, self._transport)) + if wait: + self.join(timeout=timeout) + self._transport.unregister_listener(self) if self._transport.state == TransportState.CONNECTED: try: - self._transport.disconnect() + self._transport.disconnect(wait=wait) except Exception: self._logger.exception("Error while disconnecting from transport {}".format(self._transport)) error = True @@ -262,6 +265,9 @@ class Protocol(ListenerAware, TransportListener): def repair(self): pass + def join(self, timeout=None): + pass + def on_job_started(self, job, *args, **kwargs): self.notify_listeners("on_protocol_job_started", self, job, *args, **kwargs) self.state = ProtocolState.PROCESSING diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index 78d75e29e..84b7d39e4 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -1005,6 +1005,15 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt def repair(self, *args, **kwargs): self._on_comm_ok() + def join(self, timeout=None): + if timeout is not None: + stop = monotonic_time() + timeout + while (self._command_queue.unfinished_tasks or self._send_queue.unfinished_tasks) and monotonic_time() < stop: + time.sleep(0.1) + else: + self._command_queue.join() + self._send_queue.join() + ##~~ State handling def _is_operational(self): @@ -1283,7 +1292,7 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt if self._internal_flags["dwelling_until"] and monotonic_time() > self._internal_flags["dwelling_until"]: self._internal_flags["dwelling_until"] = False - if self.state == ProtocolState.CONNECTING: + if self.state == ProtocolState.CONNECTING and len(line): hello = self.flavor.command_hello() if hello: self._tickle(hello) @@ -1553,8 +1562,10 @@ class ReprapGcodeProtocol(Protocol, ThreeDPrinterProtocolMixin, MotorControlProt self._clear_to_send.set() else: - self._to_logfile_with_terminal("WARNING! Received an error from the printer's firmware, ignoring that as configured " - "but you might want to investigate what happened here! Error: {}".format(error)) + message = "--- WARNING! Received an error from the printer's firmware, ignoring that as configured " \ + "but you might want to investigate what happened here! Error: {}".format(error) + self._to_logfile_with_terminal(message, level=logging.WARNING) + self.process_protocol_log(message) self._clear_to_send.set() def _on_error_communication(self, error_type): diff --git a/src/octoprint/comm/transport/__init__.py b/src/octoprint/comm/transport/__init__.py index 9a2a26297..43b9d941f 100644 --- a/src/octoprint/comm/transport/__init__.py +++ b/src/octoprint/comm/transport/__init__.py @@ -117,11 +117,11 @@ class Transport(ListenerAware): self.state = TransportState.CONNECTED self.notify_listeners("on_transport_connected", self) - def disconnect(self, error=None): + def disconnect(self, error=None, wait=True): if self.state == TransportState.DISCONNECTED: raise TransportNotConnectedError("Already disconnected") - success = self.drop_connection() + success = self.drop_connection(wait=wait) if not success and not error: error = "Error disconnecting transport" @@ -136,7 +136,7 @@ class Transport(ListenerAware): def create_connection(self, *args, **kwargs): return True - def drop_connection(self): + def drop_connection(self, wait=True): return True def read(self, size=None, timeout=None): diff --git a/src/octoprint/comm/transport/serialtransport.py b/src/octoprint/comm/transport/serialtransport.py index ae09a44c1..ba2fa5548 100644 --- a/src/octoprint/comm/transport/serialtransport.py +++ b/src/octoprint/comm/transport/serialtransport.py @@ -130,7 +130,7 @@ class SerialTransport(Transport): return True - def drop_connection(self): + def drop_connection(self, wait=True): error = False if self._serial is not None: diff --git a/src/octoprint/comm/transport/sockettransport.py b/src/octoprint/comm/transport/sockettransport.py index ad8ca4c0e..ceef306e8 100644 --- a/src/octoprint/comm/transport/sockettransport.py +++ b/src/octoprint/comm/transport/sockettransport.py @@ -39,7 +39,7 @@ class TcpTransport(Transport): return True - def drop_connection(self): + def drop_connection(self, wait=True): try: self._socket.close() return True