mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.5 KiB
52 lines
1.5 KiB
using System.Collections.Concurrent; |
|
using ErsatzTV.Core.Interfaces.Metadata; |
|
using MediatR; |
|
|
|
namespace ErsatzTV.Core.Metadata; |
|
|
|
public class ScannerProxyService(IMediator mediator) : IScannerProxyService |
|
{ |
|
private readonly ConcurrentDictionary<int, decimal> _activeLibraries = []; |
|
private readonly ConcurrentDictionary<Guid, int> _scans = new(); |
|
|
|
public Option<Guid> StartScan(int libraryId) |
|
{ |
|
if (!_activeLibraries.TryAdd(libraryId, 0)) |
|
{ |
|
return Option<Guid>.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<decimal> GetProgress(int libraryId) => _activeLibraries.TryGetValue(libraryId, out decimal progress) |
|
? progress |
|
: Option<decimal>.None; |
|
}
|
|
|