PostService with job reply

This commit is contained in:
2023-07-25 10:30:42 -07:00
parent 98c6262a4d
commit 0111a25e69
16 changed files with 261 additions and 55 deletions

View File

@ -22,11 +22,12 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
private readonly IPostService _PostService;
private readonly ISerialService _SerialService;
private readonly ILastScanService _LastScanService;
private readonly List<Notification> _Notifications;
private readonly ILogger<TimedHostedService> _Logger;
private readonly IHttpClientFactory _HttpClientFactory;
private readonly ILinuxGroupManager _LinuxGroupManager;
private readonly IHubContext<NotificationHub> _HubContext;
private readonly Dictionary<string, InputReader> _Readers;
private readonly IHubContext<NotificationHub> _HubContext;
private readonly Dictionary<EventCode, char> _CharToEventCodes;
private readonly List<(string MethodName, Timer Timer)> _Timers;
@ -36,6 +37,7 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
_Logger = logger;
_Readers = new();
_ExecutionCount = 0;
_Notifications = new();
_HubContext = hubContext;
_CharToEventCodes = new();
_AppSettings = appSettings;
@ -47,9 +49,15 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
_LinuxGroupManager = linuxGroupManager;
_Calendar = new CultureInfo("en-US").Calendar;
Timer writeTimer = new(Write, null, Timeout.Infinite, Timeout.Infinite);
Timer postToTimer = new(PostTo, null, Timeout.Infinite, Timeout.Infinite);
Timer shareToTimer = new(ShareTo, null, Timeout.Infinite, Timeout.Infinite);
Timer scanForNewInputsTimer = new(ScanForNewInputs, null, Timeout.Infinite, Timeout.Infinite);
if (!string.IsNullOrEmpty(_AppSettings.SerialPortName))
_Timers.Add((nameof(Write), writeTimer));
if (!string.IsNullOrEmpty(_AppSettings.PostTo))
_Timers.Add((nameof(PostTo), postToTimer));
if (!string.IsNullOrEmpty(_AppSettings.FileShare))
_Timers.Add((nameof(ShareTo), shareToTimer));
#if Linux
_Timers.Add((nameof(ScanForNewInputs), scanForNewInputsTimer));
#endif
@ -129,15 +137,9 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
Result<string> result = _LastScanService.GetScan();
if (!string.IsNullOrEmpty(result.Results))
{
Notification notification = new(e, result.Results);
Notification notification = new(e, result.Results, null);
_Notifications.Add(notification);
_ = _HubContext.Clients.All.SendAsync(nameof(NotificationHub.NotifyAll), notification);
if (!string.IsNullOrEmpty(_AppSettings.FileShare))
_FileService.Write(_AppSettings.EquipmentName, _AppSettings.FileShare, _Calendar, notification);
if (!string.IsNullOrEmpty(_AppSettings.PostTo))
{
HttpClient httpClient = _HttpClientFactory.CreateClient(nameof(TimedHostedService));
_ = _PostService.PostAsync(_AppSettings.PostTo, httpClient, notification);
}
}
}
}
@ -225,4 +227,82 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
catch (Exception ex) { _Logger.LogError(ex, $"{nameof(Write)}-{nameof(Timer)}.{nameof(Timer.Change)}"); }
}
private void PostTo()
{
if (!string.IsNullOrEmpty(_AppSettings.PostTo))
{
Task<HttpResponseMessage>? httpResponseMessage;
lock (_Notifications)
{
if (!_Notifications.Any())
httpResponseMessage = null;
else
{
HttpClient httpClient = _HttpClientFactory.CreateClient(nameof(TimedHostedService));
httpResponseMessage = _PostService.PostAsync(_AppSettings.PostTo, httpClient, _Notifications.Last());
_Notifications.Clear();
}
}
if (httpResponseMessage is not null)
{
httpResponseMessage.Wait();
if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception(httpResponseMessage.Result.StatusCode.ToString());
Task<string> content = httpResponseMessage.Result.Content.ReadAsStringAsync();
content.Wait();
Notification notification = new(null, null, content.Result);
_ = _HubContext.Clients.All.SendAsync(nameof(NotificationHub.NotifyAll), notification);
}
}
}
private void PostTo(object? sender)
{
try
{ PostTo(); }
catch (Exception ex) { _Logger.LogError(ex, nameof(PostTo)); }
try
{
Timer? timer = GetTimer(nameof(PostTo));
if (timer is not null)
{
TimeSpan timeSpan = new(DateTime.Now.AddMilliseconds(_AppSettings.PostToEvery).Ticks - DateTime.Now.Ticks);
_ = timer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
}
catch (Exception ex) { _Logger.LogError(ex, $"{nameof(PostTo)}-{nameof(Timer)}.{nameof(Timer.Change)}"); }
}
private void ShareTo()
{
if (!string.IsNullOrEmpty(_AppSettings.FileShare))
{
lock (_Notifications)
{
if (_Notifications.Any())
{
_FileService.Write(_AppSettings.EquipmentName, _AppSettings.FileShare, _Calendar, _Notifications.Last());
_Notifications.Clear();
}
}
}
}
private void ShareTo(object? sender)
{
try
{ ShareTo(); }
catch (Exception ex) { _Logger.LogError(ex, nameof(ShareTo)); }
try
{
Timer? timer = GetTimer(nameof(ShareTo));
if (timer is not null)
{
TimeSpan timeSpan = new(DateTime.Now.AddMilliseconds(_AppSettings.ShareToEvery).Ticks - DateTime.Now.Ticks);
_ = timer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
}
catch (Exception ex) { _Logger.LogError(ex, $"{nameof(ShareTo)}-{nameof(Timer)}.{nameof(Timer.Change)}"); }
}
}