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

View File

@ -1,9 +1,10 @@
using AutoScan.Interfaces;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Diagnostics; using System.Diagnostics;
namespace CasaBotApp; namespace AutoScan.Implementations;
public class DVRScanner public class DVRScanner : IDVRScanner
{ {
private readonly ILogger<DVRScanner> _logger; private readonly ILogger<DVRScanner> _logger;
private const string _mediaPath = @".\media"; // Replace with your desired file path 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.Models;
using AutoScan.Options; using AutoScan.Options;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Net.Http.Json; using System.Net.Http.Json;
namespace AutoScan; namespace AutoScan.Implementations;
public class ShinobiConnector public class ShinobiConnector : IDVRConnector
{ {
private readonly ILogger<ShinobiConnector> _logger; private readonly ILogger<ShinobiConnector> _logger;
private readonly HttpClient _httpClient; 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 AutoScan.Options;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -9,13 +10,13 @@ public class DownloaderJob : IJob
{ {
private readonly ILogger<DownloaderJob> _logger; private readonly ILogger<DownloaderJob> _logger;
private readonly AutoScanOptions _options; 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; _logger = logger;
_options = options.Value; _options = options.Value;
_shinobiConnector = shinobiConnector; _dvrConnector = dvrConnector;
} }
public async Task Execute(IJobExecutionContext context) public async Task Execute(IJobExecutionContext context)
@ -33,7 +34,7 @@ public class DownloaderJob : IJob
return; 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 //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 now = DateTime.Now;
var minutes = _options.From.Split(":")[1]; 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); 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); _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 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)
@ -64,7 +65,7 @@ public class DownloaderJob : IJob
foreach (var video in videos) foreach (var video in videos)
{ {
_logger.LogDebug("Downloading video {Filename}", video.filename); _logger.LogDebug("Downloading video {Filename}", video.filename);
await _shinobiConnector.DownloadMonitorVideo(video, _options.MediaFolder); await _dvrConnector.DownloadMonitorVideo(video, _options.MediaFolder);
} }
context.Result = new JobResult() context.Result = new JobResult()

View File

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