using System.Collections.Concurrent; using ErsatzTV.Core.Interfaces.Metadata; using MediatR; namespace ErsatzTV.Core.Metadata; public class ScannerProxyService(IMediator mediator) : IScannerProxyService { private readonly ConcurrentDictionary _activeLibraries = []; private readonly ConcurrentDictionary _scans = new(); public Option StartScan(int libraryId) { if (!_activeLibraries.TryAdd(libraryId, 0)) { return Option.None; } var buildId = Guid.NewGuid(); _scans[buildId] = libraryId; return buildId; } public void EndScan(Guid scanId) { if (_scans.TryRemove(scanId, out int libraryId)) { _activeLibraries.TryRemove(libraryId, out _); } } public async Task Progress(Guid scanId, decimal percentComplete) { //logger.LogInformation("Scanning {ScanId}", scanId); if (_scans.TryGetValue(scanId, out int libraryId)) { //logger.LogDebug("Scan progress {Progress} for library {LibraryId}", percentComplete, libraryId); _activeLibraries[libraryId] = percentComplete; var progress = new LibraryScanProgress(libraryId, percentComplete); await mediator.Publish(progress); } } public bool IsActive(Guid scanId) => _scans.ContainsKey(scanId); public Option GetProgress(int libraryId) => _activeLibraries.TryGetValue(libraryId, out decimal progress) ? progress : Option.None; }