From a24d3f26aeaa7572802a18bcf5e430bd44a2532b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 16 Mar 2021 11:02:01 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20Fix=20caching=20behaviour=20?= =?UTF-8?q?on=20SD=20card=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `force=true` is supplied, the SD card files should be refreshed. Also finally documented the `force` parameter in the API docs. Closes #4051 --- docs/api/files.rst | 8 ++++++-- src/octoprint/printer/standard.py | 20 +++++++++++--------- src/octoprint/server/api/files.py | 4 ++-- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/docs/api/files.rst b/docs/api/files.rst index d295ee2b9..0757bde41 100644 --- a/docs/api/files.rst +++ b/docs/api/files.rst @@ -14,7 +14,8 @@ Retrieve all files .. http:get:: /api/files Retrieve information regarding all files currently available and regarding the disk space still available - locally in the system. + locally in the system. The results are cached for performance reasons. If you + want to override the cache, supply the query parameter ``force`` and set it to ``true``. By default only returns the files and folders in the root directory. If the query parameter ``recursive`` is provided and set to ``true``, returns all files and folders. @@ -234,6 +235,7 @@ Retrieve all files "free": "3.2GB" } + :param force: If set to ``true``, forces a refresh, overriding the cache. :param recursive: If set to ``true``, return all files and folders recursively. Otherwise only return items on same level. :statuscode 200: No error @@ -245,7 +247,8 @@ Retrieve files from specific location .. http:get:: /api/files/(string:location) Retrieve information regarding the files currently available on the selected `location` and -- if targeting - the ``local`` location -- regarding the disk space still available locally in the system. + the ``local`` location -- regarding the disk space still available locally in the system. The results are cached for performance reasons. If you + want to override the cache, supply the query parameter ``force`` and set it to ``true``. By default only returns the files and folders in the root directory. If the query parameter ``recursive`` is provided and set to ``true``, returns all files and folders. @@ -305,6 +308,7 @@ Retrieve files from specific location :param location: The origin location from which to retrieve the files. Currently only ``local`` and ``sdcard`` are supported, with ``local`` referring to files stored in OctoPrint's ``uploads`` folder and ``sdcard`` referring to files stored on the printer's SD card (if available). + :param force: If set to ``true``, forces a refresh, overriding the cache. :param recursive: If set to ``true``, return all files and folders recursively. Otherwise only return items on same level. :statuscode 200: No error :statuscode 404: If `location` is neither ``local`` nor ``sdcard`` diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 5316b8d1e..deef6978a 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -920,16 +920,18 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): # ~~ sd file handling def get_sd_files(self, *args, **kwargs): - if self._comm is None or not self._comm.isSdReady(): - result = [] - else: - result = list( - map( - lambda x: {"name": x[0][1:], "size": x[1], "display": x[2]}, - self._comm.getSdFiles(), - ) + if not self.is_sd_ready(): + return [] + + if kwargs.get("refresh"): + self.refresh_sd_files(blocking=True) + + return list( + map( + lambda x: {"name": x[0][1:], "size": x[1], "display": x[2]}, + self._comm.getSdFiles(), ) - return result + ) def add_sd_file( self, filename, path, on_success=None, on_failure=None, *args, **kwargs diff --git a/src/octoprint/server/api/files.py b/src/octoprint/server/api/files.py index 385daee99..a1608d4a4 100644 --- a/src/octoprint/server/api/files.py +++ b/src/octoprint/server/api/files.py @@ -157,7 +157,7 @@ def readGcodeFiles(): recursive=recursive, allow_from_cache=not force, ) - files.extend(_getFileList(FileDestinations.SDCARD)) + files.extend(_getFileList(FileDestinations.SDCARD, allow_from_cache=not force)) usage = psutil.disk_usage(settings().getBaseFolder("uploads", check_writable=False)) return jsonify(files=files, free=usage.free, total=usage.total) @@ -254,7 +254,7 @@ def _getFileList( origin, path=None, filter=None, recursive=False, level=0, allow_from_cache=True ): if origin == FileDestinations.SDCARD: - sdFileList = printer.get_sd_files() + sdFileList = printer.get_sd_files(refresh=not allow_from_cache) files = [] if sdFileList is not None: From 91733d314f03ab4f57264dcdb8ab84327b624401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 16 Mar 2021 11:34:17 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20New=20action=20commands=20for?= =?UTF-8?q?=20SD=20signaling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/features/action_commands.rst | 21 +++++++++++++++++++++ src/octoprint/util/comm.py | 13 +++++++++++++ 2 files changed, 34 insertions(+) diff --git a/docs/features/action_commands.rst b/docs/features/action_commands.rst index 6e5f8830a..b3d298e2e 100644 --- a/docs/features/action_commands.rst +++ b/docs/features/action_commands.rst @@ -54,6 +54,27 @@ resumed disconnect When this command is received from the printer, OctoPrint will immediately disconnect from it. +sd_inserted + When this command is received from the printer, OctoPrint will assume an SD card is present in the printer, + set the corresponding internal state flags and send a file list request. This command is only recognized + if SD support is enabled in OctoPrint. + + .. versionadded:: 1.6.0 + +sd_ejected + When this command is received from the printer, OctoPrint will assume the SD card has been removed from + the printer and clear the corresponding internal state flags. This command is only recognized + if SD support is enabled in OctoPrint. + + .. versionadded:: 1.6.0 + +sd_updated + When this command is received from the printer, OctoPrint will assume something on the SD card in the + printer has changed and trigger a file list request. This command is only recognized + if SD support is enabled in OctoPrint. + + .. versionadded:: 1.6.0 + If the bundled :ref:`Action Command Prompt Support Plugin ` is enabled (which should be the case by default), OctoPrint will also interactive dialog creation through its :ref:`supported commands `. diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 6dcc9f3b7..ae16eb05a 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -2288,6 +2288,19 @@ class MachineCom(object): elif action_name == "disconnect": self._log("Disconnecting on request of the printer...") self._callback.on_comm_force_disconnect() + elif self._sdEnabled and action_name == "sd_inserted": + self._log("Printer reported SD card as inserted") + self._sdAvailable = True + self.refreshSdFiles() + self._callback.on_comm_sd_state_change(self._sdAvailable) + elif self._sdEnabled and action_name == "sd_ejected": + self._log("Printer reported SD card as ejected") + self._sdAvailable = False + self._sdFiles = [] + self._callback.on_comm_sd_state_change(self._sdAvailable) + elif self._sdEnabled and action_name == "sd_updated": + self._log("Printer reported a change on the SD card") + self.refreshSdFiles() else: for name, hook in self._printer_action_hooks.items(): try: From 08922975f313a887be6051604526c55cf4359b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 16 Mar 2021 12:56:41 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20Ignore=20SD=20messages=20if?= =?UTF-8?q?=20sd=20support=20is=20disabled?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/util/comm.py | 253 +++++++++++++++++++------------------ 1 file changed, 129 insertions(+), 124 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index ae16eb05a..eceeecdd4 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -2339,7 +2339,7 @@ class MachineCom(object): ##~~ SD file list # if we are currently receiving an sd file list, each line is just a filename, so just read it and abort processing - if self._sdFileList and "End file list" not in line: + if self._sdEnabled and self._sdFileList and "End file list" not in line: preprocessed_line = line fileinfo = preprocessed_line.split(None, 2) if len(fileinfo) == 3: @@ -2799,140 +2799,145 @@ class MachineCom(object): ) ##~~ SD Card handling - elif ( - "SD init fail" in line - or "volume.init failed" in line - or "openRoot failed" in line - ): - self._sdAvailable = False - self._sdFiles = [] - self._callback.on_comm_sd_state_change(self._sdAvailable) - elif "SD card ok" in line and not self._sdAvailable: - self._sdAvailable = True - self.refreshSdFiles() - self._callback.on_comm_sd_state_change(self._sdAvailable) - elif "Begin file list" in line: - self._sdFiles = [] - self._sdFileList = True - elif "End file list" in line: - self._sdFileList = False - self._callback.on_comm_sd_files(self.getSdFiles()) - elif "SD printing byte" in line: - # answer to M27, at least on Marlin, Repetier and Sprinter: "SD printing byte %d/%d" - match = regex_sdPrintingByte.search(line) - if match: - try: - current = int(match.group("current")) - total = int(match.group("total")) - except Exception: - self._logger.exception("Error while parsing SD status report") - else: - if ( - current == total == 0 - and self.isSdPrinting() - and not self.isStarting() - and not self.isStarting() - and not self.isFinishing() - ): - # apparently not SD printing - some Marlin versions report it like that for some reason - self._consecutive_not_sd_printing += 1 - if ( - self._consecutive_not_sd_printing - > self._consecutive_not_sd_printing_maximum - ): - self.cancelPrint(external_sd=True) - - else: - self._consecutive_not_sd_printing = 0 - if self.isSdFileSelected(): - - # If we are not yet sd printing, the current does not equal the total, is larger - # than zero and has increased since the last time we saw a position report, then - # yes, this looks like we just started printing due to an external trigger. - if ( - not self.isSdPrinting() - and current != total - and current > 0 - and self._currentFile - and current > self._currentFile.pos - ): - self.startPrint(external_sd=True) - - self._currentFile.pos = current - if self._currentFile.size == 0: - self._currentFile.size = total - - if not self._currentFile.done: - self._callback.on_comm_progress() - elif ( - "Not SD printing" in line - and self.isSdPrinting() - and not self.isStarting() - and not self.isFinishing() - ): - self._consecutive_not_sd_printing += 1 + elif self._sdEnabled: if ( - self._consecutive_not_sd_printing - > self._consecutive_not_sd_printing_maximum + "SD init fail" in line + or "volume.init failed" in line + or "openRoot failed" in line ): - # something went wrong, printer is reporting that we actually are not printing right now... - self.cancelPrint(external_sd=True) - elif "File opened" in line and not self._ignore_select: - # answer to M23, at least on Marlin, Repetier and Sprinter: "File opened:%s Size:%d" - match = regex_sdFileOpened.search(line) - if match: - name = match.group("name") - size = int(match.group("size")) - else: - name = "Unknown" - size = 0 - user = None + self._sdAvailable = False + self._sdFiles = [] + self._callback.on_comm_sd_state_change(self._sdAvailable) + elif "SD card ok" in line and not self._sdAvailable: + self._sdAvailable = True + self.refreshSdFiles() + self._callback.on_comm_sd_state_change(self._sdAvailable) + elif "Begin file list" in line: + self._sdFiles = [] + self._sdFileList = True + elif "End file list" in line: + self._sdFileList = False + self._callback.on_comm_sd_files(self.getSdFiles()) + elif "SD printing byte" in line: + # answer to M27, at least on Marlin, Repetier and Sprinter: "SD printing byte %d/%d" + match = regex_sdPrintingByte.search(line) + if match: + try: + current = int(match.group("current")) + total = int(match.group("total")) + except Exception: + self._logger.exception( + "Error while parsing SD status report" + ) + else: + if ( + current == total == 0 + and self.isSdPrinting() + and not self.isStarting() + and not self.isStarting() + and not self.isFinishing() + ): + # apparently not SD printing - some Marlin versions report it like that for some reason + self._consecutive_not_sd_printing += 1 + if ( + self._consecutive_not_sd_printing + > self._consecutive_not_sd_printing_maximum + ): + self.cancelPrint(external_sd=True) - if self._sdFileToSelect: - name = self._sdFileToSelect - user = self._sdFileToSelectUser + else: + self._consecutive_not_sd_printing = 0 + if self.isSdFileSelected(): - self._sdFileToSelect = None - self._sdFileToSelectUser = None + # If we are not yet sd printing, the current does not equal the total, is larger + # than zero and has increased since the last time we saw a position report, then + # yes, this looks like we just started printing due to an external trigger. + if ( + not self.isSdPrinting() + and current != total + and current > 0 + and self._currentFile + and current > self._currentFile.pos + ): + self.startPrint(external_sd=True) - self._currentFile = PrintingSdFileInformation(name, size, user=user) - elif "File selected" in line: - if self._ignore_select: - self._ignore_select = False - elif self._currentFile is not None and self.isSdFileSelected(): - # final answer to M23, at least on Marlin, Repetier and Sprinter: "File selected" - self._callback.on_comm_file_selected( - self._currentFile.getFilename(), - self._currentFile.getFilesize(), - True, - user=self._currentFile.getUser(), + self._currentFile.pos = current + if self._currentFile.size == 0: + self._currentFile.size = total + + if not self._currentFile.done: + self._callback.on_comm_progress() + elif ( + "Not SD printing" in line + and self.isSdPrinting() + and not self.isStarting() + and not self.isFinishing() + ): + self._consecutive_not_sd_printing += 1 + if ( + self._consecutive_not_sd_printing + > self._consecutive_not_sd_printing_maximum + ): + # something went wrong, printer is reporting that we actually are not printing right now... + self.cancelPrint(external_sd=True) + elif "File opened" in line and not self._ignore_select: + # answer to M23, at least on Marlin, Repetier and Sprinter: "File opened:%s Size:%d" + match = regex_sdFileOpened.search(line) + if match: + name = match.group("name") + size = int(match.group("size")) + else: + name = "Unknown" + size = 0 + user = None + + if self._sdFileToSelect: + name = self._sdFileToSelect + user = self._sdFileToSelectUser + + self._sdFileToSelect = None + self._sdFileToSelectUser = None + + self._currentFile = PrintingSdFileInformation( + name, size, user=user ) - elif "Writing to file" in line and self.isStreaming(): - self._changeState(self.STATE_PRINTING) - elif "Done printing file" in line and self.isSdPrinting(): - # printer is reporting file finished printing - self._changeState(self.STATE_FINISHING) + elif "File selected" in line: + if self._ignore_select: + self._ignore_select = False + elif self._currentFile is not None and self.isSdFileSelected(): + # final answer to M23, at least on Marlin, Repetier and Sprinter: "File selected" + self._callback.on_comm_file_selected( + self._currentFile.getFilename(), + self._currentFile.getFilesize(), + True, + user=self._currentFile.getUser(), + ) + elif "Writing to file" in line and self.isStreaming(): + self._changeState(self.STATE_PRINTING) + elif "Done printing file" in line and self.isSdPrinting(): + # printer is reporting file finished printing + self._changeState(self.STATE_FINISHING) - self._currentFile.done = True - self._currentFile.pos = 0 + self._currentFile.done = True + self._currentFile.pos = 0 - self.sendCommand("M400", part_of_job=True) - self._callback.on_comm_print_job_done() + self.sendCommand("M400", part_of_job=True) + self._callback.on_comm_print_job_done() - def finalize(): - self._changeState(self.STATE_OPERATIONAL) + def finalize(): + self._changeState(self.STATE_OPERATIONAL) - self.sendCommand(SendQueueMarker(finalize), part_of_job=True) - self._continue_sending() - elif "Done saving file" in line: - if self._trigger_ok_for_m29: - # workaround for most versions of Marlin out in the wild - # not sending an ok after saving a file + self.sendCommand(SendQueueMarker(finalize), part_of_job=True) + self._continue_sending() + elif "Done saving file" in line: + if self._trigger_ok_for_m29: + # workaround for most versions of Marlin out in the wild + # not sending an ok after saving a file + self._handle_ok() + elif "File deleted" in line and line.strip().endswith("ok"): + # buggy Marlin version that doesn't send a proper line break after the "File deleted" statement, fixed in + # current versions self._handle_ok() - elif "File deleted" in line and line.strip().endswith("ok"): - # buggy Marlin version that doesn't send a proper line break after the "File deleted" statement, fixed in - # current versions - self._handle_ok() ##~~ Message handling self._callback.on_comm_message(line)