diff --git a/Views/SettingsDisplay.cs b/Views/SettingsDisplay.cs index b72d113..ecd1dfb 100644 --- a/Views/SettingsDisplay.cs +++ b/Views/SettingsDisplay.cs @@ -60,7 +60,7 @@ namespace Akari.Provider.WaveshareUART.Views { _port = _radioPort.RadioLabels[_radioPort.SelectedItem].ToString(); - File.WriteAllText(TUIManager.SettingsFilePath, _port); + File.WriteAllText(WaveshareUARTProvider.SerialPortFilePath, _port); } } } diff --git a/Views/TUIManager.cs b/Views/TUIManager.cs index 12607c7..e56b716 100644 --- a/Views/TUIManager.cs +++ b/Views/TUIManager.cs @@ -1,5 +1,7 @@ using Akari.Provider.WaveshareUART.Users; +using System; using System.IO; +using System.Net; using System.Threading.Tasks; using Terminal.Gui; using WaveshareUARTFingerprintSensor; @@ -8,8 +10,6 @@ namespace Akari.Provider.WaveshareUART.Views { public class TUIManager : Toplevel { - public const string SettingsFilePath = "settings.txt"; - public WaveshareUARTProvider Provider => WaveshareUARTProvider.Instance; public FingerprintSensor FingerprintSensor => Provider.FingerprintSensor; public UsersManager UsersManager => Provider.UsersManager; @@ -17,6 +17,7 @@ namespace Akari.Provider.WaveshareUART.Views private Label _serialPortLabel; private Label _comparisonLevelLabel; private Label _userCountLabel; + private Label _serverIPLabel; private bool _needToClear; public TUIManager() @@ -72,11 +73,17 @@ namespace Akari.Provider.WaveshareUART.Views Y = Pos.Bottom(_comparisonLevelLabel), Width = Dim.Fill() }; + _serverIPLabel = new Label("Server IP: N/A") + { + X = Pos.Left(_comparisonLevelLabel), + Y = Pos.Bottom(_userCountLabel), + Width = Dim.Fill() + }; var manageUsersButton = new Button("_Manage Users") { X = Pos.Center(), - Y = Pos.Center() - 1 + Y = Pos.Center() - 2 }; manageUsersButton.Clicked += ManageUsersButton_Clicked; @@ -87,6 +94,20 @@ namespace Akari.Provider.WaveshareUART.Views }; setComparisonLevelButton.Clicked += SetComparisonLevelButton_Clicked; + var setServerIP = new Button("Set Server _IP") + { + X = Pos.Center(), + Y = Pos.Bottom(setComparisonLevelButton) + 1 + }; + setServerIP.Clicked += SetServerIP_Clicked; + + var setServerPort = new Button("Set Server _Port") + { + X = Pos.Center(), + Y = Pos.Bottom(setServerIP) + }; + setServerPort.Clicked += SetServerPort_Clicked; + var quitButton = new Button("_Quit") { X = Pos.Center(), @@ -98,13 +119,16 @@ namespace Akari.Provider.WaveshareUART.Views _serialPortLabel, _comparisonLevelLabel, _userCountLabel, + _serverIPLabel, manageUsersButton, setComparisonLevelButton, + setServerIP, + setServerPort, quitButton ); // Init Sensor - if (!File.Exists(SettingsFilePath)) + if (!File.Exists(WaveshareUARTProvider.SerialPortFilePath)) { _needToClear = true; @@ -127,6 +151,7 @@ namespace Akari.Provider.WaveshareUART.Views UpdateSerialPort(); UpdateUserCount(); UpdateComparisonLevel(); + UpdateServerIP(); } private void UpdateSerialPort() @@ -147,6 +172,14 @@ namespace Akari.Provider.WaveshareUART.Views } } + private void UpdateServerIP() + { + if (File.Exists(WaveshareUARTProvider.ServerIPFilePath) && File.Exists(WaveshareUARTProvider.ServerPortFilePath)) + { + _serverIPLabel.Text = $"Server IP: {File.ReadAllText(WaveshareUARTProvider.ServerIPFilePath)}:{File.ReadAllText(WaveshareUARTProvider.ServerPortFilePath)}"; + } + } + private void SetComparisonLevelButton_Clicked() { if (new EntryDialog("Comparison Level", i => int.TryParse(i.ToString(), out var n) && n > 0 && n < 10, "Need to be a valid number in 0-9 range").TryShow(out var input)) @@ -160,56 +193,24 @@ namespace Akari.Provider.WaveshareUART.Views UpdateComparisonLevel(); } - private void AddUserButton_Clicked() + private void SetServerIP_Clicked() { - if (new EntryDialog("User id", i => ushort.TryParse(i.ToString(), out var n), "Need to be a valid user id").TryShow(out var input)) + if (new EntryDialog("Server IP", i => Uri.CheckHostName(i.ToString()) != UriHostNameType.Unknown, "Need to be a valid ip address").TryShow(out var input)) { - var dialog = new FingerprintDialog("Add Fingerprint", "Can't add fingerprint, try to place your finger flat on the sensor"); - - Task.Run(() => - { - switch (FingerprintSensor.AddFingerprint(ushort.Parse(input.ToString()), UserPermission.Level1)) - { - case ResponseType.Success: - dialog.Cancel(); - - Application.MainLoop.Invoke(() => MessageBox.Query("Add Fingerprint", "Successfully added fingerprint", "Ok")); - - break; - case ResponseType.Full: - dialog.ErrorMessage = "Sensor full, can't add more users"; - - dialog.CancelAndShowError(); - - break; - case ResponseType.NoUser: - dialog.ErrorMessage = "Can't add fingerprint, invalid id"; - - dialog.CancelAndShowError(); - - break; - case ResponseType.FingerOccupied: - dialog.ErrorMessage = "Can't add fingerprint, finger already registered"; - - dialog.CancelAndShowError(); - - break; - case ResponseType.UserOccupied: - dialog.ErrorMessage = "Can't add fingerprint, id already used"; - - dialog.CancelAndShowError(); - - break; - default: - dialog.CancelAndShowError(); - break; - } - }); - - dialog.Show(); - - UpdateUserCount(); + File.WriteAllText(WaveshareUARTProvider.ServerIPFilePath, input.ToString()); } + + UpdateServerIP(); + } + + private void SetServerPort_Clicked() + { + if (new EntryDialog("Server Port", i => short.TryParse(i.ToString(), out _), "Need to be a valid port").TryShow(out var input)) + { + File.WriteAllText(WaveshareUARTProvider.ServerPortFilePath, input.ToString()); + } + + UpdateServerIP(); } private void ManageUsersButton_Clicked() @@ -221,7 +222,7 @@ namespace Akari.Provider.WaveshareUART.Views private void ChangeConfig() { - File.Delete(SettingsFilePath); + File.Delete(WaveshareUARTProvider.SerialPortFilePath); Application.Run(new SettingsDisplay()); diff --git a/WaveshareUARTProvider.cs b/WaveshareUARTProvider.cs index 8292ad2..0f13c40 100644 --- a/WaveshareUARTProvider.cs +++ b/WaveshareUARTProvider.cs @@ -1,4 +1,5 @@ using Akari.Provider.WaveshareUART.Users; +using System; using System.IO; using WaveshareUARTFingerprintSensor; @@ -6,7 +7,9 @@ namespace Akari.Provider.WaveshareUART { public class WaveshareUARTProvider { - public const string SettingsFilePath = "settings.txt"; + public const string SerialPortFilePath = "serial.txt"; + public const string ServerIPFilePath = "ip.txt"; + public const string ServerPortFilePath = "port.txt"; public static WaveshareUARTProvider Instance { get; } @@ -25,7 +28,21 @@ namespace Akari.Provider.WaveshareUART public void InitSensor() { - FingerprintSensor = new FingerprintSensor(File.ReadAllText(SettingsFilePath)); + try + { + FingerprintSensor = new FingerprintSensor(File.ReadAllText(SerialPortFilePath)); + } + catch (Exception e) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(e); + + Console.WriteLine(); + Console.WriteLine($"Couldn't read settings file '{SerialPortFilePath}'"); + Console.WriteLine($"Launch the program with '-c' to config the provider"); + + Environment.Exit(1); + } FingerprintSensor.Start(); }