Edit Mapping

This commit is contained in:
Stefan Ostermann 2024-11-18 22:12:57 +01:00
parent 5cbe653063
commit 6ffd4a5a05
2 changed files with 56 additions and 3 deletions

View File

@ -44,6 +44,15 @@ const char index_html[] PROGMEM = R"rawliteral(
<form method="POST" action="/upload" enctype="multipart/form-data"><input type="file" name="data"/><input type="submit" name="upload" value="Upload" title="Upload File"></form>
<h2>Edit RFID Mapping</h2>
<form id="editMappingForm">
<label for="rfid">RFID:</label>
<input type="text" id="rfid" name="rfid" required><br>
<label for="song">Song:</label>
<input type="text" id="song" name="song" required><br>
<button type="button" onclick="editMapping()">Update Mapping</button>
</form>
<script>
setInterval(getState, 4000);
setInterval(updateProgress, 500); // Update progress every second
@ -180,6 +189,20 @@ const char index_html[] PROGMEM = R"rawliteral(
}
function editMapping() {
var rfid = document.getElementById('rfid').value;
var song = document.getElementById('song').value;
var xhr = new XMLHttpRequest();
xhr.open("POST", "/edit_mapping", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.send("rfid=" + encodeURIComponent(rfid) + "&song=" + encodeURIComponent(song));
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
alert("Mapping updated successfully!");
}
};
}
</script>
</body>
</html>)rawliteral";

View File

@ -144,7 +144,7 @@ void deactivateRFID()
String humanReadableSize(const size_t bytes) {
if (bytes < 1024) return String(bytes) + " B";
else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + " KB";
else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB";
else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB";
else return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB";
}
@ -176,10 +176,11 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
uint32_t getBatteryVoltageMv() {
uint32_t voltage = analogReadMilliVolts(BAT_VOLTAGE_PIN);
voltage *= 2;//*2 because of the voltage divider.
Serial.print("Battery Voltage: ");
Serial.println(voltage*2);
Serial.println(voltage);
Serial.println(" mV");
return voltage*2;//*2 because of the voltage divider.
return voltage;
}
@ -365,6 +366,33 @@ String getState()
return output;
}
// Function to save the rfid_map to the mapping file
void saveMappingToFile(const char *filename) {
File file = SD.open(filename, FILE_WRITE);
if (file) {
for (const auto &pair : rfid_map) {
file.println(pair.first + "=" + pair.second);
}
file.close();
Serial.println("Mapping saved to file.");
} else {
Serial.println("Error opening file for writing.");
}
}
// Function to handle edit requests
void editMapping(AsyncWebServerRequest *request) {
if (request->hasParam("rfid", true) && request->hasParam("song", true)) {
String rfid = request->getParam("rfid", true)->value();
String song = request->getParam("song", true)->value();
rfid_map[rfid] = song;
saveMappingToFile(mapping_file.c_str());
request->send(200, "text/plain", "Mapping updated");
} else {
request->send(400, "text/plain", "Invalid parameters");
}
}
std::map<String, String> readDataFromFile(const char *filename) {
File file = SD.open(filename);
@ -645,6 +673,8 @@ void setup()
server.on("/volume", HTTP_POST, volume_action);
server.on("/edit_mapping", HTTP_POST, editMapping);
// run handleUpload function when any file is uploaded
server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200);