initial
This commit is contained in:
83
main/Server.cpp
Normal file
83
main/Server.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
#include "Server.hpp"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "Config.hpp"
|
||||
#include "WiFi.h"
|
||||
#include "index.hpp"
|
||||
|
||||
WebServer web_server;
|
||||
|
||||
void WebServer::Init() {
|
||||
running = false;
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
request->send(200, "text/html", index_html);
|
||||
});
|
||||
|
||||
// Endpoint to get config
|
||||
server.on("/config", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
JsonDocument doc;
|
||||
JsonArray arr = doc["mac_address"].to<JsonArray>();
|
||||
for (int i = 0; i < 6; i++) {
|
||||
arr.add(config.mac_address[i]);
|
||||
}
|
||||
doc["beacon_id"] = config.beacon_id;
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
Serial.printf("Config requested: %s\n", out.c_str());
|
||||
request->send(200, "application/json", out);
|
||||
});
|
||||
|
||||
// Endpoint to set config
|
||||
server.on("/config", HTTP_POST, [](AsyncWebServerRequest *request) {
|
||||
if (request->hasParam("mac_address", true)) {
|
||||
String macStr = request->getParam("mac_address", true)->value();
|
||||
int idx = 0;
|
||||
int last = 0;
|
||||
for (int i = 0; i < macStr.length() && idx < 6; i++) {
|
||||
if (macStr[i] == ',') {
|
||||
config.mac_address[idx++] = macStr.substring(last, i).toInt();
|
||||
last = i + 1;
|
||||
}
|
||||
}
|
||||
if (idx < 6) config.mac_address[idx] = macStr.substring(last).toInt();
|
||||
}
|
||||
if (request->hasParam("beacon_id", true)) {
|
||||
config.beacon_id = request->getParam("beacon_id", true)->value().toInt();
|
||||
}
|
||||
config.Save();
|
||||
request->send(200, "text/plain", "Config updated");
|
||||
});
|
||||
|
||||
// Endpoint to restart system
|
||||
server.on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
|
||||
request->send(200, "text/plain", "Restarting...");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
digitalWrite(LED_BUILTIN, i % 2);
|
||||
delay(50);
|
||||
};
|
||||
|
||||
ESP.restart();
|
||||
});
|
||||
}
|
||||
|
||||
void WebServer::Start() {
|
||||
if (running) return;
|
||||
WiFi.disconnect();
|
||||
IPAddress local_ip(192, 168, 1, 1);
|
||||
IPAddress gateway(192, 168, 1, 1);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
WiFi.softAPConfig(local_ip, gateway, subnet);
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(String("beacon_") + String(config.beacon_id, HEX), "password");
|
||||
running = true;
|
||||
server.begin();
|
||||
// Start server logic here
|
||||
}
|
||||
|
||||
void WebServer::Stop() {
|
||||
if (!running) return;
|
||||
running = false;
|
||||
server.end();
|
||||
// Stop server logic here
|
||||
}
|
||||
Reference in New Issue
Block a user