Implemented new CSV based USERDB
This commit is contained in:
		
							parent
							
								
									1494f00abf
								
							
						
					
					
						commit
						57a48839bc
					
				| @ -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<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; | ||||
| UserDb::UserDb() | ||||
| { | ||||
| } | ||||
| 
 | ||||
| void UserDb::test(){ | ||||
|     Serial.println("Starting UserDB Read"); | ||||
|     File userdb_file = LittleFS.open("userdb.csv","r"); | ||||
|     if (!userdb_file) | ||||
| User UserDb::user_by_pin(String cmp) | ||||
| { | ||||
|     Serial.println("Stearching for user with pin number " + cmp); | ||||
|     Iterator it = begin(); | ||||
|     do | ||||
|     { | ||||
|         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 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()); | ||||
| } | ||||
|  | ||||
							
								
								
									
										116
									
								
								src/UserDb.h
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								src/UserDb.h
									
									
									
									
									
								
							| @ -2,21 +2,26 @@ | ||||
| #include "Persistence.h" | ||||
| #include "LittleFS.h" | ||||
| #include <sstream> | ||||
| #include <iterator> | ||||
| #include <cstddef> | ||||
| 
 | ||||
| namespace userdb | ||||
| { | ||||
|     class UserDb | ||||
|     enum USERATTRIBUTES | ||||
|     { | ||||
|     private: | ||||
|          | ||||
| 
 | ||||
|     public: | ||||
|         UserDb(); | ||||
|         void test(); | ||||
|         void PrintAll(); | ||||
|         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; | ||||
| @ -29,6 +34,99 @@ namespace userdb | ||||
|             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")); } | ||||
|     }; | ||||
| } | ||||
| @ -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());
 | ||||
| 	 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user