Refactored logging system to require a TAG as first argument on all LOG_* macros, enabling per-subsystem log filtering and cleaner output. Each subsystem now defines its own TAG (e.g. "BellEngine", "Player"). Also overhauled Logging.hpp/cpp with improved level control and output. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
127 lines
4.6 KiB
C++
127 lines
4.6 KiB
C++
/*
|
|
* ═══════════════════════════════════════════════════════════════════════════════════
|
|
* SETTINGSWEBSERVER.CPP - Network Mode Settings Web Interface Implementation
|
|
* ═══════════════════════════════════════════════════════════════════════════════════
|
|
*/
|
|
|
|
#include "SettingsWebServer.hpp"
|
|
|
|
#define TAG "SettingsWebServer"
|
|
#include "SettingsPage.h"
|
|
#include "../ConfigManager/ConfigManager.hpp"
|
|
#include "../Networking/Networking.hpp"
|
|
#include "../Logging/Logging.hpp"
|
|
|
|
SettingsWebServer::SettingsWebServer(AsyncWebServer& server,
|
|
ConfigManager& configManager,
|
|
Networking& networking)
|
|
: _server(server)
|
|
, _configManager(configManager)
|
|
, _networking(networking) {
|
|
}
|
|
|
|
SettingsWebServer::~SettingsWebServer() {
|
|
}
|
|
|
|
void SettingsWebServer::begin() {
|
|
LOG_INFO(TAG, "SettingsWebServer - Initializing settings web interface");
|
|
|
|
// GET /settings - Main settings page
|
|
_server.on("/settings", HTTP_GET,
|
|
[this](AsyncWebServerRequest* request) {
|
|
handleSettingsPage(request);
|
|
}
|
|
);
|
|
|
|
// POST /api/set-mode - Set network mode
|
|
_server.on("/api/set-mode", HTTP_POST,
|
|
[this](AsyncWebServerRequest* request) {
|
|
handleSetMode(request);
|
|
}
|
|
);
|
|
|
|
// POST /api/reboot - Reboot device
|
|
_server.on("/api/reboot", HTTP_POST,
|
|
[this](AsyncWebServerRequest* request) {
|
|
handleReboot(request);
|
|
}
|
|
);
|
|
|
|
LOG_INFO(TAG, "SettingsWebServer - Endpoints registered");
|
|
LOG_INFO(TAG, " GET /settings - Settings page");
|
|
LOG_INFO(TAG, " POST /api/set-mode - Set network mode");
|
|
LOG_INFO(TAG, " POST /api/reboot - Reboot device");
|
|
}
|
|
|
|
void SettingsWebServer::handleSettingsPage(AsyncWebServerRequest* request) {
|
|
LOG_DEBUG(TAG, "SettingsWebServer - Settings page requested");
|
|
String html = generateSettingsHTML();
|
|
request->send(200, "text/html", html);
|
|
}
|
|
|
|
void SettingsWebServer::handleSetMode(AsyncWebServerRequest* request) {
|
|
if (!request->hasParam("mode", true)) {
|
|
request->send(400, "text/plain", "Missing mode parameter");
|
|
return;
|
|
}
|
|
|
|
String mode = request->getParam("mode", true)->value();
|
|
LOG_INFO(TAG, "SettingsWebServer - Mode change requested: %s", mode.c_str());
|
|
|
|
if (mode == "ap") {
|
|
// Switch to permanent AP mode
|
|
_configManager.setPermanentAPMode(true);
|
|
_configManager.saveNetworkConfig();
|
|
LOG_INFO(TAG, "✅ Permanent AP mode enabled - will activate on reboot");
|
|
request->send(200, "text/plain", "AP mode enabled. Device will reboot in 3 seconds.");
|
|
|
|
// Reboot after 3 seconds
|
|
delay(3000);
|
|
ESP.restart();
|
|
|
|
} else if (mode == "station") {
|
|
// Switch to station mode (router mode)
|
|
_configManager.setPermanentAPMode(false);
|
|
_configManager.saveNetworkConfig();
|
|
LOG_INFO(TAG, "✅ Station mode enabled - will activate on reboot");
|
|
request->send(200, "text/plain", "Station mode enabled. Device will reboot in 3 seconds.");
|
|
|
|
// Reboot after 3 seconds
|
|
delay(3000);
|
|
ESP.restart();
|
|
|
|
} else {
|
|
request->send(400, "text/plain", "Invalid mode. Use 'ap' or 'station'");
|
|
}
|
|
}
|
|
|
|
void SettingsWebServer::handleReboot(AsyncWebServerRequest* request) {
|
|
LOG_INFO(TAG, "SettingsWebServer - Reboot requested");
|
|
request->send(200, "text/plain", "Rebooting device in 2 seconds...");
|
|
|
|
delay(2000);
|
|
ESP.restart();
|
|
}
|
|
|
|
String SettingsWebServer::generateSettingsHTML() {
|
|
bool isAPMode = _networking.isInAPMode();
|
|
String currentIP = _networking.getLocalIP();
|
|
String deviceUID = _configManager.getDeviceUID();
|
|
String fwVersion = _configManager.getFwVersion();
|
|
|
|
// Load HTML template from PROGMEM
|
|
String html = String(FPSTR(SETTINGS_PAGE_HTML));
|
|
|
|
// Replace placeholders with dynamic values
|
|
html.replace("%MODE_BADGE_CLASS%", isAPMode ? "ap" : "station");
|
|
html.replace("%MODE_TEXT%", isAPMode ? "AP Mode" : "Station Mode");
|
|
html.replace("%CURRENT_IP%", currentIP);
|
|
html.replace("%DEVICE_UID%", deviceUID);
|
|
html.replace("%FW_VERSION%", fwVersion);
|
|
html.replace("%AP_ACTIVE_CLASS%", isAPMode ? "active" : "");
|
|
html.replace("%STATION_ACTIVE_CLASS%", !isAPMode ? "active" : "");
|
|
html.replace("%SELECTED_MODE%", isAPMode ? "ap" : "station");
|
|
|
|
return html;
|
|
}
|