PostService
This commit is contained in:
@ -6,6 +6,7 @@ using Barcode.Host.Shared.Models;
|
||||
using Barcode.Host.Shared.Models.Stateless;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Serilog.Context;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Barcode.Host.Server.HostedService;
|
||||
|
||||
@ -15,28 +16,36 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
|
||||
public event InputReader.RaiseKeyPress? OnKeyPress;
|
||||
|
||||
private readonly int _ExecutionCount;
|
||||
protected readonly Calendar _Calendar;
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly IFileService _FileService;
|
||||
private readonly IPostService _PostService;
|
||||
private readonly ISerialService _SerialService;
|
||||
private readonly ILastScanService _LastScanService;
|
||||
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 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, IHubContext<NotificationHub> hubContext)
|
||||
public TimedHostedService(ILogger<TimedHostedService> logger, AppSettings appSettings, ILinuxGroupManager linuxGroupManager, ILastScanService lastScanService, ISerialService serialService, IHttpClientFactory httpClientFactory, IHubContext<NotificationHub> hubContext, IFileService fileService, IPostService postService)
|
||||
{
|
||||
_Timers = new();
|
||||
_Readers = new();
|
||||
_Logger = logger;
|
||||
_Readers = new();
|
||||
_ExecutionCount = 0;
|
||||
_HubContext = hubContext;
|
||||
_CharToEventCodes = new();
|
||||
_AppSettings = appSettings;
|
||||
_FileService = fileService;
|
||||
_PostService = postService;
|
||||
_SerialService = serialService;
|
||||
_LastScanService = lastScanService;
|
||||
_HttpClientFactory = httpClientFactory;
|
||||
_LinuxGroupManager = linuxGroupManager;
|
||||
_Calendar = new CultureInfo("en-US").Calendar;
|
||||
Timer writeTimer = new(Write, null, Timeout.Infinite, Timeout.Infinite);
|
||||
Timer scanForNewInputsTimer = new(ScanForNewInputs, null, Timeout.Infinite, Timeout.Infinite);
|
||||
if (!string.IsNullOrEmpty(_AppSettings.SerialPortName))
|
||||
@ -122,6 +131,13 @@ public class TimedHostedService : IHostedService, IAggregateInputReader, IDispos
|
||||
{
|
||||
Notification notification = new(e, result.Results);
|
||||
_ = _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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,10 @@ public record AppSettings(string BuildNumber,
|
||||
string Company,
|
||||
int ClearLastScanServiceAfter,
|
||||
string DeviceNameEndsWith,
|
||||
string EquipmentName,
|
||||
int ExpectedScanLengthA,
|
||||
int ExpectedScanLengthB,
|
||||
string FileShare,
|
||||
string GitCommitSeven,
|
||||
string LinuxDevicePath,
|
||||
bool IsDevelopment,
|
||||
@ -16,6 +18,7 @@ public record AppSettings(string BuildNumber,
|
||||
string MockRoot,
|
||||
string MonAResource,
|
||||
string MonASite,
|
||||
string PostTo,
|
||||
string RootPassword,
|
||||
string SerialPortName,
|
||||
string URLs,
|
||||
|
@ -12,8 +12,10 @@ public class AppSettings
|
||||
[Display(Name = "Company"), Required] public string Company { get; set; }
|
||||
[Display(Name = "Last Scan Service Clear After"), Required] public int? ClearLastScanServiceAfter { 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; }
|
||||
@ -22,6 +24,7 @@ public class AppSettings
|
||||
[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 = "PostTo"), Required] public string PostTo { get; set; }
|
||||
[Display(Name = "RootPassword"), Required] public string RootPassword { get; set; }
|
||||
[Display(Name = "Serial Port Name"), Required] public string SerialPortName { get; set; }
|
||||
[Display(Name = "URLs"), Required] public string URLs { get; set; }
|
||||
@ -49,10 +52,14 @@ public class AppSettings
|
||||
throw new NullReferenceException(nameof(ClearLastScanServiceAfter));
|
||||
if (appSettings.DeviceNameEndsWith is null)
|
||||
throw new NullReferenceException(nameof(DeviceNameEndsWith));
|
||||
if (appSettings.EquipmentName is null)
|
||||
throw new NullReferenceException(nameof(EquipmentName));
|
||||
if (appSettings.ExpectedScanLengthA is null)
|
||||
throw new NullReferenceException(nameof(ExpectedScanLengthA));
|
||||
if (appSettings.ExpectedScanLengthB is null)
|
||||
throw new NullReferenceException(nameof(ExpectedScanLengthB));
|
||||
if (appSettings.FileShare is null)
|
||||
throw new NullReferenceException(nameof(FileShare));
|
||||
if (appSettings.GitCommitSeven is null)
|
||||
throw new NullReferenceException(nameof(GitCommitSeven));
|
||||
if (appSettings.LinuxDevicePath is null)
|
||||
@ -69,6 +76,8 @@ public class AppSettings
|
||||
throw new NullReferenceException(nameof(MonAResource));
|
||||
if (appSettings.MonASite is null)
|
||||
throw new NullReferenceException(nameof(MonASite));
|
||||
if (appSettings.PostTo is null)
|
||||
throw new NullReferenceException(nameof(PostTo));
|
||||
if (appSettings.RootPassword is null)
|
||||
throw new NullReferenceException(nameof(RootPassword));
|
||||
if (appSettings.SerialPortName is null)
|
||||
@ -84,8 +93,10 @@ public class AppSettings
|
||||
appSettings.Company,
|
||||
appSettings.ClearLastScanServiceAfter.Value,
|
||||
appSettings.DeviceNameEndsWith,
|
||||
appSettings.EquipmentName,
|
||||
appSettings.ExpectedScanLengthA.Value,
|
||||
appSettings.ExpectedScanLengthB.Value,
|
||||
appSettings.FileShare,
|
||||
appSettings.GitCommitSeven,
|
||||
appSettings.LinuxDevicePath,
|
||||
appSettings.IsDevelopment.Value,
|
||||
@ -94,6 +105,7 @@ public class AppSettings
|
||||
appSettings.MockRoot,
|
||||
appSettings.MonAResource,
|
||||
appSettings.MonASite,
|
||||
appSettings.PostTo,
|
||||
appSettings.RootPassword,
|
||||
appSettings.SerialPortName,
|
||||
appSettings.URLs,
|
||||
|
@ -1,5 +1,10 @@
|
||||
@page
|
||||
<div class="container">
|
||||
<div class="row p-1">
|
||||
<div class="col-6">
|
||||
<h1 style="text-align:center">Scan RDS barcode before starting BioRad</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-6">
|
||||
<h1 id="data" style="text-align:center"></h1>
|
||||
|
@ -6,7 +6,7 @@
|
||||
<title>Barcode Host</title>
|
||||
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
|
||||
</head>
|
||||
<body>
|
||||
<body style="background-color: #191919; color: white">
|
||||
<div class="container">
|
||||
<main role="main" class="pb-3">
|
||||
@RenderBody()
|
||||
|
@ -58,6 +58,10 @@ public class Program
|
||||
_ = webApplicationBuilder.Services.AddSignalR();
|
||||
_ = webApplicationBuilder.Services.AddControllersWithViews();
|
||||
_ = webApplicationBuilder.Services.AddSingleton(_ => appSettings);
|
||||
_ = webApplicationBuilder.Services.AddHttpClient();
|
||||
_ = webApplicationBuilder.Services.AddSingleton<IFileService, FileService>();
|
||||
_ = webApplicationBuilder.Services.AddSingleton<IPostService, PostService>();
|
||||
_ = webApplicationBuilder.Services.AddSingleton<ISerialService, SerialService>();
|
||||
_ = webApplicationBuilder.Services.AddSingleton<ISerialService, SerialService>();
|
||||
_ = webApplicationBuilder.Services.AddSingleton<ILastScanService, LastScanService>();
|
||||
_ = webApplicationBuilder.Services.AddSingleton<ILinuxGroupManager, LinuxGroupManager>();
|
||||
|
21
Server/Services/FileService.cs
Normal file
21
Server/Services/FileService.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using Barcode.Host.Shared.Models;
|
||||
using Barcode.Host.Shared.Models.Stateless;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Barcode.Host.Server.Services;
|
||||
|
||||
public class FileService : IFileService
|
||||
{
|
||||
|
||||
void IFileService.Write(string equipmentName, string fileShare, Calendar? calendar, Notification notification)
|
||||
{
|
||||
DateTime dateTime = DateTime.Now;
|
||||
calendar ??= new CultureInfo("en-US").Calendar;
|
||||
string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
|
||||
string directory = Path.Combine(fileShare, weekOfYear, dateTime.ToString("yyyy-MM-dd_HH"));
|
||||
if (!Directory.Exists(directory))
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
File.WriteAllText(Path.Combine(directory, $"{equipmentName}.csv"), notification.LastScanServiceResultValue);
|
||||
}
|
||||
|
||||
}
|
34
Server/Services/PostService.cs
Normal file
34
Server/Services/PostService.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Barcode.Host.Shared.Models;
|
||||
using Barcode.Host.Shared.Models.Stateless;
|
||||
using System.Text;
|
||||
|
||||
namespace Barcode.Host.Server.Services;
|
||||
|
||||
public class PostService : IPostService
|
||||
{
|
||||
|
||||
private static Task<HttpResponseMessage> PostAsync(string postTo, HttpClient httpClient, Notification notification)
|
||||
{
|
||||
Task<HttpResponseMessage> result;
|
||||
string json = System.Text.Json.JsonSerializer.Serialize(notification);
|
||||
StringContent stringContent = new(json, Encoding.UTF8, "application/json");
|
||||
result = httpClient.PostAsync(postTo, stringContent);
|
||||
return result;
|
||||
}
|
||||
|
||||
Task<HttpResponseMessage> IPostService.PostAsync(string postTo, HttpClient httpClient, Notification notification)
|
||||
{
|
||||
Task<HttpResponseMessage> result = PostAsync(postTo, httpClient, notification);
|
||||
return result;
|
||||
}
|
||||
|
||||
HttpResponseMessage IPostService.Post(string postTo, HttpClient httpClient, Notification notification)
|
||||
{
|
||||
HttpResponseMessage result;
|
||||
Task<HttpResponseMessage> httpResponseMessage = PostAsync(postTo, httpClient, notification);
|
||||
httpResponseMessage.Wait();
|
||||
result = httpResponseMessage.Result;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +1,13 @@
|
||||
{
|
||||
"BuildNumber": "1",
|
||||
"Company": "Infineon Technologies Americas Corp.",
|
||||
"ClearLastScanServiceAfter": 250,
|
||||
"DeviceNameEndsWith": "Symbol Bar Code Scanner",
|
||||
"EquipmentName": "",
|
||||
"ExpectedScanLengthA": 8,
|
||||
"ExpectedScanLengthB": 14,
|
||||
"ExpectedScanLengthBExample": "1TO172125.1.11",
|
||||
"ClearLastScanServiceAfter": 250,
|
||||
"FileShare": "",
|
||||
"GitCommitSeven": "1234567",
|
||||
"LinuxDevicePath": "/proc/bus/input/devices",
|
||||
"Logging": {
|
||||
@ -22,6 +24,7 @@
|
||||
"MockRoot": "",
|
||||
"MonAResource": "OI_Metrology_Viewer_EC",
|
||||
"MonASite": "auc",
|
||||
"PostTo": "",
|
||||
"SerialPortName": "/dev/ttyUSB0",
|
||||
"Serilog": {
|
||||
"Using": [
|
||||
|
Reference in New Issue
Block a user