From 2e766a7eca2d80b46b5f81e295c03c51622742c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Sat, 23 Feb 2013 18:08:59 +0100 Subject: [PATCH 1/2] Added custom controls with multiple commands Closes #48 --- octoprint/server.py | 28 ++++++++++++--------- octoprint/static/js/ui.js | 51 ++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/octoprint/server.py b/octoprint/server.py index 920ba9c..7eec703 100644 --- a/octoprint/server.py +++ b/octoprint/server.py @@ -129,21 +129,25 @@ def disconnect(): @app.route(BASEURL + "control/command", methods=["POST"]) def printerCommand(): - command = request.form["command"] + if "application/json" in request.headers["Content-Type"]: + data = request.json - # if parameters for the command are given, retrieve them from the request and format the command string with them - parameters = {} - for requestParameter in request.values.keys(): - if not requestParameter.startswith("parameter_"): - continue + parameters = {} + if "parameters" in data.keys(): parameters = data["parameters"] - parameterName = requestParameter[len("parameter_"):] - parameterValue = request.values[requestParameter] - parameters[parameterName] = parameterValue - if len(parameters) > 0: - command = command % parameters + commands = [] + if "command" in data.keys(): commands = [data["command"]] + elif "commands" in data.keys(): commands = data["commands"] + + commandsToSend = [] + for command in commands: + commandToSend = command + if len(parameters) > 0: + commandToSend = command % parameters + commandsToSend.append(commandToSend) + + printer.commands(commandsToSend) - printer.command(command) return jsonify(SUCCESS) @app.route(BASEURL + "control/print", methods=["POST"]) diff --git a/octoprint/static/js/ui.js b/octoprint/static/js/ui.js index 4b5fb18..990e197 100644 --- a/octoprint/static/js/ui.js +++ b/octoprint/static/js/ui.js @@ -399,7 +399,7 @@ function ControlsViewModel() { } self._enhanceControl = function(control) { - if (control.type == "parametric_command") { + if (control.type == "parametric_command" || control.type == "parametric_commands") { for (var i = 0; i < control.input.length; i++) { control.input[i].value = control.input[i].default; } @@ -453,25 +453,33 @@ function ControlsViewModel() { if (!command) return; - if (command.type == "command") { - $.ajax({ - url: AJAX_BASEURL + "control/command", - type: "POST", - dataType: "json", - data: "command=" + command.command - }) - } else if (command.type="parametric_command") { - var data = {"command": command.command}; - for (var i = 0; i < command.input.length; i++) { - data["parameter_" + command.input[i].parameter] = command.input[i].value; - } - $.ajax({ - url: AJAX_BASEURL + "control/command", - type: "POST", - dataType: "json", - data: data - }) + var data = undefined; + if (command.type == "command" || command.type == "parametric_command") { + // single command + data = {"command" : command.command}; + } else if (command.type == "commands" || command.type == "parametric_commands") { + // multi command + data = {"commands": command.commands}; } + + if (command.type == "parametric_command" || command.type == "parametric_commands") { + // parametric command(s) + data["parameters"] = {}; + for (var i = 0; i < command.input.length; i++) { + data["parameters"][command.input[i].parameter] = command.input[i].value; + } + } + + if (!data) + return; + + $.ajax({ + url: AJAX_BASEURL + "control/command", + type: "POST", + dataType: "json", + contentType: "application/json; charset=UTF-8", + data: JSON.stringify(data) + }) } self.displayMode = function(customControl) { @@ -479,8 +487,10 @@ function ControlsViewModel() { case "section": return "customControls_sectionTemplate"; case "command": + case "commands": return "customControls_commandTemplate"; case "parametric_command": + case "parametric_commands": return "customControls_parametricCommandTemplate"; default: return "customControls_emptyTemplate"; @@ -1262,7 +1272,8 @@ $(function() { url: AJAX_BASEURL + "control/command", type: "POST", dataType: "json", - data: "command=" + command + contentType: "application/json; charset=UTF-8", + data: JSON.stringify({"command": command}) }) } }) From 55244c2f7ce343ba5d15e787363b92bc714d8902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Sat, 23 Feb 2013 18:13:04 +0100 Subject: [PATCH 2/2] Added example for multiple commands --- README.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a95a495..4420a4e 100644 --- a/README.md +++ b/README.md @@ -122,11 +122,14 @@ The following example config should explain the available options: # Controls consist at least of a name, a type and type-specific further attributes. Currently recognized types are # - section: Creates a visual section in the UI, you can use this to separate functional blocks # - command: Creates a button that sends a defined GCODE command to the printer when clicked + # - commands: Creates a button that sends multiple defined GCODE commands to the printer when clicked # - parametric_command: Creates a button that sends a parameterized GCODE command to the printer, parameters # needed for the command are added to the UI as input fields, are named and can such be referenced from the command + # - parametric_commands: Like parametric_command, but supports multiple commands # # The following example defines a control for enabling the cooling fan with a variable speed defined by the user - # (default 255) and a control for disabling the fan, all within a section named "Fan". + # (default 255) and a control for disabling the fan, all within a section named "Fan", and two example controls with + # multiple commands in a section "Example for multiple commands". controls: - name: Fan type: section @@ -141,7 +144,28 @@ The following example config should explain the available options: - name: Disable Fan type: command command: M107 - + - name: Example for multiple commands + type: section + children: + - name: Move X (static) + type: commands + commands: + - G91 + - G1 X10 F3000 + - G90 + - name: Move X (parametric) + type: parametric_commands + commands: + - G91 + - G1 X%(distance)s F%(speed)s + - G90 + input: + - default: 10 + name: Distance + parameter: distance + - default: 3000 + name: Speed + parameter: speed Setup on a Raspberry Pi running Raspbian ----------------------------------------