diff --git a/src/octoprint/comm/protocol/__init__.py b/src/octoprint/comm/protocol/__init__.py index 16ab4fbac..727ff8e92 100644 --- a/src/octoprint/comm/protocol/__init__.py +++ b/src/octoprint/comm/protocol/__init__.py @@ -61,6 +61,15 @@ class Protocol(MessageReceiver, StateReceiver, LogReceiver): self._transport.connect(opt) def disconnect(self, on_error=False): + if self.is_printing(): + payload = None + if self._current_file is not None: + payload = { + "file": self._current_file.getFilename(), + "filename": os.path.basename(self._current_file.getFilename()), + "origin": self._current_file.getFileLocation() + } + eventManager().fire(Events.PRINT_FAILED, payload) self._transport.disconnect(on_error) def send_manually(self, command, high_priority=False): @@ -81,16 +90,31 @@ class Protocol(MessageReceiver, StateReceiver, LogReceiver): self._current_file.start() self._changeState(State.PRINTING) + eventManager().fire(Events.PRINT_STARTED, { + "file": self._current_file.getFilename(), + "filename": os.path.basename(self._current_file.getFilename()), + "origin": self._current_file.getFileLocation() + }) def pause_print(self): + payload = { + "file": self._current_file.getFilename(), + "filename": os.path.basename(self._current_file.getFilename()), + "origin": self._current_file.getFileLocation() + } + if self._state == State.PRINTING: self._changeState(State.PAUSED) + eventManager().fire(Events.PRINT_PAUSED, payload) + elif self._state == State.PAUSED: self._changeState(State.PRINTING) + eventManager().fire(Events.PRINT_RESUMED, payload) def cancel_print(self): eventManager().fire(Events.PRINT_CANCELLED, { "file": self._current_file.getFilename(), + "filename": os.path.basename(self._current_file.getFilename()), "origin": self._current_file.getFileLocation() }) self._changeState(State.OPERATIONAL) diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index 31502da9a..460d70473 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -325,6 +325,11 @@ class RepRapProtocol(Protocol): def _fileTransferFinished(self, current_file): if isinstance(current_file, StreamingGcodeFileInformation): self.send_manually(RepRapProtocol.COMMAND_SD_END_WRITE(current_file.getRemoteFilename())) + eventManager().fire(Events.TRANSFER_DONE, { + "local": local, + "remote": remote, + "time": self.getPrintTime() + }) else: self._logger.warn("Finished file transfer to printer's SD card, but could not determine remote filename, assuming 'unknown.gco' for end-write-command") self.send_manually(RepRapProtocol.COMMAND_SD_END_WRITE("unknown.gco")) diff --git a/src/octoprint/comm/transport/__init__.py b/src/octoprint/comm/transport/__init__.py index a6fec3a28..ae63c6181 100644 --- a/src/octoprint/comm/transport/__init__.py +++ b/src/octoprint/comm/transport/__init__.py @@ -5,6 +5,7 @@ __copyright__ = "Copyright (C) 2014 Gina Häußge - Released under terms of the import logging +from octoprint.events import eventManager, Events class Transport(object): @@ -35,7 +36,7 @@ class Transport(object): self.changeState(State.DISCONNECTED_WITH_ERROR) else: self.changeState(State.DISCONNECTED) - + eventManager().fire(Events.DISCONNECTED) def send(self, command): pass diff --git a/src/octoprint/comm/transport/serialTransport.py b/src/octoprint/comm/transport/serialTransport.py index 014cbccc1..0f464ed1f 100644 --- a/src/octoprint/comm/transport/serialTransport.py +++ b/src/octoprint/comm/transport/serialTransport.py @@ -17,7 +17,7 @@ from . import Transport, TransportProperties, State from octoprint.util import getExceptionString from octoprint.util.virtual import VirtualPrinter from octoprint.settings import settings - +from octoprint.events import eventManager, Events class SerialTransport(Transport): @@ -126,17 +126,17 @@ class SerialTransport(Transport): self._serial = VirtualPrinter(timeout=self._communicationTimeout, writeTimeout=self._writeTimeout) self.changeState(State.CONNECTED) self._transport_logger.debug("Connected to VIRTUAL printer") - return True else: try: self._serial = serial.Serial(self._port, self._baudrate, timeout=self._communicationTimeout, writeTimeout=self._writeTimeout) self.changeState(State.CONNECTED) self._transport_logger.debug("Connected to %s" % self._serial) - return True except: self.logError("Unexpected error while connecting to serial port: %s %s" % (self._port, getExceptionString())) self.onError("Failed to open serial port, permissions correct?") return False + eventManager().fire(Events.CONNECTED, {"port": self._port, "baudrate": self._baudrate}) + return True def _readline(self): if self._serial is None: diff --git a/src/octoprint/events.py b/src/octoprint/events.py index 4a3d2023e..a73b70760 100644 --- a/src/octoprint/events.py +++ b/src/octoprint/events.py @@ -293,7 +293,7 @@ class CommandTrigger(GenericEventListener): def _executeGcodeCommand(self, command): commands = [command] if isinstance(command, (list, tuple, set)): - self.logger.debug("Executing GCode commands: %r" % command) + self._logger.debug("Executing GCode commands: %r" % command) commands = list(command) else: self._logger.debug("Executing GCode command: %s" % command)