add callback for on success/error for hook sdcopy
This commit is contained in:
1 parent
5396daa7f6
commit
f8f461b804
2 files changed
+33
-25
No files matched your search
+24
-5
@@ -1146,8 +1146,18 @@ octoprint.printer.sdcardupload
|
||||
|
||||
via hook, you can change the upload system on the sd card of the printer.
|
||||
|
||||
when your copy is finished, you must return the status of the copy (true or false)
|
||||
|
||||
when your copy is finished, you must call the callback (success_hook_sdcopy or error_hook_sdcopy) for notify the status (it's important)
|
||||
|
||||
these callback take into parameter a specific payload :
|
||||
.. code-block:: python
|
||||
payload = {
|
||||
"local": filename,
|
||||
"remote": remoteName,
|
||||
"time": time.time() - timestart
|
||||
}
|
||||
|
||||
``
|
||||
|
||||
**Example:**
|
||||
.. code-block:: python
|
||||
|
||||
@@ -1155,15 +1165,21 @@ octoprint.printer.sdcardupload
|
||||
|
||||
class CustomSdCardUploadPlugin(octoprint.plugin.OctoPrintPlugin):
|
||||
|
||||
def sdcard(self, printer, filename, remoteName, absolutePath, *args, **kwargs):
|
||||
def sdcard(self, printer, filename, remoteName, absolutePath, success_hook_sdcopy, error_hook_sdcopy, *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
|
||||
return True
|
||||
payload = {
|
||||
"local": filename,
|
||||
"remote": remoteName,
|
||||
"time": time.time() - timestart
|
||||
}
|
||||
success_hook_sdcopy(payload);
|
||||
|
||||
__plugin_name__ = "Custom sd card upload plugin"
|
||||
|
||||
@@ -1188,7 +1204,10 @@ octoprint.printer.sdcardupload
|
||||
:param str filename: filename inside octoprint
|
||||
:param str remoteName: name inside sd card printer
|
||||
:param str absolutePath: absolutePath inside octoprint
|
||||
:return: True if success of False if error
|
||||
:param callback : absolutePath inside octoprint
|
||||
:param str absolutePath: absolutePath inside octoprint
|
||||
:return success_hook_sdcopy: for notify success (important), this callback take payload parameter
|
||||
:return error_hook_sdcopy: for notify error (important), this callback take payload parameter
|
||||
|
||||
.. _sec-plugins-hook-server-http-bodysize:
|
||||
|
||||
|
||||
@@ -653,19 +653,23 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback):
|
||||
return []
|
||||
return map(lambda x: (x[0][1:], x[1]), self._comm.getSdFiles())
|
||||
|
||||
|
||||
def success_hook_sdcopy(self, payload, *args, **kwargs):
|
||||
self.on_comm_file_transfer_done(payload["remote"])
|
||||
eventManager().fire(Events.TRANSFER_DONE, payload)
|
||||
|
||||
def error_hook_sdcopy(self, payload, *args, **kwargs):
|
||||
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():
|
||||
self._logger.error("No connection to printer or printer is busy")
|
||||
return
|
||||
|
||||
def default_add_sd_file(self, printer, filename, remoteName, absolutePath, *args, **kwargs):
|
||||
def default_add_sd_file(self, printer, filename, remoteName, absolutePath, on_success=None, on_failure=None, *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
|
||||
|
||||
self._streamingFinishedCallback = on_success
|
||||
self._streamingFailedCallback = on_failure
|
||||
@@ -681,26 +685,11 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback):
|
||||
else:
|
||||
# probably something else added through a plugin, use it's basename as-is
|
||||
remoteName = os.path.basename(filename)
|
||||
sd_card_uploads = self.sd_card_upload_hooks.items() + [("default", self.default_add_sd_file)]
|
||||
sd_card_uploads = self.sd_card_upload_hooks.items() + [("default", 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()
|
||||
result_hook = hook(self, filename, remoteName, absolutePath)
|
||||
if result_hook is not None:
|
||||
payload["time"] = round(time.time() - timestart, 2)
|
||||
#End calculates total processing time
|
||||
if result_hook:
|
||||
self.on_comm_file_transfer_done(payload["remote"])
|
||||
eventManager().fire(Events.TRANSFER_DONE, payload)
|
||||
else:
|
||||
eventManager().fire(Events.TRANSFER_FAILED, payload)
|
||||
hook(self, filename, remoteName, absolutePath, self.success_hook_sdcopy, self.error_hook_sdcopy)
|
||||
#only one impl authorize
|
||||
break
|
||||
except:
|
||||
|
||||
Reference in new issue
Block a user