443 lines
21 KiB
C++
443 lines
21 KiB
C++
#ifndef MAINWEBSERVER_H
|
|
#define MAINWEBSERVER_H
|
|
|
|
#include "Arduino.h"
|
|
#include "stdlib_noniso.h"
|
|
#include "WiFi.h"
|
|
#include "AsyncTCP.h"
|
|
#include "Update.h"
|
|
#include "esp_int_wdt.h"
|
|
#include "esp_task_wdt.h"
|
|
#include "ESPAsyncWebServer.h"
|
|
#include "SPIFFS.h"
|
|
#include "hardware.h"
|
|
#include "settings.h"
|
|
|
|
class MainWebServerClass{
|
|
|
|
public:
|
|
|
|
void begin(AsyncWebServer *server, const char* username = "", const char* password = ""){
|
|
_server = server;
|
|
|
|
if(strlen(username) > 0){
|
|
_authRequired = true;
|
|
_username = username;
|
|
_password = password;
|
|
}else{
|
|
_authRequired = false;
|
|
_username = "";
|
|
_password = "";
|
|
}
|
|
|
|
_server->on("/", HTTP_GET, [&](AsyncWebServerRequest *request){
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
|
|
request->send(SPIFFS, "/index.html", String(), false);
|
|
});
|
|
|
|
_server->on("/version", HTTP_GET, [&](AsyncWebServerRequest *request){
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
request->send(200, "application/json", "{\"version\":\"3.5.7\"}");
|
|
});
|
|
|
|
_server->onNotFound([&](AsyncWebServerRequest *request){
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
request->send(200, "application/json", "{\"msg\":\"The content you are looking for was not found\"}");
|
|
});
|
|
|
|
#ifdef RELAY1
|
|
_server->on("/state/relay", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"state\":\""+stateDoor()+"\"}");
|
|
request->send(response);
|
|
});
|
|
#endif
|
|
|
|
_server->on("/settings/relaydisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+String(settings.getDoorDisabled())+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/relaydisabled", HTTP_POST, [&] (AsyncWebServerRequest *request) {
|
|
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
|
|
if (request->hasParam("value", true)) {
|
|
String value = String(request->arg("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);
|
|
}
|
|
});
|
|
|
|
_server->on("/settings/relaymode", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+settings.getDoorMode()+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/relaymode", HTTP_POST, [&] (AsyncWebServerRequest *request) {
|
|
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
if (request->hasParam("value", true)) {
|
|
String value = String(request->arg("value"));
|
|
|
|
if (value == "LATCH" || value == "TOGGLE") {
|
|
settings.setDoorMode(value);
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to "+value+"\"}");
|
|
request->send(response);
|
|
}
|
|
else {
|
|
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"value should be LATCH or TOGGLE\"}");
|
|
request->send(response);
|
|
}
|
|
}
|
|
else {
|
|
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"Missing 'value' param.\"}");
|
|
request->send(response);
|
|
}
|
|
});
|
|
|
|
_server->on("/settings/tinance2creds", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"identifier\":\""+settings.tinance2ReaderIdentifier+"\", \"key\":\""+settings.tinance2ReaderKey+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/tinance2creds", HTTP_POST, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
if(request->hasParam("identifier", true)) {} //This is important, otherwise the sketch will crash if there is no body
|
|
else {
|
|
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"No identifier\"}");
|
|
request->send(response);
|
|
}
|
|
String identifier = String(request->arg("identifier"));
|
|
|
|
if(request->hasParam("key", true)) {} //This is important, otherwise the sketch will crash if there is no body
|
|
else {
|
|
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"No key\"}");
|
|
request->send(response);
|
|
}
|
|
String key = String(request->arg("key"));
|
|
|
|
settings.tinance2ReaderIdentifier = identifier;
|
|
settings.tinance2ReaderKey = key;
|
|
|
|
settings.saveSetting("t2", "ri", settings.tinance2ReaderIdentifier, "string");
|
|
settings.saveSetting("t2", "rk", settings.tinance2ReaderKey, "string");
|
|
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Tinance credentials updated\"}");
|
|
request->send(response);
|
|
|
|
});
|
|
|
|
|
|
_server->on("/settings/tinance2url", HTTP_POST, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
if(request->hasParam("url", true)) {} //This is important, otherwise the sketch will crash if there is no body
|
|
else {
|
|
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"No url\"}");
|
|
request->send(response);
|
|
}
|
|
String url = String(request->arg("url"));
|
|
|
|
settings.tinance2_url = url;
|
|
settings.tinance2_url_validatecard = settings.tinance2_url+"/accesscontrol/api/check-card-id";
|
|
settings.tinance2_url_readerinfo = settings.tinance2_url+"/accesscontrol/api/readerinfo";
|
|
settings.tinance2_url_acls = settings.tinance2_url+"/accesscontrol/api/acls";
|
|
settings.tinance2_url_log = settings.tinance2_url+"/accesscontrol/api/readerlog";
|
|
|
|
settings.saveSetting("t2", "rooturl", settings.tinance2_url, "string");
|
|
settings.saveSetting("t2", "vcurl", settings.tinance2_url_validatecard, "string");
|
|
settings.saveSetting("t2", "riurl", settings.tinance2_url_readerinfo, "string");
|
|
settings.saveSetting("t2", "aclurl", settings.tinance2_url_acls, "string");
|
|
settings.saveSetting("t2", "rluri", settings.tinance2_url_log, "string");
|
|
|
|
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Tinance URL updated\"}");
|
|
request->send(response);
|
|
|
|
});
|
|
|
|
_server->on("/settings/tinance2url", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+settings.tinance2_url+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
|
|
_server->on("/settings/tinance2validatecardurl", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+settings.tinance2_url_validatecard+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
|
|
_server->on("/settings/tinance2readerinfourl", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+settings.tinance2_url_readerinfo+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/tinance2aclsurl", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+settings.tinance2_url_acls+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
|
|
_server->on("/settings/tinance2logurl", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+settings.tinance2_url_log+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/webhooklockenabled", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+String(settings.webhookLockEnabled)+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/webhooklockenabled", HTTP_POST, [&] (AsyncWebServerRequest *request) {
|
|
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
|
|
if (request->hasParam("value", true)) {
|
|
String value = String(request->arg("value"));
|
|
if (value == "0") {
|
|
settings.webhookLockEnabled = 0;
|
|
settings.saveSetting("wh", "lockena", String(settings.webhookLockEnabled), "bool");
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to 0\"}");
|
|
request->send(response);
|
|
}
|
|
else if (value =="1") {
|
|
settings.webhookLockEnabled = 1;
|
|
settings.saveSetting("wh", "lockena", String(settings.webhookLockEnabled), "bool");
|
|
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);
|
|
}
|
|
});
|
|
|
|
|
|
_server->on("/settings/webhooklockhook", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+settings.webhookLockHook+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/webhooklockhook", HTTP_POST, [&] (AsyncWebServerRequest *request) {
|
|
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
|
|
if (request->hasParam("value", true)) {
|
|
String value = String(request->arg("value"));
|
|
settings.webhookLockHook = value;
|
|
settings.saveSetting("wh", "lockurl", settings.webhookLockHook, "string");
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to "+value+"\"}");
|
|
request->send(response);
|
|
}
|
|
else {
|
|
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"Missing 'value' param.\"}");
|
|
request->send(response);
|
|
}
|
|
});
|
|
|
|
_server->on("/settings/webhookunlockenabled", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+String(settings.webhookUnlockEnabled)+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/webhookunlockenabled", HTTP_POST, [&] (AsyncWebServerRequest *request) {
|
|
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
|
|
if (request->hasParam("value", true)) {
|
|
String value = String(request->arg("value"));
|
|
if (value == "0") {
|
|
settings.webhookUnlockEnabled = 0;
|
|
settings.saveSetting("wh", "unlockena", String(settings.webhookUnlockEnabled), "bool");
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to 0\"}");
|
|
request->send(response);
|
|
}
|
|
else if (value =="1") {
|
|
settings.webhookUnlockEnabled = 1;
|
|
settings.saveSetting("wh", "unlockena", String(settings.webhookUnlockEnabled), "bool");
|
|
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);
|
|
}
|
|
});
|
|
|
|
_server->on("/settings/webhookunlockhook", HTTP_GET, [&] (AsyncWebServerRequest *request) {
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+settings.webhookUnlockHook+"\"}");
|
|
request->send(response);
|
|
});
|
|
|
|
_server->on("/settings/webhookunlockhook", HTTP_POST, [&] (AsyncWebServerRequest *request) {
|
|
|
|
if(_authRequired){
|
|
if(!request->authenticate(_username.c_str(), _password.c_str())){
|
|
return request->requestAuthentication();
|
|
}
|
|
}
|
|
|
|
if (request->hasParam("value", true)) {
|
|
String value = String(request->arg("value"));
|
|
settings.webhookUnlockHook = value;
|
|
settings.saveSetting("wh", "unlockurl", settings.webhookUnlockHook, "string");
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"value set to "+value+"\"}");
|
|
request->send(response);
|
|
}
|
|
else {
|
|
AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"Missing 'value' param.\"}");
|
|
request->send(response);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
// deprecated, keeping for backward compatibility
|
|
void loop() {
|
|
}
|
|
|
|
private:
|
|
AsyncWebServer *_server;
|
|
String _username = "";
|
|
String _password = "";
|
|
bool _authRequired = false;
|
|
};
|
|
|
|
MainWebServerClass MainWebServer;
|
|
#endif
|