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

@ -1,6 +0,0 @@
namespace Barcode.Host.Shared.KeyboardMouse.Abstract;
public interface IAggregateInputReader
{
event InputReader.RaiseKeyPress OnKeyPress;
}

View File

@ -1,72 +0,0 @@
using Barcode.Host.Shared.KeyboardMouse.Abstract;
using Microsoft.Extensions.Logging;
namespace Barcode.Host.Shared.KeyboardMouse;
public class AggregateInputReader : IDisposable, IAggregateInputReader
{
private readonly ILogger<InputReader> _InputReaderLogger;
private Dictionary<string, InputReader>? _Readers = new();
public event InputReader.RaiseKeyPress? OnKeyPress;
public AggregateInputReader(ILogger<InputReader> inputReaderLogger)
{
_InputReaderLogger = inputReaderLogger;
System.Timers.Timer timer = new()
{
Interval = 10 * 1000,
Enabled = true
};
timer.Elapsed += (_, _) => Scan();
timer.Start();
}
private void ReaderOnOnKeyPress(KeyPressEvent e) => OnKeyPress?.Invoke(e);
private void Scan()
{
string[] files = Directory.GetFiles("/dev/input/", "event*");
foreach (string file in files)
{
if (_Readers is not null && _Readers.ContainsKey(file))
{
continue;
}
InputReader reader = new(file, _InputReaderLogger);
reader.OnKeyPress += ReaderOnOnKeyPress;
_Readers?.Add(file, reader);
}
IEnumerable<InputReader>? deadReaders = _Readers?.Values.Where(r => r.Faulted);
if (deadReaders is not null)
{
foreach (InputReader? dr in deadReaders)
{
_ = _Readers?.Remove(dr.Path);
dr.OnKeyPress -= ReaderOnOnKeyPress;
dr.Dispose();
}
}
}
public void Dispose()
{
if (_Readers is not null)
{
foreach (InputReader d in _Readers.Values)
{
d.OnKeyPress -= ReaderOnOnKeyPress;
d.Dispose();
}
}
_Readers = null;
}
}

View File

@ -2,66 +2,52 @@ namespace Barcode.Host.Shared.KeyboardMouse;
public static class DeviceReader
{
public static IEnumerable<LinuxDevice> Get(string path = "/proc/bus/input/devices")
public static IEnumerable<LinuxDevice> Get(string path)
{
List<LinuxDevice> devices = new();
using FileStream filestream = new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using StreamReader reader = new(filestream);
LinuxDevice linuxDevice = new();
while (!reader.EndOfStream)
List<LinuxDevice> linuxDevices = new();
using FileStream filestream = new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using StreamReader streamReader = new(filestream);
while (!streamReader.EndOfStream)
{
string? line = reader.ReadLine();
string? line = streamReader.ReadLine();
if (string.IsNullOrWhiteSpace(line))
{
if (!string.IsNullOrWhiteSpace(linuxDevice.Name))
{
devices.Add(linuxDevice);
linuxDevices.Add(linuxDevice);
linuxDevice = new LinuxDevice();
}
continue;
}
if (line.StartsWith("I"))
ApplyIdentifier(line, linuxDevice);
else if (line.StartsWith("N"))
linuxDevice.Name = line.Substring(9, line.Length - 9 - 1);
else if (line.StartsWith("P"))
linuxDevice.PhysicalPath = line[8..];
else if (line.StartsWith("S"))
linuxDevice.SysFsPath = line[9..];
else if (line.StartsWith("U"))
linuxDevice.UniqueIdentificationCode = line[8..];
else if (line.StartsWith("H"))
linuxDevice.Handlers = line[12..]
.Split(" ")
.Where(h => !string.IsNullOrWhiteSpace(h))
.ToList();
else if (line.StartsWith("B"))
linuxDevice.Bitmaps.Add(line[3..]);
}
return devices;
return linuxDevices;
}
private static void ApplyIdentifier(string line, LinuxDevice linuxDevice)
{
string[] values = line[3..]
.Split(" ");
string[] values = line[3..].Split(" ");
foreach (string v in values)
{
string[] kvp = v.Split("=");
switch (kvp[0])
{
case "Bus":
@ -79,4 +65,5 @@ public static class DeviceReader
}
}
}
}

View File

@ -4,59 +4,42 @@ namespace Barcode.Host.Shared.KeyboardMouse;
public class InputReader : IDisposable
{
private readonly ILogger<InputReader> _Logger;
private long _Ticks;
private bool _Disposing;
private readonly int _PiOffset;
private const int _BufferLength = 24;
private static readonly int _PiOffset;
private readonly FileStream? _FileStream;
private readonly ILogger<IDisposable> _Logger;
private readonly byte[] _Buffer = new byte[_BufferLength];
private FileStream? _Stream;
private bool _Disposing;
public delegate void RaiseKeyPress(KeyPressEvent e);
public delegate void RaiseMouseMove(MouseMoveEvent e);
public event RaiseKeyPress? OnKeyPress;
public event RaiseMouseMove? OnMouseMove;
public string Path { get; }
public string Path { get; init; }
public bool Faulted { get; private set; }
static InputReader()
{
if (RunningOnRaspberryPi())
{
_PiOffset = -8;
}
}
public event RaiseKeyPress? OnKeyPress;
public event RaiseMouseMove? OnMouseMove;
public InputReader(
string path,
ILogger<InputReader> logger)
{
_Logger = logger;
public delegate void RaiseKeyPress(KeyPressEvent e);
public delegate void RaiseMouseMove(MouseMoveEvent e);
public InputReader(string path, ILogger<IDisposable> logger)
{
Path = path;
_Logger = logger;
if (RunningOnRaspberryPi())
_PiOffset = -8;
try
{
_Stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
{ _FileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); }
catch (UnauthorizedAccessException ex)
{
_Logger.LogError(ex, "Current user doesn't have permissions to access input data. Add user to input group to correct this error");
logger.LogError(ex, "Current user doesn't have permissions to access input data. Add user to input group to correct this error");
Faulted = true;
}
catch (IOException ex)
{
_Logger.LogWarning(ex, $"Error occurred while trying to build stream for {path}");
logger.LogWarning(ex, $"Error occurred while trying to build stream for {path}");
Faulted = true;
}
_ = Task.Run(Run);
}
@ -66,33 +49,27 @@ public class InputReader : IDisposable
{
if (_Disposing)
break;
try
{
if (!Faulted && _Stream is not null)
{
_ = _Stream.Read(_Buffer, 0, _BufferLength);
}
if (!Faulted && _FileStream is not null)
_ = _FileStream.Read(_Buffer, 0, _BufferLength);
}
catch (IOException ex)
{
_Logger.LogInformation(ex, $"Error occured while trying to read from the stream for {Path}");
Faulted = true;
}
EventType type = GetEventType();
short code = GetCode();
int value = GetValue();
switch (type)
{
case EventType.EV_SYN:
_Ticks = DateTime.Now.Ticks;
break;
case EventType.EV_KEY:
HandleKeyPressEvent(code, value);
HandleKeyPressEvent();
break;
case EventType.EV_REL:
MouseAxis axis = (MouseAxis)code;
MouseMoveEvent e = new(axis, value);
OnMouseMove?.Invoke(e);
HandleMouseMoveEvent();
break;
}
}
@ -107,9 +84,7 @@ public class InputReader : IDisposable
_Buffer[22 + _PiOffset],
_Buffer[23 + _PiOffset]
};
int value = BitConverter.ToInt32(valueBits, 0);
return value;
}
@ -120,9 +95,7 @@ public class InputReader : IDisposable
_Buffer[18 + _PiOffset],
_Buffer[19 + _PiOffset]
};
short code = BitConverter.ToInt16(codeBits, 0);
return code;
}
@ -133,27 +106,35 @@ public class InputReader : IDisposable
_Buffer[16 + _PiOffset],
_Buffer[17 + _PiOffset]
};
short type = BitConverter.ToInt16(typeBits, 0);
EventType eventType = (EventType)type;
return eventType;
}
private void HandleKeyPressEvent(short code, int value)
private void HandleKeyPressEvent()
{
EventCode c = (EventCode)code;
KeyState s = (KeyState)value;
KeyPressEvent e = new(c, s);
OnKeyPress?.Invoke(e);
short code = GetCode();
int value = GetValue();
KeyState keyState = (KeyState)value;
EventCode eventCode = (EventCode)code;
KeyPressEvent keyPressEvent = new(eventCode, keyState, new TimeSpan(DateTime.Now.Ticks - _Ticks));
OnKeyPress?.Invoke(keyPressEvent);
}
private void HandleMouseMoveEvent()
{
short code = GetCode();
int value = GetValue();
MouseAxis axis = (MouseAxis)code;
MouseMoveEvent e = new(axis, value);
OnMouseMove?.Invoke(e);
}
public void Dispose()
{
_Disposing = true;
_Stream?.Dispose();
_Stream = null;
_FileStream?.Dispose();
GC.SuppressFinalize(this);
}
private static bool RunningOnRaspberryPi()
@ -163,4 +144,5 @@ public class InputReader : IDisposable
bool runningOnPi = text.Any(l => l.Contains("Raspberry Pi"));
return runningOnPi;
}
}

View File

@ -2,13 +2,16 @@ namespace Barcode.Host.Shared.KeyboardMouse;
public readonly struct KeyPressEvent
{
public KeyPressEvent(EventCode code, KeyState state)
public EventCode EventCode { get; init; }
public KeyState KeyState { get; init; }
public TimeSpan TimeSpan { get; init; }
public KeyPressEvent(EventCode eventCode, KeyState keyState, TimeSpan timeSpan)
{
Code = code;
State = state;
EventCode = eventCode;
KeyState = keyState;
TimeSpan = timeSpan;
}
public EventCode Code { get; }
public KeyState State { get; }
}

View File

@ -2,17 +2,14 @@ namespace Barcode.Host.Shared.KeyboardMouse;
public class LinuxDevice
{
public List<string> Bitmaps { get; set; } = new();
public List<string> Handlers { get; set; } = new();
public LinuxDeviceIdentifier Identifier { get; set; } = new();
public string? Name { get; set; }
public string? PhysicalPath { get; set; }
public string? SysFsPath { get; set; }
public string? UniqueIdentificationCode { get; set; }
public List<string> Handlers { get; set; } = new();
public List<string> Bitmaps { get; set; } = new();
}

View File

@ -2,11 +2,10 @@ namespace Barcode.Host.Shared.KeyboardMouse;
public class LinuxDeviceIdentifier
{
public string? Bus { get; set; }
public string? Vendor { get; set; }
public string? Product { get; set; }
public string? Vendor { get; set; }
public string? Version { get; set; }
}

View File

@ -2,13 +2,14 @@ namespace Barcode.Host.Shared.KeyboardMouse;
public readonly struct MouseMoveEvent
{
public int Amount { get; }
public MouseAxis Axis { get; }
public MouseMoveEvent(MouseAxis axis, int amount)
{
Axis = axis;
Amount = amount;
}
public MouseAxis Axis { get; }
public int Amount { get; }
}