2025-06-04 09:42:48 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Seyounth.Auto.Hs.Runtime.Scanner;
|
|
|
|
|
|
|
|
|
|
public class ScannerService : IScannerService
|
|
|
|
|
{
|
2025-06-24 13:58:06 +08:00
|
|
|
|
public ScannerService(ILogger<ScannerService> logger,IEnumerable<IScanner> scanners)
|
2025-06-04 09:42:48 +08:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2025-06-24 13:58:06 +08:00
|
|
|
|
_scanners.AddRange(scanners);
|
2025-06-04 09:42:48 +08:00
|
|
|
|
//todo:向_scanners里添加HikScanner
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|