212 lines
6.7 KiB
C
212 lines
6.7 KiB
C
#include <Arduino.h>
|
|
#include "master.h"
|
|
#include <ESPAsyncWebServer.h>
|
|
#include <ArduinoJson.h>
|
|
#include <SPIFFS.h>
|
|
#include <esp_wifi.h>
|
|
|
|
|
|
#include <buttonassigh.h>
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
void setupRoutes(){
|
|
// Web Server Routes
|
|
|
|
// SPIFFS initialisieren
|
|
|
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
request->send(SPIFFS, "/index.html", "text/html");
|
|
});
|
|
|
|
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
request->send(SPIFFS, "/settings.html", "text/html");
|
|
});
|
|
|
|
server.on("/about", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
request->send(SPIFFS, "/about.html", "text/html");
|
|
});
|
|
|
|
server.on("/api/data", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
request->send(200, "application/json", getTimerDataJSON());
|
|
});
|
|
|
|
server.on("/api/reset-best", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
Serial.println("/api/reset-best called");
|
|
timerData.bestTime1 = 0;
|
|
timerData.bestTime2 = 0;
|
|
saveBestTimes();
|
|
DynamicJsonDocument doc(64);
|
|
doc["success"] = true;
|
|
String result;
|
|
serializeJson(doc, result);
|
|
request->send(200, "application/json", result);
|
|
});
|
|
|
|
server.on("/api/unlearn-button", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
Serial.println("/api/unlearn-button called");
|
|
unlearnButton();
|
|
request->send(200, "application/json", "{\"success\":true}");
|
|
|
|
});
|
|
|
|
|
|
server.on("/api/set-max-time", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
Serial.println("/api/set-max-time called");
|
|
bool changed = false;
|
|
if (request->hasParam("maxTime", true)) {
|
|
maxTimeBeforeReset = request->getParam("maxTime", true)->value().toInt() * 1000;
|
|
changed = true;
|
|
}
|
|
if (request->hasParam("maxTimeDisplay", true)) {
|
|
maxTimeDisplay = request->getParam("maxTimeDisplay", true)->value().toInt() * 1000;
|
|
changed = true;
|
|
}
|
|
if (changed) {
|
|
saveSettings();
|
|
DynamicJsonDocument doc(32);
|
|
doc["success"] = true;
|
|
String result;
|
|
serializeJson(doc, result);
|
|
request->send(200, "application/json", result);
|
|
} else {
|
|
request->send(400, "application/json", "{\"success\":false}");
|
|
}
|
|
});
|
|
|
|
server.on("/api/get-settings", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
Serial.println("/api/get-settings called");
|
|
DynamicJsonDocument doc(256);
|
|
doc["maxTime"] = maxTimeBeforeReset / 1000;
|
|
doc["maxTimeDisplay"] = maxTimeDisplay / 1000;
|
|
String result;
|
|
serializeJson(doc, result);
|
|
request->send(200, "application/json", result);
|
|
});
|
|
|
|
server.on("/api/start-learning", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
Serial.println("/api/start-learning called");
|
|
learningMode = true;
|
|
learningStep = 0;
|
|
DynamicJsonDocument doc(64);
|
|
doc["success"] = true;
|
|
String result;
|
|
serializeJson(doc, result);
|
|
Serial.println("Learning mode started");
|
|
request->send(200, "application/json", result);
|
|
});
|
|
|
|
server.on("/api/stop-learning", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
Serial.println("/api/stop-learning called");
|
|
learningMode = false;
|
|
learningStep = 0;
|
|
DynamicJsonDocument doc(64);
|
|
doc["success"] = true;
|
|
String result;
|
|
serializeJson(doc, result);
|
|
Serial.println("Learning mode stopped");
|
|
request->send(200, "application/json", result);
|
|
});
|
|
|
|
server.on("/api/learn/status", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
DynamicJsonDocument doc(256);
|
|
doc["active"] = learningMode;
|
|
doc["step"] = learningStep;
|
|
String response;
|
|
serializeJson(doc, response);
|
|
request->send(200, "application/json", response);
|
|
});
|
|
|
|
server.on("/api/buttons/status", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
DynamicJsonDocument doc(128);
|
|
doc["lane1Start"] = buttonConfigs.start1.isAssigned;
|
|
doc["lane1Stop"] = buttonConfigs.stop1.isAssigned;
|
|
doc["lane2Start"] = buttonConfigs.start2.isAssigned;
|
|
doc["lane2Stop"] = buttonConfigs.stop2.isAssigned;
|
|
String result;
|
|
serializeJson(doc, result);
|
|
request->send(200, "application/json", result);
|
|
});
|
|
|
|
server.on("/api/info", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
DynamicJsonDocument doc(256);
|
|
|
|
// IP address
|
|
IPAddress ip = WiFi.softAPIP();
|
|
doc["ip"] = ip.toString();
|
|
doc["channel"] = WiFi.channel();
|
|
|
|
// MAC address
|
|
uint8_t mac[6];
|
|
esp_wifi_get_mac(WIFI_IF_STA, mac);
|
|
char macStr[18];
|
|
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
doc["mac"] = macStr;
|
|
|
|
// Free memory
|
|
doc["freeMemory"] = ESP.getFreeHeap();
|
|
|
|
// Connected buttons (count assigned)
|
|
int connected = 0;
|
|
if (buttonConfigs.start1.isAssigned) connected++;
|
|
if (buttonConfigs.stop1.isAssigned) connected++;
|
|
if (buttonConfigs.start2.isAssigned) connected++;
|
|
if (buttonConfigs.stop2.isAssigned) connected++;
|
|
doc["connectedButtons"] = connected;
|
|
|
|
doc["valid"] = checkLicence() > 0 ? "Ja" : "Nein";
|
|
doc["tier"] = checkLicence() ;
|
|
|
|
|
|
String result;
|
|
serializeJson(doc, result);
|
|
request->send(200, "application/json", result);
|
|
});
|
|
|
|
// Setze WLAN-Name und Passwort (POST)
|
|
server.on("/api/set-wifi", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
Serial.println("/api/set-wifi called");
|
|
String ssid, password;
|
|
if (request->hasParam("ssid", true)) {
|
|
ssid = request->getParam("ssid", true)->value();
|
|
}
|
|
if (request->hasParam("password", true)) {
|
|
password = request->getParam("password", true)->value();
|
|
}
|
|
if (ssid.length() > 0) {
|
|
// Hier speichern wir die neuen Werte (z.B. in Preferences oder global)
|
|
// Beispiel: strcpy(ssidSTA, ssid.c_str());
|
|
// Beispiel: strcpy(passwordSTA, password.c_str());
|
|
// In deinem Projekt ggf. persistent speichern!
|
|
// Hier als global (unsicher, nach Neustart verloren!):
|
|
ssidSTA = strdup(ssid.c_str());
|
|
passwordSTA = strdup(password.c_str());
|
|
// Rückmeldung
|
|
DynamicJsonDocument doc(64);
|
|
doc["success"] = true;
|
|
String result;
|
|
serializeJson(doc, result);
|
|
request->send(200, "application/json", result);
|
|
Serial.println("WiFi-Settings updated (nur bis zum Neustart aktiv!)");
|
|
} else {
|
|
request->send(400, "application/json", "{\"success\":false,\"error\":\"SSID fehlt\"}");
|
|
}
|
|
});
|
|
|
|
// Liefert aktuelle WLAN-Einstellungen (GET)
|
|
server.on("/api/get-wifi", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
DynamicJsonDocument doc(128);
|
|
doc["ssid"] = ssidSTA ? ssidSTA : "";
|
|
doc["password"] = passwordSTA ? passwordSTA : "";
|
|
String result;
|
|
serializeJson(doc, result);
|
|
request->send(200, "application/json", result);
|
|
});
|
|
|
|
// Statische Dateien
|
|
server.serveStatic("/", SPIFFS, "/");
|
|
server.begin();
|
|
Serial.println("Web Server gestartet");
|
|
|
|
} |