using internal settings system

This commit is contained in:
Matthew Frost 2023-12-28 10:14:36 +01:00
parent b276e849af
commit 905907d8e4
6 changed files with 117 additions and 35 deletions

43
.vscode/settings.json vendored
View file

@ -8,6 +8,47 @@
"vector": "cpp", "vector": "cpp",
"string_view": "cpp", "string_view": "cpp",
"initializer_list": "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"
} }
} }

View file

@ -281,8 +281,8 @@ resources:
parentId: fld_fa644d484dd64e78b90e3df0c486e7d6 parentId: fld_fa644d484dd64e78b90e3df0c486e7d6
modified: 1686678985051 modified: 1686678985051
created: 1686676992334 created: 1686676992334
url: http://{{ _.url }}/settings/get/DoorDisabled url: http://{{ _.url }}/settings/get/getDoorDisabled
name: settings - get - DoorDisabled name: settings - get - getDoorDisabled
description: "" description: ""
method: GET method: GET
body: body:
@ -323,8 +323,8 @@ resources:
parentId: fld_fa644d484dd64e78b90e3df0c486e7d6 parentId: fld_fa644d484dd64e78b90e3df0c486e7d6
modified: 1686690326021 modified: 1686690326021
created: 1686676506109 created: 1686676506109
url: http://{{ _.url }}/settings/set/DoorDisabled?value=1 url: http://{{ _.url }}/settings/set/getDoorDisabled?value=1
name: settings - set - DoorDisabled name: settings - set - getDoorDisabled
description: "" description: ""
method: GET method: GET
body: body:

View file

@ -103,17 +103,17 @@ class MainWebServerClass{
}); });
#endif #endif
_server->on("/settings/get/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { _server->on("/settings/get/getDoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) {
if(_authRequired){ if(_authRequired){
if(!request->authenticate(_username.c_str(), _password.c_str())){ if(!request->authenticate(_username.c_str(), _password.c_str())){
return request->requestAuthentication(); 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); request->send(response);
}); });
_server->on("/settings/set/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { _server->on("/settings/set/getDoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) {
String value; String value;
if(_authRequired){ if(_authRequired){

View file

@ -6,6 +6,7 @@
class Settings { class Settings {
private: private:
bool disableDoor; bool disableDoor;
String doorMode;
public: public:
Settings(); Settings();
@ -14,7 +15,10 @@ public:
void saveToEEPROM(); void saveToEEPROM();
void setDisableDoor(bool value); void setDisableDoor(bool value);
bool DoorDisabled(); bool getDoorDisabled();
void setDoorMode(String value);
String getDoorMode();
}; };
#endif // SETTINGS_H #endif // SETTINGS_H

View file

@ -16,19 +16,19 @@ void localAcl(String cardID) {
Serial.println("LOCAL_AUTH: Access granted!"); Serial.println("LOCAL_AUTH: Access granted!");
#endif #endif
// Perform actions for authorized access // Perform actions for authorized access
#ifdef LATCH_DOOR if (settings.getDoorMode() == "LATCH") {
if (!settings.DoorDisabled()) { if (!settings.getDoorDisabled()) {
unlockDoor(false); unlockDoor(false);
delay(RELAY_DELAY); delay(RELAY_DELAY);
lockDoor(false); lockDoor(false);
} }
#endif }
#ifdef TOGGLE_DOOR if (settings.getDoorMode() == "TOGGLE") {
if (!settings.DoorDisabled()) { if (!settings.getDoorDisabled()) {
toggleDoor(); toggleDoor();
delay(RELAY_DELAY); delay(RELAY_DELAY);
} }
#endif }
} else { } else {
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
Serial.println("LOCAL_AUTH: Access denied!"); Serial.println("LOCAL_AUTH: Access denied!");
@ -277,19 +277,19 @@ void handleData1Interrupt() {
// Check the response code // Check the response code
if (httpResponseCode == 200) { if (httpResponseCode == 200) {
Serial.println("Tinance2 Door Access Granted"); Serial.println("Tinance2 Door Access Granted");
#ifdef LATCH_DOOR if (settings.getDoorMode() == "LATCH") {
if (!settings.DoorDisabled()) { if (!settings.getDoorDisabled()) {
unlockDoor(false); unlockDoor(false);
delay(RELAY_DELAY); delay(RELAY_DELAY);
lockDoor(false); lockDoor(false);
} }
#endif }
#ifdef TOGGLE_DOOR if (settings.getDoorMode() == "TOGGLE") {
if (!settings.DoorDisabled()) { if (!settings.getDoorDisabled()) {
toggleDoor(); toggleDoor();
delay(RELAY_DELAY); delay(RELAY_DELAY);
} }
#endif }
} else if (httpResponseCode == 400 || httpResponseCode == 401 || httpResponseCode == 402 || httpResponseCode == 403) { } else if (httpResponseCode == 400 || httpResponseCode == 401 || httpResponseCode == 402 || httpResponseCode == 403) {
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
Serial.println("Tinance2 Door Access Denied"); Serial.println("Tinance2 Door Access Denied");
@ -341,6 +341,15 @@ void handleData1Interrupt() {
Serial.println("JSON Reader Enabled: " + json["enabled"].as<String>()); Serial.println("JSON Reader Enabled: " + json["enabled"].as<String>());
#endif #endif
} }
if (json.containsKey("mode")) {
String doorMode = json["mode"].as<String>();
settings.setDoorMode(doorMode);
#ifdef SERIAL_DEBUG
Serial.println("JSON Reader Mode: " + json["mode"].as<String>());
#endif
}
} }
} }
#endif #endif
@ -598,41 +607,41 @@ void setup() {
#ifdef TINANCE2_BACKEND #ifdef TINANCE2_BACKEND
bool previousDoorDisabled = false; // Declare the missing variable bool previousgetDoorDisabled = false; // Declare the missing variable
bool isFirstRun = true; // Declare the missing variable bool isFirstRun = true; // Declare the missing variable
void updateDoorStatus() { void updateDoorStatus() {
bool previousDoorDisabled = false; // Declare the missing variable bool previousgetDoorDisabled = false; // Declare the missing variable
bool isFirstRun = true; // Declare the missing variable bool isFirstRun = true; // Declare the missing variable
if (settings.DoorDisabled() != previousDoorDisabled && !isFirstRun) { if (settings.getDoorDisabled() != previousgetDoorDisabled && !isFirstRun) {
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
Serial.print("DoorDisabled setting changed to: "); Serial.print("getDoorDisabled setting changed to: ");
Serial.println(settings.DoorDisabled()); Serial.println(settings.getDoorDisabled());
Serial.print("Previous DoorDisabled setting: "); Serial.print("Previous getDoorDisabled setting: ");
Serial.println(previousDoorDisabled); Serial.println(previousgetDoorDisabled);
Serial.print("isFirstRun: "); Serial.print("isFirstRun: ");
Serial.println(isFirstRun); Serial.println(isFirstRun);
#endif #endif
if (settings.DoorDisabled()) { if (settings.getDoorDisabled()) {
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
Serial.println("Disabling Door due to DoorDisabled setting"); Serial.println("Disabling Door due to getDoorDisabled setting");
#endif #endif
unlockDoor(false); // Provide the required argument unlockDoor(false); // Provide the required argument
} else { } else {
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
Serial.println("Enabling Door due to DoorDisabled setting"); Serial.println("Enabling Door due to getDoorDisabled setting");
#endif #endif
lockDoor(false); lockDoor(false);
} }
} }
// Update the previousDoorDisabled variable // Update the previousgetDoorDisabled variable
previousDoorDisabled = settings.DoorDisabled(); previousgetDoorDisabled = settings.getDoorDisabled();
isFirstRun = false; isFirstRun = false;
} }

View file

@ -6,18 +6,30 @@ Preferences settings_preferences;
Settings::Settings() { Settings::Settings() {
// Set default value for disableDoor // Set default value for disableDoor
disableDoor = false; 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() { void Settings::loadFromEEPROM() {
settings_preferences.begin("settings"); settings_preferences.begin("settings");
disableDoor = settings_preferences.getBool("disableDoor", false); 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(); settings_preferences.end();
} }
void Settings::saveToEEPROM() { void Settings::saveToEEPROM() {
settings_preferences.begin("settings"); settings_preferences.begin("settings");
settings_preferences.putBool("disableDoor", disableDoor); settings_preferences.putBool("disableDoor", disableDoor);
settings_preferences.putString("doorMode", doorMode); // Save doorMode to EEPROM
settings_preferences.end(); settings_preferences.end();
} }
@ -32,6 +44,22 @@ void Settings::setDisableDoor(bool value) {
} }
} }
bool Settings::DoorDisabled() { bool Settings::getDoorDisabled() {
return disableDoor; 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;
}