feat: add shinobi connections

This commit is contained in:
Guillermo Marcel 2025-02-13 19:10:25 -03:00
parent affae8ff48
commit da070f5e57
2 changed files with 105 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using CasaBotApp;
using AutoScan;
using CasaBotApp;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@ -14,25 +15,30 @@ IConfigurationRoot configuration = new ConfigurationBuilder()
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(configuration);
serviceCollection.AddLogging(builder =>
{
builder.AddConfiguration(configuration.GetSection("Logging"));
builder.AddConsole();
});
serviceCollection.AddSingleton(new BotConfiguration()
{
Token = configuration.GetValue<string>("TelegramToken") ?? ""
});
serviceCollection.Configure<AutoScan.Options.AutoScanOptions>(configuration.GetSection("AutoScan"));
serviceCollection.AddSingleton<BotHandler>();
serviceCollection.AddSingleton<ShinobiConnector>();
serviceCollection.AddSingleton<DVRScanner>();
serviceCollection.AddSingleton<FfmpegWrapper>();
serviceCollection.AddSingleton<AutoScanApp>();
serviceCollection.AddHttpClient();
var serviceProvider = serviceCollection.BuildServiceProvider();
var logger = serviceProvider.GetService<ILogger<Program>>()!;
var botHandler = serviceProvider.GetService<BotHandler>();
if (botHandler is null)
{
Console.WriteLine("Bot is not initialized");
return;
}
var botHandler = serviceProvider.GetService<BotHandler>()!;
using var cts = new CancellationTokenSource();
@ -40,8 +46,38 @@ botHandler.Start(cts.Token);
_ = SendMessageToSubscribers(cts.Token);
var videoFileName = configuration.GetValue<string>("VideoFileName") ?? "video.mp4";
if (configuration.GetValue<bool>("Fetch"))
{
var shinobiConnector = serviceProvider.GetService<ShinobiConnector>()!;
await shinobiConnector.FetchLastVideo(videoFileName);
}
Console.WriteLine($"bot is running... Press Enter to terminate");
if (configuration.GetValue<bool>("Scan"))
{
var dvrScanner = serviceProvider.GetService<DVRScanner>()!;
await dvrScanner.ScanVideos(cts.Token);
}
if(configuration.GetValue<bool>("Screenshot"))
{
var detected = "2025-02-12T07-00-02.DSME_0001.avi";
var ffmpegWrapper = serviceProvider.GetService<FfmpegWrapper>()!;
var duration = await ffmpegWrapper.GetVideoDuration($@".\media\detected\{detected}");
logger.LogInformation("Video duration: {Duration}", duration);
var middleTime = (duration / 2).Add(TimeSpan.FromSeconds(0.5));
//Extract frame at middle time
var screenshotPath = $@".\media\detected\{detected}-ss.png";
await ffmpegWrapper.ExtractFrame($@".\media\detected\{detected}", screenshotPath, middleTime);
logger.LogInformation("Screenshot extracted at {MiddleTime}", middleTime);
//botHandler.Subscribe(115151151);
await botHandler.UpdatePhoto(screenshotPath);
}
logger.LogInformation("Bot started");
logger.LogInformation("Press any key to stop the bot...");
Console.ReadLine();
cts.Cancel(); // stop the bot
return;

View File

@ -0,0 +1,61 @@
using Microsoft.Extensions.Logging;
namespace CasaBotApp;
public class ShinobiConnector
{
private readonly ILogger<ShinobiConnector> _logger;
private readonly string _shinobivUrl = "https://shinobi.francelsoft.com";
private readonly string _apikey = "OGD6nsGGzA1NL48M5Tg7Wbzto62oPl";
private readonly string _groupId = "aInxuWCYLI";
private readonly string _monitorId = "mQ3kQ5qjKK";
private readonly HttpClient _httpClient;
public ShinobiConnector(ILogger<ShinobiConnector> logger, HttpClient httpClient)
{
_logger = logger;
_httpClient = httpClient;
}
public async Task FetchLastVideo(string filename = "2025-02-12T08-00-01.mp4")
{
//fetch video from shinobi endpoint using example file "2025-02-12T08-00-01.mp4"
const string fetchVideoEndpoint = "/{0}/videos/{1}/{2}/{3}";
var endpoint = string.Format(_shinobivUrl+fetchVideoEndpoint, _apikey, _groupId, _monitorId, filename);
_logger.LogInformation("Fetching video from endpoint: {Endpoint}", endpoint);
//fetch video
const string mediaPath = @".\media\"; // Replace with your desired file path
var videoPath = mediaPath + filename;
try
{
//make sure the directory exists
Directory.CreateDirectory(Path.GetDirectoryName(mediaPath)!);
_logger.LogDebug("Cleaning media folder");
CleanDirectory(mediaPath);
_logger.LogDebug("Downloading video...");
var videoData = await _httpClient.GetByteArrayAsync(endpoint);
//Write the video to the file
await File.WriteAllBytesAsync(videoPath, videoData);
_logger.LogInformation("Video downloaded successfully!");
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while downloading the video");
}
}
private void CleanDirectory(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
foreach (var file in di.GetFiles())
{
file.Delete();
}
}
}