46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
|
|||
|
namespace Seyounth.Auto.Hs.Runtime.Scanner;
|
|||
|
|
|||
|
public class ScannerService : IScannerService
|
|||
|
{
|
|||
|
public ScannerService(ILogger<ScannerService> logger)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
//todo:向_scanners里添加HikScanner
|
|||
|
|
|||
|
_scanners.ForEach(x => x.OnScanned += (barcode) => OnScanned?.Invoke(x, barcode));
|
|||
|
}
|
|||
|
|
|||
|
public IReadOnlyList<IScanner> Scanners => _scanners;
|
|||
|
|
|||
|
private readonly ILogger<ScannerService> _logger;
|
|||
|
|
|||
|
private readonly List<IScanner> _scanners = new List<IScanner>();
|
|||
|
|
|||
|
public async Task StartAsync()
|
|||
|
{
|
|||
|
await Task.WhenAll(_scanners.Select(x => x.ConnectAsync()
|
|||
|
.ContinueWith(t =>
|
|||
|
{
|
|||
|
if (t.IsCompletedSuccessfully)
|
|||
|
_logger.LogInformation($"Scanner {x.Id} connected successfully.");
|
|||
|
else
|
|||
|
_logger.LogError($"Scanner {x.Id} failed to connect, error: {t.Exception?.Message}");
|
|||
|
})));
|
|||
|
}
|
|||
|
|
|||
|
public async Task StopAsync()
|
|||
|
{
|
|||
|
await Task.WhenAll(_scanners.Select(x => x.DisconnectAsync()
|
|||
|
.ContinueWith(t =>
|
|||
|
{
|
|||
|
if (t.IsCompletedSuccessfully)
|
|||
|
_logger.LogInformation($"Scanner {x.Id} disconnected.");
|
|||
|
else
|
|||
|
_logger.LogError($"Scanner {x.Id} failed to disconnect, error: {t.Exception?.Message}");
|
|||
|
})));
|
|||
|
}
|
|||
|
|
|||
|
public event Action<IScanner, string>? OnScanned;
|
|||
|
}
|