refactor: extract dvr scanner and connector to interfaces

This commit is contained in:
Guillermo Marcel 2025-02-16 13:36:37 -03:00
parent 27dfcb291a
commit 461c545cb0
7 changed files with 40 additions and 13 deletions

View File

@ -1,3 +1,5 @@
using AutoScan.Implementations;
using AutoScan.Interfaces;
using AutoScan.Listener;
using CasaBotApp;
using Microsoft.Extensions.DependencyInjection;
@ -9,8 +11,9 @@ public static class DependencyInjectionExtensions
{
public static void AddAutoScan(this IServiceCollection services)
{
services.AddSingleton<ShinobiConnector>();
services.AddSingleton<DVRScanner>();
services.AddSingleton<IDVRConnector, ShinobiConnector>();
services.AddSingleton<IDVRScanner, DVRScanner>();
services.AddSingleton<FfmpegWrapper>();
services.AddSingleton<AutoScanApp>();
services.AddQuartz(q =>

View File

@ -1,9 +1,10 @@
using AutoScan.Interfaces;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
namespace CasaBotApp;
namespace AutoScan.Implementations;
public class DVRScanner
public class DVRScanner : IDVRScanner
{
private readonly ILogger<DVRScanner> _logger;
private const string _mediaPath = @".\media"; // Replace with your desired file path

View File

@ -1,12 +1,13 @@
using AutoScan.Interfaces;
using AutoScan.Models;
using AutoScan.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Net.Http.Json;
namespace AutoScan;
namespace AutoScan.Implementations;
public class ShinobiConnector
public class ShinobiConnector : IDVRConnector
{
private readonly ILogger<ShinobiConnector> _logger;
private readonly HttpClient _httpClient;

View File

@ -0,0 +1,9 @@
using AutoScan.Models;
namespace AutoScan.Interfaces;
public interface IDVRConnector
{
Task<List<VideoDetail>> FetchMonitorVideosBetween(DateTime from, DateTime to);
Task DownloadMonitorVideo(VideoDetail video, string downloadFolder);
}

View File

@ -0,0 +1,6 @@
namespace AutoScan.Interfaces;
public interface IDVRScanner
{
Task ScanVideos(CancellationToken cancellationToken = default);
}

View File

@ -1,3 +1,4 @@
using AutoScan.Interfaces;
using AutoScan.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@ -9,13 +10,13 @@ public class DownloaderJob : IJob
{
private readonly ILogger<DownloaderJob> _logger;
private readonly AutoScanOptions _options;
private readonly ShinobiConnector _shinobiConnector;
private readonly IDVRConnector _dvrConnector;
public DownloaderJob(ILogger<DownloaderJob> logger, IOptionsSnapshot<AutoScanOptions> options, ShinobiConnector shinobiConnector)
public DownloaderJob(ILogger<DownloaderJob> logger, IOptionsSnapshot<AutoScanOptions> options, IDVRConnector dvrConnector)
{
_logger = logger;
_options = options.Value;
_shinobiConnector = shinobiConnector;
_dvrConnector = dvrConnector;
}
public async Task Execute(IJobExecutionContext context)
@ -33,7 +34,7 @@ public class DownloaderJob : IJob
return;
}
//create from variable with the datetime of last night with the variables in options.From (23:00) and options.FromDayBefore (true) [yesterday]
//time to start retrieving videos
//for example, if options.From is 23:00 and options.FromDayBefore is true, from should be yesterday at 23:00
var now = DateTime.Now;
var minutes = _options.From.Split(":")[1];
@ -49,7 +50,7 @@ public class DownloaderJob : IJob
var to = new DateTime(now.Year, now.Month, now.Day, int.Parse(hours), int.Parse(minutes), 0);
_logger.LogInformation("Fetching videos from {From} to {To}", from, to);
var videos = await _shinobiConnector.FetchMonitorVideosBetween(from, to);
var videos = await _dvrConnector.FetchMonitorVideosBetween(from, to);
//if the amount of videos is greater than the max amount in options, log a warning
if (_options.MaxAmount > 0 && videos.Count > _options.MaxAmount)
@ -64,7 +65,7 @@ public class DownloaderJob : IJob
foreach (var video in videos)
{
_logger.LogDebug("Downloading video {Filename}", video.filename);
await _shinobiConnector.DownloadMonitorVideo(video, _options.MediaFolder);
await _dvrConnector.DownloadMonitorVideo(video, _options.MediaFolder);
}
context.Result = new JobResult()

View File

@ -1,3 +1,4 @@
using AutoScan.Interfaces;
using Microsoft.Extensions.Logging;
using Quartz;
@ -6,13 +7,18 @@ namespace AutoScan.Jobs;
public class ScannerJob : IJob
{
private readonly ILogger<ScannerJob> _logger;
public ScannerJob(ILogger<ScannerJob> logger)
private readonly IDVRScanner _scanner;
public ScannerJob(ILogger<ScannerJob> logger, IDVRScanner scanner)
{
_logger = logger;
_scanner = scanner;
}
public Task Execute(IJobExecutionContext context)
{
_logger.LogWarning("ScannerJob is not implemented yet!");
_logger.LogInformation("Scanner {scannerName} is ready to scan!", _scanner.GetType().Name);
return Task.CompletedTask;
}
}