diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index b6c4d0892..9a0ac14b7 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -1137,25 +1137,16 @@ octoprint.server.http.access_validator Implementing this hook will make your plugin require a restart of OctoPrint for enabling/disabling it fully. -.. _sec-plugins-hook-octoprint-printer-cardupload: +.. _sec-plugins-hook-octoprint-printer-sdcardupload: -octoprint.printer.cardupload +octoprint.printer.sdcardupload ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. py:function:: sd_card_upload_hook(filename, absolutePath, remoteName, on_success, on_error, *args, **kwargs) +.. py:function:: sd_card_upload_hook(printer, filename, absolutePath, remoteName, *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. + when your copy is finished, you must return the status of the copy (true or false) **Example:** .. code-block:: python @@ -1164,17 +1155,15 @@ octoprint.printer.cardupload 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): + def sdcard(self, printer, filename, remoteName, absolutePath, *args, **kwargs): self._logger.info("custom sd card upload") - timestart = time.time() + # you can use the printer methode... + # for exemple : + # printer.release_sd_card(self) + # printer.init_sd_card(self) + # printer.refresh_sd_files() # do something - payload = { - "local": filename, - "remote": remoteName, - "time": time.time() - timestart - } - on_success(payload); - return remoteName + return True __plugin_name__ = "Custom sd card upload plugin" @@ -1185,7 +1174,7 @@ octoprint.printer.cardupload __plugin_implementation__ = plugin global __plugin_hooks__ - __plugin_hooks__ = {"octoprint.printer.cardupload": plugin.custom_sd_card_upload_plugin} + __plugin_hooks__ = {"octoprint.printer.sdcardupload": plugin.custom_sd_card_upload_plugin} global __plugin_name__ __plugin_name__ = "custom sd card upload plugin" @@ -1195,16 +1184,11 @@ octoprint.printer.cardupload .. versionadded:: 1.3.11 + :param object printer: access to printer methode :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 + :param str absolutePath: absolutePath inside octoprint + :return: True if success of False if error .. _sec-plugins-hook-server-http-bodysize: diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 76d14f045..bb03fba94 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -94,7 +94,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): self._logger.exception("Error while processing analysis queues from {}".format(name)) #hook card upload - self.sd_card_upload_hooks = plugin_manager().get_hooks("octoprint.printer.cardupload") + self.sd_card_upload_hooks = plugin_manager().get_hooks("octoprint.printer.sdcardupload") # comm self._comm = None @@ -652,14 +652,14 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): if self._comm is None or not self._comm.isSdReady(): return [] return 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 default_add_sd_file(self, printer, filename, remoteName, absolutePath, *args, **kwargs): + self._create_estimator("stream") + self._comm.startFileTransfer(absolutePath, filename, "/" + remoteName, + special=not valid_file_type(filename, "gcode"), + tags=kwargs.get("tags", set()) | {"trigger:printer.add_sd_file"}) + return None + 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(): self._logger.error("No connection to printer or printer is busy") @@ -679,22 +679,32 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): else: # probably something else added through a plugin, use it's basename as-is remoteName = os.path.basename(filename) + default_add_sd_file + sd_card_uploads = self.sd_card_upload_hooks.items() + [("default", self.default_add_sd_file)] + for name, hook in sd_card_uploads: + try: + payload = { + "local": filename, + "remote": remoteName, + "time": 0 + } + self._logger.info("run sd card upload with {}".format(name)) + #Start calculates total processing time + timestart = time.time() + resultHook = hook(self, filename, remoteName, absolutePath) + if not resultHook is None: + payload["time"] = round(time.time() - timestart, 2) + #End calculates total processing time + if resultHook: + self.on_comm_file_transfer_done(payload["remote"]) + eventManager().fire(Events.TRANSFER_DONE, payload) + else: + eventManager().fire(Events.TRANSFER_FAILED, payload) + #only one impl authorize + break + except: + self._logger.exception("Error while processing analysis queues from {}".format(name)) - if len(self.sd_card_upload_hooks.items()) > 0: - for name, hook in self.sd_card_upload_hooks.items(): - try: - self._logger.info("run sd card upload with {}".format(name)) - #only one impl authorize - return hook(filename, absolutePath, remoteName, self.is_sd_ready, self.init_sd_card, self.release_sd_card, self.refresh_sd_files, self.success_hook_sdcopy, self.error_hook_sdcopy) - except: - self._logger.exception("Error while processing analysis queues from {}".format(name)) - else: - self._logger.info("Use default sd card upload") - self._create_estimator("stream") - self._comm.startFileTransfer(absolutePath, filename, "/" + remoteName, - special=not valid_file_type(filename, "gcode"), - tags=kwargs.get("tags", set()) | {"trigger:printer.add_sd_file"}) - return remoteName def delete_sd_file(self, filename, *args, **kwargs):