Implemented new CSV based USERDB

This commit is contained in:
Jean Jacques Avril 2022-02-16 17:07:29 +01:00
parent 1494f00abf
commit 57a48839bc
3 changed files with 179 additions and 55 deletions

View File

@ -2,44 +2,70 @@
using namespace userdb; using namespace userdb;
UserDb::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<str_length;i++){
if(instring.charAt(i) == ','){
locations[location_index]=i;
location_index++;
}
}
UserDb::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;
return res;
}
void UserDb::test(){
Serial.println("Starting UserDB Read");
File userdb_file = LittleFS.open("userdb.csv","r");
if (!userdb_file)
{ {
Serial.println("Failed to open file for reading");
return;
}
unsigned int line = 0;
while(userdb_file.available()){
String temp = userdb_file.readStringUntil('\n');
Serial.println(read_csv_line(temp).toString());
line++;
}
} }
User UserDb::user_by_pin(String cmp)
{
Serial.println("Stearching for user with pin number " + cmp);
Iterator it = begin();
do
{
User temp = *it;
if (temp.user_pin == cmp)
return temp;
++it;
} while (it.has_next());
return User{.enabled = false};
}
User UserDb::user_by_rfid(String cmp)
{
Serial.println("Stearching for user with rfid " + cmp);
Iterator it = begin();
do
{
User temp = *it;
if (temp.rfid_uid == cmp)
return temp;
++it;
} while (it.has_next());
return User{.enabled = false};
}
User UserDb::user_by_uid(unsigned long cmp){
Serial.println("Stearching for user with user id " + String(cmp));
Iterator it = begin();
do
{
User temp = *it;
if (temp.uid == cmp)
return temp;
++it;
} while (it.has_next());
return User{.enabled = false};
}
void UserDb::print_to_serial()
{
Serial.println("Starting UserDB Test Read");
// File userdb_file = LittleFS.open("userdb.csv","r");
// if (!userdb_file)
//{
// Serial.println("Failed to open file for reading");
// return;
// }
// unsigned int line = 0;
// while(userdb_file.available()){
// String temp = userdb_file.readStringUntil('\n');
// User res = read_csv_line(temp);
// Serial.println(res.toString());
// line++;
// }
Iterator it = begin();
do
{
User temp = *it;
Serial.println(temp.toString());
++it;
} while (it.has_next());
}

View File

@ -2,21 +2,26 @@
#include "Persistence.h" #include "Persistence.h"
#include "LittleFS.h" #include "LittleFS.h"
#include <sstream> #include <sstream>
#include <iterator>
#include <cstddef>
namespace userdb namespace userdb
{ {
class UserDb enum USERATTRIBUTES
{ {
private: LINE_ID,
USER_ID,
FIRST_NAME,
public: LAST_NAME,
UserDb(); RFID_UID,
void test(); USER_PIN,
void PrintAll(); ENABLED
};
struct User struct User
{ {
// User(unsigned long new_uid): uid(new_uid){} // User(unsigned long new_uid): uid(new_uid){}
unsigned long uid; unsigned long uid;
unsigned int line;
mutable String first_name; mutable String first_name;
mutable String last_name; mutable String last_name;
mutable String rfid_uid; mutable String rfid_uid;
@ -29,6 +34,99 @@ namespace userdb
return "ID: " + String(uid) + " Name: " + last_name + " " + first_name + " Enabled: " + (enabled ? "Yes" : "No"); return "ID: " + String(uid) + " Name: " + last_name + " " + first_name + " Enabled: " + (enabled ? "Yes" : "No");
} }
}; };
User read_csv_line(String);
class UserDb
{
private:
public:
UserDb();
void print_to_serial();
void PrintAll();
// 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)
{
int locations[5];
int location_index = 0;
int str_length = instring.length() - 1;
for (int i = 0; i < str_length; i++)
{
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;
};
Iterator begin() { return Iterator(LittleFS.open("userdb.csv", "r")); }
}; };
} }

View File

@ -37,7 +37,7 @@ void setup()
Serial.print("Starting"); Serial.print("Starting");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!"); Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.println(WiFi.softAP(config.SSID) ? "Ready" : "Failed!"); Serial.println(WiFi.softAP(config.SSID) ? "Ready" : "Failed!");
userdatabase.test(); userdatabase.print_to_serial();
//delay(500); //delay(500);
keyboard.begin(&Wire); keyboard.begin(&Wire);
rfid.begin(); rfid.begin();
@ -45,10 +45,10 @@ void setup()
//while(users.countUsers()<100){ //while(users.countUsers()<100){
// users.addUser("Harry","Potter","","1234"); // users.addUser("Harry","Potter","","1234");
//} //}
Serial.println(userdatabase.user_by_pin("12348").toString());
//users.PrintAllToSerial(); //users.PrintAllToSerial();
//Serial.println(persistence.TestRead()); //Serial.println(persistence.TestRead());
users.ExportToPersistence(); //users.ExportToPersistence();
//Serial.println(persistence.TestRead()); //Serial.println(persistence.TestRead());
} }