From 9f483b89c4344c453662ad7b41311b5da544c5e8 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Mon, 15 Apr 2024 21:07:26 +0200 Subject: [PATCH] managing the eeprom --- include/mainwebserver.h | 207 ++++++++++++++++++++++++++++++++++++++-- include/settings.h | 2 +- src/settings.cpp | 11 +-- src/tinance2.cpp | 6 +- 4 files changed, 209 insertions(+), 17 deletions(-) diff --git a/include/mainwebserver.h b/include/mainwebserver.h index 7e88b68..84ff4b2 100644 --- a/include/mainwebserver.h +++ b/include/mainwebserver.h @@ -112,7 +112,7 @@ class MainWebServerClass{ }); #endif - _server->on("/settings/get/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { if(_authRequired){ if(!request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); @@ -122,7 +122,7 @@ class MainWebServerClass{ request->send(response); }); - _server->on("/settings/set/DoorDisabled", HTTP_POST, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/DoorDisabled", HTTP_POST, [&] (AsyncWebServerRequest *request) { String value; if(_authRequired){ @@ -155,7 +155,7 @@ class MainWebServerClass{ }); - _server->on("/settings/get/tinanceCreds", HTTP_GET, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/tinanceCreds", HTTP_GET, [&] (AsyncWebServerRequest *request) { if(_authRequired){ if(!request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); @@ -165,7 +165,7 @@ class MainWebServerClass{ request->send(response); }); - _server->on("/settings/set/tinanceCreds", HTTP_POST, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/tinanceCreds", HTTP_POST, [&] (AsyncWebServerRequest *request) { if(_authRequired){ if(!request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); @@ -198,7 +198,7 @@ class MainWebServerClass{ }); - _server->on("/settings/validatecardurl", HTTP_POST, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/tinanceurl", HTTP_POST, [&] (AsyncWebServerRequest *request) { if(_authRequired){ if(!request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); @@ -211,14 +211,24 @@ class MainWebServerClass{ } String url = String(request->arg("url")); - settings.tinance2_url_validatecard = url; - settings.saveSetting("t2", "vcurl", settings.tinance2_url_validatecard, "string"); + settings.tinance2_url = url; + settings.saveSetting("t2", "rooturl", settings.tinance2_url, "string"); AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Tinance URL updated\"}"); request->send(response); }); + _server->on("/settings/tinanceurl", 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/validatecardurl", HTTP_GET, [&] (AsyncWebServerRequest *request) { if(_authRequired){ @@ -231,6 +241,189 @@ class MainWebServerClass{ }); + _server->on("/settings/readerinfourl", 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/aclsurl", 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/logurl", 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) { + String value; + + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } + + if (request->hasParam("value")) { + value = request->getParam("value")->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) { + String value; + + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } + + if (request->hasParam("value")) { + value = request->getParam("value")->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) { + String value; + + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } + + if (request->hasParam("value")) { + value = request->getParam("value")->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) { + String value; + + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } + + if (request->hasParam("value")) { + value = request->getParam("value")->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); + } + }); } diff --git a/include/settings.h b/include/settings.h index 07e078b..c55ec8d 100644 --- a/include/settings.h +++ b/include/settings.h @@ -16,7 +16,7 @@ private: String doorMode; public: Settings(); - + String tinance2_url; String tinance2_url_validatecard; String tinance2_url_readerinfo; String tinance2_url_acls; diff --git a/src/settings.cpp b/src/settings.cpp index f7e72a7..7f9b7ff 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -14,12 +14,11 @@ void Settings::loadFromEEPROM() { settings_preferences.begin("t2", false); tinance2ReaderIdentifier = settings_preferences.getString("ri", "default"); tinance2ReaderKey = settings_preferences.getString("rk", "default"); - tinance2_url_validatecard = settings_preferences.getString("vcurl","https://tinance2.board.techinc.nl/accesscontrol/api/check-card-id"); - tinance2_url_readerinfo = settings_preferences.getString("riurl","https://tinance2.board.techinc.nl/accesscontrol/api/readerinfo"); - tinance2_url_acls = settings_preferences.getString("aclurl","https://tinance2.board.techinc.nl/accesscontrol/api/acls"); - tinance2_url_log = settings_preferences.getString("rluri","https://tinance2.board.techinc.nl/accesscontrol/api/readerlog"); - - + tinance2_url = settings_preferences.getString("rooturl","https://tinance2.board.techinc.nl"); + tinance2_url_validatecard = settings_preferences.getString("vcurl",tinance2_url+"/accesscontrol/api/check-card-id"); + tinance2_url_readerinfo = settings_preferences.getString("riurl",tinance2_url+"/accesscontrol/api/readerinfo"); + tinance2_url_acls = settings_preferences.getString("aclurl",tinance2_url+"/accesscontrol/api/acls"); + tinance2_url_log = settings_preferences.getString("rluri",tinance2_url+"/accesscontrol/api/readerlog"); settings_preferences.end(); settings_preferences.begin("wh", false); diff --git a/src/tinance2.cpp b/src/tinance2.cpp index d69d945..a481f18 100644 --- a/src/tinance2.cpp +++ b/src/tinance2.cpp @@ -85,7 +85,7 @@ HTTPClient tinance2_http; String payload = "{\"message\":\"" + String(message) + "\"}"; // Send the HTTP request and get the response Tinance2HttpClient httpClient; - std::pair responsePair = httpClient.sendHttpRequest(tinance2_url_log, "POST", payload); + std::pair responsePair = httpClient.sendHttpRequest(settings.tinance2_url_log, "POST", payload); String response = responsePair.first; int httpResponseCode = responsePair.second; @@ -119,7 +119,7 @@ HTTPClient tinance2_http; String payload = "{\"full_card_id\":\"" + String(fullCardID) + "\"}"; // Send the HTTP request and get the response Tinance2HttpClient httpClient; - std::pair responsePair = httpClient.sendHttpRequest(tinance2_url_validatecard, "POST", payload); + std::pair responsePair = httpClient.sendHttpRequest(settings.tinance2_url_validatecard, "POST", payload); String response = responsePair.first; int httpResponseCode = responsePair.second; @@ -184,7 +184,7 @@ while (true) { if (WiFi.status() == WL_CONNECTED) { Tinance2HttpClient httpClient; - std::pair responsePair = httpClient.sendHttpRequest(tinance2_url_readerinfo, "GET", ""); + std::pair responsePair = httpClient.sendHttpRequest(settings.tinance2_url_readerinfo, "GET", ""); String response = responsePair.first; int httpResponseCode = responsePair.second;