240 lines
7.5 KiB
C#
240 lines
7.5 KiB
C#
using Akari.Provider.WaveshareUART.Users;
|
|
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
using Terminal.Gui;
|
|
using WaveshareUARTFingerprintSensor;
|
|
|
|
namespace Akari.Provider.WaveshareUART.Views
|
|
{
|
|
public class TUIManager : Toplevel
|
|
{
|
|
public WaveshareUARTProvider Provider => WaveshareUARTProvider.Instance;
|
|
public FingerprintSensor FingerprintSensor => Provider.FingerprintSensor;
|
|
public UsersManager UsersManager => Provider.UsersManager;
|
|
|
|
private Label _serialPortLabel;
|
|
private Label _comparisonLevelLabel;
|
|
private Label _userCountLabel;
|
|
private Label _serverIPLabel;
|
|
private bool _needToClear;
|
|
|
|
public TUIManager()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
ColorScheme = Colors.Error;
|
|
|
|
// Creates the top-level window to show
|
|
var win = new Window("TUIManager")
|
|
{
|
|
X = 0,
|
|
Y = 1, // Leave one row for the toplevel menu
|
|
|
|
// By using Dim.Fill(), it will automatically resize without manual intervention
|
|
Width = Dim.Fill(),
|
|
Height = Dim.Fill()
|
|
};
|
|
|
|
win.ColorScheme = Colors.ColorSchemes["Dialog"];
|
|
|
|
Add(win);
|
|
|
|
// Creates a menubar, the item "New" has a help menu.
|
|
var menu = new MenuBar(new MenuBarItem[] {
|
|
new MenuBarItem ("_Options", new MenuItem [] {
|
|
new MenuItem ("_Change Config", "", () => { ChangeConfig(); }),
|
|
new MenuItem ("_Quit", "", () => { Application.RequestStop(); })
|
|
})
|
|
});
|
|
|
|
Add(menu);
|
|
|
|
// Window Content
|
|
_serialPortLabel = new Label("Serial Port:")
|
|
{
|
|
X = 2,
|
|
Y = 1,
|
|
Width = Dim.Fill()
|
|
};
|
|
_comparisonLevelLabel = new Label("Comparison Level: 0")
|
|
{
|
|
X = Pos.Left(_serialPortLabel),
|
|
Y = Pos.Bottom(_serialPortLabel),
|
|
Width = Dim.Fill()
|
|
};
|
|
_userCountLabel = new Label($"Users: {UsersManager.Count}")
|
|
{
|
|
X = Pos.Left(_comparisonLevelLabel),
|
|
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() - 2
|
|
};
|
|
manageUsersButton.Clicked += ManageUsersButton_Clicked;
|
|
|
|
var setComparisonLevelButton = new Button("Set Comparison _Level")
|
|
{
|
|
X = Pos.Center(),
|
|
Y = Pos.Bottom(manageUsersButton) + 1
|
|
};
|
|
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(),
|
|
Y = Pos.Percent(95)
|
|
};
|
|
quitButton.Clicked += Quit_Clicked;
|
|
|
|
win.Add(
|
|
_serialPortLabel,
|
|
_comparisonLevelLabel,
|
|
_userCountLabel,
|
|
_serverIPLabel,
|
|
manageUsersButton,
|
|
setComparisonLevelButton,
|
|
setServerIP,
|
|
setServerPort,
|
|
quitButton
|
|
);
|
|
|
|
// Init Sensor
|
|
if (!File.Exists(WaveshareUARTProvider.SerialPortFilePath))
|
|
{
|
|
_needToClear = true;
|
|
|
|
Application.Run(new SettingsDisplay());
|
|
}
|
|
|
|
Provider.InitSensor();
|
|
|
|
if (_needToClear)
|
|
{
|
|
if (!Provider.FingerprintSensor.DeleteAllUsers())
|
|
{
|
|
MessageBox.ErrorQuery("Provider", "Can't clear registered fingerprints, check if the sensor is working correctly", "Ok");
|
|
}
|
|
|
|
_needToClear = false;
|
|
}
|
|
|
|
// Update gui
|
|
UpdateSerialPort();
|
|
UpdateUserCount();
|
|
UpdateComparisonLevel();
|
|
UpdateServerIP();
|
|
}
|
|
|
|
private void UpdateSerialPort()
|
|
{
|
|
_serialPortLabel.Text = $"Serial Port: {FingerprintSensor.PortName}";
|
|
}
|
|
|
|
private void UpdateUserCount()
|
|
{
|
|
_userCountLabel.Text = $"Users: {UsersManager.Count}";
|
|
}
|
|
|
|
private void UpdateComparisonLevel()
|
|
{
|
|
if (FingerprintSensor.TryGetComparisonLevel(out byte comparisonLevel))
|
|
{
|
|
_comparisonLevelLabel.Text = $"Comparison Level: {comparisonLevel}";
|
|
}
|
|
}
|
|
|
|
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))
|
|
{
|
|
if (!FingerprintSensor.TrySetComparisonLevel(byte.Parse(input.ToString())))
|
|
{
|
|
MessageBox.ErrorQuery("Comparison Level", "Can't set comparison level", "Ok");
|
|
}
|
|
}
|
|
|
|
UpdateComparisonLevel();
|
|
}
|
|
|
|
private void SetServerIP_Clicked()
|
|
{
|
|
if (new EntryDialog("Server IP", i => Uri.CheckHostName(i.ToString()) != UriHostNameType.Unknown, "Need to be a valid ip address").TryShow(out var input))
|
|
{
|
|
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()
|
|
{
|
|
Application.Run(new ManageUsersDisplay());
|
|
|
|
UpdateUserCount();
|
|
}
|
|
|
|
private void ChangeConfig()
|
|
{
|
|
File.Delete(WaveshareUARTProvider.SerialPortFilePath);
|
|
|
|
Application.Run(new SettingsDisplay());
|
|
|
|
Provider.InitSensor();
|
|
|
|
UpdateSerialPort();
|
|
}
|
|
|
|
private void Quit_Clicked()
|
|
{
|
|
Application.RequestStop();
|
|
}
|
|
}
|
|
}
|