public static List <FingerAccount> parseJson() { DirectoryInfo directoryInfo = new DirectoryInfo(Environment.CurrentDirectory); var fil = Log.QueryMultiChoice("Json database file?", new List <string>(directoryInfo.EnumerateFiles().Select(n => n.Name)) { "None" }); if (fil == "None") { return(fingerAccounts); } JObject jObject = null; try { jObject = JObject.Parse(File.ReadAllText(fil)); } catch (JsonReaderException) { Log.Warning("Invalid Json file"); } catch (FileNotFoundException) { Log.Warning("File no longer exists"); } catch (Exception e) { Log.Error("Parsing", e); } if (jObject == null) { return(fingerAccounts); } try { var ffff = new List <FingerAccount>(); foreach (JObject val in (JArray)jObject["users"]) { var usr = new FingerAccount((string)val["name"], (int)val["templateId"]); usr._totalTime = new TimeSpan((long)val["totalTime"]); usr.creationDate = (DateTime)val["creationDate"]; ffff.Add(usr); } var sele = Log.QueryMultiChoice("Database Merge Type", new List <string>() { "Preserve duplicates", "Renew database", "Append existing" }); if (sele == "Preserve duplicates") { ffff.AddRange(fingerAccounts); return(ffff); } else if (sele == "Renew database") { return(ffff); } else if (sele == "Append existing") { ffff.RemoveAll(n => fingerAccounts.Any(m => m.templateId.Equals(n.templateId))); fingerAccounts.AddRange(ffff); return(fingerAccounts); } } catch (Exception) { Log.Error("Could not parse Json"); } return(new List <FingerAccount>()); }
static void Main(string[] args) { while (arduino == null) { string port = Log.QueryMultiChoice("Port name?", SerialPort.GetPortNames().ToList()); ushort baud = Convert.ToUInt16(Log.QueryMultiChoice("Baud rate?", new List <string>() { "9600", "57600" })); arduino = new SerialPort(port, baud); try { arduino.Open(); Task.Run(() => ListenPort(arduino)); } catch (Exception e) { Log.Error("Could not open " + port, e); arduino = null; } } while (HostName == null) { HostName = Log.Query("Hostname?", "iansweb.org"); if (!(HostName.Length > 1)) { Console.WriteLine("Invalid Address"); HostName = null; } } Console.WriteLine($"COM Port: {arduino.PortName}"); Console.WriteLine($"Baud Rate: {arduino.BaudRate}"); Console.WriteLine($"HostName: {HostName}"); Console.WriteLine($"Port: {Port}"); Task.Run(StartWebServer); fingerRelay = new FingerRelay(arduino); fingerAccounts = parseJson(); Task.Run(AutoSaveDatabase); Log.Info("Autosaving databases every " + new TimeSpan(0, 0, 600000).Minutes + " minutes"); while (true) { string choice = Log.QueryMultiChoice("Main Menu", mainMenuItems.ToList()); if (choice == mainMenuItems[0]) { //View keys foreach (var fingeraccount in fingerAccounts) { Console.WriteLine($"{fingeraccount.Name}, {fingeraccount.loggedIn}, {fingeraccount.templateId}"); } Console.ReadKey(true); } else if (choice == mainMenuItems[1]) { //Add key int id = 0; while (id == 0) { if (Log.QueryBool("Create new scan?")) { id = FingerWizard.CreateAScan(); } else { try { id = Convert.ToInt16(Log.Query("Template Id", (fingerRelay.TemplateCount + 1).ToString())); } catch (Exception) { Console.WriteLine("Invalid Integer"); } } } var newuser = new FingerAccount(Log.Query("User's Name", ("user" + (fingerRelay.TemplateCount + 1).ToString())), id); fingerAccounts.Add(newuser); } else if (choice == mainMenuItems[2]) { //Log In var users = fingerAccounts.Where(n => !(n.loggedIn)); if (users.Count() > 0) { string user = Log.QueryMultiChoice("User to log in?", users.Select(n => n.Name).ToList()); var matches = fingerAccounts.Where(n => n.Name == user && !(n.loggedIn)); if (matches.Count() > 0) { Console.WriteLine($"Logged in the first account that has the name of {matches.First().Name}"); matches.First().LogIn(); } else { Console.WriteLine("No matches found"); } } else { Console.WriteLine("No users are logged out"); } } else if (choice == mainMenuItems[3]) { //Log Out var users = fingerAccounts.Where(n => n.loggedIn); if (users.Count() > 0) { string user = Log.QueryMultiChoice("User to log out?", users.Select(n => n.Name).ToList()); var matches = fingerAccounts.Where(n => n.Name == user && n.loggedIn); if (matches.Count() > 0) { Console.WriteLine($"Logged out the first account that has the name of {matches.First().Name}"); matches.First().LogOut(); } else { Console.WriteLine("No matches found"); } } else { Console.WriteLine("No users are logged in"); } } else if (choice == mainMenuItems[4]) { if (fingerAccounts.Count > 0) { //DirectoryInfo directoryInfo = new DirectoryInfo("."); //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(new System.Drawing.Bitmap(Log.QueryMultiChoice("Source bitmap?", directoryInfo.EnumerateFiles().Where(n => n.Name.EndsWith(".bmp")).Select(n => n.Name).ToList()))); //var finger = FingerOperations.GetFingerAccount(bitmap, fingerAccounts); //Console.WriteLine($"{finger.Name}, {finger.loggedIn}"); Log.Info("Place thumb on scanner"); while (true) { var scan = FingerWizard.Scan(false, false); if (scan < 0) { Log.Info("Scan failed"); } else { if (fingerAccounts.Any(n => n.templateId == scan)) { var acc = fingerAccounts.Where(n => n.templateId == scan).First(); Log.Success("Scan found " + acc.Name + ", template id " + scan); acc.ToggleLogINOUT(); if (acc.loggedIn) { Log.Info("Logged in"); } else { Log.Info("Logged out"); } } else { Log.Info($"No accounts found for this scan Id:{scan}"); } } } } else { Console.WriteLine("No fingers for comparison!"); } } else if (choice == mainMenuItems[5]) { if (fingerRelay != null) { Console.WriteLine($"{fingerRelay.TemplateCount} Templates"); } else { Console.WriteLine("FingerRelay non-existant"); } } else if (choice == mainMenuItems[6]) { var dele = Log.QueryMultiChoice("Clear what?", new List <string>() { "Users", "Templates", "Both", "Go back" }); switch (dele) { case "Users": fingerAccounts = new List <FingerAccount>(); break; case "Templates": fingerRelay.SendCommandGetData(FingerRelay.Commands.CLEAR_DATABASE); break; case "Both": fingerAccounts = new List <FingerAccount>(); fingerRelay.SendCommandGetData(FingerRelay.Commands.CLEAR_DATABASE); break; case "Go back": break; } } else if (choice == mainMenuItems[7]) { SaveJson().GetAwaiter(); } else if (choice == mainMenuItems[8]) { FingerWizard.DeleteWizard(); } } }