Save
This commit is contained in:
		
							parent
							
								
									b6b4786b87
								
							
						
					
					
						commit
						c91ab95a59
					
				
							
								
								
									
										64
									
								
								src/UserDb.h
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								src/UserDb.h
									
									
									
									
									
								
							| @ -22,7 +22,7 @@ namespace userdb | ||||
|     { | ||||
|         // User(unsigned long new_uid): uid(new_uid){}
 | ||||
|         unsigned long uid; | ||||
|         unsigned int line; | ||||
|         uint32_t line; | ||||
|         mutable String first_name; | ||||
|         mutable String last_name; | ||||
|         mutable String rfid_uid; | ||||
| @ -47,7 +47,7 @@ namespace userdb | ||||
|         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, String *match, USERATTRIBUTES attr) | ||||
|         static User read_csv_line(String &instring, uint32_t line, String *match, USERATTRIBUTES attr) | ||||
|         { | ||||
|             int locations[5]; | ||||
|             int location_index = 0; | ||||
| @ -87,7 +87,65 @@ namespace userdb | ||||
|             res.line = line; | ||||
|             return res; | ||||
|         } | ||||
|         static User read_csv_line(String &instring, uint32_t line) | ||||
|         { | ||||
|             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.rfid_uid = instring.substring(locations[2] + 1, locations[3]); | ||||
|             res.user_pin = instring.substring(locations[3] + 1, locations[4]); | ||||
|             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.enabled = instring.charAt(locations[4] + 1) == '1' ? true : false; | ||||
|             res.line = line; | ||||
|             return res; | ||||
|         } | ||||
|         static bool remove_user(User &user) | ||||
|         { | ||||
|             return remove_user_by_line("userdb.csv", user.line); | ||||
|         } | ||||
|         static bool remove_user_by_line(const char *file, uint32_t line) | ||||
|         { | ||||
|             const char *temp_file = ".tmp.csv"; | ||||
|             File old_db = LittleFS.open(file, "r"); | ||||
|             File new_db = LittleFS.open(temp_file, "w+"); | ||||
|             uint32_t curr_line = 0; | ||||
| 
 | ||||
|             while (old_db.available()) | ||||
|             { | ||||
|                 if (curr_line == line) | ||||
|                     while (old_db.read() != '\n') | ||||
|                     { | ||||
|                     } | ||||
|                 else | ||||
|                 { | ||||
|                     if (new_db.position() > 0) | ||||
|                         new_db.write('\n'); | ||||
|                     old_db.sendUntil(new_db, '\n'); | ||||
|                 } | ||||
|                 curr_line++; | ||||
|             } | ||||
|             old_db.close(); | ||||
| #ifdef DEBUG | ||||
|             new_db.seek(0); | ||||
|             while (new_db.available()) | ||||
|                 new_db.sendAvailable(Serial); | ||||
| #endif | ||||
|             new_db.close(); | ||||
|             LittleFS.remove(file); | ||||
|             LittleFS.rename(temp_file, file); | ||||
|             return false; | ||||
|         } | ||||
|         struct Iterator | ||||
|         { | ||||
|             using iterator_category = std::input_iterator_tag; | ||||
| @ -156,7 +214,7 @@ namespace userdb | ||||
|             USERATTRIBUTES filter_attr; | ||||
|             String *match; | ||||
|             bool available = true; | ||||
|             unsigned int line = 0; | ||||
|             uint32_t line = 0; | ||||
|         }; | ||||
|         Iterator begin() { return Iterator(LittleFS.open("userdb.csv", "r")); } | ||||
|         Iterator begin_with_filter(String *match, USERATTRIBUTES attr) { return Iterator(LittleFS.open("userdb.csv", "r"), match, attr); } | ||||
|  | ||||
| @ -47,6 +47,7 @@ void setup() | ||||
| 	keyboard.begin(&Wire); | ||||
| 	rfid.begin(); | ||||
| 	iface.begin(&keyboard); | ||||
| 	userdatabase.remove_user_by_line("userdb.csv",2); | ||||
| } | ||||
| 
 | ||||
| void loop() | ||||
| @ -56,21 +57,27 @@ void loop() | ||||
| 	userdb::User login_user; | ||||
| 	if (iface.pinAvailable()) | ||||
| 	{ | ||||
| 		unsigned long delta = millis(); | ||||
| 		login_user = userdatabase.user_by_pin(iface.getPin()); | ||||
| 		Serial.println("Query duration: " +String(millis()-delta)); | ||||
| 		if (login_user.enabled == false) | ||||
| 		{ | ||||
| 			iface.showMessage("Login failed!", "-> Pin incorrect", 3000); | ||||
| 			return; | ||||
| 		} | ||||
| 
 | ||||
| 	} | ||||
| 	else if (rfid.available()) | ||||
| 	{ | ||||
| 		unsigned long delta = millis(); | ||||
| 		login_user = userdatabase.user_by_rfid(rfid.getID()); | ||||
| 		Serial.println("Query duration: " +String(millis()-delta)); | ||||
| 		if (login_user.enabled == false) | ||||
| 		{ | ||||
| 			iface.showMessage("Login failed!", "-> Unkown Card", 3000); | ||||
| 			return; | ||||
| 		} | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 	if (login_user.enabled == true) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user