50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
|
|||
|
namespace Seyounth.Auto.Hs.Runtime.Printer;
|
|||
|
|
|||
|
public class PrinterService : IPrinterService
|
|||
|
{
|
|||
|
public IReadOnlyList<IPrinter> Printers => _printers;
|
|||
|
|
|||
|
private readonly ILogger<PrinterService> _logger;
|
|||
|
|
|||
|
private readonly List<IPrinter> _printers = new List<IPrinter>();
|
|||
|
|
|||
|
public PrinterService(ILogger<PrinterService> logger)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
//todo: load printers from configuration or new
|
|||
|
}
|
|||
|
|
|||
|
public async Task StartAsync()
|
|||
|
{
|
|||
|
await Task.WhenAll(Printers.Select(x => x.ConnectAsync()
|
|||
|
.ContinueWith(t =>
|
|||
|
{
|
|||
|
if (t.IsCompletedSuccessfully)
|
|||
|
_logger.LogInformation($"Printer {x.Id} connected");
|
|||
|
else
|
|||
|
_logger.LogError(t.Exception, $"Printer {x.Id} failed to connect,error: {t.Exception.Message}");
|
|||
|
})));
|
|||
|
}
|
|||
|
|
|||
|
public async Task StopAsync()
|
|||
|
{
|
|||
|
await Task.WhenAll(Printers.Select(x => x.DisconnectAsync()
|
|||
|
.ContinueWith(t =>
|
|||
|
{
|
|||
|
if (t.IsCompletedSuccessfully)
|
|||
|
_logger.LogInformation($"Printer {x.Id} disconnected");
|
|||
|
else
|
|||
|
_logger.LogError(t.Exception, $"Printer {x.Id} failed to disconnect,error: {t.Exception.Message}");
|
|||
|
})));
|
|||
|
}
|
|||
|
|
|||
|
public Task PrintAsync(int id, string content)
|
|||
|
{
|
|||
|
var printer = Printers.FirstOrDefault(x => x.Id == id);
|
|||
|
if (printer == null)
|
|||
|
throw new ArgumentException("Printer not found", nameof(id));
|
|||
|
return printer.PrintAsync(content);
|
|||
|
}
|
|||
|
}
|