Adjusted logic to use new csv storage

This commit is contained in:
Jean Jacques Avril 2022-02-16 23:08:28 +01:00
parent 57a48839bc
commit fc44a3fd8d
7 changed files with 1275 additions and 56 deletions

File diff suppressed because it is too large Load Diff

10
data/userdb.csv.bak Normal file
View 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

View File

@ -84,7 +84,9 @@ void Keyboard::scanColumn(uint8_t *key_ptr, uint8_t start, uint8_t stop)
_timeElapsed = millis();
this->_lastKey = *key_ptr;
this->_buffer.push_back(mapChr(*key_ptr));
#ifdef DEBUG
Serial.print(*key_ptr);
#endif
return;
}
}

View File

@ -32,7 +32,9 @@ void Rfid::scan()
if (this->_rfid != this->_lastRfid || millis() > this->_lastRfidScan + RFID_TIMEOUT)
{
_status = 1;
#ifdef DEBUG
Serial.print(this->_rfid);
#endif
this->_lastRfid = this->_rfid;
this->_lastRfidScan = millis();
}

View File

@ -48,24 +48,8 @@ User UserDb::user_by_uid(unsigned long cmp){
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
{
for(Iterator it = begin(); it.has_next(); ++it){
User temp = *it;
Serial.println(temp.toString());
++it;
} while (it.has_next());
}
}

View File

@ -26,7 +26,7 @@ namespace userdb
mutable String last_name;
mutable String rfid_uid;
mutable String user_pin;
mutable bool enabled = true;
mutable bool enabled = false;
bool operator<(const User &o) const { return uid < o.uid; }
bool operator==(const User &o) const { return uid == o.uid; }
String toString()
@ -53,7 +53,7 @@ namespace userdb
int str_length = instring.length() - 1;
for (int i = 0; i < str_length; i++)
{
if (instring.charAt(i) == ',')
if (instring.charAt(i) == ';')
{
locations[location_index] = i;
location_index++;
@ -106,11 +106,11 @@ namespace userdb
}
bool has_next()
{
return db_file.available();
return available;
}
void next()
{
if (has_next())
if (db_file.available())
{
String temp = db_file.readStringUntil('\n');
current = read_csv_line(temp, line);
@ -118,6 +118,8 @@ namespace userdb
}
else
{
available=false;
current=User{};
db_file.close();
}
};
@ -125,6 +127,7 @@ namespace userdb
private:
File db_file;
User current;
bool available = true;
unsigned int line = 0;
};
Iterator begin() { return Iterator(LittleFS.open("userdb.csv", "r")); }

View File

@ -6,7 +6,7 @@
#include "Interface.h"
#include <ESP8266WiFi.h>
#include <vector>
#include "Users.h"
//#include "Users.h"
#include "UserDb.h"
#include "Persistence.h"
// File persistence
@ -27,46 +27,56 @@ 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!");
else
Serial.println(WiFi.softAP(config.SSID) ? "Ready" : "Failed!");
delay(250);
#ifdef DEBUG
userdatabase.print_to_serial();
//delay(500);
#endif
keyboard.begin(&Wire);
rfid.begin();
iface.begin(&keyboard);
//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();
//Serial.println(persistence.TestRead());
}
void loop()
{
keyboard.scanAsync();
rfid.scan();
std::vector<Users::User> logon_users;
if (iface.pinAvailable() && !users.checkPin(iface.getPin(), &logon_users))
iface.showMessage("Login failed!", "-> Pin incorrect", 3000);
else if (rfid.available() && !users.checkRfid(rfid.getID(), &logon_users))
iface.showMessage("Login failed!", "-> Unkown Card", 3000);
else if (logon_users.size() > 0)
userdb::User login_user;
if (iface.pinAvailable())
{
iface.greetUser(logon_users[0].first_name + " " + logon_users[0].last_name);
Serial.print(users.toString(&logon_users[0]));
logon_users.clear();
login_user = userdatabase.user_by_pin(iface.getPin());
if (login_user.enabled == false)
{
iface.showMessage("Login failed!", "-> Pin incorrect", 3000);
return;
}
}
else if (rfid.available())
{
login_user = userdatabase.user_by_rfid(rfid.getID());
if (login_user.enabled == false)
{
iface.showMessage("Login failed!", "-> Unkown Card", 3000);
return;
}
}
if (login_user.enabled == true)
{
iface.greetUser(login_user.first_name + " " + login_user.last_name);
Serial.println("Logon from User " + login_user.toString());
}
iface.render();
}