Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c0d3814398 | |||
| 509bf853a1 | |||
| 2b2e40a967 | |||
| a04c841225 |
@@ -93,7 +93,7 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="MQTTService.cs" />
|
||||
<Compile Include="TcpService.cs" />
|
||||
<Compile Include="Views\EditUserDisplay.cs" />
|
||||
<Compile Include="Views\EntryDialog.cs" />
|
||||
<Compile Include="Views\FingerprintDialog.cs" />
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
using Akari.Provider.WaveshareUART.Users;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using WaveshareUARTFingerprintSensor;
|
||||
|
||||
namespace Akari.Provider.WaveshareUART
|
||||
{
|
||||
public class MQTTService
|
||||
{
|
||||
public FingerprintSensor FingerprintSensor => WaveshareUARTProvider.Instance.FingerprintSensor;
|
||||
public UsersManager UsersManager => WaveshareUARTProvider.Instance.UsersManager;
|
||||
|
||||
public MQTTService()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Console.WriteLine("Starting MQTT service...");
|
||||
|
||||
InitMQTT();
|
||||
|
||||
Console.WriteLine($"There is {UsersManager.Count} users registered");
|
||||
|
||||
FingerprintSensor.Waked += FingerprintSensor_Waked;
|
||||
|
||||
FingerprintSensor.Sleep();
|
||||
|
||||
Console.WriteLine("Waiting for fingerprints...");
|
||||
|
||||
Thread.Sleep(-1);
|
||||
|
||||
FingerprintSensor.Waked -= FingerprintSensor_Waked;
|
||||
}
|
||||
|
||||
private void InitMQTT()
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
private void SendAuth(User user)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
private void FingerprintSensor_Waked(FingerprintSensor sender)
|
||||
{
|
||||
FingerprintSensor.Wake();
|
||||
|
||||
if (FingerprintSensor.TryComparison1N(out var userInfo))
|
||||
{
|
||||
if (TryFindUser(userInfo.userID, out var user))
|
||||
{
|
||||
Console.WriteLine($"{user.Name} fingerprint recognized");
|
||||
|
||||
SendAuth(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Found unknown fingerprint with id {userInfo.userID}");
|
||||
}
|
||||
}
|
||||
|
||||
FingerprintSensor.Sleep();
|
||||
}
|
||||
|
||||
private bool TryFindUser(ushort userID, out User user)
|
||||
{
|
||||
ushort id = (ushort)((userID / UsersManager.RangeLength) * UsersManager.RangeLength + UsersManager.RangeStart);
|
||||
|
||||
if (UsersManager[id] is User u)
|
||||
{
|
||||
user = u;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
user = default;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
138
TcpService.cs
Normal file
138
TcpService.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using Akari.Provider.WaveshareUART.Users;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using WaveshareUARTFingerprintSensor;
|
||||
|
||||
namespace Akari.Provider.WaveshareUART
|
||||
{
|
||||
public class TcpService
|
||||
{
|
||||
public const int ConnectTimeout = 5_000;
|
||||
|
||||
public FingerprintSensor FingerprintSensor => WaveshareUARTProvider.Instance.FingerprintSensor;
|
||||
public UsersManager UsersManager => WaveshareUARTProvider.Instance.UsersManager;
|
||||
|
||||
private string _ip;
|
||||
private int _port;
|
||||
|
||||
public TcpService()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Console.WriteLine("Starting Tcp service...");
|
||||
|
||||
InitTcp();
|
||||
|
||||
Console.WriteLine($"There is {UsersManager.Count} users registered");
|
||||
|
||||
FingerprintSensor.Waked += FingerprintSensor_Waked;
|
||||
|
||||
FingerprintSensor.Sleep();
|
||||
|
||||
Console.WriteLine("Waiting for fingerprints...");
|
||||
|
||||
Thread.Sleep(-1);
|
||||
|
||||
FingerprintSensor.Waked -= FingerprintSensor_Waked;
|
||||
}
|
||||
|
||||
private void InitTcp()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ip = File.ReadAllText(WaveshareUARTProvider.ServerIPFilePath);
|
||||
_port = int.Parse(File.ReadAllText(WaveshareUARTProvider.ServerPortFilePath));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(e);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Couldn't read setting files");
|
||||
Console.WriteLine($"Launch the program with '-c' to config the provider");
|
||||
|
||||
Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private void SendAuth(User user)
|
||||
{
|
||||
using (var client = new TcpClient())
|
||||
{
|
||||
if (!client.ConnectAsync(_ip, _port).Wait(ConnectTimeout))
|
||||
{
|
||||
Console.WriteLine("Can't connect to server, timed out");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var stream = client.GetStream();
|
||||
|
||||
var data = Encoding.UTF8.GetBytes($"{user.Name}${user.Token}");
|
||||
|
||||
stream.Write(data, 0, data.Length);
|
||||
}
|
||||
}
|
||||
|
||||
private void FingerprintSensor_Waked(FingerprintSensor sender)
|
||||
{
|
||||
FingerprintSensor.Wake();
|
||||
|
||||
if (FingerprintSensor.TryComparison1N(out var userInfo))
|
||||
{
|
||||
if (TryFindUser(userInfo.userID, out var user))
|
||||
{
|
||||
Console.WriteLine($"{user.Name} fingerprint recognized");
|
||||
|
||||
try
|
||||
{
|
||||
SendAuth(user);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var oldColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
|
||||
Console.WriteLine("Tried to send an auth packet but an error occured.");
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine(e);
|
||||
|
||||
Console.ForegroundColor = oldColor;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Found unknown fingerprint with id {userInfo.userID}");
|
||||
}
|
||||
}
|
||||
|
||||
FingerprintSensor.Sleep();
|
||||
}
|
||||
|
||||
private bool TryFindUser(ushort userID, out User user)
|
||||
{
|
||||
ushort id = (ushort)((userID / UsersManager.RangeLength) * UsersManager.RangeLength + UsersManager.RangeStart);
|
||||
|
||||
if (UsersManager[id] is User u)
|
||||
{
|
||||
user = u;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
user = default;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -34,7 +51,7 @@ namespace Akari.Provider.WaveshareUART
|
||||
{
|
||||
InitSensor();
|
||||
|
||||
new MQTTService().Start();
|
||||
new TcpService().Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user