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

104 lines
3.3 KiB
C#

using AutoScan;
using AutoScan.Options;
using CasaBotApp;
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 services = new ServiceCollection();
services.AddSingleton(configuration);
services.AddLogging(builder =>
{
builder.AddConfiguration(configuration.GetSection("Logging"));
//add time to logs
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "[HH:mm:ss] ";
});
});
services.Configure<TelegramOptions>(configuration.GetSection("Telegram"));
services.Configure<AutoScanOptions>(configuration.GetSection("AutoScan"));
services.Configure<ShinobiOptions>(configuration.GetSection("Shinobi"));
services.AddSingleton<BotHandler>();
services.AddAutoScan();
services.AddHttpClient();
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetService<ILogger<Program>>()!;
var botHandler = serviceProvider.GetService<BotHandler>()!;
var autoScanApp = serviceProvider.GetService<AutoScanApp>()!;
using var cts = new CancellationTokenSource();
_ = autoScanApp.Run(cts.Token);
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);
// }
//
// 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;
async Task SendMessageToSubscribers(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
botHandler.Update("Hello from CasaBot! at " + DateTime.Now);
}
}