From 73a85646c59461dddfc435462b42c52416fd05ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 30 Jun 2016 12:56:18 +0200 Subject: [PATCH] Added only pause/resume sub commands to job API Implements #1393 --- docs/api/job.rst | 76 ++++++++++++++++--- src/octoprint/printer/__init__.py | 17 ++++- src/octoprint/printer/standard.py | 21 ++++- src/octoprint/server/api/job.py | 12 ++- .../static/js/app/viewmodels/printerstate.js | 28 ++++++- src/octoprint/templates/sidebar/state.jinja2 | 2 +- 6 files changed, 136 insertions(+), 20 deletions(-) diff --git a/docs/api/job.rst b/docs/api/job.rst index eeb3b071..aae6f863 100644 --- a/docs/api/job.rst +++ b/docs/api/job.rst @@ -19,17 +19,38 @@ Issue a job command Starts the print of the currently selected file. For selecting a file, see :ref:`Issue a file command `. If a print job is already active, a :http:statuscode:`409` will be returned. + cancel + Cancels the current print job. If no print job is active (either paused or printing), a :http:statuscode:`409` + will be returned. + restart Restart the print of the currently selected file from the beginning. There must be an active print job for this to work and the print job must currently be paused. If either is not the case, a :http:statuscode:`409` will be returned. - pause - Pauses/unpauses the current print job. If no print job is active (either paused or printing), a :http:statuscode:`409` - will be returned. + Equivalent to issuing a ``cancel`` command while paused, directly followed by a ``start`` command. - cancel - Cancels the current print job. If no print job is active (either paused or printing), a :http:statuscode:`409` - will be returned. + pause + Pauses/resumes/toggles the current print job. Accepts one optional additional parameter ``action`` + specifying which action to take. Valid values for this parameter are: + + pause + Pauses the current job if it's printing, does nothing if it's already paused. + resume + Resumes the current job if it's paused, does nothing if it's printing. + toggle + Toggles the pause state of the job, pausing it if it's printing and resuming it if it's currently paused. + + In order to stay backwards compatible to earlier iterations of this API, the default + action to take if no ``action`` parameter is supplied is to toggle the print job status. + + If no print job is active (either paused or printing), a :http:statuscode:`409` will be returned. + + .. note:: + + While the approach to implement pause/resume/toggle behaviour through sub commands via the ``action`` + parameter instead of having dedicated ``pause``, ``resume`` and ``toggle`` commands seems clumsy, this path + was chosen to have the API stay backwards compatible to prior versions which only offered the toggle + behaviour under the ``pause`` command. Upon success, a status code of :http:statuscode:`204` and an empty body is returned. @@ -50,6 +71,23 @@ Issue a job command HTTP/1.1 204 No Content + **Example Cancel Request** + + .. sourcecode:: http + + POST /api/job HTTP/1.1 + Host: example.com + Content-Type: application/json + X-Api-Key: abcdef... + + { + "command": "cancel" + } + + .. sourcecode:: http + + HTTP/1.1 204 No Content + **Example Restart Request** .. sourcecode:: http @@ -77,14 +115,15 @@ Issue a job command X-Api-Key: abcdef... { - "command": "pause" + "command": "pause", + "action": "pause" } .. sourcecode:: http HTTP/1.1 204 No Content - **Example Cancel Request** + **Example Resume Request** .. sourcecode:: http @@ -94,7 +133,26 @@ Issue a job command X-Api-Key: abcdef... { - "command": "cancel" + "command": "pause", + "action": "resume" + } + + .. sourcecode:: http + + HTTP/1.1 204 No Content + + **Example Pause Toggle Request** + + .. sourcecode:: http + + POST /api/job HTTP/1.1 + Host: example.com + Content-Type: application/json + X-Api-Key: abcdef... + + { + "command": "pause", + "action": "toggle" } .. sourcecode:: http diff --git a/src/octoprint/printer/__init__.py b/src/octoprint/printer/__init__.py index 9265f598..25426dc2 100644 --- a/src/octoprint/printer/__init__.py +++ b/src/octoprint/printer/__init__.py @@ -236,11 +236,26 @@ class PrinterInterface(object): """ raise NotImplementedError() + def pause_print(self): + """ + Pauses the current print job if it is currently running, does nothing otherwise. + """ + raise NotImplementedError() + + def resume_print(self): + """ + Resumes the current print job if it is currently paused, does nothing otherwise. + """ + raise NotImplementedError() + def toggle_pause_print(self): """ Pauses the current print job if it is currently running or resumes it if it is currently paused. """ - raise NotImplementedError() + if self.is_printing(): + self.pause_print() + elif self.is_paused(): + self.resume_print() def cancel_print(self): """ diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 69707c6a..8567594f 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -404,14 +404,29 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): self._setCurrentZ(None) self._comm.startPrint(pos=pos) - def toggle_pause_print(self): + def pause_print(self): """ - Pause the current printjob. + Pause the current printjob. """ if self._comm is None: return - self._comm.setPause(not self._comm.isPaused()) + if self._comm.isPaused(): + return + + self._comm.setPause(True) + + def resume_print(self): + """ + Resume the current printjob. + """ + if self._comm is None: + return + + if not self._comm.isPaused(): + return + + self._comm.setPause(False) def cancel_print(self): """ diff --git a/src/octoprint/server/api/job.py b/src/octoprint/server/api/job.py index b2d9b02e..d4862c4b 100644 --- a/src/octoprint/server/api/job.py +++ b/src/octoprint/server/api/job.py @@ -43,7 +43,15 @@ def controlJob(): elif command == "pause": if not activePrintjob: return make_response("Printer is neither printing nor paused, 'pause' command cannot be performed", 409) - printer.toggle_pause_print() + action = data.get("action", "toggle") + if action == "toggle": + printer.toggle_pause_print() + elif action == "pause": + printer.pause_print() + elif action == "resume": + printer.resume_print() + else: + return make_response("Unknown action '{}', allowed values for action parameter are 'pause', 'resume' and 'toggle'".format(action), 400) elif command == "cancel": if not activePrintjob: return make_response("Printer is neither printing nor paused, 'cancel' command cannot be performed", 409) @@ -58,4 +66,4 @@ def jobState(): "job": currentData["job"], "progress": currentData["progress"], "state": currentData["state"]["text"] - }) \ No newline at end of file + }) diff --git a/src/octoprint/static/js/app/viewmodels/printerstate.js b/src/octoprint/static/js/app/viewmodels/printerstate.js index b8db6700..f0696e92 100644 --- a/src/octoprint/static/js/app/viewmodels/printerstate.js +++ b/src/octoprint/static/js/app/viewmodels/printerstate.js @@ -223,21 +223,41 @@ $(function() { }; - self.pause = function() { - self._jobCommand("pause"); + self.onlyPause = function() { + self.pause("pause"); + }; + + self.onlyResume = function() { + self.pause("resume"); + }; + + self.pause = function(action) { + action = action || "toggle"; + self._jobCommand("pause", {"action": action}); }; self.cancel = function() { self._jobCommand("cancel"); }; - self._jobCommand = function(command, callback) { + self._jobCommand = function(command, payload, callback) { + if (arguments.length == 1) { + payload = {}; + callback = undefined; + } else if (arguments.length == 2 && typeof payload === "function") { + callback = payload; + payload = {}; + } + + var data = _.extend(payload, {}); + data.command = command; + $.ajax({ url: API_BASEURL + "job", type: "POST", dataType: "json", contentType: "application/json; charset=UTF-8", - data: JSON.stringify({command: command}), + data: JSON.stringify(data), success: function(response) { if (callback != undefined) { callback(); diff --git a/src/octoprint/templates/sidebar/state.jinja2 b/src/octoprint/templates/sidebar/state.jinja2 index 972a2605..feff1535 100644 --- a/src/octoprint/templates/sidebar/state.jinja2 +++ b/src/octoprint/templates/sidebar/state.jinja2 @@ -15,6 +15,6 @@