Implemented new CSV based USERDB
This commit is contained in:
		
							parent
							
								
									1494f00abf
								
							
						
					
					
						commit
						57a48839bc
					
				| @ -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(){ | User UserDb::user_by_pin(String cmp) | ||||||
|     Serial.println("Starting UserDB Read"); | { | ||||||
|     File userdb_file = LittleFS.open("userdb.csv","r"); |     Serial.println("Stearching for user with pin number " + cmp); | ||||||
|     if (!userdb_file) |     Iterator it = begin(); | ||||||
|  |     do | ||||||
|     { |     { | ||||||
|         Serial.println("Failed to open file for reading"); |         User temp = *it; | ||||||
|         return; |         if (temp.user_pin == cmp) | ||||||
|     } |             return temp; | ||||||
|     unsigned int line = 0; |         ++it; | ||||||
|     while(userdb_file.available()){ |     } while (it.has_next()); | ||||||
|         String temp = userdb_file.readStringUntil('\n'); |     return User{.enabled = false}; | ||||||
|          | } | ||||||
|         Serial.println(read_csv_line(temp).toString()); | User UserDb::user_by_rfid(String cmp) | ||||||
|         line++; | { | ||||||
|     } |     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()); | ||||||
|  | } | ||||||
|  | |||||||
							
								
								
									
										130
									
								
								src/UserDb.h
									
									
									
									
									
								
							
							
						
						
									
										130
									
								
								src/UserDb.h
									
									
									
									
									
								
							| @ -2,33 +2,131 @@ | |||||||
| #include "Persistence.h" | #include "Persistence.h" | ||||||
| #include "LittleFS.h" | #include "LittleFS.h" | ||||||
| #include <sstream> | #include <sstream> | ||||||
|  | #include <iterator> | ||||||
|  | #include <cstddef> | ||||||
|  | 
 | ||||||
| namespace userdb | 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 |     class UserDb | ||||||
|     { |     { | ||||||
|     private: |     private: | ||||||
|          |  | ||||||
| 
 |  | ||||||
|     public: |     public: | ||||||
|         UserDb(); |         UserDb(); | ||||||
|         void test(); |         void print_to_serial(); | ||||||
|         void PrintAll(); |         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){}
 |             int locations[5]; | ||||||
|             unsigned long uid; |             int location_index = 0; | ||||||
|             mutable String first_name; |             int str_length = instring.length() - 1; | ||||||
|             mutable String last_name; |             for (int i = 0; i < str_length; i++) | ||||||
|             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"); |                 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")); } | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| @ -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());
 | ||||||
| 	 | 	 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user