Add TcpService
This commit is contained in:
@@ -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,25 +1,33 @@
|
||||
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 MQTTService
|
||||
public class TcpService
|
||||
{
|
||||
public FingerprintSensor FingerprintSensor => WaveshareUARTProvider.Instance.FingerprintSensor;
|
||||
public UsersManager UsersManager => WaveshareUARTProvider.Instance.UsersManager;
|
||||
|
||||
public MQTTService()
|
||||
private string _ip;
|
||||
private int _port;
|
||||
|
||||
public TcpService()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Console.WriteLine("Starting MQTT service...");
|
||||
Console.WriteLine("Starting Tcp service...");
|
||||
|
||||
InitMQTT();
|
||||
InitTcp();
|
||||
|
||||
Console.WriteLine($"There is {UsersManager.Count} users registered");
|
||||
|
||||
@@ -34,14 +42,36 @@ namespace Akari.Provider.WaveshareUART
|
||||
FingerprintSensor.Waked -= FingerprintSensor_Waked;
|
||||
}
|
||||
|
||||
private void InitMQTT()
|
||||
private void InitTcp()
|
||||
{
|
||||
// TODO
|
||||
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)
|
||||
{
|
||||
// TODO
|
||||
using (var client = new TcpClient(_ip, _port))
|
||||
{
|
||||
var stream = client.GetStream();
|
||||
|
||||
var data = Convert.FromBase64String(user.Token);
|
||||
|
||||
stream.Write(data, 0, data.Length);
|
||||
}
|
||||
}
|
||||
|
||||
private void FingerprintSensor_Waked(FingerprintSensor sender)
|
||||
@@ -54,7 +84,22 @@ namespace Akari.Provider.WaveshareUART
|
||||
{
|
||||
Console.WriteLine($"{user.Name} fingerprint recognized");
|
||||
|
||||
SendAuth(user);
|
||||
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
|
||||
{
|
||||
@@ -51,7 +51,7 @@ namespace Akari.Provider.WaveshareUART
|
||||
{
|
||||
InitSensor();
|
||||
|
||||
new MQTTService().Start();
|
||||
new TcpService().Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user