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(scannerJob)
|
||||
.AddJob(cleanJob)
|
||||
.OnFinish(async () => await OnScanCompleted?.Invoke())
|
||||
.OnFinish(OnScanCompleted)
|
||||
.Build();
|
||||
|
||||
_scheduler.ListenerManager.AddJobListener(chainer, GroupMatcher<JobKey>.GroupEquals(GROUP_NAME));
|
||||
@ -107,7 +107,6 @@ public class AutoScanApp
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
private string CronFromAt(string at)
|
||||
{
|
||||
var parts = at.Split(':');
|
||||
|
@ -17,6 +17,7 @@ public static class DependencyInjectionExtensions
|
||||
|
||||
services.AddSingleton<FfmpegWrapper>();
|
||||
services.AddSingleton<AutoScanApp>();
|
||||
services.AddSingleton<ISnapshoter, Snapshoter>();
|
||||
services.AddQuartz();
|
||||
services.AddTransient<IChainerListenerFactory, ChainerListenerFactory>();
|
||||
|
||||
|
@ -95,7 +95,7 @@ public class ShinobiConnector : IDVRConnector
|
||||
_logger.LogDebug("Fetching video stream from endpoint: {Endpoint}", endpoint);
|
||||
var monitors = await _httpClient.GetFromJsonAsync<Monitor[]>(endpoint);
|
||||
|
||||
_cachedVideoStream = monitors?.FirstOrDefault()?.MonitorDetail?.auto_host ?? null;
|
||||
_cachedVideoStream = monitors?.FirstOrDefault()?.MonitorDetail?.MonitorStreamUrl ?? null;
|
||||
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 Dictionary<JobKey, JobKey> _chainLinks;
|
||||
public Func<Task> OnJobChainFinished { get; set; }
|
||||
public Func<Task>? OnJobChainFinished { get; set; }
|
||||
|
||||
public ChainerListener(string name, ILogger<ChainerListener> logger)
|
||||
{
|
||||
@ -56,7 +56,8 @@ public class ChainerListener : JobListenerSupport
|
||||
if (_chainLinks.ContainsValue(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;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ public class ChainerListenerBuilder
|
||||
|
||||
private readonly List<IJobDetail> _chain = [];
|
||||
|
||||
private Func<Task> _finishCallback = () => Task.CompletedTask;
|
||||
private Func<Task>? _finishCallback = () => Task.CompletedTask;
|
||||
|
||||
public ChainerListenerBuilder(ChainerListener chainerListener)
|
||||
{
|
||||
@ -21,7 +21,7 @@ public class ChainerListenerBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
public ChainerListenerBuilder OnFinish(Func<Task> callback)
|
||||
public ChainerListenerBuilder OnFinish(Func<Task>? callback)
|
||||
{
|
||||
_finishCallback = callback;
|
||||
return this;
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AutoScan.Models;
|
||||
|
||||
@ -13,12 +14,16 @@ public class Monitor
|
||||
public int port { 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 string auto_host { get; set; }
|
||||
public string muser { get; set; }
|
||||
public string mpass { get; set; }
|
||||
[JsonPropertyName("auto_host")]
|
||||
public string? MonitorStreamUrl { 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 string? Exe { get; set; }
|
||||
public string? FFMpeg { get; set; }
|
||||
public string? ConfigFile { get; set; }
|
||||
public string? DetectionFolder { get; set; }
|
||||
public string? SnapshotFolder { get; set; }
|
||||
public bool RunDry { get; set; } = false;
|
||||
}
|
@ -44,6 +44,9 @@
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="appsettings.Docker.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -8,6 +8,7 @@ WORKDIR /source
|
||||
#COPY --link *.csproj .
|
||||
COPY ["CasaBotApp/CasaBotApp.csproj", "CasaBotApp/"]
|
||||
COPY ["AutoScan/AutoScan.csproj", "AutoScan/"]
|
||||
COPY ["ControlServer/ControlServer.csproj", "ControlServer/"]
|
||||
RUN dotnet restore "CasaBotApp/CasaBotApp.csproj" -a $TARGETARCH
|
||||
|
||||
# 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 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 CasaBotApp/appsettings.Docker.json ./appsettings.json
|
||||
ENTRYPOINT ["dotnet", "CasaBotApp.dll"]
|
||||
|
@ -16,16 +16,17 @@ public class AlarmBotOrquestrator
|
||||
private readonly AutoScanApp _autoScanApp;
|
||||
private readonly IControlServer _controlServer;
|
||||
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;
|
||||
_botHandler = botHandler;
|
||||
_autoScanApp = autoScanApp;
|
||||
_controlServer = controlServer;
|
||||
_shinobiLinkFactory = shinobiLinkFactory;
|
||||
_dvrConnector = dvrConnector;
|
||||
_snapshoter = snapshoter;
|
||||
}
|
||||
|
||||
public void RegisterCommands()
|
||||
@ -73,7 +74,7 @@ public class AlarmBotOrquestrator
|
||||
{
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
stopwatch.Start();
|
||||
var outputPath = await TakeSnapshot();
|
||||
var outputPath = await _snapshoter.TakeSnapshot();
|
||||
stopwatch.Stop();
|
||||
if (string.IsNullOrEmpty(outputPath))
|
||||
{
|
||||
@ -139,69 +140,33 @@ public class AlarmBotOrquestrator
|
||||
|
||||
public void RegisterControlServer()
|
||||
{
|
||||
_controlServer.OnEvent(async se =>
|
||||
_controlServer.OnEvent(async sensorEvent =>
|
||||
{
|
||||
var mediaPath = await TakeSnapshot();
|
||||
var mediaPath = await _snapshoter.TakeSnapshot();
|
||||
if (string.IsNullOrEmpty(mediaPath))
|
||||
{
|
||||
await _botHandler.AlertText("Unauthorized access detected 🚨🚨🚨, but no media available");
|
||||
return;
|
||||
}
|
||||
if (se.Type == EventType.Fired)
|
||||
if (sensorEvent.Type == EventType.Fired)
|
||||
{
|
||||
await _botHandler.AlertPhoto(mediaPath,
|
||||
"Unauthorized access detected 🚨 🚨 🚨",
|
||||
[
|
||||
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);
|
||||
_controlServer.AuthorizeEvent(se.EventId);
|
||||
return Task.FromResult("Authorization not implemented");
|
||||
_logger.LogWarning("Authorizing event {EventId}", sensorEvent.EventId);
|
||||
_controlServer.AuthorizeEvent(sensorEvent.EventId);
|
||||
return Task.FromResult("Entrance authorized");
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
if (se.Type == EventType.DisarmedEntrance)
|
||||
if (sensorEvent.Type == EventType.DisarmedEntrance)
|
||||
{
|
||||
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/",
|
||||
"Scanner": {
|
||||
"Exe": "dvr-scan",
|
||||
"FFMpeg": "ffmpeg",
|
||||
"ConfigFile": "./dvr-scan.cfg",
|
||||
"DetectionFolder": "./media/detections/",
|
||||
"SnapshotFolder": "./media/snapshots/",
|
||||
"RunDry": false
|
||||
}
|
||||
},
|
||||
|
@ -30,8 +30,10 @@
|
||||
"MediaFolder": "./media/originals/",
|
||||
"Scanner": {
|
||||
"Exe": "./dvr-scanner/dvr-scan.exe",
|
||||
"FFMpeg": "./dvr-scanner/ffmpeg.exe",
|
||||
"ConfigFile": "./dvr-scanner/dvr-scan.cfg",
|
||||
"DetectionFolder": "./media/detections/",
|
||||
"SnapshotFolder": "./media/snapshots/",
|
||||
"RunDry": false
|
||||
},
|
||||
"RemoveOriginalFiles": false,
|
||||
|
Loading…
Reference in New Issue
Block a user