started implementing the csv parser in order to achieve more runtime and memory efficency
This commit is contained in:
parent
24657d8ac6
commit
1494f00abf
10
data/userdb.csv
Normal file
10
data/userdb.csv
Normal file
@ -0,0 +1,10 @@
|
||||
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
|
|
45
src/UserDb.cpp
Normal file
45
src/UserDb.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "UserDb.h"
|
||||
|
||||
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<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++;
|
||||
}
|
||||
}
|
||||
|
34
src/UserDb.h
Normal file
34
src/UserDb.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include "Persistence.h"
|
||||
#include "LittleFS.h"
|
||||
#include <sstream>
|
||||
namespace userdb
|
||||
{
|
||||
class UserDb
|
||||
{
|
||||
private:
|
||||
|
||||
|
||||
public:
|
||||
UserDb();
|
||||
void test();
|
||||
void PrintAll();
|
||||
struct User
|
||||
{
|
||||
// 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()
|
||||
{
|
||||
return "ID: " + String(uid) + " Name: " + last_name + " " + first_name + " Enabled: " + (enabled ? "Yes" : "No");
|
||||
}
|
||||
};
|
||||
User read_csv_line(String);
|
||||
};
|
||||
}
|
11
src/main.cpp
11
src/main.cpp
@ -7,9 +7,11 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <vector>
|
||||
#include "Users.h"
|
||||
#include "UserDb.h"
|
||||
#include "Persistence.h"
|
||||
// File persistence
|
||||
Persistence persistence;
|
||||
userdb::UserDb userdatabase;
|
||||
// Rfid
|
||||
Rfid rfid;
|
||||
// i2C Bus
|
||||
@ -35,15 +37,16 @@ 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();
|
||||
//delay(500);
|
||||
keyboard.begin(&Wire);
|
||||
rfid.begin();
|
||||
iface.begin(&keyboard);
|
||||
while(users.countUsers()<100){
|
||||
users.addUser("Harry","Potter","","1234");
|
||||
}
|
||||
//while(users.countUsers()<100){
|
||||
// users.addUser("Harry","Potter","","1234");
|
||||
//}
|
||||
|
||||
users.PrintAllToSerial();
|
||||
//users.PrintAllToSerial();
|
||||
//Serial.println(persistence.TestRead());
|
||||
users.ExportToPersistence();
|
||||
//Serial.println(persistence.TestRead());
|
||||
|
Loading…
x
Reference in New Issue
Block a user