From 76da74aa612cbdbaef24a4a82f82326e489e7c4e Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Sat, 13 Apr 2024 23:51:35 +0200 Subject: [PATCH] reader identity in settings --- include/aclwebserver.h | 6 +++++- include/mainwebserver.h | 41 ++++++++++++++++++++++++++++++++++++++++- include/settings.h | 10 ++++++++++ src/settings.cpp | 35 +++++++++++++++++++++++++++++++++-- src/tinance2.cpp | 4 ++-- 5 files changed, 90 insertions(+), 6 deletions(-) diff --git a/include/aclwebserver.h b/include/aclwebserver.h index a25f6d5..e04edd2 100644 --- a/include/aclwebserver.h +++ b/include/aclwebserver.h @@ -177,9 +177,13 @@ class ACLWebServerClass{ acl.removeUser(cardId); acl.saveToEEPROM(); request->send(201); // Created - }); + }); + } + + + // deprecated, keeping for backward compatibility void loop() { } diff --git a/include/mainwebserver.h b/include/mainwebserver.h index 2da360a..26278f1 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.1.9\"}"); + request->send(200, "application/json", "{\"version\":\"3.2.3\"}"); }); _server->onNotFound([&](AsyncWebServerRequest *request){ @@ -153,6 +153,45 @@ class MainWebServerClass{ request->send(response); } }); + + + _server->on("/settings/get/tinanceCreds", 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", "{\"identifier\":\""+String(settings.getTinance2ReaderIdentifier())+"\", \"key\":\""+String(settings.getTinance2ReaderKey())+"\"}"); + request->send(response); + }); + + _server->on("/settings/set/tinanceCreds", HTTP_GET, [&] (AsyncWebServerRequest *request) { + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } + if(request->hasParam("identifier", true)) {} //This is important, otherwise the sketch will crash if there is no body + else { + AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"No identifier\"}"); + request->send(response); + } + String identifier = String(request->arg("identifier")); + + if(request->hasParam("key", true)) {} //This is important, otherwise the sketch will crash if there is no body + else { + AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"No key\"}"); + request->send(response); + } + String key = String(request->arg("key")); + + settings.setTinance2ReaderIdentifier(identifier); + settings.setTinance2ReaderKey(key); + + AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Tinance credentials updated\"}"); + request->send(response); + + }); } // deprecated, keeping for backward compatibility diff --git a/include/settings.h b/include/settings.h index 13dbd55..02c68f1 100644 --- a/include/settings.h +++ b/include/settings.h @@ -14,18 +14,28 @@ class Settings { private: bool disableDoor; String doorMode; + String tinance2ReaderIdentifier; + String tinance2ReaderKey; public: Settings(); void loadFromEEPROM(); void saveToEEPROM(); + void saveStringToEEPROM(String string, String value); + void saveBoolToEEPROM(String string, String value); void setDisableDoor(bool value); bool getDoorDisabled(); void setDoorMode(String value); String getDoorMode(); + + void setTinance2ReaderIdentifier(String value); + String getTinance2ReaderIdentifier(); + + void setTinance2ReaderKey(String value); + String getTinance2ReaderKey(); }; extern Settings settings; diff --git a/src/settings.cpp b/src/settings.cpp index ebae80a..597b2aa 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -6,17 +6,30 @@ Settings::Settings() : disableDoor(false), doorMode("LATCH") { } void Settings::loadFromEEPROM() { - settings_preferences.begin("settings"); + settings_preferences.begin("settings", false); disableDoor = settings_preferences.getBool("disableDoor", false); doorMode = settings_preferences.getString("doorMode", "LATCH"); settings_preferences.end(); + + settings_preferences.begin("t2", false); + tinance2ReaderIdentifier = settings_preferences.getString("ri", "default"); + tinance2ReaderKey = settings_preferences.getString("rk", "default"); + settings_preferences.end(); } void Settings::saveToEEPROM() { - settings_preferences.begin("settings"); + 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) { @@ -65,4 +78,22 @@ String Settings::getDoorMode() { return doorMode; } +void Settings::setTinance2ReaderIdentifier(String value) { + tinance2ReaderIdentifier = value; + saveToEEPROM(); + +} +String Settings::getTinance2ReaderIdentifier() { + return tinance2ReaderIdentifier; +} + +void Settings::setTinance2ReaderKey(String value){ + tinance2ReaderKey = value; + saveToEEPROM(); + +} +String Settings::getTinance2ReaderKey(){ + return tinance2ReaderKey; +} + Settings settings; \ No newline at end of file diff --git a/src/tinance2.cpp b/src/tinance2.cpp index 7f18af8..fe70010 100644 --- a/src/tinance2.cpp +++ b/src/tinance2.cpp @@ -17,8 +17,8 @@ HTTPClient tinance2_http; // Set the headers tinance2_http.addHeader("Content-Type", "application/json"); - tinance2_http.addHeader("X-Identifier", tinance2_reader_identifer); - tinance2_http.addHeader("X-Access-Key", tinance2_reader_key); + tinance2_http.addHeader("X-Identifier", settings.getTinance2ReaderIdentifier()); + tinance2_http.addHeader("X-Access-Key", settings.getTinance2ReaderKey()); // Set the HTTP timeout to 5 seconds tinance2_http.setTimeout(5000);