WIP: More work on the documentation of the js client lib

This commit is contained in:
Gina Häußge 2016-06-30 12:06:08 +02:00
parent b8405becb3
commit cec2f7d917
14 changed files with 533 additions and 38 deletions

View file

@ -4,12 +4,6 @@
Printer operations
******************
.. warning::
This part of the API is still heavily in development, especially anything that has to do with temperature control.
If you happen to want to develop against it, you should drop me an email to make sure I can give you a heads-up when
something changes.
.. contents::
Printer control is mostly achieved through the use of commands, issued to resources reflecting components of the
@ -35,6 +29,29 @@ SD card
Besides that, OctoPrint also provides a :ref:`full state report of the printer <sec-api-printer-state>`.
.. note::
You might be wondering why all command responses below only return a ``204`` with an empty body instead of
the output of the just sent commands. There are two reasons for this.
OctoPrint's internal webserver is single threaded and can only handle one request at a time. This is
not a problem generally since asynchronous programming allows to just have one request which is waiting for
data from a long running backend operation to sleep while handling other requests. The internal framework
used for providing the REST API though, Flask, is based on WSGI, which is synchrounous in nature. This means
that it is impossible to wait in a non blocking wait while handling a request on the REST API. So in order to
return the response of a command sent to the printer, the single thread of the webserver would have to be blocked
by the API while the response wasn't available yet. Which in turn would mean that the whole web server would
stop responding while waiting for the printer to reply, which, depending on the command in question (e.g. homing)
can take a long while. That would be a bad idea.
The second reason is that thanks to a large number of firmwares out there having a `particular bug <https://github.com/MarlinFirmware/Marlin/commit/acc0e7527914948656ccabba35f7faedc94ef885>`_
that makes it impossible to track the output of sent commands, identifying the correct response to a given
sent command is pretty much hit-and-miss. That situation gets even worse considering that it's next to impossible
to distinguish firmware versions which have that bug from firmware versions which don't have it.
Hence OctoPrint currently doesn't offer any synchronous way of retrieving the output of responses from the printer.
If you need the printer's serial communication, you'll need to subscribe to :ref:`push updates <sec-api-push>`.
.. _sec-api-printer-state:
Retrieve the current printer state

View file

@ -211,7 +211,7 @@
will contain the requested file as raw string/binary.
:param string url: URL to download
:param object opts: Addtional options for the request
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.upload(url, file, filename, additional)
@ -307,7 +307,7 @@
:param string url: The URL to ``POST`` the command to
:param string command: The command to issue
:param object payload: Additional payload data for the command
:param object opts: Addtional options for the request
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.getSimpleApiUrl(plugin)
@ -338,7 +338,7 @@
});
:param string plugin: The identifier of the plugin
:param object opts: Addtional options for the request
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.simpleApiCommand(plugin, command, payload, opts)
@ -359,7 +359,7 @@
:param string plugin: The identifier of the plugin
:param string command: The command to issue
:param object payload: Additional payload data for the command
:param object opts: Addtional options for the request
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.getBlueprintUrl(plugin)

View file

@ -9,6 +9,9 @@
See :ref:`Get connection settings <sec-api-connection-current>` for the response format.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.connection.connect(data, opts)
Connects to the printer, optionally using the provided connection ``data`` as parameters.
@ -23,15 +26,24 @@
See :ref:`Issue a connection command <sec-api-connection-command>` for more details.
:param object data: Connection data to use
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.connection.disconnect(opts)
Disconnects from the printer.
See :ref:`Issue a connection command <sec-api-connection-command>` for more details.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.connection.fakeAck(opts)
Triggers a fake acknowledgement (``ok``) on the printer.
See :ref:`Issue a connection command <sec-api-connection-command>` for more details.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response

View file

@ -5,11 +5,53 @@
.. js:function:: OctoPrint.control.getCustomControls(opts)
Retrieves the defined custom controls from the server.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.control.sendGcode(commands, opts)
Sends the provided ``commands`` to the printer.
Corresponds to the :ref:`Send an arbitrary command to the printer <sec-api-printer-arbcommand>` API,
see there for details.
:param list or string commands: One or more commands to send to the printer.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.control.sendGcodeWithParameters(commands, parameters, opts)
Sends the provided ``commands`` to the printer, replacing contained placeholders with
the provided ``parameters`` first.
Corresponds to the :ref:`Send an arbitrary command to the printer <sec-api-printer-arbcommand>` API,
see there for details.
:param list or string commands: One or more commands to send to the printer
:param object parameters: Parameters (key-value-pairs) to replace placeholders in ``commands`` with
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.control.sendGcodeScript(script, context, opts)
Sends the provided ``script`` to the printer, enhancing the template with the
specified ``context``.
:param string script: Name of the script to send to the printer
:param object context: Template context
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.control.sendGcodeScriptWithParameters(script, context, parameters, opts)
Sends the provided ``script`` to the printer, enhancing the template with the
specified ``context`` and ``parameters``.
:param string script: Name of the script to send to the printer
:param object context: Template context
:param object parameters: Template parameters
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response

View file

@ -0,0 +1,30 @@
.. sec-jsclientlib-files:
:mod:`OctoPrint.files`
----------------------
.. js:function:: OctoPrint.files.get(location, filename, opts)
.. js:function:: OctoPrint.files.list(opts)
.. js:function:: OctoPrint.files.listForLocation(location, opts)
.. js:function:: OctoPrint.files.select(location, filename, print, opts)
.. js:function:: OctoPrint.files.slice(location, filename, parameters, opts)
.. js:function:: OctoPrint.files.delete(location, filename, opts)
.. js:function:: OctoPrint.files.copy(location, filename, destination, opts)
.. js:function:: OctoPrint.files.move(location, filename, destination, opts)
.. js:function:: OctoPrint.files.createFolder(location, name, path, opts)
.. js:function:: OctoPrint.files.upload(location, file, data)
.. js:function:: OctoPrint.files.download(location, filename, opts)
.. js:function:: OctoPrint.files.pathForElement(element)
.. js:function:: OctoPrint.files.elementByPath(location, startElement)

View file

@ -11,3 +11,8 @@ JavaScript Client Library
browser.rst
connection.rst
control.rst
files.rst
job.rst
languages.rst
logs.rst
printer.rst

45
docs/jsclientlib/job.rst Normal file
View file

@ -0,0 +1,45 @@
.. sec-jsclientlib-job:
:mod:`OctoPrint.job`
--------------------
.. js:function:: OctoPrint.job.get(opts)
Retrieves information about the current job.
.. js:function:: OctoPrint.job.start(opts)
.. js:function:: OctoPrint.job.cancel(opts)
Cancels the current job.
.. js:function:: OctoPrint.job.restart(opts)
Restarts the current job. This is equivalent to cancelling and immediately restarting
the job.
**Example:**
.. code-block:: javascript
OctoPrint.job.restart();
// the above is a shorthand for:
OctoPrint.job.cancel()
.done(function(response) {
OctoPrint.job.start();
});
.. js:function:: OctoPrint.job.pause(opts)
Pauses the current job if it's running, does nothing if it's already paused.
.. js:function:: OctoPrint.job.resume(opts)
Resumes the current job if it's currently pause, does nothing if it's running.
.. js:function:: OctoPrint.job.togglePause(opts)
Resumes a paused job and pauses a running a print.

View file

@ -0,0 +1,10 @@
.. sec-jsclientlib-languages:
:mod:`OctoPrint.languages`
--------------------------
.. js:function:: OctoPrint.languages.list(opts)
.. js:function:: OctoPrint.languages.upload(file)
.. js:function:: OctoPrint.languages.delete(locale, pack, opts)

10
docs/jsclientlib/logs.rst Normal file
View file

@ -0,0 +1,10 @@
.. sec-jsclientlib-logs:
:mod:`OctoPrint.logs`
---------------------
.. js:function:: OctoPrint.logs.list(opts)
.. js:function:: OctoPrint.logs.delete(opts)
.. js:function:: OctoPrint.logs.download(file, opts)

View file

@ -0,0 +1,313 @@
.. sec-jsclientlib-printer:
:mod:`OctoPrint.printer`
------------------------
.. note::
All commands here that interact with the printer (anything that sends a command) will
resolve the returned `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ when the
request to the server enqueuing the command has been processed, *not* when the command
was actually sent to or processed by the printer.
See :ref:`the note here <sec-api-printer>` for an explanation why that is the case.
.. contents::
:local:
.. js:function:: OctoPrint.printer.getFullState(flags, opts)
Retrieves the full printer state, including temperature information, sd state and general
printer state.
The ``flags`` object can be used to specify the data to retrieve further via the following
properties:
* ``history``: a boolean value to specify whether the temperature history should be included (``true``)
or not (``false``), defaults to it not being included
* ``limit``: an integer value to specify how many history entries to include
* ``exclude``: a string value of comma-separated fields to exclude from the returned result.
See :ref:`Retrieve the current printer state <sec-api-printer-state>` for more details.
:param object flags: Flags that further specify which data to retrieve, see above for details
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.getToolState(flags, opts)
Retrieves the current printer extruder state/temperature information, and optionally also the temperature
history.
The ``flags`` object can be used to specify the data to retrieve further via the following
properties:
* ``history``: a boolean value to specify whether the temperature history should be included (``true``)
or not (``false``), defaults to it not being included
* ``limit``: an integer value to specify how many history entries to include
See :ref:`Retrieve the current tool state <sec-api-printer-toolstate>` for more details.
:param object flags: Flags that further specify which data to retrieve, see above for details
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.setToolTargetTemperatures(targets, opts)
Sets the given temperatures on the printer's extruders.
``targets`` is expected to be an object mapping tool identifier to target temperature to set.
**Example:**
Set first hotend to 220°C and second to 205°C.
.. code-block:: javascript
OctoPrint.printer.setToolTargetTemperatures({"tool0": 220, "tool1": 205});
See the ``target`` command in :ref:`Issue a tool command <sec-api-printer-toolcommand>` for more details.
:param object targets: The targets to set
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.setToolTemperatureOffsets(offsets, opts)
Sets the given temperature offsets for the printer's extruders.
``offsets`` is expected to be an object mapping tool identifier to offset to set.
**Example:**
Set the offset for the first hotend's temperature to +10°C and the offset for the second hotend's
temperature to -5°C.
.. code-block:: javascript
OctoPrint.printer.setToolTemperatureOffsets({"tool0": 10, "tool1": -5});
See the ``offset`` command in :ref:`Issue a tool command <sec-api-printer-toolcommand>` for more details.
:param object offsets: The offsets to set
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.selectTool(tool, opts)
Selects the printer's current extruder.
``tool`` is the identifier of the extruder to select.
**Example:**
Select the second tool, extrude 5mm of filament, then select the first tool.
.. code-block:: javascript
OctoPrint.printer.selectTool("tool1")
.done(function(response) {
OctoPrint.printer.extrude(5.0)
.done(function(response) {
OctoPrint.printer.selectTool("tool0");
});
});
See the ``select`` command in :ref:`Issue a tool command <sec-api-printer-toolcommand>` for more details.
:param string tool: The tool identifier of the extruder to select
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.extrude(amount, opts)
Extrudes or retracts ``amount`` mm of filament on the currently selected extruder.
**Example:**
Extrude 5mm of filament on the current extruder, then retract 2mm.
.. code-block:: javascript
OctoPrint.printer.extrude(5.0)
.done(function(response) {
OctoPrint.printer.extrude(-2.0);
});
See the ``extrude`` command in :ref:`Issue a tool command <sec-api-printer-toolcommand>` for more details.
:param float amount: The amount of filament to extrude/retract.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.setFlowrate(factor, opts)
Sets the current flowrate multiplier.
``factor`` is expected to be a integer value between 75 and 125 representing the new flowrate percentage.
See the ``flowrate`` command in :ref:`Issue a tool command <sec-api-printer-toolcommand>` for more details.
:param integer factor: The flowrate as percentage
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.getBedState(data, opts)
Retrieves the current printer bed state/temperature information, and optionally also the temperature
history.
The ``flags`` object can be used to specify the data to retrieve further via the following
properties:
* ``history``: a boolean value to specify whether the temperature history should be included (``true``)
or not (``false``), defaults to it not being included
* ``limit``: an integer value to specify how many history entries to include
See :ref:`Retrieve the current bed state <sec-api-printer-bedstate>` for more details.
:param object flags: Flags that further specify which data to retrieve, see above for details
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.setBedTargetTemperature(target, opts)
Sets the given temperature on the printer's heated bed (if available).
``target`` is expected to be a the target temperature as a float value.
**Example:**
Set the bed to 90°C.
.. code-block:: javascript
OctoPrint.printer.setBedTargetTemperature(90.0);
See the ``target`` command in :ref:`Issue a bed command <sec-api-printer-bedcommand>` for more details.
:param float target: The target to set
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.setBedTemperatureOffset(offset, opts)
Sets the given temperature offset for the printer's heated bed (if available).
``offset`` is expected to be the temperature offset to set.
**Example:**
Set the offset for the bed to -5°C.
.. code-block:: javascript
OctoPrint.printer.setToolTemperatureOffsets(-5);
See the ``offset`` command in :ref:`Issue a bed command <sec-api-printer-bedcommand>` for more details.
:param object offsets: The offsets to set
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.jog(amounts, opts)
Jogs the specified axes by the specified ``amounts``.
``amounts`` is expected to be an object with properties reflecting the axes to be jogged by the specified
amount given as value.
**Example:**
Jog X by 10mm.
.. code-block:: javascript
OctoPrint.printer.jog({"x", 10.0});
Jog Y by -5mm and Z by 0.2mm.
.. code-block:: javascript
OctoPrint.printer.jog({"y": -5.0, "z": 0.2});
See the ``jog`` command in :ref:`Issue a print head command <sec-api-printer-printheadcommand>` for more details.
:param object amounts: Key-value-pairs of axes to jog and amount to jog it.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.home(axes, opts)
Homes the specified ``axes``.
``axes`` is expected to be an array of strings specifying the axes to home.
**Example:**
Home the X and Y axis.
.. code-block:: javascript
OctoPrint.printer.home(["x", "y"]);
Home the Z axis.
.. code-block:: javascript
OctoPrint.printer.home(["z"]);
See the ``home`` command in :ref:`Issue a print head command <sec-api-printer-printheadcommand>` for more details.
:param array axes: List of axes to home
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.setFeedrate(factor, opts)
Sets the feedrate multiplier to use.
``factor`` is expected to be a integer value between 0 and 200 representing the new feedrate percentage.
See the ``feedrate`` command in :ref:`Issue a print head command <sec-api-printer-printheadcommand>` for more details.
:param integer factor: The feedrate multiplier as percentage
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.getSdState(opts)
Retrieves the current ready state of the printer's SD card.
See :ref:`Retrieve the current SD state <sec-api-printer-sdstate>` for more details.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.initSd(opts)
Instructs the printer to initialize its SD card (if present).
See the ``init`` command in :ref:`Issue an SD command <sec-api-printer-sdcommand>` for more details.
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.refreshSd(opts)
Instructs the printer to refresh the list of files on the SD card (if present).
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. js:function:: OctoPrint.printer.releaseSd(opts)
Instructs the printer to release its SD card (if present).
:param object opts: Additional options for the request
:returns Promise: A `jQuery Promise <http://api.jquery.com/Types/#Promise>`_ for the request's response
.. seealso::
:ref:`REST API: Printer operations <sec-api-printer>`
Documentation of the API functionality covered with this client library module.

View file

@ -83,13 +83,13 @@
return issueFileCommand(location, filename, "move", { destination: destination }, opts);
},
createFolder: function (location, name, path) {
createFolder: function (location, name, path, opts) {
var data = "foldername=" + name;
if (path != undefined && path != "") {
data = "foldername:" + path + "/" + name;
data = "foldername=" + path + "/" + name;
}
return OctoPrint.post(resourceForLocation(location), data);
return OctoPrint.post(resourceForLocation(location), data, opts);
},

View file

@ -7,8 +7,13 @@
})(window || this, function(OctoPrint) {
var url = "api/job";
var issueCommand = function(command, opts) {
return OctoPrint.issueCommand(url, command, {}, opts);
var issueCommand = function(command, payload, opts) {
if (arguments.length == 2) {
opts = payload;
payload = {};
}
return OctoPrint.issueCommand(url, command, payload, opts);
};
OctoPrint.job = {
@ -22,7 +27,13 @@
return issueCommand("restart", opts);
},
pause: function(opts) {
return issueCommand("pause", opts);
return issueCommand("pause", {"action": "pause"}, opts);
},
resume: function(opts) {
return issueCommand("pause", {"action": "resume"}, opts)
},
togglePause: function(opts) {
return issueCommand("pause", {"action": "toggle"}, opts);
},
cancel: function(opts) {
return issueCommand("cancel", opts);

View file

@ -28,12 +28,12 @@
};
OctoPrint.printer = {
getFullState: function (data, opts) {
data = data || {};
getFullState: function (flags, opts) {
flags = flags || {};
var history = data.history || undefined;
var limit = data.limit || undefined;
var exclude = data.exclude || undefined;
var history = flags.history || undefined;
var limit = flags.limit || undefined;
var exclude = flags.exclude || undefined;
var getUrl = url;
if (history || exclude) {
@ -53,11 +53,11 @@
return OctoPrint.get(getUrl, opts);
},
getToolState: function (data, opts) {
data = data || {};
getToolState: function (flags, opts) {
flags = flags || {};
var history = data.history || undefined;
var limit = data.limit || undefined;
var history = flags.history || undefined;
var limit = flags.limit || undefined;
var getUrl = toolUrl;
if (history) {
@ -70,11 +70,11 @@
return OctoPrint.get(getUrl, opts);
},
getBedState: function (data, opts) {
data = data || {};
getBedState: function (flags, opts) {
flags = flags || {};
var history = data.history || undefined;
var limit = data.limit || undefined;
var history = flags.history || undefined;
var limit = flags.limit || undefined;
var getUrl = bedUrl;
if (history) {
@ -91,13 +91,13 @@
return OctoPrint.get(sdUrl, opts);
},
jog: function (data, opts) {
data = data || {};
jog: function (amounts, opts) {
amounts = amounts || {};
var payload = {};
if (data.x) payload.x = data.x;
if (data.y) payload.y = data.y;
if (data.z) payload.z = data.z;
if (amounts.x) payload.x = amounts.x;
if (amounts.y) payload.y = amounts.y;
if (amounts.z) payload.z = amounts.z;
return issuePrintheadCommand("jog", payload, opts);
},
@ -172,11 +172,11 @@
return issueToolCommand("flowrate", payload, opts);
},
setBedTargetTemperature: function (temperature, opts) {
temperature = temperature || 0;
setBedTargetTemperature: function (target, opts) {
target = target || 0;
var payload = {
target: temperature
target: target
};
return issueBedCommand("target", payload, opts);

View file

@ -221,7 +221,7 @@ $(function() {
};
self.pause = function() {
OctoPrint.job.pause();
OctoPrint.job.togglePause();
};
self.cancel = function() {