47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using Inspectron.Epson.PrintServer.PrintServices;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Inspectron.Epson.PrintServer;
|
|
|
|
public class PrintLoop
|
|
{
|
|
private readonly global::PrintServer _printServer;
|
|
private readonly IPrintService _printService;
|
|
private readonly IPrintJobSource _jobSource;
|
|
private readonly IAssignedPrinterRepository _assignedPrinterRepository;
|
|
private readonly ILogger _logger;
|
|
private CancellationTokenSource? _cancellationSource;
|
|
private CancellationToken _cancellationToken;
|
|
|
|
public PrintLoop(global::PrintServer printServer,IPrintService printService, IPrintJobSource jobSource, IAssignedPrinterRepository assignedPrinterRepository, ILogger logger)
|
|
{
|
|
_printServer = printServer;
|
|
_printService = printService;
|
|
_jobSource = jobSource;
|
|
_assignedPrinterRepository = assignedPrinterRepository;
|
|
_logger = logger;
|
|
}
|
|
|
|
public Task StartAsync()
|
|
{
|
|
_cancellationSource = new CancellationTokenSource();
|
|
_cancellationToken = _cancellationSource.Token;
|
|
_= Task.Run(Loop);
|
|
return Task.CompletedTask;
|
|
}
|
|
public async Task Loop()
|
|
{
|
|
while (!_cancellationSource!.Token.IsCancellationRequested)
|
|
{
|
|
var job = await _jobSource.GetNextJobAsync(_cancellationToken);
|
|
|
|
_printServer.SubmitJob(job.IP, job);
|
|
}
|
|
}
|
|
|
|
public Task StopAsync()
|
|
{
|
|
_cancellationSource!.Cancel();
|
|
return Task.CompletedTask;
|
|
}
|
|
} |