Files
project-vesper/vesper/src/SettingsWebServer/SettingsWebServer.cpp

366 lines
12 KiB
C++

/*
* ═══════════════════════════════════════════════════════════════════════════════════
* SETTINGSWEBSERVER.CPP - Network Mode Settings Web Interface Implementation
* ═══════════════════════════════════════════════════════════════════════════════════
*/
#include "SettingsWebServer.hpp"
#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("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("SettingsWebServer - Endpoints registered");
LOG_INFO(" GET /settings - Settings page");
LOG_INFO(" POST /api/set-mode - Set network mode");
LOG_INFO(" POST /api/reboot - Reboot device");
}
void SettingsWebServer::handleSettingsPage(AsyncWebServerRequest* request) {
LOG_DEBUG("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("SettingsWebServer - Mode change requested: %s", mode.c_str());
if (mode == "ap") {
// Switch to permanent AP mode
_configManager.setPermanentAPMode(true);
_configManager.saveNetworkConfig();
LOG_INFO("✅ 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("✅ 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("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();
String html = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>VESPER Network Settings</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
padding: 40px;
max-width: 500px;
width: 100%;
}
.header {
text-align: center;
margin-bottom: 30px;
}
.header h1 {
color: #667eea;
font-size: 32px;
margin-bottom: 10px;
}
.header p {
color: #666;
font-size: 14px;
}
.status-card {
background: #f8f9fa;
border-radius: 12px;
padding: 20px;
margin-bottom: 30px;
}
.status-item {
display: flex;
justify-content: space-between;
margin-bottom: 12px;
font-size: 14px;
}
.status-item:last-child {
margin-bottom: 0;
}
.status-label {
color: #666;
font-weight: 500;
}
.status-value {
color: #333;
font-weight: 600;
}
.mode-badge {
display: inline-block;
padding: 4px 12px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
}
.mode-badge.ap {
background: #e3f2fd;
color: #1976d2;
}
.mode-badge.station {
background: #e8f5e9;
color: #388e3c;
}
.section-title {
font-size: 18px;
color: #333;
margin-bottom: 20px;
font-weight: 600;
}
.mode-selector {
display: flex;
gap: 15px;
margin-bottom: 30px;
}
.mode-option {
flex: 1;
background: #f8f9fa;
border: 2px solid #e0e0e0;
border-radius: 12px;
padding: 20px;
cursor: pointer;
transition: all 0.3s ease;
text-align: center;
}
.mode-option:hover {
border-color: #667eea;
background: #f0f4ff;
}
.mode-option.active {
border-color: #667eea;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.mode-option h3 {
font-size: 16px;
margin-bottom: 8px;
}
.mode-option p {
font-size: 12px;
opacity: 0.8;
}
.btn {
width: 100%;
padding: 15px;
border: none;
border-radius: 10px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
margin-bottom: 10px;
}
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}
.btn-secondary {
background: #f8f9fa;
color: #666;
}
.btn-secondary:hover {
background: #e9ecef;
}
.info-box {
background: #fff3cd;
border-left: 4px solid #ffc107;
padding: 15px;
border-radius: 8px;
margin-top: 20px;
font-size: 13px;
color: #856404;
}
.footer {
text-align: center;
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #e0e0e0;
color: #999;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>VESPER Settings</h1>
<p>Network Configuration</p>
</div>
<div class="status-card">
<div class="status-item">
<span class="status-label">Current Mode:</span>
<span class="status-value">
<span class="mode-badge )rawliteral" + String(isAPMode ? "ap" : "station") + R"rawliteral(">
)rawliteral" + String(isAPMode ? "AP Mode" : "Station Mode") + R"rawliteral(
</span>
</span>
</div>
<div class="status-item">
<span class="status-label">IP Address:</span>
<span class="status-value">)rawliteral" + currentIP + R"rawliteral(</span>
</div>
<div class="status-item">
<span class="status-label">Device UID:</span>
<span class="status-value">)rawliteral" + deviceUID + R"rawliteral(</span>
</div>
<div class="status-item">
<span class="status-label">Firmware:</span>
<span class="status-value">v)rawliteral" + fwVersion + R"rawliteral(</span>
</div>
</div>
<div class="section-title">Select Network Mode</div>
<div class="mode-selector">
<div class="mode-option )rawliteral" + String(isAPMode ? "active" : "") + R"rawliteral(" onclick="selectMode('ap')">
<h3>AP Mode</h3>
<p>Direct Connection<br>192.168.4.1</p>
</div>
<div class="mode-option )rawliteral" + String(!isAPMode ? "active" : "") + R"rawliteral(" onclick="selectMode('station')">
<h3>Router Mode</h3>
<p>Connect via Router<br>WiFi/Ethernet</p>
</div>
</div>
<button class="btn btn-primary" onclick="applyMode()">Apply & Reboot</button>
<button class="btn btn-secondary" onclick="rebootDevice()">Reboot Device</button>
<div class="info-box">
Device will reboot after applying changes. Make sure to reconnect to the correct network after reboot.
</div>
<div class="footer">
VESPER Bell Automation System<br>
Advanced Bell Systems
</div>
</div>
<script>
let selectedMode = ')rawliteral" + String(isAPMode ? "ap" : "station") + R"rawliteral(';
function selectMode(mode) {
selectedMode = mode;
document.querySelectorAll('.mode-option').forEach(el => {
el.classList.remove('active');
});
event.target.closest('.mode-option').classList.add('active');
}
function applyMode() {
if (confirm('Device will reboot and switch to ' + (selectedMode === 'ap' ? 'AP Mode' : 'Router Mode') + '. Continue?')) {
fetch('/api/set-mode', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'mode=' + selectedMode
}).then(response => {
alert('Rebooting... Please wait 10 seconds and reconnect.');
});
}
}
function rebootDevice() {
if (confirm('Reboot device now?')) {
fetch('/api/reboot', {method: 'POST'}).then(() => {
alert('Rebooting... Please wait 10 seconds.');
});
}
}
</script>
</body>
</html>
)rawliteral";
return html;
}