diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index b97d8ff8f..a291a5900 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -1107,6 +1107,73 @@ octoprint.server.api.after_request .. _sec-plugins-hook-server-http-before_request: +octoprint.printer.cardupload +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. py:function:: sd_card_upload_hook(filename, absolutePath, remoteName, on_success, on_error, *args, **kwargs) + + via hook, you can change the upload system on the sd card of the printer. + + when your copy is finished, you must use one of the on_success or on_error functions, depending on the status of the copy. + these two methods take into parameter a specific payload : + .. code-block:: python + + payload = { + "local": filename, + "remote": remoteName, + "time": time.time() - timestart + } + This hook must also return the file name on the sd card when done. + + **Example:** + .. code-block:: python + + # coding=utf-8 + + class CustomSdCardUploadPlugin(octoprint.plugin.OctoPrintPlugin): + + def sdcard(self, filename, absolutePath, remoteName, is_sd_ready, init_sd_card, release_sd_card, refresh_sd_files, on_success=None, on_failure=None, *args, **kwargs): + self._logger.info("custom sd card upload") + timestart = time.time() + # do something + payload = { + "local": filename, + "remote": remoteName, + "time": time.time() - timestart + } + on_success(payload); + return remoteName + + __plugin_name__ = "Custom sd card upload plugin" + + def __plugin_load__(): + plugin = CustomSdCardUploadPlugin() + + global __plugin_implementation__ + __plugin_implementation__ = plugin + + global __plugin_hooks__ + __plugin_hooks__ = {"octoprint.printer.cardupload": plugin.custom_sd_card_upload_plugin} + + global __plugin_name__ + __plugin_name__ = "custom sd card upload plugin" + + + `` + + .. versionadded:: 1.3.9 + + :param str filename: filename inside octoprint + :param str absolutePath: absolutePath inside octoprint + :param str remoteName: name inside sd card printer + :param function is_sd_ready: check if sd card is ready on printer (no parameter) + :param function init_sd_card: initialize sd function, take self in parameter + :param function release_sd_card: release sd function, take self in parameter + :param function refresh_sd_files: refresh sd function, take self in parameter + :param function on_success: success function + :param function on_error: error function + :return: remoteName on sdcard + octoprint.server.api.before_request ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 989b6095e..16ee2138a 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -648,7 +648,14 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): def get_sd_files(self, *args, **kwargs): if self._comm is None or not self._comm.isSdReady(): return [] - return map(lambda x: (x[0][1:], x[1]), self._comm.getSdFiles()) + return list(map(lambda x: (x[0][1:], x[1]), self._comm.getSdFiles())) + + def success_hook_sdcopy(self, payload): + self.on_comm_file_transfer_done(payload["remote"]) + eventManager().fire(Events.TRANSFER_DONE, payload) + + def error_hook_sdcopy(self, payload): + eventManager().fire(Events.TRANSFER_FAILED, payload) def add_sd_file(self, filename, absolutePath, on_success=None, on_failure=None, *args, **kwargs): if not self._comm or self._comm.isBusy() or not self._comm.isSdReady():