Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix config read only stuff #30

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions src/main/java/com/team766/web/ConfigUI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.team766.web;

import java.util.Map;
import java.util.ArrayList;
import com.team766.config.ConfigFileReader;
import com.team766.config.ConfigValueParseException;

public class ConfigUI implements WebServer.Handler {
@Override
Expand All @@ -10,17 +12,73 @@ public String endpoint() {
}

@Override
public String handle(final Map<String, Object> params) {
String r = "<h1>Configuration (READ ONLY)</h1>";
r += "<p>";
r += "To edit, copy contents and place in <code>src/main/deploy/configs</code> directory.<br/>";
r += "See <a href=\"https://docs.google.com/document/d/1eKOG5iWi2tijRls1L9qZ4xBZx50y1EriIk2QBUTv86g/edit#\">docs</a> for more information.<br/>";
r += "</p>";
public String handle(Map<String, Object> params) {
String r = "<h1>Configuration</h1>";

// TODO: handle empty config file case.
r += "<textarea name=\"configJson\" readonly disabled style=\"background: lightgray; box-sizing: border-box; width: 100%; height: 400px;\">";
r += ConfigFileReader.getInstance().getJsonString();
r += "<div id=\"results\">\n";
if (params.containsKey("configJson")) {
String configJsonString = (String)params.get("configJson");
ArrayList<String> validationErrors = new ArrayList<String>();
try {
ConfigFileReader.getInstance().reloadFromJson(configJsonString);
} catch (ConfigValueParseException ex) {
validationErrors.add(ex.toString());
} catch (Exception ex) {
validationErrors.add("Failed to parse config json: " + ex);
}
if (validationErrors.isEmpty()) {
r += "<p>New configuration (v" + ConfigFileReader.getInstance().getGeneration() + ") has been applied</p>";
r += "<p><b>Remember to click Restart Robot Code in the driver station if you have changed any motor/sensor settings</b></p>";
if (params.containsKey("saveToFile")) {
try {
ConfigFileReader.getInstance().saveFile(configJsonString);
} catch (Exception ex) {
validationErrors.add("Could not save config file: " + ex.toString());
}
}
}
if (validationErrors.size() > 0) {
r += "<p>Errors:\n";
r += "<ul>\n";
for (String error : validationErrors) {
r += "<li>" + error + "</li>\n";
}
r += "</ul></p>\n";
}
}
r += "</div>\n";

r += String.join("\n", new String[]{
"<script>",
" function submitForm(oFormElement) {",
" var xhttp = new XMLHttpRequest();",
" xhttp.onreadystatechange = function() {",
" if (this.readyState == 4 && this.status == 200) {",
" var newDoc = new DOMParser().parseFromString(this.responseText, 'text/html')",
" var oldDiv = document.getElementById('results');",
" oldDiv.parentNode.replaceChild(",
" document.importNode(newDoc.querySelector('#results'), true),",
" oldDiv);",
" }",
" };",
" xhttp.open(oFormElement.method, oFormElement.getAttribute('action'), true);",
" xhttp.send(new URLSearchParams(new FormData(oFormElement)));",
" return false;",
" }",
"</script>",
});

r += "<form method=\"post\" action=\"" + endpoint() + "\" onsubmit=\"return submitForm(this);\">\n";
r += "<textarea name=\"configJson\" style=\"box-sizing: border-box; width: 100%; height: 400px;\">";
if (params.containsKey("configJson")) {
r += (String)params.get("configJson");
} else {
r += ConfigFileReader.getInstance().getJsonString();
}
r += "</textarea>\n";
r += "<p>Save to config file on robot? <input type=\"checkbox\" name=\"saveToFile\"></p>\n";
r += "<p><input type=\"submit\" value=\"Apply\"></p></form>\n";

return r;
}

Expand Down