Skip to content

Commit

Permalink
Add errors handler.
Browse files Browse the repository at this point in the history
Add timeout for upload file.
Add softwareupdate hook.
  • Loading branch information
om2804 committed Aug 1, 2022
1 parent a9808e2 commit 5cf79c4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
49 changes: 38 additions & 11 deletions octoprint_mkswifisdcard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def change_state_handler(event, data):

def set_upload_progress(progress):
self._plugin_manager.send_plugin_message(
"mkswifisdcard", dict(progress=int(progress))
self._identifier, dict(progress=int(progress))
)

def run_upload():
Expand All @@ -69,16 +69,23 @@ def run_upload():
return remote_name

def upload_via_wifi(self, path, filename, host, callback):
res = requests.post(
url="http://%s/upload?X-Filename=%s" % (host, filename),
data=ProgressUpload(path, callback),
headers={
"Content-Type": "application/octet-stream",
"Connection": "keep-alive",
},
)
try:
res = requests.post(
url="http://%s/upload?X-Filename=%s" % (host, filename),
data=ProgressUpload(path, callback),
headers={
"Content-Type": "application/octet-stream",
"Connection": "keep-alive",
},
timeout=60
)
except Exception as e:
self._logger.exception("Uploading to sdwire failed: {}".format(e))
self.notify_error_to_user("Uploading to sdwire failed: {}".format(e))


def notify_error_to_user(self, message):
self._plugin_manager.send_plugin_message(self._identifier, dict(error=message))

# TemplatePlugin mixin
def get_template_configs(self):
return [{"type": "settings", "custom_bindings": False}]
Expand All @@ -93,6 +100,24 @@ def get_assets(self):
# core UI here.
return {"js": ["js/mkswifisdcard.js"]}

# Softwareupdate hook
def get_update_information(self):
return {
"mkswifisdcard": {
"displayName": "mkswifisdcard",
"displayVersion": self._plugin_version,

# version check: github repository
"type": "github_release",
"user": "om2804",
"repo": "octoPrint-mkswifisdcard",
"current": self._plugin_version,

# update method: pip
"pip": "https://github.com/om2804/octoprint_mkswifisdcard/archive/{target_version}.zip",
}
}


class ProgressUpload:
def __init__(self, filename, callback, chunk_size=1048576):
Expand All @@ -113,6 +138,7 @@ def __len__(self):
return self.file_size



# Set the Python version your plugin is compatible with below. Recommended is Python 3 only for all new plugins.
# OctoPrint 1.8.0 onwards only supports Python 3.
__plugin_pythoncompat__ = ">=3,<4" # Only Python 3
Expand All @@ -124,5 +150,6 @@ def __plugin_load__():

global __plugin_hooks__
__plugin_hooks__ = {
"octoprint.printer.sdcardupload": __plugin_implementation__.custom_upload_to_sd
"octoprint.printer.sdcardupload": __plugin_implementation__.custom_upload_to_sd,
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
}
8 changes: 8 additions & 0 deletions octoprint_mkswifisdcard/static/js/mkswifisdcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ $(function () {
self.filesViewModel._setProgressBar(data["progress"], 'Uploading to sd - ' + data["progress"] + '%...', false);
}

if (data.hasOwnProperty("error")) {
new PNotify({
title: 'MKS WiFi SD-card Error',
text: '<div class="row-fluid"><p>Error of uploading to sd-card over WiFi. Check your settings.</p><p><pre style="padding-top: 5px;">'+data["error"]+'</pre></p>',
hide: true
});
return;
}
}
}

Expand Down

0 comments on commit 5cf79c4

Please sign in to comment.