Ready to test in Fab

This commit is contained in:
2023-06-03 19:05:08 -07:00
parent 1f5deedc73
commit 2c38ecb399
46 changed files with 1159 additions and 347 deletions

View File

@ -0,0 +1,93 @@
using Barcode.Host.Shared.DataModels;
using Barcode.Host.Shared.KeyboardMouse;
using Barcode.Host.Shared.Models.Stateless;
namespace Barcode.Host.Server.Services;
public class LastScanService : ILastScanService
{
private readonly List<(EventCode EventCode, char Char)> _EventCodes;
public LastScanService() =>
_EventCodes = new();
void ILastScanService.Clear()
{
lock (_EventCodes)
_EventCodes.Clear();
}
void ILastScanService.Add(EventCode eventCode, char @char)
{
lock (_EventCodes)
_EventCodes.Add((eventCode, @char));
}
Result<string> ILastScanService.GetScan()
{
Result<string> result;
char[] chars;
lock (_EventCodes)
chars = _EventCodes.Select(l => l.Char).ToArray();
result = new() { Results = new string(chars), TotalRows = chars.Length };
return result;
}
int ILastScanService.GetCount()
{
int result;
lock (_EventCodes)
result = _EventCodes.Count;
return result;
}
List<(EventCode, char)> ILastScanService.IncludeEventCodes()
{
List<(EventCode, char)> results = new()
{
(EventCode.A, 'A'),
(EventCode.B, 'B'),
(EventCode.C, 'C'),
(EventCode.D, 'D'),
(EventCode.E, 'E'),
(EventCode.F, 'F'),
(EventCode.G, 'G'),
(EventCode.H, 'H'),
(EventCode.I, 'I'),
(EventCode.J, 'J'),
(EventCode.K, 'K'),
(EventCode.L, 'L'),
(EventCode.M, 'M'),
(EventCode.N, 'N'),
(EventCode.O, 'O'),
(EventCode.P, 'P'),
(EventCode.Q, 'Q'),
(EventCode.R, 'R'),
(EventCode.S, 'S'),
(EventCode.T, 'T'),
(EventCode.U, 'U'),
(EventCode.V, 'V'),
(EventCode.W, 'W'),
(EventCode.X, 'X'),
(EventCode.Y, 'Y'),
(EventCode.Z, 'Z'),
(EventCode.Num0, '0'),
(EventCode.Num1, '1'),
(EventCode.Num2, '2'),
(EventCode.Num3, '3'),
(EventCode.Num4, '4'),
(EventCode.Num5, '5'),
(EventCode.Num6, '6'),
(EventCode.Num7, '7'),
(EventCode.Num8, '8'),
(EventCode.Num9, '9'),
(EventCode.Minus, '-'),
(EventCode.Dot, '.'),
(EventCode.Slash, '/'),
(EventCode.Space, ' '),
};
return results;
}
}

View File

@ -0,0 +1,42 @@
using Barcode.Host.Shared.Models.Stateless;
using CliWrap;
using CliWrap.Buffered;
namespace Barcode.Host.Server.Services;
public class LinuxGroupManager : ILinuxGroupManager
{
public async Task<bool> IsInInputGroup()
{
BufferedCommandResult result = await Cli.Wrap("id")
.ExecuteBufferedAsync();
string output = result.StandardOutput;
const StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries;
bool inInputGroup = output.Split(new[] { ' ' }, options)
.First(p => p.StartsWith("groups"))
.Remove(0, "groups".Length)
.Split(',', options)
.Any(p => p.Contains("input"));
return inInputGroup;
}
public async Task AddUserToInputGroup(string password)
{
using CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(10));
_ = await Cli.Wrap("bash")
.WithArguments($"-c \"echo '{password}' | sudo -S gpasswd -a $USER input")
.ExecuteBufferedAsync(cts.Token);
}
public async Task RebootSystem(string password)
{
using CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(10));
_ = await Cli.Wrap("bash")
.WithArguments($"-c \"echo '{password}' | sudo -S reboot\"")
.ExecuteBufferedAsync(cts.Token);
}
}

View File

@ -0,0 +1,44 @@
using Barcode.Host.Server.Models;
using Barcode.Host.Shared.Models.Stateless;
using System.Text;
namespace Barcode.Host.Server.Services;
public class SerialService : ISerialService
{
private string _LastRaw;
private readonly AppSettings _AppSettings;
private readonly System.IO.Ports.SerialPort _SerialPort;
public SerialService(AppSettings appSettings)
{
_LastRaw = string.Empty;
_AppSettings = appSettings;
_SerialPort = new("/dev/ttyUSB0", 9600) { ReadTimeout = 2 };
}
void ISerialService.Open() =>
_SerialPort.Open();
void ISerialService.Close() =>
_SerialPort.Close();
void ISerialService.SerialPortWrite(int count, string raw)
{
if (raw != _LastRaw)
{
string message;
if (count == _AppSettings.ExpectedScanLengthA)
message = $" {raw[2..]} {DateTime.Now:h:m tt}";
else if (count == _AppSettings.ExpectedScanLengthB)
message = $" {raw[2..]}";
else
message = $" {raw}";
byte[] bytes = Encoding.ASCII.GetBytes(message);
_SerialPort.Write(bytes, 0, bytes.Length);
_LastRaw = raw;
}
}
}