diff --git a/src/CasaBot/AutoScan/Implementations/DVRScanner.cs b/src/CasaBot/AutoScan/Implementations/DVRScanner.cs index c07fed6..041c561 100644 --- a/src/CasaBot/AutoScan/Implementations/DVRScanner.cs +++ b/src/CasaBot/AutoScan/Implementations/DVRScanner.cs @@ -1,46 +1,56 @@ using AutoScan.Interfaces; +using AutoScan.Options; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System.Diagnostics; namespace AutoScan.Implementations; public class DVRScanner : IDVRScanner { - private readonly ILogger _logger; - private const string _mediaPath = @".\media"; // Replace with your desired file path - private const string _dvrScannerFile = @".\dvr-scanner\dvr-scan.exe"; - private const string _dvrScannerConfig = @".\dvr-scanner\dvr-scan.cfg"; - - //.\dvr-scanner\dvr-scan.exe -i .\media\*.mp4 -c .\dvr-scanner\dvr-scan.cfg - public DVRScanner(ILogger logger) + private readonly ILogger _logger; + private readonly AutoScanOptions _options; + + private DateTime? _startedAt; + public DVRScanner(IOptionsSnapshot options, ILoggerFactory loggerFactory) { - _logger = logger; + _options = options.Value; + _logger = loggerFactory.CreateLogger(nameof(DVRScanner)); } public async Task ScanVideos(CancellationToken cancellationToken = default) { + if(_options.Scanner == null) + { + throw new ArgumentNullException(nameof(_options.Scanner)); + } try { - //make sure the directory exists - Directory.CreateDirectory(Path.GetDirectoryName(_mediaPath)!); - - _logger.LogDebug("Scanning videos..."); + var folderParam = Path.Combine(_options.MediaFolder!, "*.mp4"); + var arguments = $"-i {folderParam} -c {_options.Scanner.ConfigFile} --thumbnails highscore"; + _logger.LogDebug("Executing command: {_dvrScannerFile} {arguments}", _options.Scanner.Exe, arguments); var process = new Process { StartInfo = new ProcessStartInfo { - FileName = _dvrScannerFile, - Arguments = $"-i {_mediaPath}\\*.mp4 -c {_dvrScannerConfig}", + FileName = _options.Scanner.Exe, + Arguments = arguments, RedirectStandardOutput = true, + RedirectStandardError = false, UseShellExecute = false, CreateNoWindow = true, - - } }; + if(_options.Scanner.RunDry) + { + _logger.LogInformation("Dry run enabled, skipping execution..."); + return; + } + _startedAt = DateTime.Now; process.Start(); + process.PriorityClass = ProcessPriorityClass.High; cancellationToken.Register(() => { _logger.LogDebug("DVR Process status: ID: {Id}, HasExited: {HasExited}, Responding: {Responding}", @@ -52,9 +62,8 @@ public class DVRScanner : IDVRScanner }); _logger.LogDebug("DVR Process started with ID: {Id}", process.Id); - await process.WaitForExitAsync(cancellationToken); - + await UpdateProcessUntilExits(process, cancellationToken); _logger.LogInformation("Videos scanned successfully!"); } catch (Exception ex) @@ -62,4 +71,17 @@ public class DVRScanner : IDVRScanner _logger.LogError(ex, "An error occurred while scanning the videos"); } } + + private async Task UpdateProcessUntilExits(Process process, CancellationToken cancellationToken = default) + { + while (!process.HasExited && !cancellationToken.IsCancellationRequested) + { + + await Task.Delay(15000, cancellationToken); + + var duration = DateTime.Now - _startedAt; + _logger.LogTrace("[{duration}] DVR Scan running", duration); + + } + } } \ No newline at end of file diff --git a/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs b/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs index e01b1e7..34aeda0 100644 --- a/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs +++ b/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs @@ -38,15 +38,14 @@ public class ShinobiConnector : IDVRConnector _logger.LogInformation("Found {Count} videos in the specified range", response.videos.Count); - foreach (var video in response.videos.OrderBy(x => x.time)) + + return response.videos.Select(video => { video.end = video.end.ToLocalTime(); video.time = video.time.ToLocalTime(); _logger.LogDebug("Video: {Filename} - Time: {time} - Ends: {end}", video.filename, video.time, video.end); - } - - return response.videos; - + return video; + }).OrderBy(x => x.time).ToList(); } public async Task DownloadMonitorVideo(VideoDetail video, string downloadFolder, CancellationToken cancellationToken = default) diff --git a/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs b/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs index 24645c9..041e016 100644 --- a/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs +++ b/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs @@ -53,7 +53,7 @@ public class DownloaderJob : IJob var videos = await _dvrConnector.FetchMonitorVideosBetween(from, to, context.CancellationToken); //if the amount of videos is greater than the max amount in options, log a warning - if (_options.MaxAmount > 0 && videos.Count > _options.MaxAmount) + if (_options.MaxAmount > 0 && videos.Count > _options.MaxAmount) { _logger.LogWarning("Amount of videos fetched is greater than the max amount in options ({MaxAmount})", _options.MaxAmount); videos = videos.Take(_options.MaxAmount).ToList(); diff --git a/src/CasaBot/AutoScan/Jobs/ScannerJob.cs b/src/CasaBot/AutoScan/Jobs/ScannerJob.cs index db76fb0..e13cc93 100644 --- a/src/CasaBot/AutoScan/Jobs/ScannerJob.cs +++ b/src/CasaBot/AutoScan/Jobs/ScannerJob.cs @@ -16,8 +16,9 @@ public class ScannerJob : IJob } public Task Execute(IJobExecutionContext context) { - _logger.LogWarning("ScannerJob is not implemented yet!"); _logger.LogInformation("Scanner {scannerName} is ready to scan!", _scanner.GetType().Name); + + _scanner.ScanVideos(); return Task.CompletedTask; }