From 905907d8e46ae9fa41b0a5a823e55d42dad0b78d Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Thu, 28 Dec 2023 10:14:36 +0100 Subject: [PATCH] using internal settings system --- .vscode/settings.json | 43 +++++++++++++++++++++++- Insomnia-local-acl-api-spec.yaml | 8 ++--- include/mainwebserver.h | 6 ++-- include/settings.h | 6 +++- src/main.cpp | 57 ++++++++++++++++++-------------- src/settings.cpp | 32 ++++++++++++++++-- 6 files changed, 117 insertions(+), 35 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d4f336c..41636b3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,47 @@ "vector": "cpp", "string_view": "cpp", "initializer_list": "cpp", - "regex": "cpp" + "regex": "cpp", + "atomic": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" } } \ No newline at end of file diff --git a/Insomnia-local-acl-api-spec.yaml b/Insomnia-local-acl-api-spec.yaml index 9e1f4fa..5258a24 100644 --- a/Insomnia-local-acl-api-spec.yaml +++ b/Insomnia-local-acl-api-spec.yaml @@ -281,8 +281,8 @@ resources: parentId: fld_fa644d484dd64e78b90e3df0c486e7d6 modified: 1686678985051 created: 1686676992334 - url: http://{{ _.url }}/settings/get/DoorDisabled - name: settings - get - DoorDisabled + url: http://{{ _.url }}/settings/get/getDoorDisabled + name: settings - get - getDoorDisabled description: "" method: GET body: @@ -323,8 +323,8 @@ resources: parentId: fld_fa644d484dd64e78b90e3df0c486e7d6 modified: 1686690326021 created: 1686676506109 - url: http://{{ _.url }}/settings/set/DoorDisabled?value=1 - name: settings - set - DoorDisabled + url: http://{{ _.url }}/settings/set/getDoorDisabled?value=1 + name: settings - set - getDoorDisabled description: "" method: GET body: diff --git a/include/mainwebserver.h b/include/mainwebserver.h index ed39944..76bbecb 100644 --- a/include/mainwebserver.h +++ b/include/mainwebserver.h @@ -103,17 +103,17 @@ class MainWebServerClass{ }); #endif - _server->on("/settings/get/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/get/getDoorDisabled", 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.DoorDisabled())+"\"}"); + AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+String(settings.getDoorDisabled())+"\"}"); request->send(response); }); - _server->on("/settings/set/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { + _server->on("/settings/set/getDoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { String value; if(_authRequired){ diff --git a/include/settings.h b/include/settings.h index 8cc3dda..7f1ed85 100644 --- a/include/settings.h +++ b/include/settings.h @@ -6,6 +6,7 @@ class Settings { private: bool disableDoor; + String doorMode; public: Settings(); @@ -14,7 +15,10 @@ public: void saveToEEPROM(); void setDisableDoor(bool value); - bool DoorDisabled(); + bool getDoorDisabled(); + + void setDoorMode(String value); + String getDoorMode(); }; #endif // SETTINGS_H diff --git a/src/main.cpp b/src/main.cpp index f074202..6eaa0be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,19 +16,19 @@ void localAcl(String cardID) { Serial.println("LOCAL_AUTH: Access granted!"); #endif // Perform actions for authorized access - #ifdef LATCH_DOOR - if (!settings.DoorDisabled()) { + if (settings.getDoorMode() == "LATCH") { + if (!settings.getDoorDisabled()) { unlockDoor(false); delay(RELAY_DELAY); lockDoor(false); } - #endif - #ifdef TOGGLE_DOOR - if (!settings.DoorDisabled()) { + } + if (settings.getDoorMode() == "TOGGLE") { + if (!settings.getDoorDisabled()) { toggleDoor(); delay(RELAY_DELAY); } - #endif + } } else { #ifdef SERIAL_DEBUG Serial.println("LOCAL_AUTH: Access denied!"); @@ -277,19 +277,19 @@ void handleData1Interrupt() { // Check the response code if (httpResponseCode == 200) { Serial.println("Tinance2 Door Access Granted"); - #ifdef LATCH_DOOR - if (!settings.DoorDisabled()) { + if (settings.getDoorMode() == "LATCH") { + if (!settings.getDoorDisabled()) { unlockDoor(false); delay(RELAY_DELAY); lockDoor(false); } - #endif - #ifdef TOGGLE_DOOR - if (!settings.DoorDisabled()) { + } + if (settings.getDoorMode() == "TOGGLE") { + if (!settings.getDoorDisabled()) { toggleDoor(); delay(RELAY_DELAY); } - #endif + } } else if (httpResponseCode == 400 || httpResponseCode == 401 || httpResponseCode == 402 || httpResponseCode == 403) { #ifdef SERIAL_DEBUG Serial.println("Tinance2 Door Access Denied"); @@ -341,6 +341,15 @@ void handleData1Interrupt() { Serial.println("JSON Reader Enabled: " + json["enabled"].as()); #endif } + + if (json.containsKey("mode")) { + String doorMode = json["mode"].as(); + settings.setDoorMode(doorMode); + #ifdef SERIAL_DEBUG + Serial.println("JSON Reader Mode: " + json["mode"].as()); + #endif + } + } } #endif @@ -598,41 +607,41 @@ void setup() { #ifdef TINANCE2_BACKEND - bool previousDoorDisabled = false; // Declare the missing variable + bool previousgetDoorDisabled = false; // Declare the missing variable bool isFirstRun = true; // Declare the missing variable void updateDoorStatus() { - bool previousDoorDisabled = false; // Declare the missing variable + bool previousgetDoorDisabled = false; // Declare the missing variable bool isFirstRun = true; // Declare the missing variable - if (settings.DoorDisabled() != previousDoorDisabled && !isFirstRun) { + if (settings.getDoorDisabled() != previousgetDoorDisabled && !isFirstRun) { #ifdef SERIAL_DEBUG - Serial.print("DoorDisabled setting changed to: "); - Serial.println(settings.DoorDisabled()); - Serial.print("Previous DoorDisabled setting: "); - Serial.println(previousDoorDisabled); + Serial.print("getDoorDisabled setting changed to: "); + Serial.println(settings.getDoorDisabled()); + Serial.print("Previous getDoorDisabled setting: "); + Serial.println(previousgetDoorDisabled); Serial.print("isFirstRun: "); Serial.println(isFirstRun); #endif - if (settings.DoorDisabled()) { + if (settings.getDoorDisabled()) { #ifdef SERIAL_DEBUG - Serial.println("Disabling Door due to DoorDisabled setting"); + Serial.println("Disabling Door due to getDoorDisabled setting"); #endif unlockDoor(false); // Provide the required argument } else { #ifdef SERIAL_DEBUG - Serial.println("Enabling Door due to DoorDisabled setting"); + Serial.println("Enabling Door due to getDoorDisabled setting"); #endif lockDoor(false); } } - // Update the previousDoorDisabled variable - previousDoorDisabled = settings.DoorDisabled(); + // Update the previousgetDoorDisabled variable + previousgetDoorDisabled = settings.getDoorDisabled(); isFirstRun = false; } diff --git a/src/settings.cpp b/src/settings.cpp index d28d2c9..d320bfc 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -6,18 +6,30 @@ Preferences settings_preferences; Settings::Settings() { // Set default value for disableDoor disableDoor = false; - + #ifdef LATCH_DOOR + doorMode = "LATCH"; // Set default value for doorMode + #endif + #ifdef TOGGLE_DOOR + doorMode = "TOGGLE"; // Set default value for doorMode + #endif } void Settings::loadFromEEPROM() { settings_preferences.begin("settings"); disableDoor = settings_preferences.getBool("disableDoor", false); + #ifdef LATCH_DOOR + doorMode = settings_preferences.getString("doorMode", "LATCH"); // Load doorMode from EEPROM + #endif + #ifdef TOGGLE_DOOR + doorMode = settings_preferences.getString("doorMode", "TOGGLE"); // Load doorMode from EEPROM + #endif settings_preferences.end(); } void Settings::saveToEEPROM() { settings_preferences.begin("settings"); settings_preferences.putBool("disableDoor", disableDoor); + settings_preferences.putString("doorMode", doorMode); // Save doorMode to EEPROM settings_preferences.end(); } @@ -32,6 +44,22 @@ void Settings::setDisableDoor(bool value) { } } -bool Settings::DoorDisabled() { +bool Settings::getDoorDisabled() { return disableDoor; } + +void Settings::setDoorMode(const String mode) { + if (mode == "LATCH" || mode == "TOGGLE") { + if (doorMode != mode) { + doorMode = mode; + #ifdef SERIAL_DEBUG + Serial.print("Settings::setDoorMode: "); + Serial.println(doorMode.c_str()); + #endif + saveToEEPROM(); // Save the setting immediately to Preferences + } + } +} +String Settings::getDoorMode() { + return doorMode; +} \ No newline at end of file