using internal settings system
This commit is contained in:
parent
b276e849af
commit
905907d8e4
6 changed files with 117 additions and 35 deletions
43
.vscode/settings.json
vendored
43
.vscode/settings.json
vendored
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -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:
|
||||
|
|
|
@ -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){
|
||||
|
|
|
@ -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
|
||||
|
|
57
src/main.cpp
57
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<String>());
|
||||
#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
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue