mirror of
https://github.com/HendrikRauh/dmx-interface.git
synced 2025-05-19 10:32:56 +00:00
moved loading of config to request handler
This commit is contained in:
parent
fb142d0491
commit
366585b263
3 changed files with 35 additions and 49 deletions
|
@ -20,7 +20,7 @@ void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
config.begin("dmx", false);
|
config.begin("dmx", true);
|
||||||
|
|
||||||
uint8_t universe1 = config.getUChar("universe-1", 1);
|
uint8_t universe1 = config.getUChar("universe-1", 1);
|
||||||
uint8_t universe2 = config.getUChar("universe-2", 1);
|
uint8_t universe2 = config.getUChar("universe-2", 1);
|
||||||
|
@ -40,6 +40,8 @@ void setup()
|
||||||
IPAddress defaultGateway(192, 168, 1, 1);
|
IPAddress defaultGateway(192, 168, 1, 1);
|
||||||
IPAddress gateway = config.getUInt("gateway", defaultGateway);
|
IPAddress gateway = config.getUInt("gateway", defaultGateway);
|
||||||
|
|
||||||
|
config.end();
|
||||||
|
|
||||||
// WiFi stuff
|
// WiFi stuff
|
||||||
// WiFi.begin(ssid, pwd);
|
// WiFi.begin(ssid, pwd);
|
||||||
WiFi.softAP(ssid, pwd);
|
WiFi.softAP(ssid, pwd);
|
||||||
|
@ -79,8 +81,8 @@ void setup()
|
||||||
|
|
||||||
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
|
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
|
||||||
|
|
||||||
server.on("/config", HTTP_GET, [&, ipMethod, defaultIp, subnet, connection, gateway, ssid, pwd, direction1, universe1, direction2, universe2](AsyncWebServerRequest *request)
|
server.on("/config", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
{ onGetConfig(connection, ssid, pwd, ipMethod, defaultIp, subnet, gateway, universe1, direction1, universe2, direction2, request); });
|
{ onGetConfig(config, request); });
|
||||||
|
|
||||||
server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
|
server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
|
||||||
{
|
{
|
||||||
|
|
|
@ -70,42 +70,33 @@ Direction parseDirection(uint8_t direction)
|
||||||
|
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|
||||||
void onGetConfig(
|
void onGetConfig(Preferences config, AsyncWebServerRequest *request)
|
||||||
Connection connection,
|
|
||||||
String ssid,
|
|
||||||
String pwd,
|
|
||||||
IpMethod ipMethod,
|
|
||||||
uint32_t ip,
|
|
||||||
uint32_t subnet,
|
|
||||||
uint32_t gateway,
|
|
||||||
uint8_t universe1,
|
|
||||||
Direction direction1,
|
|
||||||
uint8_t universe2,
|
|
||||||
Direction direction2,
|
|
||||||
AsyncWebServerRequest *request)
|
|
||||||
{
|
{
|
||||||
|
config.begin("dmx", true);
|
||||||
|
|
||||||
|
IPAddress defaultIp(192, 168, 1, 201);
|
||||||
|
IPAddress ip = config.getUInt("ip", defaultIp);
|
||||||
|
|
||||||
|
IPAddress defaultSubnet(255, 255, 255, 0);
|
||||||
|
IPAddress subnet = config.getUInt("subnet", defaultSubnet);
|
||||||
|
|
||||||
|
IPAddress defaultGateway(192, 168, 1, 1);
|
||||||
|
IPAddress gateway = config.getUInt("gateway", defaultGateway);
|
||||||
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
|
doc["connection"] = config.getUInt("connection", WiFiSta);
|
||||||
|
doc["ssid"] = config.getString("ssid", "artnet");
|
||||||
|
doc["password"] = config.getString("password", "mbgmbgmbg");
|
||||||
|
doc["ip-method"] = config.getUInt("ip-method"), Static;
|
||||||
|
doc["ip"] = ip.toString();
|
||||||
|
doc["subnet"] = subnet.toString();
|
||||||
|
doc["gateway"] = gateway.toString();
|
||||||
|
doc["universe-1"] = config.getUChar("universe-1", 1);
|
||||||
|
doc["direction-1"] = config.getUInt("direction-1", Output);
|
||||||
|
doc["universe-2"] = config.getUChar("universe-2", 1);
|
||||||
|
doc["direction-2"] = config.getUInt("direction-2", Input);
|
||||||
|
|
||||||
IPAddress ipAddr = ip;
|
config.end();
|
||||||
String ipString = ipAddr.toString();
|
|
||||||
|
|
||||||
ipAddr = subnet;
|
|
||||||
String subnetString = ipAddr.toString();
|
|
||||||
|
|
||||||
ipAddr = gateway;
|
|
||||||
String gatewayString = ipAddr.toString();
|
|
||||||
|
|
||||||
doc["connection"] = connection;
|
|
||||||
doc["ssid"] = ssid;
|
|
||||||
doc["password"] = pwd;
|
|
||||||
doc["ip-method"] = ipMethod;
|
|
||||||
doc["ip"] = ipString;
|
|
||||||
doc["subnet"] = subnetString;
|
|
||||||
doc["gateway"] = gatewayString;
|
|
||||||
doc["universe-1"] = universe1;
|
|
||||||
doc["direction-1"] = direction1;
|
|
||||||
doc["universe-2"] = universe2;
|
|
||||||
doc["direction-2"] = direction2;
|
|
||||||
|
|
||||||
String jsonString;
|
String jsonString;
|
||||||
serializeJson(doc, jsonString);
|
serializeJson(doc, jsonString);
|
||||||
|
@ -122,6 +113,8 @@ void onPutConfig(AsyncWebServerRequest *request, uint8_t *data, size_t len, size
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
config.begin("dmx", false);
|
||||||
|
|
||||||
IpMethod ipMethod = parseIpMethod(doc["ip-method"].as<uint8_t>());
|
IpMethod ipMethod = parseIpMethod(doc["ip-method"].as<uint8_t>());
|
||||||
config.putUInt("ip-method", ipMethod);
|
config.putUInt("ip-method", ipMethod);
|
||||||
|
|
||||||
|
@ -155,10 +148,13 @@ void onPutConfig(AsyncWebServerRequest *request, uint8_t *data, size_t len, size
|
||||||
Direction direction2 = parseDirection(doc["direction-2"].as<uint8_t>());
|
Direction direction2 = parseDirection(doc["direction-2"].as<uint8_t>());
|
||||||
config.putInt("direction-2", direction2);
|
config.putInt("direction-2", direction2);
|
||||||
|
|
||||||
|
config.end();
|
||||||
|
|
||||||
request->send(200);
|
request->send(200);
|
||||||
}
|
}
|
||||||
catch (::std::invalid_argument &e)
|
catch (::std::invalid_argument &e)
|
||||||
{
|
{
|
||||||
|
config.end();
|
||||||
request->send(400, "text/plain", e.what());
|
request->send(400, "text/plain", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,19 +31,7 @@ enum Direction
|
||||||
};
|
};
|
||||||
const uint8_t DIRECTION_SIZE = 2;
|
const uint8_t DIRECTION_SIZE = 2;
|
||||||
|
|
||||||
void onGetConfig(
|
void onGetConfig(Preferences config, AsyncWebServerRequest *request);
|
||||||
Connection connection,
|
|
||||||
String ssid,
|
|
||||||
String pwd,
|
|
||||||
IpMethod ipMethod,
|
|
||||||
uint32_t ip,
|
|
||||||
uint32_t subnet,
|
|
||||||
uint32_t gateway,
|
|
||||||
uint8_t universe1,
|
|
||||||
Direction direction1,
|
|
||||||
uint8_t universe2,
|
|
||||||
Direction direction2,
|
|
||||||
AsyncWebServerRequest *request);
|
|
||||||
|
|
||||||
void onPutConfig(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total);
|
void onPutConfig(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue