casa-bot/src/CasaBot/CasaBotApp/Program.cs

97 lines
3.1 KiB
C#
Raw Normal View History

2025-02-13 19:10:25 -03:00
using AutoScan;
using CasaBotApp;
2025-02-12 19:15:20 -03:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
// See https://aka.ms/new-console-template for more information
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environment}.json", true, true)
.AddEnvironmentVariables()
.Build();
var serviceCollection = new ServiceCollection();
2025-02-13 19:10:25 -03:00
serviceCollection.AddSingleton(configuration);
2025-02-12 19:15:20 -03:00
serviceCollection.AddLogging(builder =>
{
2025-02-13 19:10:25 -03:00
builder.AddConfiguration(configuration.GetSection("Logging"));
2025-02-12 19:15:20 -03:00
builder.AddConsole();
});
serviceCollection.AddSingleton(new BotConfiguration()
{
Token = configuration.GetValue<string>("TelegramToken") ?? ""
});
2025-02-13 19:10:25 -03:00
serviceCollection.Configure<AutoScan.Options.AutoScanOptions>(configuration.GetSection("AutoScan"));
2025-02-12 19:15:20 -03:00
serviceCollection.AddSingleton<BotHandler>();
2025-02-13 19:10:25 -03:00
serviceCollection.AddSingleton<ShinobiConnector>();
serviceCollection.AddSingleton<DVRScanner>();
serviceCollection.AddSingleton<FfmpegWrapper>();
serviceCollection.AddSingleton<AutoScanApp>();
serviceCollection.AddHttpClient();
2025-02-12 19:15:20 -03:00
var serviceProvider = serviceCollection.BuildServiceProvider();
2025-02-13 19:10:25 -03:00
var logger = serviceProvider.GetService<ILogger<Program>>()!;
2025-02-12 19:15:20 -03:00
2025-02-13 19:10:25 -03:00
var botHandler = serviceProvider.GetService<BotHandler>()!;
2025-02-12 19:15:20 -03:00
using var cts = new CancellationTokenSource();
botHandler.Start(cts.Token);
_ = SendMessageToSubscribers(cts.Token);
2025-02-13 19:10:25 -03:00
var videoFileName = configuration.GetValue<string>("VideoFileName") ?? "video.mp4";
if (configuration.GetValue<bool>("Fetch"))
{
var shinobiConnector = serviceProvider.GetService<ShinobiConnector>()!;
await shinobiConnector.FetchLastVideo(videoFileName);
}
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);
}
2025-02-12 19:15:20 -03:00
2025-02-13 19:10:25 -03:00
logger.LogInformation("Bot started");
logger.LogInformation("Press any key to stop the bot...");
2025-02-12 19:15:20 -03:00
Console.ReadLine();
cts.Cancel(); // stop the bot
return;
2025-02-12 19:22:18 -03:00
2025-02-12 19:15:20 -03:00
async Task SendMessageToSubscribers(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
botHandler.Update("Hello from CasaBot! at " + DateTime.Now);
}
}