From ab49a21f8542989828797c65db077bbdeee1a863 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Mon, 15 Apr 2024 23:02:32 +0200 Subject: [PATCH] fixed up settings --- data/index.html | 38 +++++++++++++++++-- include/main.h | 4 +- include/mainwebserver.h | 84 ++++++++++++++++++++--------------------- include/secrets.h | 8 ---- include/settings.h | 2 - src/settings.cpp | 24 ++---------- 6 files changed, 80 insertions(+), 80 deletions(-) diff --git a/data/index.html b/data/index.html index 857ac56..f41ca65 100644 --- a/data/index.html +++ b/data/index.html @@ -149,17 +149,23 @@ Controls the GPIO pin - /state/relay1 + /state/relay GET N/A Returns the state of relay1 - /settings/doordisabled + /settings/relaydisabled GET, POST value - Gets or sets the door disabled value + Gets or sets the relay disabled value + + /settings/relaymode + GET, POST + value + Gets or sets the relay mode currently "LATCH" or "TOGGLE" + /settings/tinance2creds GET, POST @@ -220,6 +226,32 @@ value Gets or sets the webhook unlock hook + + /users/create + GET, POST + cardid, desc + Creates a local / backup user + + + /users/update + GET, POST + cardid, newCardId, desc + updates a local / backup user if cardid is the same set newCardId to same value. + + + /users/remove + GET, POST + cardid + removes a local / backup user + + + /users/users + GET, POST + N/A + lists all local / backup user + + + diff --git a/include/main.h b/include/main.h index 60434d5..c190c98 100644 --- a/include/main.h +++ b/include/main.h @@ -10,9 +10,7 @@ #ifdef WIFI #include "WiFi.h" - #ifdef WEBHOOKS - #include "webhooks.h" - #endif + #include "webhooks.h" #endif #ifdef LOCAL_ACL diff --git a/include/mainwebserver.h b/include/mainwebserver.h index 1aef68c..1c19d81 100644 --- a/include/mainwebserver.h +++ b/include/mainwebserver.h @@ -46,7 +46,7 @@ class MainWebServerClass{ return request->requestAuthentication(); } } - request->send(200, "application/json", "{\"version\":\"3.5.6\"}"); + request->send(200, "application/json", "{\"version\":\"3.5.7\"}"); }); _server->onNotFound([&](AsyncWebServerRequest *request){ @@ -58,48 +58,8 @@ class MainWebServerClass{ request->send(200, "application/json", "{\"msg\":\"The content you are looking for was not found\"}"); }); - _server->on("/gpio", HTTP_POST, [&] (AsyncWebServerRequest *request) { - - if(_authRequired){ - if(!request->authenticate(_username.c_str(), _password.c_str())){ - return request->requestAuthentication(); - } - } - - // GET input1 value on /gpio?output=&state= - if (request->hasParam("output", true) && request->hasParam("state", true)) { - String paramOutput = String(request->arg("output")); - String paramState = String(request->arg("state")); - - #ifdef RELAY1 - if (paramOutput == "relay1") { - if (paramState == String(0)){ - unlockDoor(false); - AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Door Unlocked.\"}"); - request->send(response); - } - else if (paramState == String(1)) { - lockDoor(false); - AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Door Locked.\"}"); - request->send(response); - } - else { - AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Incorect state provide 0 to unlock and 1 to lock.\"}"); - request->send(response); - } - } - - - #endif - - AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"Error with request, incorrect GPIO pin number.\"}"); - request->send(response); - - } - }); - #ifdef RELAY1 - _server->on("/state/relay1", HTTP_GET, [&] (AsyncWebServerRequest *request) { + _server->on("/state/relay", HTTP_GET, [&] (AsyncWebServerRequest *request) { if(_authRequired){ if(!request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); @@ -110,7 +70,7 @@ class MainWebServerClass{ }); #endif - _server->on("/settings/doordisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/relaydisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { if(_authRequired){ if(!request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); @@ -120,7 +80,7 @@ class MainWebServerClass{ request->send(response); }); - _server->on("/settings/doordisabled", HTTP_POST, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/relaydisabled", HTTP_POST, [&] (AsyncWebServerRequest *request) { if(_authRequired){ if(!request->authenticate(_username.c_str(), _password.c_str())){ @@ -152,6 +112,42 @@ class MainWebServerClass{ } }); + _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){ diff --git a/include/secrets.h b/include/secrets.h index 163c11b..ee4880c 100644 --- a/include/secrets.h +++ b/include/secrets.h @@ -20,12 +20,4 @@ extern const char* http_password; #endif - #ifdef WEBHOOKS - #ifdef WEBHOOK_UNLOCK - extern const char* webhook_unlock_url; - #endif - #ifdef WEBHOOK_LOCK - extern const char* webhook_lock_url; - #endif - #endif #endif \ No newline at end of file diff --git a/include/settings.h b/include/settings.h index c55ec8d..652b4d3 100644 --- a/include/settings.h +++ b/include/settings.h @@ -31,8 +31,6 @@ public: void loadFromEEPROM(); void saveToEEPROM(); void saveSetting(const String& namespaceStr, const String& key, const String& value, const String& typeStr); - void saveStringToEEPROM(String string, String value); - void saveBoolToEEPROM(String string, String value); void setDisableDoor(bool value); bool getDoorDisabled(); diff --git a/src/settings.cpp b/src/settings.cpp index 7f9b7ff..d664c64 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -33,7 +33,7 @@ void Settings::saveSetting(const String& namespaceStr, const String& key, const if (typeStr == "bool") { bool currentValue = settings_preferences.getBool(key.c_str()); - bool newValue = (value == "true"); + bool newValue = value; if (currentValue != newValue) { settings_preferences.begin(namespaceStr.c_str(), false); settings_preferences.putBool(key.c_str(), newValue); @@ -68,22 +68,6 @@ void Settings::saveSetting(const String& namespaceStr, const String& key, const } - -void Settings::saveToEEPROM() { - settings_preferences.begin("settings", false); - settings_preferences.putBool("disableDoor", disableDoor); - settings_preferences.putString("doorMode", doorMode); - settings_preferences.end(); - - settings_preferences.begin("t2", false); - settings_preferences.putString("ri", tinance2ReaderIdentifier); - settings_preferences.putString("rk", tinance2ReaderKey); - settings_preferences.end(); - - // Save the settings to EEPROM - -} - void Settings::setDisableDoor(bool value) { if (disableDoor != value) { disableDoor = value; @@ -95,10 +79,10 @@ void Settings::setDisableDoor(bool value) { #endif #ifdef SERIAL_DEBUG - Serial.print("Settings::setDisableDoor: "); + Serial.print("Settings::setDisableDoor: " + value); Serial.println(disableDoor); #endif - saveToEEPROM(); + saveSetting("settings", "disableDoor", String(disableDoor), "bool"); } } @@ -121,7 +105,7 @@ void Settings::setDoorMode(const String mode) { Serial.print("Settings::setDoorMode: "); Serial.println(doorMode); #endif - saveToEEPROM(); + saveSetting("settings", "doorMode", doorMode, "string"); } } }