Added SinaglR
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
using Barcode.Host.Server.Hubs;
|
||||
using Barcode.Host.Server.Models;
|
||||
using Barcode.Host.Shared.DataModels;
|
||||
using Barcode.Host.Shared.KeyboardMouse;
|
||||
using Barcode.Host.Shared.Models;
|
||||
using Barcode.Host.Shared.Models.Stateless;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Serilog.Context;
|
||||
|
||||
namespace Barcode.Host.Server.HostedService;
|
||||
@ -19,15 +20,18 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
|
||||
private readonly ILastScanService _LastScanService;
|
||||
private readonly ILogger<TimedHostedService> _Logger;
|
||||
private readonly ILinuxGroupManager _LinuxGroupManager;
|
||||
private readonly IHubContext<NotificationHub> _HubContext;
|
||||
private readonly Dictionary<string, InputReader> _Readers;
|
||||
private readonly Dictionary<EventCode, char> _CharToEventCodes;
|
||||
private readonly List<(string MethodName, Timer Timer)> _Timers;
|
||||
|
||||
public TimedHostedService(ILogger<TimedHostedService> logger, AppSettings appSettings, ILinuxGroupManager linuxGroupManager, ILastScanService lastScanService, ISerialService serialService)
|
||||
public TimedHostedService(ILogger<TimedHostedService> logger, AppSettings appSettings, ILinuxGroupManager linuxGroupManager, ILastScanService lastScanService, ISerialService serialService, IHubContext<NotificationHub> hubContext)
|
||||
{
|
||||
_Timers = new();
|
||||
_Readers = new();
|
||||
_Logger = logger;
|
||||
_ExecutionCount = 0;
|
||||
_HubContext = hubContext;
|
||||
_CharToEventCodes = new();
|
||||
_AppSettings = appSettings;
|
||||
_SerialService = serialService;
|
||||
@ -35,7 +39,11 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
|
||||
_LinuxGroupManager = linuxGroupManager;
|
||||
Timer writeTimer = new(Write, null, Timeout.Infinite, Timeout.Infinite);
|
||||
Timer scanForNewInputsTimer = new(ScanForNewInputs, null, Timeout.Infinite, Timeout.Infinite);
|
||||
_Timers = new List<(string, Timer)>() { (nameof(Write), writeTimer), (nameof(ScanForNewInputs), scanForNewInputsTimer) };
|
||||
if (!string.IsNullOrEmpty(_AppSettings.SerialPortName))
|
||||
_Timers.Add((nameof(Write), writeTimer));
|
||||
#if Linux
|
||||
_Timers.Add((nameof(ScanForNewInputs), scanForNewInputsTimer));
|
||||
#endif
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken stoppingToken)
|
||||
@ -44,7 +52,9 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
|
||||
using (LogContext.PushProperty("MethodName", methodName))
|
||||
{
|
||||
_Logger.LogInformation($"Timed Hosted Service: {_AppSettings.GitCommitSeven}:{Environment.ProcessId} running.");
|
||||
_SerialService.Open();
|
||||
if (!string.IsNullOrEmpty(_AppSettings.SerialPortName))
|
||||
_SerialService.Open();
|
||||
#if Linux
|
||||
if (!_LinuxGroupManager.IsInInputGroup().WaitAsync(stoppingToken).Result)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_AppSettings.RootPassword))
|
||||
@ -52,6 +62,7 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
|
||||
_ = _LinuxGroupManager.AddUserToInputGroup(_AppSettings.RootPassword);
|
||||
_ = _LinuxGroupManager.RebootSystem(_AppSettings.RootPassword);
|
||||
}
|
||||
#endif
|
||||
List<(EventCode, char)> collection = _LastScanService.IncludeEventCodes();
|
||||
foreach ((EventCode eventCode, char @char) in collection)
|
||||
_CharToEventCodes.Add(eventCode, @char);
|
||||
@ -101,7 +112,19 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
|
||||
if (e.TimeSpan.TotalMilliseconds > _AppSettings.ClearLastScanServiceAfter)
|
||||
_LastScanService.Clear();
|
||||
if (e.KeyState == KeyState.KeyUp && _CharToEventCodes.TryGetValue(e.EventCode, out char @char))
|
||||
{
|
||||
_LastScanService.Add(e.EventCode, @char);
|
||||
int count = _LastScanService.GetCount();
|
||||
if (count > _AppSettings.NotifyMinimum)
|
||||
{
|
||||
Result<string> result = _LastScanService.GetScan();
|
||||
if (!string.IsNullOrEmpty(result.Results))
|
||||
{
|
||||
Notification notification = new(e, result.Results);
|
||||
_ = _HubContext.Clients.All.SendAsync(nameof(NotificationHub.NotifyAll), notification);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Timer? GetTimer(string methodName)
|
||||
@ -157,12 +180,15 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
|
||||
|
||||
private void Write()
|
||||
{
|
||||
int count = _LastScanService.GetCount();
|
||||
if (count > 0)
|
||||
if (!string.IsNullOrEmpty(_AppSettings.SerialPortName))
|
||||
{
|
||||
Result<string> result = _LastScanService.GetScan();
|
||||
if (!string.IsNullOrEmpty(result.Results))
|
||||
_SerialService.SerialPortWrite(count, result.Results);
|
||||
int count = _LastScanService.GetCount();
|
||||
if (count > 0)
|
||||
{
|
||||
Result<string> result = _LastScanService.GetScan();
|
||||
if (!string.IsNullOrEmpty(result.Results))
|
||||
_SerialService.SerialPortWrite(count, result.Results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user