200 lines
6.0 KiB
C#
200 lines
6.0 KiB
C#
using Akari.Provider.WaveshareUART.Users;
|
|
using NStack;
|
|
using System.Threading.Tasks;
|
|
using Terminal.Gui;
|
|
using WaveshareUARTFingerprintSensor;
|
|
|
|
namespace Akari.Provider.WaveshareUART.Views
|
|
{
|
|
public class EditUserDisplay : Toplevel
|
|
{
|
|
public UsersManager UsersManager => WaveshareUARTProvider.Instance.UsersManager;
|
|
public FingerprintSensor FingerprintSensor => WaveshareUARTProvider.Instance.FingerprintSensor;
|
|
|
|
private readonly User _user;
|
|
private Label _infoLabel;
|
|
private Label _fingerprintsCountLabel;
|
|
private Label _startLabel;
|
|
|
|
public EditUserDisplay(User user)
|
|
{
|
|
_user = user;
|
|
|
|
Init();
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
Modal = true;
|
|
|
|
ColorScheme = Colors.Error;
|
|
|
|
// Creates the top-level window to show
|
|
var win = new Window("Manage Users")
|
|
{
|
|
X = 0,
|
|
Y = 0,
|
|
|
|
// By using Dim.Fill(), it will automatically resize without manual intervention
|
|
Width = Dim.Fill(),
|
|
Height = Dim.Fill()
|
|
};
|
|
|
|
win.ColorScheme = Colors.ColorSchemes["Dialog"];
|
|
|
|
Add(win);
|
|
|
|
// Window Content
|
|
_infoLabel = new Label($"Name: {_user.Name}")
|
|
{
|
|
X = 2,
|
|
Y = 1,
|
|
Width = Dim.Fill()
|
|
};
|
|
|
|
_startLabel = new Label($"First ID: {_user.Start}")
|
|
{
|
|
X = Pos.Left(_infoLabel),
|
|
Y = Pos.Bottom(_infoLabel),
|
|
Width = Dim.Fill()
|
|
};
|
|
|
|
_fingerprintsCountLabel = new Label($"Fingerprints: {_user.Count}")
|
|
{
|
|
X = Pos.Left(_startLabel),
|
|
Y = Pos.Bottom(_startLabel),
|
|
Width = Dim.Fill()
|
|
};
|
|
|
|
var addButton = new Button("_Add Fingerprint")
|
|
{
|
|
X = Pos.Right(this) - 69,
|
|
Y = Pos.Bottom(this) - 3
|
|
};
|
|
addButton.Clicked += AddButton_Clicked;
|
|
|
|
var editTokenButton = new Button("Edit Token")
|
|
{
|
|
X = Pos.Right(addButton) + 1,
|
|
Y = Pos.Top(addButton)
|
|
};
|
|
editTokenButton.Clicked += EditTokenButton_Clicked;
|
|
|
|
var clearButton = new Button("Clear Fingerprints")
|
|
{
|
|
X = Pos.Right(editTokenButton) + 1,
|
|
Y = Pos.Top(editTokenButton)
|
|
};
|
|
clearButton.Clicked += ClearButton_Clicked;
|
|
|
|
var quitButton = new Button("_Quit")
|
|
{
|
|
X = Pos.Right(clearButton) + 1,
|
|
Y = Pos.Top(clearButton)
|
|
};
|
|
quitButton.Clicked += () => { Application.RequestStop(); };
|
|
|
|
win.Add(_infoLabel, _startLabel, _fingerprintsCountLabel, addButton, editTokenButton, clearButton, quitButton);
|
|
}
|
|
|
|
private void UpdateCount()
|
|
{
|
|
_fingerprintsCountLabel.Text = $"Fingerprints: {_user.Count}";
|
|
}
|
|
|
|
private void AddButton_Clicked()
|
|
{
|
|
if (_user.IsFull())
|
|
{
|
|
MessageBox.ErrorQuery("Add Fingerprint", "Can't add more fingerprints to this user", "Ok");
|
|
return;
|
|
}
|
|
|
|
var dialog = new FingerprintDialog("Add Fingerprint", "Can't add fingerprint, try to place your finger flat on the sensor");
|
|
|
|
Task.Run(() =>
|
|
{
|
|
// In case an external program messed with the fingerprints
|
|
FingerprintSensor.DeleteUser(_user.Position);
|
|
|
|
switch (FingerprintSensor.AddFingerprint(_user.Position, UserPermission.Level1))
|
|
{
|
|
case ResponseType.Success:
|
|
dialog.Cancel();
|
|
|
|
lock (_user)
|
|
{
|
|
_user.IncrementPosition();
|
|
}
|
|
|
|
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();
|
|
|
|
UsersManager.Save();
|
|
|
|
UpdateCount();
|
|
}
|
|
|
|
private void EditTokenButton_Clicked()
|
|
{
|
|
if (new EntryDialog("Edit Token", s => !string.IsNullOrWhiteSpace(s.ToString()), "Token cannot be empty").TryShow(out ustring input))
|
|
{
|
|
_user.Token = input.ToString();
|
|
|
|
UsersManager.Save();
|
|
}
|
|
}
|
|
|
|
private void ClearButton_Clicked()
|
|
{
|
|
if (MessageBox.Query("Delete Users", "Do you really to clear all the user fingerprints?", "yes", "No") != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (ushort i = _user.Start; i < _user.Position; i++)
|
|
{
|
|
FingerprintSensor.DeleteUser(i);
|
|
}
|
|
|
|
_user.ResetPosition();
|
|
|
|
UsersManager.Save();
|
|
|
|
UpdateCount();
|
|
}
|
|
}
|
|
}
|