Add project files.
This commit is contained in:
66
Inspectron.Epson/Queue/PrintServer.cs
Normal file
66
Inspectron.Epson/Queue/PrintServer.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
public class PrintServer
|
||||
{
|
||||
private readonly IPrintService _printService;
|
||||
private readonly ILogger _logger;
|
||||
private readonly Dictionary<string, PrinterQueue> _printerQueues;
|
||||
|
||||
public PrintServer(IPrintService printService, ILogger logger)
|
||||
{
|
||||
_printService = printService;
|
||||
_logger = logger;
|
||||
_printerQueues = new Dictionary<string, PrinterQueue>();
|
||||
|
||||
}
|
||||
|
||||
public void RegisterPrinter(string printerIp)
|
||||
{
|
||||
if (_printerQueues.ContainsKey(printerIp))
|
||||
return;
|
||||
|
||||
var queue = new PrinterQueue(printerIp, _printService,_logger);
|
||||
_printerQueues[printerIp] = queue;
|
||||
queue.Start();
|
||||
Console.WriteLine($"Printer {printerIp} registered");
|
||||
}
|
||||
|
||||
public void UnregisterPrinter(string printerIp)
|
||||
{
|
||||
if (_printerQueues.TryGetValue(printerIp, out var queue))
|
||||
{
|
||||
queue.StopAsync().Wait();
|
||||
_printerQueues.Remove(printerIp);
|
||||
Console.WriteLine($"Printer {printerIp} unregistered");
|
||||
}
|
||||
}
|
||||
|
||||
public void SubmitJob(string printerIp, PrintJob job)
|
||||
{
|
||||
if (_printerQueues.TryGetValue(printerIp, out var queue))
|
||||
{
|
||||
queue.Enqueue(job);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning( $"Printer {printerIp} not found. Job cannot be submitted.");
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<string, int> GetQueueStatus()
|
||||
{
|
||||
var status = new Dictionary<string, int>();
|
||||
foreach (var kvp in _printerQueues)
|
||||
{
|
||||
status[kvp.Key] = kvp.Value.QueueLength;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
public async Task ShutdownAsync()
|
||||
{
|
||||
var stopTasks = _printerQueues.Values.Select(q => q.StopAsync());
|
||||
await Task.WhenAll(stopTasks);
|
||||
Console.WriteLine("Print server shut down");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user