settings api completed

This commit is contained in:
Jean Jacques Avril 2022-03-20 16:46:21 +01:00
parent dc995d6e7e
commit 8caffd50b2
3 changed files with 28 additions and 16 deletions

View File

@ -36,3 +36,6 @@
+ Mode 0 - station; 1 - client 4C uint8 + Mode 0 - station; 1 - client 4C uint8
+ AuthError Timeout (secs) 4D + AuthError Timeout (secs) 4D
+ OpenLock hold (secs) 4E + OpenLock hold (secs) 4E
### 20.03.2022
+ settings API implemented and tested
+ TODO: factory reset

View File

@ -22,13 +22,13 @@ bool WebConsole::init(Config *config, userdb::UserDb *userdb)
Serial.println(WiFi.softAPConfig(_config->ip, _config->gw, _config->subnet) ? "Ready" : "Failed!"); Serial.println(WiFi.softAPConfig(_config->ip, _config->gw, _config->subnet) ? "Ready" : "Failed!");
Serial.print("\t2. DNS config... "); Serial.print("\t2. DNS config... ");
Serial.println(_dnsServer->start(53, "*", _config->ip) ? "Ready" : "Failed!"); Serial.println(_dnsServer->start(53, "*", _config->ip) ? "Ready" : "Failed!");
Serial.print("\t3 AP setup <SSID:\"" + String(_config->SSID) + "\"..."); Serial.print("\t3 AP setup SSID:\"" + String(_config->SSID) + "\"... ");
if (strlen(_config->PASS) > 0) if (strlen(_config->PASS) > 0)
Serial.println(WiFi.softAP(_config->SSID, _config->PASS) ? "Ready" : "Failed!"); Serial.println(WiFi.softAP(_config->SSID, _config->PASS) ? "Ready" : "Failed!");
else else
Serial.println(WiFi.softAP(_config->SSID) ? "Ready" : "Failed!"); Serial.println(WiFi.softAP(_config->SSID) ? "Ready" : "Failed!");
WiFi.hostname("Doorlock"); WiFi.hostname("Doorlock");
Serial.println("Please connect via http://" +WiFi.softAPIP().toString()+"/"); Serial.println("Please connect via http://" + WiFi.softAPIP().toString() + "/");
// Webserver Setup // Webserver Setup
this->_server = new ESP8266WebServer(80); this->_server = new ESP8266WebServer(80);
@ -45,7 +45,7 @@ bool WebConsole::init(Config *config, userdb::UserDb *userdb)
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_GET, std::bind(&WebConsole::_getUser, this)); _server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_GET, std::bind(&WebConsole::_getUser, this));
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_PUT, std::bind(&WebConsole::_createUser, this)); _server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_PUT, std::bind(&WebConsole::_createUser, this));
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_POST, std::bind(&WebConsole::_updateUser, this)); _server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_POST, std::bind(&WebConsole::_updateUser, this));
_server->on(UriBraces("/api/config/{}"), std::bind(&WebConsole::_deleteUser, this)); _server->on(UriBraces("/api/config"), std::bind(&WebConsole::_settings, this));
_server->serveStatic("/", LittleFS, "/s/"); _server->serveStatic("/", LittleFS, "/s/");
_server->onNotFound(std::bind(&WebConsole::_handleUnknown, this)); _server->onNotFound(std::bind(&WebConsole::_handleUnknown, this));
return true; return true;
@ -131,6 +131,8 @@ void WebConsole::_auth()
void WebConsole::_settings() void WebConsole::_settings()
{ {
_sendCORS(); _sendCORS();
if (!_isAuth())
return;
String action = _server->arg("action"); String action = _server->arg("action");
if (action.equals("update")) if (action.equals("update"))
{ {
@ -155,10 +157,10 @@ void WebConsole::_settings()
sucess |= 0b00000010; sucess |= 0b00000010;
} }
if (_server->hasArg("IP")) if (_server->hasArg("ip"))
{ {
IPAddress temp; IPAddress temp;
if (temp.fromString(_server->arg("IP"))) if (temp.fromString(_server->arg("ip")))
{ {
_config->ip = temp.v4(); _config->ip = temp.v4();
sucess |= 0b00000100; sucess |= 0b00000100;
@ -166,10 +168,10 @@ void WebConsole::_settings()
else else
error |= 0b00000100; error |= 0b00000100;
} }
if (_server->hasArg("SUBNET")) if (_server->hasArg("subnet"))
{ {
IPAddress temp; IPAddress temp;
if (temp.fromString(_server->arg("SUBNET"))) if (temp.fromString(_server->arg("subnet")))
{ {
_config->subnet = temp.v4(); _config->subnet = temp.v4();
sucess |= 0b00001000; sucess |= 0b00001000;
@ -177,10 +179,10 @@ void WebConsole::_settings()
else else
error |= 0b00001000; error |= 0b00001000;
} }
if (_server->hasArg("GW")) if (_server->hasArg("gw"))
{ {
IPAddress temp; IPAddress temp;
if (temp.fromString(_server->arg("GW"))) if (temp.fromString(_server->arg("gw")))
{ {
_config->gw = temp.v4(); _config->gw = temp.v4();
sucess |= 0b00010000; sucess |= 0b00010000;
@ -188,25 +190,25 @@ void WebConsole::_settings()
else else
error |= 0b00010000; error |= 0b00010000;
} }
if (_server->hasArg("MODE")) if (_server->hasArg("mode"))
{ {
_config->mode = _server->arg("MODE").toInt(); _config->mode = _server->arg("mode").toInt();
if (_config->mode >= 0 && _config->mode <= 1) if (_config->mode >= 0 && _config->mode <= 1)
sucess |= 0b00100000; sucess |= 0b00100000;
else else
error |= 0b00100000; error |= 0b00100000;
} }
if (_server->hasArg("FAIL_TIMEOUT")) if (_server->hasArg("fail_timeout"))
{ {
_config->fail_timeout = _server->arg("FAIL_TIMEOUT").toInt(); _config->fail_timeout = _server->arg("fail_timeout").toInt();
if (_config->fail_timeout >= 0 && _config->fail_timeout <= 250) if (_config->fail_timeout >= 0 && _config->fail_timeout <= 250)
sucess |= 0b01000000; sucess |= 0b01000000;
else else
error |= 0b01000000; error |= 0b01000000;
} }
if (_server->hasArg("HOLD_TIME")) if (_server->hasArg("hold_time"))
{ {
_config->hold_time = _server->arg("HOLD_TIME").toInt(); _config->hold_time = _server->arg("hold_time").toInt();
if (_config->hold_time > 0 && _config->hold_time <= 250) if (_config->hold_time > 0 && _config->hold_time <= 250)
sucess |= 0b10000000; sucess |= 0b10000000;
else else
@ -222,7 +224,15 @@ void WebConsole::_settings()
_config->loadBin(); _config->loadBin();
_server->send(200, "text/json", "{\"status\":\"failed\", \"sucess\":\"" + String(sucess) + "\", \"error\":\"" + String(error) + "\"}"); _server->send(200, "text/json", "{\"status\":\"failed\", \"sucess\":\"" + String(sucess) + "\", \"error\":\"" + String(error) + "\"}");
} }
else
_server->send(200, "text/json", "{\"status\":\"noting_todo\"}");
} }
else if (action.equals("get"))
{
_server->send(200, "text/json", "{\"SSID\":\"" + String(_config->SSID) + "\", \"PASS\":\"" + String(_config->PASS) + "\", \"ip\":\"" + IPAddress(_config->ip).toString() + "\", \"subnet\":\"" + IPAddress(_config->subnet).toString() + "\", \"gw\":\"" + IPAddress(_config->gw).toString() + "\", \"fail_timeout\":\"" + String(_config->fail_timeout) + "\", \"hold_time\":\"" + String(_config->hold_time) + "\"}");
}
else
_server->send(404, "text/plain", "unknown action");
} }
void WebConsole::_sendCORS() void WebConsole::_sendCORS()
{ {

View File

@ -34,7 +34,6 @@ void setup()
rfid.begin(); rfid.begin();
web.attachRfid(&rfid); web.attachRfid(&rfid);
iface.begin(&keyboard); iface.begin(&keyboard);
config.print();
} }
void loop() void loop()