diff --git a/EpsonPrintService/Program.cs b/EpsonPrintService/Program.cs index 535ef9c..299aef1 100644 --- a/EpsonPrintService/Program.cs +++ b/EpsonPrintService/Program.cs @@ -11,6 +11,7 @@ using Inspectron.Epson.PrintServer.Printers; using Inspectron.Epson.PrintServer.Printers.Utils; using Inspectron.Epson.PrintServer.PrintServices; using EpsonPrintService; +using Inspectron.Epson.PrintServer.JobStatusReporters; using Inspectron.Epson.Queue; StandardKernel kernel = new StandardKernel(); @@ -67,7 +68,7 @@ kernel.Bind().ToMethod(ctx => kernel.Bind().To().InSingletonScope(); kernel.Bind().To(); kernel.Bind().To().InSingletonScope(); -kernel.Bind().To(); +kernel.Bind().To(); kernel.Bind().ToSelf().InSingletonScope(); kernel.Bind().To().InSingletonScope(); kernel.Bind().ToSelf().InSingletonScope(); diff --git a/EpsonPrintService/config.txt b/EpsonPrintService/config.txt index 4a63b8d..6b657d1 100644 --- a/EpsonPrintService/config.txt +++ b/EpsonPrintService/config.txt @@ -1 +1 @@ -aHR0cHM6Ly9hcGkuZGV2Lmdhc3Ryb2phbWVzLmNoOzY4ODc1MjY2MTcxMjgxN2RlMjcxNTlmMjtGTEVEL1phQ2F5a0NrdEpCOHZzN1lqVFNyaStleGdKVWIva21WWk1lUHBNPQ== \ No newline at end of file +aHR0cHM6Ly9hcGkuZGV2Lmdhc3Ryb2phbWVzLmNoOzYzZTM3Mjk1YmQ2ZjI2ZGJkMzYxNjRjMDs3VnBaQXNTKzcwTEdKTXFRQi9nNWlkMlZIZ3FybmR4RUpXeDVLYXF2YThzPQ== \ No newline at end of file diff --git a/Inspectron.Epson/PrintServer/JobSources/SignalRPrintJobSource.cs b/Inspectron.Epson/PrintServer/JobSources/SignalRPrintJobSource.cs index 75eda1e..98da9a7 100644 --- a/Inspectron.Epson/PrintServer/JobSources/SignalRPrintJobSource.cs +++ b/Inspectron.Epson/PrintServer/JobSources/SignalRPrintJobSource.cs @@ -100,6 +100,7 @@ public class SignalRPrintJobSource: IPrintJobSource _logger.LogInformation("Received print job for working area: {PrintJob}", JsonSerializer.Serialize(args)); _printJobChannel.Writer.TryWrite(new PrintJob { + JobId = args.JobId, IP = args.PrinterIp, Document = args, }); @@ -112,6 +113,9 @@ public class SignalRPrintJobSource: IPrintJobSource public class PrintJobFromSignalR { + [JsonPropertyName("jobId")] + public string JobId { get; set; } + [JsonPropertyName("printerIp")] public string PrinterIp { get; set; } diff --git a/Inspectron.Epson/PrintServer/JobStatusReporters/JamesStatusReporter.cs b/Inspectron.Epson/PrintServer/JobStatusReporters/JamesStatusReporter.cs new file mode 100644 index 0000000..19df45e --- /dev/null +++ b/Inspectron.Epson/PrintServer/JobStatusReporters/JamesStatusReporter.cs @@ -0,0 +1,42 @@ +using Inspectron.Epson.PrintServer.ConfigurationSources; +using Inspectron.Epson.Queue; +using Microsoft.Extensions.Logging; +using System.Net.Http.Json; + +namespace Inspectron.Epson.PrintServer.JobStatusReporters; + +public class JamesStatusReporter : IJobStatusReporter +{ + private readonly HttpClient _httpClient; + private readonly EpsonPrintServiceConfiguration _configuration; + private readonly ILogger _logger; + + public JamesStatusReporter(HttpClient httpClient, EpsonPrintServiceConfiguration configuration, ILogger logger) + { + _httpClient = httpClient; + _configuration = configuration; + _logger = logger; + } + + public async Task ReportStatusAsync(PrintJob job, PrintJobStatus status) + { + try + { + var url = $"{_configuration.ApiUrl?.TrimEnd('/')}/api/printerServer/job-update"; + using var request = new HttpRequestMessage(HttpMethod.Post, url); + request.Headers.Add("X-Printer-Server-Key", _configuration.ApiKey); + request.Content = JsonContent.Create(new + { + jobId = job.JobId, + status = (int)status + }); + + var response = await _httpClient.SendAsync(request); + response.EnsureSuccessStatusCode(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to report status {Status} for job {JobId}", status, job.JobId); + } + } +} diff --git a/Inspectron.Epson/Queue/PrintJob.cs b/Inspectron.Epson/Queue/PrintJob.cs index 9c5a99b..b6ea627 100644 --- a/Inspectron.Epson/Queue/PrintJob.cs +++ b/Inspectron.Epson/Queue/PrintJob.cs @@ -4,7 +4,7 @@ namespace Inspectron.Epson.Queue; public class PrintJob { - // todo: change to ip address + public string JobId { get; set; } public string IP { get; set; } public PrintJobFromSignalR Document { get; set; } public DateTime QueuedAt { get; set; } diff --git a/Inspectron.Epson/Queue/PrintJobStatus.cs b/Inspectron.Epson/Queue/PrintJobStatus.cs index 8458613..8fb4b23 100644 --- a/Inspectron.Epson/Queue/PrintJobStatus.cs +++ b/Inspectron.Epson/Queue/PrintJobStatus.cs @@ -2,7 +2,7 @@ namespace Inspectron.Epson.Queue; public enum PrintJobStatus { - Received, - Completed, - Failed + Received=1, + Completed=2, + Failed=3 } \ No newline at end of file diff --git a/Inspectron.Epson/Queue/PrintServer.cs b/Inspectron.Epson/Queue/PrintServer.cs index 4f6b2ef..19f5409 100644 --- a/Inspectron.Epson/Queue/PrintServer.cs +++ b/Inspectron.Epson/Queue/PrintServer.cs @@ -106,6 +106,7 @@ public class PrintServer else { _logger.LogWarning( $"Printer {printerIp} not found. Job cannot be submitted."); + _statusReporter.ReportStatusAsync(job, PrintJobStatus.Failed); } }