From 57a48839bcb6e53cb744609cea456f38acb429c2 Mon Sep 17 00:00:00 2001 From: Jean Jacques Avril Date: Wed, 16 Feb 2022 17:07:29 +0100 Subject: [PATCH] Implemented new CSV based USERDB --- src/UserDb.cpp | 98 +++++++++++++++++++++++-------------- src/UserDb.h | 130 +++++++++++++++++++++++++++++++++++++++++++------ src/main.cpp | 6 +-- 3 files changed, 179 insertions(+), 55 deletions(-) diff --git a/src/UserDb.cpp b/src/UserDb.cpp index d176975..9d7f458 100644 --- a/src/UserDb.cpp +++ b/src/UserDb.cpp @@ -2,44 +2,70 @@ using namespace userdb; -UserDb::UserDb(){ - -} -UserDb::User UserDb::read_csv_line(String instring){ - int locations[5]; - int location_index=0; - int str_length = instring.length()-1; - for(int i = 0; i +#include +#include + namespace userdb { + enum USERATTRIBUTES + { + LINE_ID, + USER_ID, + FIRST_NAME, + LAST_NAME, + RFID_UID, + USER_PIN, + ENABLED + }; + struct User + { + // User(unsigned long new_uid): uid(new_uid){} + unsigned long uid; + unsigned int line; + mutable String first_name; + mutable String last_name; + mutable String rfid_uid; + mutable String user_pin; + mutable bool enabled = true; + bool operator<(const User &o) const { return uid < o.uid; } + bool operator==(const User &o) const { return uid == o.uid; } + String toString() + { + return "ID: " + String(uid) + " Name: " + last_name + " " + first_name + " Enabled: " + (enabled ? "Yes" : "No"); + } + }; + class UserDb { private: - - public: UserDb(); - void test(); + void print_to_serial(); void PrintAll(); - struct User + // User find_user(); + 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) { - // User(unsigned long new_uid): uid(new_uid){} - unsigned long uid; - mutable String first_name; - mutable String last_name; - mutable String rfid_uid; - mutable String user_pin; - mutable bool enabled = true; - bool operator<(const User &o) const { return uid < o.uid; } - bool operator==(const User &o) const { return uid == o.uid; } - String toString() + int locations[5]; + int location_index = 0; + int str_length = instring.length() - 1; + for (int i = 0; i < str_length; i++) { - return "ID: " + String(uid) + " Name: " + last_name + " " + first_name + " Enabled: " + (enabled ? "Yes" : "No"); + if (instring.charAt(i) == ',') + { + locations[location_index] = i; + location_index++; + } } + User res; + 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; + } + + struct Iterator + { + using iterator_category = std::input_iterator_tag; + using pointer = void; + using value_type = User; + using reference = User; + using difference_type = std::ptrdiff_t; + + Iterator(File data) : db_file(data) + { + if (!data) + { + Serial.println("Failed to open userdb while creating an iterator"); + } + else + { + next(); + } + } + reference operator*() const { return current; } + Iterator &operator++() + { + next(); + return *this; + } + Iterator operator++(int) + { + next(); + return *this; + } + ~Iterator() + { + db_file.close(); + } + bool has_next() + { + return db_file.available(); + } + void next() + { + if (has_next()) + { + String temp = db_file.readStringUntil('\n'); + current = read_csv_line(temp, line); + line++; + } + else + { + db_file.close(); + } + }; + + private: + File db_file; + User current; + unsigned int line = 0; }; - User read_csv_line(String); + Iterator begin() { return Iterator(LittleFS.open("userdb.csv", "r")); } }; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b52249a..ba4cdad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,7 +37,7 @@ void setup() Serial.print("Starting"); Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!"); Serial.println(WiFi.softAP(config.SSID) ? "Ready" : "Failed!"); - userdatabase.test(); + userdatabase.print_to_serial(); //delay(500); keyboard.begin(&Wire); rfid.begin(); @@ -45,10 +45,10 @@ void setup() //while(users.countUsers()<100){ // users.addUser("Harry","Potter","","1234"); //} - + Serial.println(userdatabase.user_by_pin("12348").toString()); //users.PrintAllToSerial(); //Serial.println(persistence.TestRead()); - users.ExportToPersistence(); + //users.ExportToPersistence(); //Serial.println(persistence.TestRead()); }