diff --git a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js index 3782fdd4..5e3baf7e 100644 --- a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js +++ b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js @@ -580,25 +580,19 @@ $(function() { showConfirmationDialog({ message: gettext("This will restart your OctoPrint server."), onproceed: function() { - $.ajax({ - url: self.restartCommandSpec.resource, - type: "POST", - dataType: "json", - data: "{}", - contentType: "application/json; charset=UTF-8", - success: function() { + OctoPrint.system.executeCommand("core", "restart") + .done(function() { new PNotify({ title: gettext("Restart in progress"), text: gettext("The server is now being restarted in the background") }) - }, - error: function() { + }) + .fail(function() { new PNotify({ title: gettext("Something went wrong"), text: gettext("Trying to restart the server produced an error, please check octoprint.log for details. You'll have to restart manually.") }) - } - }); + }); } }); } diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 4d1a04b7..26468cf5 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -926,6 +926,7 @@ class Server(object): "js/app/client/printerprofiles.js", "js/app/client/settings.js", "js/app/client/slicing.js", + "js/app/client/system.js", "js/app/client/timelapse.js", "js/app/client/users.js", "js/app/client/util.js", diff --git a/src/octoprint/static/js/app/client/control.js b/src/octoprint/static/js/app/client/control.js index e724b51c..15c8ca08 100644 --- a/src/octoprint/static/js/app/client/control.js +++ b/src/octoprint/static/js/app/client/control.js @@ -39,10 +39,6 @@ script: script, context: context }, opts); - }, - - executeSystemCommand: function (action, opts) { - return OctoPrint.postJson("api/system", {action: action}, opts); } } }); diff --git a/src/octoprint/static/js/app/client/system.js b/src/octoprint/static/js/app/client/system.js new file mode 100644 index 00000000..1a86419a --- /dev/null +++ b/src/octoprint/static/js/app/client/system.js @@ -0,0 +1,24 @@ +(function (global, factory) { + if (typeof define === "function" && define.amd) { + define(["OctoPrint"], factory); + } else { + factory(window.OctoPrint); + } +})(window || this, function(OctoPrint) { + var url = "api/system"; + var commandUrl = "api/system/commands"; + + OctoPrint.system = { + getCommands: function (opts) { + return OctoPrint.get(commandUrl, opts); + }, + + getCommandsForSource: function (source, opts) { + return OctoPrint.get(commandUrl + "/" + source, opts); + }, + + executeCommand: function (source, action, opts) { + return OctoPrint.postJson(commandUrl + "/" + source + "/" + action, {}, opts); + } + }; +}); diff --git a/src/octoprint/static/js/app/viewmodels/system.js b/src/octoprint/static/js/app/viewmodels/system.js index 054ceff6..63b916c6 100644 --- a/src/octoprint/static/js/app/viewmodels/system.js +++ b/src/octoprint/static/js/app/viewmodels/system.js @@ -13,15 +13,11 @@ $(function() { self.requestCommandData = function() { if (!self.loginState.isAdmin()) { - return; + return $.Deferred().reject().promise(); } - $.ajax({ - url: API_BASEURL + "system/commands", - type: "GET", - dataType: "json", - success: self.fromCommandResponse - }); + return OctoPrint.system.getCommands() + .done(self.fromCommandResponse); }; self.fromCommandResponse = function(response) { @@ -44,35 +40,41 @@ $(function() { }; self.triggerCommand = function(commandSpec) { + var deferred = $.Deferred(); + var callback = function() { - $.ajax({ - url: commandSpec.resource, - type: "POST", - dataType: "json", - data: "{}", - contentType: "application/json; charset=UTF-8", - success: function() { + OctoPrint.system.executeCommand(commandSpec.actionSource, commandSpec.action) + .done(function() { new PNotify({title: "Success", text: _.sprintf(gettext("The command \"%(command)s\" executed successfully"), {command: commandSpec.name}), type: "success"}); - }, - error: function(jqXHR, textStatus, errorThrown) { + deferred.resolve(["success", arguments]); + }) + .fail(function(jqXHR, textStatus, errorThrown) { if (!commandSpec.hasOwnProperty("ignore") || !commandSpec.ignore) { var error = "
" + _.sprintf(gettext("The command \"%(command)s\" could not be executed."), {command: commandSpec.name}) + "
"; error += pnotifyAdditionalInfo("" + jqXHR.responseText + ""); new PNotify({title: gettext("Error"), text: error, type: "error", hide: false}); + deferred.reject(["error", arguments]); + } else { + deferred.resolve(["ignored", arguments]); } - } - }) + }); }; + if (commandSpec.confirm) { showConfirmationDialog({ message: commandSpec.confirm, - onproceed: function(e) { + onproceed: function() { callback(); + }, + oncancel: function() { + deferred.reject("cancelled", arguments); } }); } else { callback(); } + + return deferred.promise(); }; self.onUserLoggedIn = function(user) {