From b6b4786b87eebcd994754ea1b3e0b28b41d21ad3 Mon Sep 17 00:00:00 2001 From: Jean Jacques Avril Date: Thu, 17 Feb 2022 00:48:11 +0100 Subject: [PATCH] Enhanced iterator with a filter option in order to copy the whole object data only when it is required (improves performance a little bit on huge databases 1000+) --- data/config.json | 2 +- data/helloWorld.txt | 1 - data/userdb.csv.bak | 10 ---------- data/users.json | 34 -------------------------------- src/UserDb.cpp | 14 +++++++------- src/UserDb.h | 47 ++++++++++++++++++++++++++++++++++++--------- src/main.cpp | 14 +++++++------- 7 files changed, 53 insertions(+), 69 deletions(-) delete mode 100644 data/helloWorld.txt delete mode 100644 data/userdb.csv.bak delete mode 100644 data/users.json diff --git a/data/config.json b/data/config.json index 8827aad..79531f6 100644 --- a/data/config.json +++ b/data/config.json @@ -1,5 +1,5 @@ { "THEMEID": 1, "SSID":"DoorLock", - "PASS":"geheim" + "PASS":"geheim123" } \ No newline at end of file diff --git a/data/helloWorld.txt b/data/helloWorld.txt deleted file mode 100644 index 30d74d2..0000000 --- a/data/helloWorld.txt +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/data/userdb.csv.bak b/data/userdb.csv.bak deleted file mode 100644 index 37c5072..0000000 --- a/data/userdb.csv.bak +++ /dev/null @@ -1,10 +0,0 @@ -1,Max1,Muster,RFID,12341,1 -2,Max2,Muster,RFID,12342,1 -3,Max3,Muster,RFID,12343,1 -4,Max4,Muster,RFID,12344,1 -5,Max5,Muster,RFID,12345,1 -6,Max6,Muster,RFID,12346,1 -7,Max7,Muster,RFID,12347,1 -8,Max8,Muster,RFID,12348,1 -9,Max9,Muster,RFID,12349,1 -10,Max20,Muster,RFID,12340,1 \ No newline at end of file diff --git a/data/users.json b/data/users.json deleted file mode 100644 index 4f2d344..0000000 --- a/data/users.json +++ /dev/null @@ -1,34 +0,0 @@ -[ - { - "ID": 1, - "FIRST_NAME": "Duplicate", - "LAST_NAME": "Test", - "RFID_UID": "", - "USER_PIN": 2323, - "ENABLED": true - }, - { - "ID": 5, - "FIRST_NAME": "JSON", - "LAST_NAME": "User1", - "RFID_UID": "", - "USER_PIN": 123123, - "ENABLED": true - }, - { - "ID": 6, - "FIRST_NAME": "JSON2", - "LAST_NAME": "User2", - "RFID_UID": "", - "USER_PIN": 321, - "ENABLED": true - }, - { - "ID": 7, - "FIRST_NAME": "JSON3", - "LAST_NAME": "User3", - "RFID_UID": "", - "USER_PIN": 147, - "ENABLED": true - } -] diff --git a/src/UserDb.cpp b/src/UserDb.cpp index 881da6a..1bc3b17 100644 --- a/src/UserDb.cpp +++ b/src/UserDb.cpp @@ -8,12 +8,12 @@ UserDb::UserDb() User UserDb::user_by_pin(String cmp) { - Serial.println("Stearching for user with pin number " + cmp); - Iterator it = begin(); + Serial.println("Searching for user with pin number " + cmp); + Iterator it = begin_with_filter(&cmp,USERATTRIBUTES::USER_PIN); do { User temp = *it; - if (temp.user_pin == cmp) + if (temp.enabled) return temp; ++it; } while (it.has_next()); @@ -21,12 +21,12 @@ User UserDb::user_by_pin(String cmp) } User UserDb::user_by_rfid(String cmp) { - Serial.println("Stearching for user with rfid " + cmp); - Iterator it = begin(); + Serial.println("Searching for user with rfid " + cmp); + Iterator it = begin_with_filter(&cmp,USERATTRIBUTES::RFID_UID); do { User temp = *it; - if (temp.rfid_uid == cmp) + if (temp.enabled) return temp; ++it; } while (it.has_next()); @@ -34,7 +34,7 @@ User UserDb::user_by_rfid(String cmp) } User UserDb::user_by_uid(unsigned long cmp){ - Serial.println("Stearching for user with user id " + String(cmp)); + Serial.println("Searching for user with user id " + String(cmp)); Iterator it = begin(); do { diff --git a/src/UserDb.h b/src/UserDb.h index 4d7c6e0..754960d 100644 --- a/src/UserDb.h +++ b/src/UserDb.h @@ -15,7 +15,8 @@ namespace userdb LAST_NAME, RFID_UID, USER_PIN, - ENABLED + ENABLED, + NONE }; struct User { @@ -46,7 +47,7 @@ namespace userdb User user_by_pin(String); User user_by_rfid(String); User user_by_uid(unsigned long); - static User read_csv_line(String instring, int line = -1) + static User read_csv_line(String &instring, int line, String *match, USERATTRIBUTES attr) { int locations[5]; int location_index = 0; @@ -60,11 +61,28 @@ namespace userdb } } User res; + if (attr == RFID_UID) + { + res.rfid_uid = instring.substring(locations[2] + 1, locations[3]); + if (!res.rfid_uid.equals(*match)) + return res; + res.user_pin = instring.substring(locations[3] + 1, locations[4]); + } + else if (attr == USER_PIN) + { + res.user_pin = instring.substring(locations[3] + 1, locations[4]); + if (!res.user_pin.equals(*match)) + return res; + res.rfid_uid = instring.substring(locations[2] + 1, locations[3]); + } + else + { + res.rfid_uid = instring.substring(locations[2] + 1, locations[3]); + res.user_pin = instring.substring(locations[3] + 1, locations[4]); + } res.uid = instring.substring(0, locations[0]).toInt(); res.first_name = instring.substring(locations[0] + 1, locations[1]); res.last_name = instring.substring(locations[1] + 1, locations[2]); - res.rfid_uid = instring.substring(locations[2] + 1, locations[3]); - res.user_pin = instring.substring(locations[3] + 1, locations[4]); res.enabled = instring.charAt(locations[4] + 1) == '1' ? true : false; res.line = line; return res; @@ -78,8 +96,16 @@ namespace userdb using reference = User; using difference_type = std::ptrdiff_t; - Iterator(File data) : db_file(data) + Iterator(File data) : Iterator(data, nullptr, NONE) { + } + Iterator(File data, String *m, USERATTRIBUTES attr) : db_file(data), filter_attr(attr), match(m) + { + if (filter_attr == NONE) + Serial.println("Started user iterator with "); + else + Serial.println("Started filtered user iterator with " + String(filter_attr) + " query: " + *match); + if (!data) { Serial.println("Failed to open userdb while creating an iterator"); @@ -113,23 +139,26 @@ namespace userdb if (db_file.available()) { String temp = db_file.readStringUntil('\n'); - current = read_csv_line(temp, line); + current = read_csv_line(temp, line, match, filter_attr); line++; } else { - available=false; - current=User{}; + available = false; + current = User{}; db_file.close(); } - }; + } private: File db_file; User current; + USERATTRIBUTES filter_attr; + String *match; bool available = true; unsigned int line = 0; }; Iterator begin() { return Iterator(LittleFS.open("userdb.csv", "r")); } + Iterator begin_with_filter(String *match, USERATTRIBUTES attr) { return Iterator(LittleFS.open("userdb.csv", "r"), match, attr); } }; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 454ed81..5719248 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,19 +27,19 @@ IPAddress gateway(192, 168, 4, 9); IPAddress subnet(255, 255, 255, 0); // User DB -//Users users(persistence); +// Users users(persistence); void setup() { Persistence::Configuration config = persistence.loadConfig(); - //users.ImportFromPersistence(); + // users.ImportFromPersistence(); Serial.begin(9600); - Serial.print("Starting"); - Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!"); - if (strlen(config.PASS)>0) - Serial.println(WiFi.softAP(config.SSID, config.PASS) ? "Ready" : "Failed!"); + Serial.println("Starting System"); + Serial.println("\t1. Network config: " + WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!"); + if (strlen(config.PASS) > 0) + Serial.println("\t1. AP setup with password: " + WiFi.softAP(config.SSID, config.PASS) ? "Ready" : "Failed!"); else - Serial.println(WiFi.softAP(config.SSID) ? "Ready" : "Failed!"); + Serial.println("\t1. AP setup without password: " +WiFi.softAP(config.SSID) ? "Ready" : "Failed!"); delay(250); #ifdef DEBUG userdatabase.print_to_serial();