Files
AquaMasterMQTT/src/preferencemanager.h
Carsten Graf 0223cceef8
Some checks failed
/ build (push) Has been cancelled
Button Simmulator, Frontend änderungen
2026-04-11 20:24:39 +02:00

161 lines
4.9 KiB
C

#pragma once
#include <Arduino.h>
#include <Preferences.h>
#include <licenceing.h>
#include <master.h>
// Persist and load button configuration
void saveButtonConfig() {
preferences.begin("buttons", false);
preferences.putBytes("config", &buttonConfigs, sizeof(buttonConfigs));
preferences.end();
}
void loadButtonConfig() {
preferences.begin("buttons", true);
size_t schLen = preferences.getBytesLength("config");
if (schLen == sizeof(buttonConfigs)) {
preferences.getBytes("config", &buttonConfigs, schLen);
}
preferences.end();
}
// Persist and load local leaderboard
void saveBestTimes() {
preferences.begin("leaderboard", false);
// Speichere Anzahl der Einträge
preferences.putUInt("count", localTimes.size());
// Speichere jeden Eintrag (kurze Schlüssel für NVS)
for (size_t i = 0; i < localTimes.size(); i++) {
String key = "e" + String(i); // e0, e1, e2, etc.
preferences.putString((key + "u").c_str(),
localTimes[i].uid); // e0u, e1u, etc.
preferences.putString((key + "n").c_str(),
localTimes[i].name); // e0n, e1n, etc.
preferences.putULong((key + "t").c_str(),
localTimes[i].timeMs); // e0t, e1t, etc.
preferences.putULong((key + "s").c_str(),
localTimes[i].timestamp); // e0s, e1s, etc.
preferences.putInt((key + "l").c_str(),
localTimes[i].lane); // e0l, e1l, etc.
}
preferences.end();
Serial.println("Lokales Leaderboard gespeichert: " +
String(localTimes.size()) + " Einträge");
}
void loadBestTimes() {
preferences.begin("leaderboard", true);
// Leere das aktuelle Leaderboard
localTimes.clear();
// Lade Anzahl der Einträge
uint32_t count = preferences.getUInt("count", 0);
// Lade jeden Eintrag (kurze Schlüssel für NVS)
for (uint32_t i = 0; i < count; i++) {
LocalTime entry;
String key = "e" + String(i); // e0, e1, e2, etc.
entry.uid =
preferences.getString((key + "u").c_str(), ""); // e0u, e1u, etc.
entry.name =
preferences.getString((key + "n").c_str(), ""); // e0n, e1n, etc.
entry.timeMs =
preferences.getULong((key + "t").c_str(), 0); // e0t, e1t, etc.
entry.timestamp =
preferences.getULong((key + "s").c_str(), 0); // e0s, e1s, etc.
entry.lane =
preferences.getInt((key + "l").c_str(), 0); // e0l, e1l, etc.
localTimes.push_back(entry);
}
preferences.end();
Serial.println("Lokales Leaderboard geladen: " + String(localTimes.size()) +
" Einträge");
}
// Persist and load general settings
void saveSettings() {
preferences.begin("settings", false);
preferences.putULong("maxTime", maxTimeBeforeReset);
preferences.putULong("maxTimeDisplay", maxTimeDisplay);
preferences.putULong("minTime", minTimeForLeaderboard);
preferences.putUInt("gamemode", gamemode);
preferences.putUInt("laneConfigType", laneConfigType);
preferences.putUInt("lane1Diff", lane1DifficultyType);
preferences.putUInt("lane2Diff", lane2DifficultyType);
preferences.end();
}
void loadSettings() {
preferences.begin("settings", true);
maxTimeBeforeReset = preferences.getULong("maxTime", 300000);
maxTimeDisplay = preferences.getULong("maxTimeDisplay", 20000);
minTimeForLeaderboard = preferences.getULong("minTime", 5000);
gamemode = preferences.getUInt("gamemode", 0);
laneConfigType = preferences.getUInt("laneConfigType", 0);
lane1DifficultyType = preferences.getUInt("lane1Diff", 0);
lane2DifficultyType = preferences.getUInt("lane2Diff", 0);
preferences.end();
}
// Persist and load WiFi settings
void saveWifiSettings() {
preferences.begin("wifi", false);
preferences.putString("ssid", ssidSTA);
preferences.putString("password", passwordSTA);
preferences.end();
delay(500);
ESP.restart();
}
void loadWifiSettings() {
preferences.begin("wifi", true);
String ssid = preferences.getString("ssid", "");
String password = preferences.getString("password", "");
ssidSTA = strdup(ssid.c_str());
passwordSTA = strdup(password.c_str());
preferences.end();
}
// Persist and load location settings
void loadLocationSettings() {
preferences.begin("location", true);
masterlocation = preferences.getString("location", "");
preferences.end();
}
void saveLocationSettings() {
preferences.begin("location", false);
preferences.putString("location", masterlocation);
preferences.end();
}
// Licence helper
int checkLicence() {
loadLicenceFromPrefs();
String id = getUniqueDeviceID();
int tier = getLicenseTier(id, licence);
return tier;
}
void saveLocationIdToPrefs(const String &locationId) {
preferences.begin("locationid", false);
preferences.putString("locationid", locationId);
preferences.end();
}
String getLocationIdFromPrefs() {
preferences.begin("locationid", true);
String locationId = preferences.getString("locationid", "");
preferences.end();
return locationId;
}