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}) }) } })