diff --git a/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs b/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs index e519a80..e01b1e7 100644 --- a/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs +++ b/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs @@ -20,7 +20,7 @@ public class ShinobiConnector : IDVRConnector _options = options.Value; } - public async Task> FetchMonitorVideosBetween(DateTime from, DateTime to) + public async Task> FetchMonitorVideosBetween(DateTime from, DateTime to, CancellationToken cancellationToken = default) { var endpoint = $"{_options.URL}/{_options.APIKey}/videos/{_options.GroupId}/{_options.MonitorId}"; endpoint += $"?start={from:yyyy-MM-ddTHH:mm:sszzz}&end={to:yyyy-MM-ddTHH:mm:sszzz}"; @@ -28,7 +28,7 @@ public class ShinobiConnector : IDVRConnector _logger.LogDebug("Fetching videos details from endpoint: {Endpoint}", endpoint); //get from the server the response with type VideoDetails - var response = await _httpClient.GetFromJsonAsync(endpoint); + var response = await _httpClient.GetFromJsonAsync(endpoint, cancellationToken); if (response is null) { @@ -49,7 +49,7 @@ public class ShinobiConnector : IDVRConnector } - public async Task DownloadMonitorVideo(VideoDetail video, string downloadFolder) + public async Task DownloadMonitorVideo(VideoDetail video, string downloadFolder, CancellationToken cancellationToken = default) { var endpoint = $"{_options.URL}{video.href}"; _logger.LogDebug("Fetching video from endpoint: {Endpoint}", endpoint); @@ -61,7 +61,7 @@ public class ShinobiConnector : IDVRConnector try { _logger.LogDebug("Downloading video..."); - var videoData = await _httpClient.GetByteArrayAsync(endpoint); + var videoData = await _httpClient.GetByteArrayAsync(endpoint, cancellationToken); if(_options.RunDry) { @@ -69,7 +69,7 @@ public class ShinobiConnector : IDVRConnector return; } //Write the video to the file - await File.WriteAllBytesAsync(videoPath, videoData); + await File.WriteAllBytesAsync(videoPath, videoData, cancellationToken); _logger.LogInformation("Video downloaded successfully!"); } catch (Exception ex) diff --git a/src/CasaBot/AutoScan/Interfaces/IDVRConnector.cs b/src/CasaBot/AutoScan/Interfaces/IDVRConnector.cs index 1ec664f..e13878a 100644 --- a/src/CasaBot/AutoScan/Interfaces/IDVRConnector.cs +++ b/src/CasaBot/AutoScan/Interfaces/IDVRConnector.cs @@ -4,6 +4,6 @@ namespace AutoScan.Interfaces; public interface IDVRConnector { - Task> FetchMonitorVideosBetween(DateTime from, DateTime to); - Task DownloadMonitorVideo(VideoDetail video, string downloadFolder); + Task> FetchMonitorVideosBetween(DateTime from, DateTime to, CancellationToken cancellationToken = default); + Task DownloadMonitorVideo(VideoDetail video, string downloadFolder, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs b/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs index f8b09ad..24645c9 100644 --- a/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs +++ b/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs @@ -50,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 _dvrConnector.FetchMonitorVideosBetween(from, to); + 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) @@ -65,7 +65,7 @@ public class DownloaderJob : IJob foreach (var video in videos) { _logger.LogDebug("Downloading video {Filename}", video.filename); - await _dvrConnector.DownloadMonitorVideo(video, _options.MediaFolder); + await _dvrConnector.DownloadMonitorVideo(video, _options.MediaFolder, context.CancellationToken); } context.Result = new JobResult()