2025-02-16 13:36:37 -03:00
|
|
|
using AutoScan.Interfaces;
|
2025-02-15 15:55:29 -03:00
|
|
|
using AutoScan.Options;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Quartz;
|
|
|
|
|
|
|
|
namespace AutoScan.Jobs;
|
|
|
|
|
|
|
|
public class DownloaderJob : IJob
|
|
|
|
{
|
|
|
|
private readonly ILogger<DownloaderJob> _logger;
|
|
|
|
private readonly AutoScanOptions _options;
|
2025-02-16 13:36:37 -03:00
|
|
|
private readonly IDVRConnector _dvrConnector;
|
2025-02-15 15:55:29 -03:00
|
|
|
|
2025-02-16 13:36:37 -03:00
|
|
|
public DownloaderJob(ILogger<DownloaderJob> logger, IOptionsSnapshot<AutoScanOptions> options, IDVRConnector dvrConnector)
|
2025-02-15 15:55:29 -03:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_options = options.Value;
|
2025-02-16 13:36:37 -03:00
|
|
|
_dvrConnector = dvrConnector;
|
2025-02-15 15:55:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute(IJobExecutionContext context)
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Scheduled scan executed at {At}", DateTime.Now);
|
|
|
|
|
|
|
|
if (_options.MediaFolder is null)
|
|
|
|
{
|
|
|
|
_logger.LogError("MediaFolder is not set in options!");
|
|
|
|
context.Result = new JobResult()
|
|
|
|
{
|
|
|
|
Status = JobResultStatus.JobFailed,
|
|
|
|
Result = "MediaFolder is not set in options!"
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-16 13:36:37 -03:00
|
|
|
//time to start retrieving videos
|
2025-02-15 15:55:29 -03: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 minutes = _options.From.Split(":")[1];
|
|
|
|
var hours = _options.From.Split(":")[0];
|
|
|
|
var from = new DateTime(now.Year, now.Month, now.Day, int.Parse(hours), int.Parse(minutes), 0);
|
|
|
|
if(_options.FromDayBefore)
|
|
|
|
from = from.AddDays(-1);
|
|
|
|
|
|
|
|
//create to variable with the datetime of last night with the variables in options.To (1:00) [today]
|
|
|
|
//for example, if options.To is 1:00, to should be today at 1:00
|
|
|
|
minutes = _options.To.Split(":")[1];
|
|
|
|
hours = _options.To.Split(":")[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);
|
2025-02-17 13:42:14 -03:00
|
|
|
var videos = await _dvrConnector.FetchMonitorVideosBetween(from, to, context.CancellationToken);
|
2025-02-15 15:55:29 -03:00
|
|
|
|
|
|
|
//if the amount of videos is greater than the max amount in options, log a warning
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
CleanMediaFolder();
|
|
|
|
|
|
|
|
//download each video to the media folder
|
|
|
|
foreach (var video in videos)
|
|
|
|
{
|
|
|
|
_logger.LogDebug("Downloading video {Filename}", video.filename);
|
2025-02-17 13:42:14 -03:00
|
|
|
await _dvrConnector.DownloadMonitorVideo(video, _options.MediaFolder, context.CancellationToken);
|
2025-02-15 15:55:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
context.Result = new JobResult()
|
|
|
|
{
|
|
|
|
Status = JobResultStatus.JobSucceeded,
|
|
|
|
Result = $"Downloaded {videos.Count} videos to {_options.MediaFolder}"
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CleanMediaFolder()
|
|
|
|
{
|
|
|
|
if (_options.MediaFolder is not null)
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(_options.MediaFolder)!);
|
|
|
|
foreach (var file in Directory.GetFiles(_options.MediaFolder))
|
|
|
|
{
|
|
|
|
File.Delete(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_options.Scanner?.DetectionFolder is not null)
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(_options.Scanner.DetectionFolder)!);
|
|
|
|
foreach (var file in Directory.GetFiles(_options.Scanner.DetectionFolder))
|
|
|
|
{
|
|
|
|
File.Delete(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_options.Screenshot?.Folder is not null)
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(_options.Screenshot.Folder)!);
|
|
|
|
foreach (var file in Directory.GetFiles(_options.Screenshot.Folder))
|
|
|
|
{
|
|
|
|
File.Delete(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|