webserver basic restructure
This commit is contained in:
parent
70e0ebed53
commit
2c1c126dc2
5 changed files with 161 additions and 198 deletions
|
@ -32,40 +32,7 @@ class ACLWebServerClass{
|
|||
_password = "";
|
||||
}
|
||||
|
||||
_server->onNotFound([&](AsyncWebServerRequest *request){
|
||||
request->send(200, "application/json", "{\"msg\":\"The content you are looking for was not found\"}");
|
||||
});
|
||||
|
||||
_server->on("/users", HTTP_GET, handleListUsers);
|
||||
|
||||
|
||||
_server->on("/users/create", HTTP_POST, handleCreateUser);
|
||||
_server->on("/users/update", HTTP_POST, handleUpdateUser);
|
||||
_server->on("/users/remove", HTTP_POST, handleRemoveUser);
|
||||
|
||||
}
|
||||
|
||||
// deprecated, keeping for backward compatibility
|
||||
void loop() {
|
||||
}
|
||||
|
||||
private:
|
||||
AsyncWebServer *_server;
|
||||
String _username = "";
|
||||
String _password = "";
|
||||
bool _authRequired = false;
|
||||
|
||||
static String outputState(int output){
|
||||
if(digitalRead(output)){
|
||||
return "checked";
|
||||
}
|
||||
else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// Handler for the '/users' endpoint to list all users
|
||||
void handleListUsers(AsyncWebServerRequest* request) {
|
||||
_server->on("/users", HTTP_GET, [&](AsyncWebServerRequest *request){
|
||||
|
||||
if(_authRequired){
|
||||
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
||||
|
@ -92,11 +59,9 @@ class ACLWebServerClass{
|
|||
|
||||
// Set the response content type to JSON
|
||||
request->send(200, "application/json", response);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
void handleCreateUser(AsyncWebServerRequest* request) {
|
||||
_server->on("/users/create", HTTP_POST, [&](AsyncWebServerRequest *request){
|
||||
if(_authRequired){
|
||||
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
||||
return request->requestAuthentication();
|
||||
|
@ -126,11 +91,9 @@ class ACLWebServerClass{
|
|||
acl.saveToEEPROM();
|
||||
request->send(201); // Create
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Handler for the '/users/update' endpoint to remove a user
|
||||
void handleUpdateUser(AsyncWebServerRequest* request) {
|
||||
_server->on("/users/update", HTTP_POST, [&](AsyncWebServerRequest *request){
|
||||
if(_authRequired){
|
||||
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
||||
return request->requestAuthentication();
|
||||
|
@ -160,10 +123,9 @@ class ACLWebServerClass{
|
|||
acl.updateUser(cardId, newCardId, desc);
|
||||
acl.saveToEEPROM();
|
||||
request->send(201); // Created
|
||||
}
|
||||
});
|
||||
|
||||
// Handler for the '/users/remove' endpoint to remove a user
|
||||
void handleRemoveUser(AsyncWebServerRequest* request) {
|
||||
_server->on("/users/remove", HTTP_POST, [&](AsyncWebServerRequest *request){
|
||||
if(_authRequired){
|
||||
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
||||
return request->requestAuthentication();
|
||||
|
@ -179,10 +141,27 @@ class ACLWebServerClass{
|
|||
acl.removeUser(cardId);
|
||||
acl.saveToEEPROM();
|
||||
request->send(201); // Created
|
||||
});
|
||||
}
|
||||
|
||||
// deprecated, keeping for backward compatibility
|
||||
void loop() {
|
||||
}
|
||||
|
||||
private:
|
||||
AsyncWebServer *_server;
|
||||
String _username = "";
|
||||
String _password = "";
|
||||
bool _authRequired = false;
|
||||
|
||||
static String outputState(int output){
|
||||
if(digitalRead(output)){
|
||||
return "checked";
|
||||
}
|
||||
else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ACLWebServerClass ACLWebServer;
|
||||
|
|
|
@ -35,9 +35,4 @@
|
|||
void toggleDoor();
|
||||
String stateDoor();
|
||||
|
||||
#ifdef LOCAL_ACL
|
||||
#include "ACL.h"
|
||||
ACL acl = {
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include "secrets.h"
|
||||
#include "settings.h"
|
||||
|
||||
Settings settings;
|
||||
|
||||
#ifdef WIFI
|
||||
#include "WiFi.h"
|
||||
#endif
|
||||
|
@ -24,7 +26,14 @@
|
|||
#endif
|
||||
|
||||
#include "mainwebserver.h"
|
||||
|
||||
#ifdef LOCAL_ACL
|
||||
#include "acl.h"
|
||||
ACL acl = {
|
||||
};
|
||||
#ifdef LOCAL_ACL_API
|
||||
#include "aclwebserver.h"
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef BUZZER
|
||||
|
|
|
@ -85,7 +85,48 @@ class MainWebServerClass{
|
|||
}
|
||||
});
|
||||
|
||||
#ifdef RELAY1
|
||||
_server->on("/state/relay1", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
||||
if(!request->authenticate(http_username, http_password))
|
||||
return request->requestAuthentication();
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"state\":\""+stateDoor()+"\"}");
|
||||
request->send(response);
|
||||
});
|
||||
#endif
|
||||
|
||||
_server->on("/settings/get/DoorDisabled", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+String(settings.DoorDisabled())+"\"}");
|
||||
request->send(response);
|
||||
});
|
||||
|
||||
_server->on("/settings/set/DoorDisabled", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
||||
String value;
|
||||
|
||||
if(!request->authenticate(http_username, http_password))
|
||||
return request->requestAuthentication();
|
||||
|
||||
if (request->hasParam("value")) {
|
||||
value = request->getParam("value")->value();
|
||||
if (value == "0") {
|
||||
settings.setDisableDoor(0);
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to 0\"}");
|
||||
request->send(response);
|
||||
}
|
||||
else if (value =="1") {
|
||||
settings.setDisableDoor(1);
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to 1\"}");
|
||||
request->send(response);
|
||||
|
||||
} else {
|
||||
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"value should be 0 or 1\"}");
|
||||
request->send(response);
|
||||
}
|
||||
}
|
||||
else {
|
||||
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"Missing 'value' param.\"}");
|
||||
request->send(response);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// deprecated, keeping for backward compatibility
|
||||
|
|
65
src/main.cpp
65
src/main.cpp
|
@ -7,7 +7,6 @@ const unsigned long displayDelay = 1000; // Delay in milliseconds after which th
|
|||
const unsigned long wifiRebootTimeout = 20000; // Delay before reboot after disconnect
|
||||
unsigned int bitCount = 0; // Variable to keep track of the bit count
|
||||
unsigned int maxReaderWaitTime = 9000; // Variable to timeout reader after too long of no data.
|
||||
Settings settings;
|
||||
AsyncWebServer server(80);
|
||||
|
||||
#ifdef WIFI
|
||||
|
@ -136,7 +135,6 @@ void setup() {
|
|||
acl.loadFromEEPROM();
|
||||
#endif
|
||||
|
||||
|
||||
settings.loadFromEEPROM();
|
||||
|
||||
// Initialize SPIFFS
|
||||
|
@ -192,75 +190,16 @@ void setup() {
|
|||
|
||||
#ifdef BUZZER
|
||||
pinMode(ALARM_PIN, OUTPUT);
|
||||
// Do not set to low or it will constantly beep.
|
||||
digitalWrite(ALARM_PIN, HIGH);
|
||||
digitalWrite(ALARM_PIN, HIGH); // Do not set to low or it will constantly beep.
|
||||
#endif
|
||||
|
||||
#ifdef WEB_SERVER
|
||||
// Route for root / web page
|
||||
|
||||
|
||||
#ifdef RELAY1
|
||||
server.on("/state/relay1", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
||||
if(!request->authenticate(http_username, http_password))
|
||||
return request->requestAuthentication();
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"state\":\""+stateDoor()+"\"}");
|
||||
request->send(response);
|
||||
});
|
||||
#endif
|
||||
|
||||
server.on("/settings/get/DoorDisabled", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+String(settings.DoorDisabled())+"\"}");
|
||||
request->send(response);
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Send a GET request to <ESP_IP>/gpio?output=<paramOutput>&state=<paramState>
|
||||
server.on("/settings/set/DoorDisabled", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
||||
String value;
|
||||
|
||||
if(!request->authenticate(http_username, http_password))
|
||||
return request->requestAuthentication();
|
||||
|
||||
if (request->hasParam("value")) {
|
||||
value = request->getParam("value")->value();
|
||||
if (value == "0") {
|
||||
settings.setDisableDoor(0);
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to 0\"}");
|
||||
request->send(response);
|
||||
}
|
||||
else if (value =="1") {
|
||||
settings.setDisableDoor(1);
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to 1\"}");
|
||||
request->send(response);
|
||||
|
||||
} else {
|
||||
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"value should be 0 or 1\"}");
|
||||
request->send(response);
|
||||
}
|
||||
}
|
||||
else {
|
||||
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"Missing 'value' param.\"}");
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef WEB_SERVER
|
||||
MainWebServer.begin(&server, http_username, http_password);
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef LOCAL_ACL
|
||||
#ifdef LOCAL_ACL_API
|
||||
|
||||
ACLWebServer.begin(&server, http_username, http_password);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
server.begin();
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in a new issue