@RenderBody() diff --git a/Server/Program.cs b/Server/Program.cs index 62f63a4..2b3f9f4 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -58,6 +58,10 @@ public class Program _ = webApplicationBuilder.Services.AddSignalR(); _ = webApplicationBuilder.Services.AddControllersWithViews(); _ = webApplicationBuilder.Services.AddSingleton(_ => appSettings); + _ = webApplicationBuilder.Services.AddHttpClient(); + _ = webApplicationBuilder.Services.AddSingleton(); + _ = webApplicationBuilder.Services.AddSingleton(); + _ = webApplicationBuilder.Services.AddSingleton(); _ = webApplicationBuilder.Services.AddSingleton(); _ = webApplicationBuilder.Services.AddSingleton(); _ = webApplicationBuilder.Services.AddSingleton(); diff --git a/Server/Services/FileService.cs b/Server/Services/FileService.cs new file mode 100644 index 0000000..7600303 --- /dev/null +++ b/Server/Services/FileService.cs @@ -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); + } + +} \ No newline at end of file diff --git a/Server/Services/PostService.cs b/Server/Services/PostService.cs new file mode 100644 index 0000000..e7fc092 --- /dev/null +++ b/Server/Services/PostService.cs @@ -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 PostAsync(string postTo, HttpClient httpClient, Notification notification) + { + Task 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 IPostService.PostAsync(string postTo, HttpClient httpClient, Notification notification) + { + Task result = PostAsync(postTo, httpClient, notification); + return result; + } + + HttpResponseMessage IPostService.Post(string postTo, HttpClient httpClient, Notification notification) + { + HttpResponseMessage result; + Task httpResponseMessage = PostAsync(postTo, httpClient, notification); + httpResponseMessage.Wait(); + result = httpResponseMessage.Result; + return result; + } + +} \ No newline at end of file diff --git a/Server/appsettings.json b/Server/appsettings.json index 7fe8344..908cf1a 100644 --- a/Server/appsettings.json +++ b/Server/appsettings.json @@ -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": [ diff --git a/Shared/Barcode.Host.Shared.csproj b/Shared/Barcode.Host.Shared.csproj index a85ad23..0c69df0 100644 --- a/Shared/Barcode.Host.Shared.csproj +++ b/Shared/Barcode.Host.Shared.csproj @@ -24,5 +24,6 @@ + \ No newline at end of file diff --git a/Shared/Models/Notification.cs b/Shared/Models/Notification.cs index e575ea6..cedb9f0 100644 --- a/Shared/Models/Notification.cs +++ b/Shared/Models/Notification.cs @@ -2,4 +2,4 @@ using Barcode.Host.Shared.KeyboardMouse; namespace Barcode.Host.Shared.Models; -public record Notification(KeyPressEvent KeyPressEvent, string LastScanServiceResultValue); \ No newline at end of file +public record Notification(KeyPressEvent? KeyPressEvent, string LastScanServiceResultValue); \ No newline at end of file diff --git a/Shared/Models/Stateless/IFileService.cs b/Shared/Models/Stateless/IFileService.cs new file mode 100644 index 0000000..cf19564 --- /dev/null +++ b/Shared/Models/Stateless/IFileService.cs @@ -0,0 +1,10 @@ +using System.Globalization; + +namespace Barcode.Host.Shared.Models.Stateless; + +public interface IFileService +{ + + void Write(string equipmentName, string fileShare, Calendar? calendar, Notification notification); + +} \ No newline at end of file diff --git a/Shared/Models/Stateless/IPostService.cs b/Shared/Models/Stateless/IPostService.cs new file mode 100644 index 0000000..bde6cfb --- /dev/null +++ b/Shared/Models/Stateless/IPostService.cs @@ -0,0 +1,9 @@ +namespace Barcode.Host.Shared.Models.Stateless; + +public interface IPostService +{ + + HttpResponseMessage Post(string postTo, HttpClient httpClient, Notification notification); + Task PostAsync(string postTo, HttpClient httpClient, Notification notification); + +} \ No newline at end of file