Location Speichern geht? abrufen der gespeicherten location aus properties muss noch gemacht werden

This commit is contained in:
Carsten Graf
2025-06-11 22:52:30 +02:00
parent 22cc4fe99c
commit b6fa2d69e7
8 changed files with 319 additions and 14 deletions

View File

@@ -82,7 +82,7 @@ UserData checkUser(const String& uid) {
}
//Function to enter user data into the database
bool enterUserData(const String& uid, const String& firstname, const String& lastname, int alter) {
bool enterUserData(const String& uid, const String& firstname, const String& lastname, const String& geburtsdatum, int alter) {
if (!backendOnline()) {
Serial.println("No internet connection, cannot enter user data.");
return false;
@@ -98,6 +98,7 @@ bool enterUserData(const String& uid, const String& firstname, const String& las
requestDoc["uid"] = uid;
requestDoc["vorname"] = firstname;
requestDoc["nachname"] = lastname;
requestDoc["geburtsdatum"] = geburtsdatum;
requestDoc["alter"] = alter;
String requestBody;
@@ -116,13 +117,43 @@ bool enterUserData(const String& uid, const String& firstname, const String& las
}
}
JsonDocument getAllLocations() {
JsonDocument locations; // Allocate memory for the JSON document
if (!backendOnline()) {
Serial.println("No internet connection, cannot fetch locations.");
return locations; // Return an empty document
}
HTTPClient http;
http.begin(String(BACKEND_SERVER) + "/api/location/");
http.addHeader("Authorization", String("Bearer ") + BACKEND_TOKEN);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
DeserializationError error = deserializeJson(locations, payload);
if (error) {
Serial.println("Failed to parse locations JSON.");
}
} else {
Serial.printf("Failed to fetch locations, HTTP code: %d\n", httpCode);
}
http.end();
return locations; // Return the populated JSON document
}
// Keep this for backward compatibility
bool userExists(const String& uid) {
return checkUser(uid).exists;
}
//Routes from the Frontend into here and then into DB backend.
void setupBackendRoutes(AsyncWebServer& server) {
server.on("/api/health", HTTP_GET, [](AsyncWebServerRequest *request) {
@@ -142,6 +173,15 @@ void setupBackendRoutes(AsyncWebServer& server) {
// Handle user retrieval logic here
});
//Location routes /api/location/
server.on("/api/location/", HTTP_GET, [](AsyncWebServerRequest *request){
String result;
serializeJson(getAllLocations(), result);
request->send(200, "application/json", result);
});
// Add more routes as needed
}

View File

@@ -184,24 +184,35 @@ void saveWifiSettings() {
preferences.putString("ssid", ssidSTA);
preferences.putString("password", passwordSTA);
preferences.end();
Serial.printf("WLAN-Einstellungen gespeichert: SSID=%s, Passwort=%s\n", ssidSTA, passwordSTA);
delay(500); // Warte 2 Sekunden, bevor der Neustart erfolgt
ESP.restart(); // Neustart des ESP32
}
void loadLocationSettings() {
preferences.begin("location", true);
masterlocation = preferences.getString("location", "");
preferences.end();
}
void saveLocationSettings() {
preferences.begin("location", false);
preferences.putString("location", masterlocation);
preferences.end();
}
void loadWifiSettings() {
preferences.begin("wifi", true);
// Neue Werte laden und dynamisch zuweisen
String ssid = preferences.getString("ssid", "");
String password = preferences.getString("password", "");
ssidSTA = strdup(ssid.c_str());
passwordSTA = strdup(password.c_str());
preferences.end();
// Debug-Ausgabe
Serial.printf("WLAN-Einstellungen geladen: SSID=%s, Passwort=%s\n", ssidSTA, passwordSTA);
}
int checkLicence() {
loadLicenceFromPrefs();
String id = getUniqueDeviceID();

View File

@@ -50,6 +50,7 @@ int learningStep = 0; // 0=Start1, 1=Stop1, 2=Start2, 3=Stop2
unsigned long maxTimeBeforeReset = 300000; // 5 Minuten default
unsigned long maxTimeDisplay = 20000; // 20 Sekunden Standard (in ms)
bool wifimodeAP = false; // AP-Modus deaktiviert
String masterlocation;
//Function Declarations
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len);
@@ -68,6 +69,8 @@ void saveSettings();
void loadSettings();
void loadWifiSettings();
void saveWifiSettings();
void loadLocationSettings();
void saveLocationSettings();
void unlearnButton();
int checkLicence();
String getTimerDataJSON();

View File

@@ -200,10 +200,11 @@ server.on("/api/users/insert", HTTP_POST, [](AsyncWebServerRequest *request) {},
String uid = doc["uid"] | "";
String vorname = doc["vorname"] | "";
String nachname = doc["nachname"] | "";
String geburtsdatum = doc["geburtsdatum"] | "";
int alter = doc["alter"] | 0;
// Validate the data
if (uid.isEmpty() || vorname.isEmpty() || nachname.isEmpty() || alter <= 0) {
if (uid.isEmpty() || vorname.isEmpty() || nachname.isEmpty() || geburtsdatum.isEmpty() || alter <= 0) {
Serial.println("Ungültige Eingabedaten");
response["success"] = false;
response["error"] = "Ungültige Eingabedaten";
@@ -215,7 +216,7 @@ server.on("/api/users/insert", HTTP_POST, [](AsyncWebServerRequest *request) {},
Serial.println("Nachname: " + nachname);
Serial.println("Alter: " + String(alter));
bool dbSuccess = enterUserData(uid, vorname, nachname, alter);
bool dbSuccess = enterUserData(uid, vorname, nachname, geburtsdatum, alter);
if (dbSuccess) {
response["success"] = true;

View File

@@ -199,7 +199,6 @@ void setupRoutes(){
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\"}");
}
@@ -215,6 +214,42 @@ void setupRoutes(){
request->send(200, "application/json", result);
});
server.on("/api/set-location", HTTP_POST, [](AsyncWebServerRequest *request) {
Serial.println("/api/set-location called");
String id, name;
if (request->hasParam("id", true)) {
id = request->getParam("id", true)->value();
}
if (request->hasParam("name", true)) {
name = request->getParam("name", true)->value();
}
masterlocation = name;
saveLocationSettings();
// Rückmeldung
DynamicJsonDocument doc(64);
doc["success"] = true;
String result;
serializeJson(doc, result);
request->send(200, "application/json", result);
});
server.on("/api/get-location", HTTP_GET, [](AsyncWebServerRequest *request){
DynamicJsonDocument doc(128);
doc["locationid"] = masterlocation ? masterlocation : "";
String result;
serializeJson(doc, result);
request->send(200, "application/json", result);
});
// Statische Dateien
server.serveStatic("/", SPIFFS, "/");
server.begin();
@@ -222,6 +257,8 @@ void setupRoutes(){
}
void setupWebSocket() {
ws.onEvent([](AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {

View File

@@ -23,7 +23,7 @@ void setupWifi() {
Serial.println("Access Point SSID: " + String(ssidSTA));
Serial.println("Access Point PW: " + String(passwordSTA));
if ((ssidSTA == nullptr) || (passwordSTA == nullptr)) {
if (ssidSTA == nullptr || passwordSTA == nullptr || String(ssidSTA).isEmpty() || String(passwordSTA).isEmpty() ) {
Serial.println("Fehler: ssidSTA oder passwordSTA ist null!");
WiFi.mode(WIFI_MODE_AP);
WiFi.softAP(ssidAP, passwordAP);