diff --git a/src/octoprint/comm/protocol/__init__.py b/src/octoprint/comm/protocol/__init__.py index b09761060..8a4621818 100644 --- a/src/octoprint/comm/protocol/__init__.py +++ b/src/octoprint/comm/protocol/__init__.py @@ -66,8 +66,10 @@ class Protocol(ListenerAware, TransportListener): self.state = ProtocolState.CONNECTING def disconnect(self): - if self.state in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR): - raise ProtocolNotConnectedError("Already disconnected") + if self.state in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR, ProtocolState.DISCONNECTING): + raise ProtocolNotConnectedError("Already disconnecting or disconnected") + + self.state = ProtocolState.DISCONNECTING self.process_protocol_log("--- Protocol {} disconnecting from transport {}...".format(self, self._transport)) @@ -167,6 +169,7 @@ class Protocol(ListenerAware, TransportListener): class ProtocolState(object): CONNECTING = "connecting" CONNECTED = "connected" + DISCONNECTING = "disconnecting" DISCONNECTED = "disconnected" PRINTING = "printing" PAUSED = "paused" diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index 582efc34b..cb8ab8335 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -85,9 +85,10 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, # misc only_from_job=False, trigger_events=True, + ignore_ok=0, # timeout - timeout = None + timeout = None, ) self._protected_state = protectedkeydict(self._internal_state) @@ -112,10 +113,8 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, self._send_queue_mutex = threading.RLock() # sending thread - self._send_queue_active = True - self.sending_thread = threading.Thread(target=self._send_loop, name="comm.sending_thread") - self.sending_thread.daemon = True - self.sending_thread.start() + self._send_queue_active = False + self._sending_thread = None def connect(self, transport, transport_args=None, transport_kwargs=None): if not isinstance(transport, Transport): @@ -124,12 +123,22 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, self._internal_state["timeout"] = self._get_timeout("connection") transport = PushingTransportWrapper(LineAwareTransportWrapper(transport), timeout=5.0) + + self._send_queue_active = True + self._sending_thread = threading.Thread(target=self._send_loop, name="comm.sending_thread") + self._sending_thread.daemon = True + self._sending_thread.start() + super(ReprapGcodeProtocol, self).connect(transport, transport_args=transport_args, transport_kwargs=transport_kwargs) + def disconnect(self): + # TODO either clear send queue, or wait until everything has been sent... self._send_queue_active = False + self._sending_thread = None + if self._temperature_poller: self._temperature_poller.cancel() self._temperature_poller = None @@ -291,6 +300,8 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, line = to_unicode(data, encoding="ascii", errors="replace").strip() lower_line = line.lower() + any_processed = False + for message in self._registered_messages: handler_method = getattr(self, "_on_{}".format(message), None) if not handler_method: @@ -302,6 +313,10 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, matches, continue_further = matches if matches: + if not any_processed: + self._on_comm_any() + any_processed = True + message_args = dict() parse_method = getattr(self.flavor, "parse_{}".format(message), None) @@ -324,6 +339,13 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, # unknown message pass + def _on_comm_any(self): + if self.state == ProtocolState.CONNECTING: + hello = self.flavor.command_hello() + if hello: + self._send_command(hello) + self._clear_to_send.set() + def _on_comm_timeout(self): if self.state == ProtocolState.CONNECTING: self.disconnect() @@ -336,6 +358,13 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, self._logger.debug("Ran into a communication timeout, but a command known to be a long runner is currently active") def _on_comm_ok(self): + if self._internal_state["ignore_ok"] > 0: + self._internal_state["ignore_ok"] -= 1 + if self._internal_state["ignore_ok"] < 0: + self._internal_state["ignore_ok"] = 0 + self._logger.debug("Ignoring this ok, ignore counter is now {}".format(self._internal_state["ignore_ok"])) + return + if self.state == ProtocolState.CONNECTING: self.state = ProtocolState.CONNECTED return @@ -355,6 +384,10 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, else: self._continue_sending() + def _on_comm_ignore_ok(self): + self._internal_state["ignore_ok"] += 1 + self._logger.info("Ignoring next ok, counter is now at {}".format(self._internal_state["ignore_ok"])) + def _on_comm_wait(self): if self.state == ProtocolState.PRINTING: self._on_comm_ok() @@ -370,14 +403,14 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, if last_communication_error is not None \ and last_communication_error == "linenumber" \ - and linenumber == self._internal_state["resend_last_linenumber"] \ + and linenumber == self._internal_state["resend_linenumber"] \ and self._internal_state["resend_delta"] is not None and self._internal_state["resend_count"] < self._internal_state["resend_delta"]: self._logger.debug("Ignoring resend request for line {}, that still originates from lines we sent before we got the first resend request".format(linenumber)) self._internal_state["resend_count"] += 1 return self._internal_state["resend_delta"] = resend_delta - self._internal_state["resend_last_linenumber"] = linenumber + self._internal_state["resend_linenumber"] = linenumber self._internal_state["resend_count"] = 0 def _on_comm_start(self): @@ -475,7 +508,7 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, with self._send_queue_mutex: def reset_resend_data(): self._internal_state["resend_delta"] = None - self._internal_state["resend_last_linenumber"] = None + self._internal_state["resend_linenumber"] = None self._internal_state["resend_count"] = 0 resend_delta = self._internal_state["resend_delta"] @@ -747,9 +780,9 @@ class ReprapGcodeProtocol(Protocol, MotorControlProtocolMixin, self._do_send_with_checksum(cmd, linenumber) def _do_send_with_checksum(self, cmd, lineNumber): - command_to_send = "N%d %s" % (lineNumber, cmd) + command_to_send = b"N%d %s" % (lineNumber, cmd) checksum = reduce(lambda x,y:x^y, map(ord, command_to_send)) - command_to_send = "%s*%d" % (command_to_send, checksum) + command_to_send = b"%s*%d" % (command_to_send, checksum) self._do_send_without_checksum(command_to_send) def _do_send_without_checksum(self, data): diff --git a/src/octoprint/comm/protocol/reprap/flavors/__init__.py b/src/octoprint/comm/protocol/reprap/flavors/__init__.py index ad2cf06cf..3632bfcd7 100644 --- a/src/octoprint/comm/protocol/reprap/flavors/__init__.py +++ b/src/octoprint/comm/protocol/reprap/flavors/__init__.py @@ -83,6 +83,10 @@ class ReprapGcodeFlavor(object): def comm_error(cls, line, lower_line, state): return line.startswith("Error:") or line.startswith("!!") + @classmethod + def comm_ignore_ok(cls, line, lower_line, state): + return False + @classmethod def error_multiline(cls, line, lower_line, state): return False @@ -335,7 +339,7 @@ class ReprapGcodeFlavor(object): # file from the root of the sd -- we'll prepend a / filename = "/" + filename - return dict(name=filename, size=int(size)) + return dict(name=filename, size=int(size) if size is not None else None) @classmethod def parse_message_sd_printing_byte(cls, line, lower_line, state): diff --git a/src/octoprint/comm/protocol/reprap/flavors/marlin.py b/src/octoprint/comm/protocol/reprap/flavors/marlin.py index 6e4deea49..9fb0dfa13 100644 --- a/src/octoprint/comm/protocol/reprap/flavors/marlin.py +++ b/src/octoprint/comm/protocol/reprap/flavors/marlin.py @@ -12,3 +12,14 @@ from octoprint.comm.protocol.reprap.flavors import ReprapGcodeFlavor class MarlinFlavor(ReprapGcodeFlavor): key = "marlin" + +class BqMarlinFlavor(MarlinFlavor): + + key = "bqmarlin" + + long_running_commands = MarlinFlavor.long_running_commands + ["G92", "M800", "M801"] + + @classmethod + def comm_ignore_ok(cls, line, lower_line, state): + return lower_line.startswith("echo:enqueueing") + diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 00acf8939..af34e6206 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -52,7 +52,9 @@ def _serial_factory(port=None, baudrate=None): except: logging.getLogger(__name__).exception("Error while creating serial via factory {}".format(name)) return None - return serial_obj + + if serial_obj is not None: + return serial_obj return None @@ -227,8 +229,8 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback, ProtocolListener, transport = SerialTransport(serial_factory=_serial_factory) from octoprint.comm.protocol.reprap import ReprapGcodeProtocol - from octoprint.comm.protocol.reprap.flavors import ReprapGcodeFlavor - protocol = ReprapGcodeProtocol(ReprapGcodeFlavor) + from octoprint.comm.protocol.reprap.flavors.marlin import BqMarlinFlavor + protocol = ReprapGcodeProtocol(BqMarlinFlavor) protocol.register_listener(self) self._transport = transport @@ -245,11 +247,10 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback, ProtocolListener, if self._protocol is not None and self._protocol.state not in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR): self._protocol.disconnect() + self._protocol.unregister_listener(self) self._protocol = None - - if self._transport is not None: - self._transport.disconnect() self._transport = None + eventManager().fire(Events.DISCONNECTED) def get_transport(self): @@ -876,7 +877,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback, ProtocolListener, elif new_state == ProtocolState.PRINTING: self._analysis_queue.pause() # do not analyse files while printing - elif new_state in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR): + elif new_state in (ProtocolState.DISCONNECTED, ProtocolState.DISCONNECTED_WITH_ERROR) and old_state != ProtocolState.DISCONNECTING: self.disconnect() self._setCurrentZ(None) self._update_progress_data()