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)}"); }
}
}

View File

@ -3,24 +3,26 @@ using System.Text.Json;
namespace Barcode.Host.Server.Models;
public record AppSettings(string BuildNumber,
string Company,
int ClearLastScanServiceAfter,
string Company,
string DeviceNameEndsWith,
string EquipmentName,
int ExpectedScanLengthA,
int ExpectedScanLengthB,
string FileShare,
string GitCommitSeven,
string LinuxDevicePath,
bool IsDevelopment,
bool IsStaging,
int NotifyMinimum,
string LinuxDevicePath,
string MockRoot,
string MonAResource,
string MonASite,
int NotifyMinimum,
string PostTo,
int PostToEvery,
string RootPassword,
string SerialPortName,
int ShareToEvery,
string URLs,
string WorkingDirectoryName,
int WriteToSerialEvery)

View File

@ -9,24 +9,26 @@ public class AppSettings
#nullable disable
[Display(Name = "Build Number"), Required] public string BuildNumber { get; set; }
[Display(Name = "Company"), Required] public string Company { get; set; }
[Display(Name = "Last Scan Service Clear After"), Required] public int? ClearLastScanServiceAfter { get; set; }
[Display(Name = "Company"), Required] public string Company { get; set; }
[Display(Name = "Device Name Ends With"), Required] public string DeviceNameEndsWith { get; set; }
[Display(Name = "Equipment Name"), Required] public string EquipmentName { get; set; }
[Display(Name = "ExpectedScanLengthA"), Required] public int? ExpectedScanLengthA { get; set; }
[Display(Name = "ExpectedScanLengthB"), Required] public int? ExpectedScanLengthB { get; set; }
[Display(Name = "File Share"), Required] public string FileShare { get; set; }
[Display(Name = "Git Commit Seven"), Required] public string GitCommitSeven { get; set; }
[Display(Name = "Linux Device Path"), Required] public string LinuxDevicePath { get; set; }
[Display(Name = "Is Development"), Required] public bool? IsDevelopment { get; set; }
[Display(Name = "Is Staging"), Required] public bool? IsStaging { get; set; }
[Display(Name = "Notify Minimum"), Required] public int? NotifyMinimum { get; set; }
[Display(Name = "Linux Device Path"), Required] public string LinuxDevicePath { get; set; }
[Display(Name = "Mock Root"), Required] public string MockRoot { get; set; }
[Display(Name = "MonA Resource"), Required] public string MonAResource { get; set; }
[Display(Name = "MonA Site"), Required] public string MonASite { get; set; }
[Display(Name = "Notify Minimum"), Required] public int? NotifyMinimum { get; set; }
[Display(Name = "PostTo"), Required] public string PostTo { get; set; }
[Display(Name = "Post to Every"), Required] public int? PostToEvery { get; set; }
[Display(Name = "RootPassword"), Required] public string RootPassword { get; set; }
[Display(Name = "Serial Port Name"), Required] public string SerialPortName { get; set; }
[Display(Name = "Share to Every"), Required] public int? ShareToEvery { get; set; }
[Display(Name = "URLs"), Required] public string URLs { get; set; }
[Display(Name = "Working Directory Name"), Required] public string WorkingDirectoryName { get; set; }
[Display(Name = "WriteToSerialEvery"), Required] public int? WriteToSerialEvery { get; set; }
@ -46,10 +48,10 @@ public class AppSettings
throw new NullReferenceException(nameof(appSettings));
if (appSettings.BuildNumber is null)
throw new NullReferenceException(nameof(BuildNumber));
if (appSettings.Company is null)
throw new NullReferenceException(nameof(Company));
if (appSettings.ClearLastScanServiceAfter is null)
throw new NullReferenceException(nameof(ClearLastScanServiceAfter));
if (appSettings.Company is null)
throw new NullReferenceException(nameof(Company));
if (appSettings.DeviceNameEndsWith is null)
throw new NullReferenceException(nameof(DeviceNameEndsWith));
if (appSettings.EquipmentName is null)
@ -62,26 +64,30 @@ public class AppSettings
throw new NullReferenceException(nameof(FileShare));
if (appSettings.GitCommitSeven is null)
throw new NullReferenceException(nameof(GitCommitSeven));
if (appSettings.LinuxDevicePath is null)
throw new NullReferenceException(nameof(LinuxDevicePath));
if (appSettings.IsDevelopment is null)
throw new NullReferenceException(nameof(IsDevelopment));
if (appSettings.IsStaging is null)
throw new NullReferenceException(nameof(IsStaging));
if (appSettings.NotifyMinimum is null)
throw new NullReferenceException(nameof(NotifyMinimum));
if (appSettings.LinuxDevicePath is null)
throw new NullReferenceException(nameof(LinuxDevicePath));
if (appSettings.MockRoot is null)
throw new NullReferenceException(nameof(MockRoot));
if (appSettings.MonAResource is null)
throw new NullReferenceException(nameof(MonAResource));
if (appSettings.MonASite is null)
throw new NullReferenceException(nameof(MonASite));
if (appSettings.NotifyMinimum is null)
throw new NullReferenceException(nameof(NotifyMinimum));
if (appSettings.PostTo is null)
throw new NullReferenceException(nameof(PostTo));
if (appSettings.PostToEvery is null)
throw new NullReferenceException(nameof(PostToEvery));
if (appSettings.RootPassword is null)
throw new NullReferenceException(nameof(RootPassword));
if (appSettings.SerialPortName is null)
throw new NullReferenceException(nameof(SerialPortName));
if (appSettings.ShareToEvery is null)
throw new NullReferenceException(nameof(ShareToEvery));
if (appSettings.URLs is null)
throw new NullReferenceException(nameof(URLs));
if (appSettings.WorkingDirectoryName is null)
@ -90,24 +96,26 @@ public class AppSettings
throw new NullReferenceException(nameof(WriteToSerialEvery));
result = new(
appSettings.BuildNumber,
appSettings.Company,
appSettings.ClearLastScanServiceAfter.Value,
appSettings.Company,
appSettings.DeviceNameEndsWith,
appSettings.EquipmentName,
appSettings.ExpectedScanLengthA.Value,
appSettings.ExpectedScanLengthB.Value,
appSettings.FileShare,
appSettings.GitCommitSeven,
appSettings.LinuxDevicePath,
appSettings.IsDevelopment.Value,
appSettings.IsStaging.Value,
appSettings.NotifyMinimum.Value,
appSettings.LinuxDevicePath,
appSettings.MockRoot,
appSettings.MonAResource,
appSettings.MonASite,
appSettings.NotifyMinimum.Value,
appSettings.PostTo,
appSettings.PostToEvery.Value,
appSettings.RootPassword,
appSettings.SerialPortName,
appSettings.ShareToEvery.Value,
appSettings.URLs,
appSettings.WorkingDirectoryName,
appSettings.WriteToSerialEvery.Value);

View File

@ -7,7 +7,12 @@
</div>
<div class="row p-1">
<div class="col-6">
<h1 id="data" style="text-align:center"></h1>
<h1 id="lastScanServiceResultValue" style="text-align:center"></h1>
</div>
</div>
<div class="row p-1">
<div class="col-6">
<h1 id="httpContentBody" style="text-align:center"></h1>
</div>
</div>
</div>

View File

@ -1,5 +1,5 @@
{
"BuildNumber": "1",
"BuildNumber": "1",
"Company": "Infineon Technologies Americas Corp.",
"ClearLastScanServiceAfter": 250,
"DeviceNameEndsWith": "Symbol Bar Code Scanner",
@ -25,6 +25,7 @@
"MonAResource": "OI_Metrology_Viewer_EC",
"MonASite": "auc",
"PostTo": "",
"PostToEvery": 1000,
"SerialPortName": "/dev/ttyUSB0",
"Serilog": {
"Using": [
@ -63,6 +64,7 @@
"Application": "Sample"
}
},
"ShareToEvery": 1000,
"RootPassword": "",
"URLs": "http://localhost:5003;",
"WorkingDirectoryName": "IFXApps",

View File

@ -3,8 +3,14 @@
var connection = new signalR.HubConnectionBuilder().withUrl("/NotificationHub").build();
connection.on("NotifyAll", function (data) {
var message = `${data.keyPressEvent.dateTime} - [${data.lastScanServiceResultValue}]`;
document.getElementById("data").innerText = message;
if (data.keyPressEvent && data.lastScanServiceResultValue) {
var innerText = `${data.keyPressEvent.dateTime} - [${data.lastScanServiceResultValue}]`;
document.getElementById("lastScanServiceResultValue").innerText = innerText;
}
else if (data.httpContentBody) {
var innerText = `${data.httpContentBody}]`; //JSON.parse() // JSON.stringify()
document.getElementById("httpContentBody").innerText = innerText;
}
});
connection.start().then(function () {