feat: add snapshot support for docker
This commit is contained in:
parent
c3bd0e5933
commit
ea4c3bb64c
@ -72,7 +72,7 @@ public class AutoScanApp
|
|||||||
.AddJob(downloaderJob)
|
.AddJob(downloaderJob)
|
||||||
.AddJob(scannerJob)
|
.AddJob(scannerJob)
|
||||||
.AddJob(cleanJob)
|
.AddJob(cleanJob)
|
||||||
.OnFinish(async () => await OnScanCompleted?.Invoke())
|
.OnFinish(OnScanCompleted)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
_scheduler.ListenerManager.AddJobListener(chainer, GroupMatcher<JobKey>.GroupEquals(GROUP_NAME));
|
_scheduler.ListenerManager.AddJobListener(chainer, GroupMatcher<JobKey>.GroupEquals(GROUP_NAME));
|
||||||
@ -107,7 +107,6 @@ public class AutoScanApp
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private string CronFromAt(string at)
|
private string CronFromAt(string at)
|
||||||
{
|
{
|
||||||
var parts = at.Split(':');
|
var parts = at.Split(':');
|
||||||
|
@ -17,6 +17,7 @@ public static class DependencyInjectionExtensions
|
|||||||
|
|
||||||
services.AddSingleton<FfmpegWrapper>();
|
services.AddSingleton<FfmpegWrapper>();
|
||||||
services.AddSingleton<AutoScanApp>();
|
services.AddSingleton<AutoScanApp>();
|
||||||
|
services.AddSingleton<ISnapshoter, Snapshoter>();
|
||||||
services.AddQuartz();
|
services.AddQuartz();
|
||||||
services.AddTransient<IChainerListenerFactory, ChainerListenerFactory>();
|
services.AddTransient<IChainerListenerFactory, ChainerListenerFactory>();
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ public class ShinobiConnector : IDVRConnector
|
|||||||
_logger.LogDebug("Fetching video stream from endpoint: {Endpoint}", endpoint);
|
_logger.LogDebug("Fetching video stream from endpoint: {Endpoint}", endpoint);
|
||||||
var monitors = await _httpClient.GetFromJsonAsync<Monitor[]>(endpoint);
|
var monitors = await _httpClient.GetFromJsonAsync<Monitor[]>(endpoint);
|
||||||
|
|
||||||
_cachedVideoStream = monitors?.FirstOrDefault()?.MonitorDetail?.auto_host ?? null;
|
_cachedVideoStream = monitors?.FirstOrDefault()?.MonitorDetail?.MonitorStreamUrl ?? null;
|
||||||
return _cachedVideoStream ?? "";
|
return _cachedVideoStream ?? "";
|
||||||
}
|
}
|
||||||
}
|
}
|
96
src/CasaBot/AutoScan/Implementations/Snapshoter.cs
Normal file
96
src/CasaBot/AutoScan/Implementations/Snapshoter.cs
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
using AutoScan.Interfaces;
|
||||||
|
using AutoScan.Options;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace AutoScan.Implementations;
|
||||||
|
|
||||||
|
public class Snapshoter : ISnapshoter
|
||||||
|
{
|
||||||
|
private readonly IDVRConnector _dvrConnector;
|
||||||
|
private readonly AutoScanOptions _options;
|
||||||
|
private readonly ILogger<Snapshoter> _logger;
|
||||||
|
|
||||||
|
public Snapshoter(IDVRConnector dvrConnector, IOptions<AutoScanOptions> options, ILogger<Snapshoter> logger)
|
||||||
|
{
|
||||||
|
_dvrConnector = dvrConnector;
|
||||||
|
_options = options.Value;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string?> TakeSnapshot()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_options.Scanner?.FFMpeg is null)
|
||||||
|
{
|
||||||
|
_logger.LogError("FFMpeg path is not set in the options");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var timer = new Stopwatch();
|
||||||
|
timer.Start();
|
||||||
|
var outputDir = _options.Scanner?.SnapshotFolder;
|
||||||
|
if (string.IsNullOrEmpty(outputDir))
|
||||||
|
{
|
||||||
|
_logger.LogError("Snapshot folder is not set in the options");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
var outputPath = Path.Combine(outputDir, "snp.jpeg");
|
||||||
|
//create if doesnt exists
|
||||||
|
if (!Directory.Exists(outputDir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(outputDir);
|
||||||
|
}
|
||||||
|
var originalFeed = await _dvrConnector.GetVideoStream();
|
||||||
|
var ffmArgs = $"-y -rtsp_transport tcp -i \"{originalFeed}\" -ss 00:00:00.500 -frames:v 1 {outputPath}";
|
||||||
|
|
||||||
|
|
||||||
|
var process = new Process
|
||||||
|
{
|
||||||
|
StartInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
//To change this, I need to make sure ffmpeg is installed
|
||||||
|
FileName = _options.Scanner?.FFMpeg,
|
||||||
|
Arguments = ffmArgs,
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
RedirectStandardError = true,
|
||||||
|
UseShellExecute = false,
|
||||||
|
CreateNoWindow = true,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
process.Start();
|
||||||
|
|
||||||
|
var timeoutSignal = new CancellationTokenSource(TimeSpan.FromSeconds(6));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await process.WaitForExitAsync(timeoutSignal.Token);
|
||||||
|
} catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
_logger.LogError("Taking snapshot timed out");
|
||||||
|
process.Kill();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// you can read the output here.
|
||||||
|
// var output = await process.StandardOutput.ReadToEndAsync();
|
||||||
|
// var error = await process.StandardError.ReadToEndAsync();
|
||||||
|
timer.Stop();
|
||||||
|
_logger.LogDebug("Taking snapshot took {Elapsed} ms", timer.ElapsedMilliseconds);
|
||||||
|
|
||||||
|
if (process.ExitCode != 0)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error taking snapshot, exit code: {ExitCode}", process.ExitCode);
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputPath;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error taking snapshot");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
src/CasaBot/AutoScan/Interfaces/ISnapshoter.cs
Normal file
6
src/CasaBot/AutoScan/Interfaces/ISnapshoter.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace AutoScan.Interfaces;
|
||||||
|
|
||||||
|
public interface ISnapshoter
|
||||||
|
{
|
||||||
|
Task<string?> TakeSnapshot();
|
||||||
|
}
|
@ -9,7 +9,7 @@ public class ChainerListener : JobListenerSupport
|
|||||||
{
|
{
|
||||||
private readonly ILogger<ChainerListener> _logger;
|
private readonly ILogger<ChainerListener> _logger;
|
||||||
private readonly Dictionary<JobKey, JobKey> _chainLinks;
|
private readonly Dictionary<JobKey, JobKey> _chainLinks;
|
||||||
public Func<Task> OnJobChainFinished { get; set; }
|
public Func<Task>? OnJobChainFinished { get; set; }
|
||||||
|
|
||||||
public ChainerListener(string name, ILogger<ChainerListener> logger)
|
public ChainerListener(string name, ILogger<ChainerListener> logger)
|
||||||
{
|
{
|
||||||
@ -56,7 +56,8 @@ public class ChainerListener : JobListenerSupport
|
|||||||
if (_chainLinks.ContainsValue(context.JobDetail.Key))
|
if (_chainLinks.ContainsValue(context.JobDetail.Key))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Job '{JobKey}' is the last in the chain", context.JobDetail.Key);
|
_logger.LogInformation("Job '{JobKey}' is the last in the chain", context.JobDetail.Key);
|
||||||
await OnJobChainFinished?.Invoke();
|
if (OnJobChainFinished is not null)
|
||||||
|
await OnJobChainFinished.Invoke();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ public class ChainerListenerBuilder
|
|||||||
|
|
||||||
private readonly List<IJobDetail> _chain = [];
|
private readonly List<IJobDetail> _chain = [];
|
||||||
|
|
||||||
private Func<Task> _finishCallback = () => Task.CompletedTask;
|
private Func<Task>? _finishCallback = () => Task.CompletedTask;
|
||||||
|
|
||||||
public ChainerListenerBuilder(ChainerListener chainerListener)
|
public ChainerListenerBuilder(ChainerListener chainerListener)
|
||||||
{
|
{
|
||||||
@ -21,7 +21,7 @@ public class ChainerListenerBuilder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChainerListenerBuilder OnFinish(Func<Task> callback)
|
public ChainerListenerBuilder OnFinish(Func<Task>? callback)
|
||||||
{
|
{
|
||||||
_finishCallback = callback;
|
_finishCallback = callback;
|
||||||
return this;
|
return this;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace AutoScan.Models;
|
namespace AutoScan.Models;
|
||||||
|
|
||||||
@ -13,12 +14,16 @@ public class Monitor
|
|||||||
public int port { get; init; }
|
public int port { get; init; }
|
||||||
public string? path { get; init; }
|
public string? path { get; init; }
|
||||||
|
|
||||||
public MonitorDetail? MonitorDetail => JsonSerializer.Deserialize<MonitorDetail>(details);
|
public MonitorDetail? MonitorDetail =>
|
||||||
|
details != null ? JsonSerializer.Deserialize<MonitorDetail>(details) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MonitorDetail
|
public class MonitorDetail
|
||||||
{
|
{
|
||||||
public string auto_host { get; set; }
|
[JsonPropertyName("auto_host")]
|
||||||
public string muser { get; set; }
|
public string? MonitorStreamUrl { get; set; }
|
||||||
public string mpass { get; set; }
|
[JsonPropertyName("muser")]
|
||||||
|
public string? Username { get; set; }
|
||||||
|
[JsonPropertyName("mpass")]
|
||||||
|
public string? Password { get; set; }
|
||||||
}
|
}
|
@ -3,7 +3,9 @@ namespace AutoScan.Options;
|
|||||||
public class ScannerOptions
|
public class ScannerOptions
|
||||||
{
|
{
|
||||||
public string? Exe { get; set; }
|
public string? Exe { get; set; }
|
||||||
|
public string? FFMpeg { get; set; }
|
||||||
public string? ConfigFile { get; set; }
|
public string? ConfigFile { get; set; }
|
||||||
public string? DetectionFolder { get; set; }
|
public string? DetectionFolder { get; set; }
|
||||||
|
public string? SnapshotFolder { get; set; }
|
||||||
public bool RunDry { get; set; } = false;
|
public bool RunDry { get; set; } = false;
|
||||||
}
|
}
|
@ -44,6 +44,9 @@
|
|||||||
<None Update="appsettings.json">
|
<None Update="appsettings.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="appsettings.Docker.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -8,6 +8,7 @@ WORKDIR /source
|
|||||||
#COPY --link *.csproj .
|
#COPY --link *.csproj .
|
||||||
COPY ["CasaBotApp/CasaBotApp.csproj", "CasaBotApp/"]
|
COPY ["CasaBotApp/CasaBotApp.csproj", "CasaBotApp/"]
|
||||||
COPY ["AutoScan/AutoScan.csproj", "AutoScan/"]
|
COPY ["AutoScan/AutoScan.csproj", "AutoScan/"]
|
||||||
|
COPY ["ControlServer/ControlServer.csproj", "ControlServer/"]
|
||||||
RUN dotnet restore "CasaBotApp/CasaBotApp.csproj" -a $TARGETARCH
|
RUN dotnet restore "CasaBotApp/CasaBotApp.csproj" -a $TARGETARCH
|
||||||
|
|
||||||
# Copy source code and publish app
|
# Copy source code and publish app
|
||||||
@ -31,6 +32,8 @@ RUN apt-get install -y python3 python3-pip
|
|||||||
#RUN apt-get update && apt-get install -y python3 python3-pip
|
#RUN apt-get update && apt-get install -y python3 python3-pip
|
||||||
RUN python3 -m pip install dvr-scan[opencv-headless] --break-system-packages
|
RUN python3 -m pip install dvr-scan[opencv-headless] --break-system-packages
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y ffmpeg
|
||||||
|
|
||||||
COPY --link --from=build /app .
|
COPY --link --from=build /app .
|
||||||
COPY CasaBotApp/appsettings.Docker.json ./appsettings.json
|
COPY CasaBotApp/appsettings.Docker.json ./appsettings.json
|
||||||
ENTRYPOINT ["dotnet", "CasaBotApp.dll"]
|
ENTRYPOINT ["dotnet", "CasaBotApp.dll"]
|
||||||
|
@ -16,16 +16,17 @@ public class AlarmBotOrquestrator
|
|||||||
private readonly AutoScanApp _autoScanApp;
|
private readonly AutoScanApp _autoScanApp;
|
||||||
private readonly IControlServer _controlServer;
|
private readonly IControlServer _controlServer;
|
||||||
private readonly IShinobiLinkFactory _shinobiLinkFactory;
|
private readonly IShinobiLinkFactory _shinobiLinkFactory;
|
||||||
private readonly IDVRConnector _dvrConnector;
|
private ISnapshoter _snapshoter;
|
||||||
|
|
||||||
public AlarmBotOrquestrator(ILogger<AlarmBotOrquestrator> logger, BotHandler botHandler, AutoScanApp autoScanApp, IControlServer controlServer, IShinobiLinkFactory shinobiLinkFactory, IDVRConnector dvrConnector)
|
public AlarmBotOrquestrator(ILogger<AlarmBotOrquestrator> logger, BotHandler botHandler, AutoScanApp autoScanApp,
|
||||||
|
IControlServer controlServer, IShinobiLinkFactory shinobiLinkFactory, ISnapshoter snapshoter)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_botHandler = botHandler;
|
_botHandler = botHandler;
|
||||||
_autoScanApp = autoScanApp;
|
_autoScanApp = autoScanApp;
|
||||||
_controlServer = controlServer;
|
_controlServer = controlServer;
|
||||||
_shinobiLinkFactory = shinobiLinkFactory;
|
_shinobiLinkFactory = shinobiLinkFactory;
|
||||||
_dvrConnector = dvrConnector;
|
_snapshoter = snapshoter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterCommands()
|
public void RegisterCommands()
|
||||||
@ -73,7 +74,7 @@ public class AlarmBotOrquestrator
|
|||||||
{
|
{
|
||||||
var stopwatch = Stopwatch.StartNew();
|
var stopwatch = Stopwatch.StartNew();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
var outputPath = await TakeSnapshot();
|
var outputPath = await _snapshoter.TakeSnapshot();
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
if (string.IsNullOrEmpty(outputPath))
|
if (string.IsNullOrEmpty(outputPath))
|
||||||
{
|
{
|
||||||
@ -139,69 +140,33 @@ public class AlarmBotOrquestrator
|
|||||||
|
|
||||||
public void RegisterControlServer()
|
public void RegisterControlServer()
|
||||||
{
|
{
|
||||||
_controlServer.OnEvent(async se =>
|
_controlServer.OnEvent(async sensorEvent =>
|
||||||
{
|
{
|
||||||
var mediaPath = await TakeSnapshot();
|
var mediaPath = await _snapshoter.TakeSnapshot();
|
||||||
if (string.IsNullOrEmpty(mediaPath))
|
if (string.IsNullOrEmpty(mediaPath))
|
||||||
{
|
{
|
||||||
await _botHandler.AlertText("Unauthorized access detected 🚨🚨🚨, but no media available");
|
await _botHandler.AlertText("Unauthorized access detected 🚨🚨🚨, but no media available");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (se.Type == EventType.Fired)
|
if (sensorEvent.Type == EventType.Fired)
|
||||||
{
|
{
|
||||||
await _botHandler.AlertPhoto(mediaPath,
|
await _botHandler.AlertPhoto(mediaPath,
|
||||||
"Unauthorized access detected 🚨 🚨 🚨",
|
"Unauthorized access detected 🚨 🚨 🚨",
|
||||||
[
|
[
|
||||||
new(OptionType.Url, "Camera Feed", _shinobiLinkFactory.BuildFeedLink()),
|
new(OptionType.Url, "Camera Feed", _shinobiLinkFactory.BuildFeedLink()),
|
||||||
new(OptionType.Action, "Authorize", $"authorize-{se.EventId}", (data, chatId ) =>
|
new(OptionType.Action, "Authorize", $"authorize-{sensorEvent.EventId}", (_, _ ) =>
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Authorizing event {EventId}", se.EventId);
|
_logger.LogWarning("Authorizing event {EventId}", sensorEvent.EventId);
|
||||||
_controlServer.AuthorizeEvent(se.EventId);
|
_controlServer.AuthorizeEvent(sensorEvent.EventId);
|
||||||
return Task.FromResult("Authorization not implemented");
|
return Task.FromResult("Entrance authorized");
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (se.Type == EventType.DisarmedEntrance)
|
if (sensorEvent.Type == EventType.DisarmedEntrance)
|
||||||
{
|
{
|
||||||
await _botHandler.UpdateText("Authorize access");
|
await _botHandler.UpdateText("Authorize access");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
private async Task<string> TakeSnapshot()
|
|
||||||
{
|
|
||||||
var timer = new Stopwatch();
|
|
||||||
timer.Start();
|
|
||||||
var outputPath = Path.Combine(".", "media", "snp", "something.jpeg");
|
|
||||||
var originalFeed = await _dvrConnector.GetVideoStream();
|
|
||||||
var ffmArgs = $"-y -i \"{originalFeed}\" -ss 00:00:00.500 -vframes 1 {outputPath}";
|
|
||||||
|
|
||||||
var process = new Process
|
|
||||||
{
|
|
||||||
StartInfo = new ProcessStartInfo
|
|
||||||
{
|
|
||||||
//To change this, I need to make sure ffmpeg is installed
|
|
||||||
FileName = "./dvr-scanner/ffmpeg.exe",
|
|
||||||
Arguments = ffmArgs,
|
|
||||||
RedirectStandardOutput = true,
|
|
||||||
RedirectStandardError = true,
|
|
||||||
UseShellExecute = false,
|
|
||||||
CreateNoWindow = true,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
process.Start();
|
|
||||||
await process.WaitForExitAsync();
|
|
||||||
// You can read the output here.
|
|
||||||
// var output = await process.StandardOutput.ReadToEndAsync();
|
|
||||||
// var error = await process.StandardError.ReadToEndAsync();
|
|
||||||
timer.Stop();
|
|
||||||
_logger.LogDebug("Taking snapshot took {Elapsed} ms", timer.ElapsedMilliseconds);
|
|
||||||
|
|
||||||
if(process.ExitCode != 0)
|
|
||||||
{
|
|
||||||
_logger.LogError("Error taking snapshot, exit code: {ExitCode}", process.ExitCode);
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
return outputPath;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -29,8 +29,10 @@
|
|||||||
"MediaFolder": "./media/originals/",
|
"MediaFolder": "./media/originals/",
|
||||||
"Scanner": {
|
"Scanner": {
|
||||||
"Exe": "dvr-scan",
|
"Exe": "dvr-scan",
|
||||||
|
"FFMpeg": "ffmpeg",
|
||||||
"ConfigFile": "./dvr-scan.cfg",
|
"ConfigFile": "./dvr-scan.cfg",
|
||||||
"DetectionFolder": "./media/detections/",
|
"DetectionFolder": "./media/detections/",
|
||||||
|
"SnapshotFolder": "./media/snapshots/",
|
||||||
"RunDry": false
|
"RunDry": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -30,8 +30,10 @@
|
|||||||
"MediaFolder": "./media/originals/",
|
"MediaFolder": "./media/originals/",
|
||||||
"Scanner": {
|
"Scanner": {
|
||||||
"Exe": "./dvr-scanner/dvr-scan.exe",
|
"Exe": "./dvr-scanner/dvr-scan.exe",
|
||||||
|
"FFMpeg": "./dvr-scanner/ffmpeg.exe",
|
||||||
"ConfigFile": "./dvr-scanner/dvr-scan.cfg",
|
"ConfigFile": "./dvr-scanner/dvr-scan.cfg",
|
||||||
"DetectionFolder": "./media/detections/",
|
"DetectionFolder": "./media/detections/",
|
||||||
|
"SnapshotFolder": "./media/snapshots/",
|
||||||
"RunDry": false
|
"RunDry": false
|
||||||
},
|
},
|
||||||
"RemoveOriginalFiles": false,
|
"RemoveOriginalFiles": false,
|
||||||
|
Loading…
Reference in New Issue
Block a user