using AutoScan; 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 serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(configuration); serviceCollection.AddLogging(builder => { builder.AddConfiguration(configuration.GetSection("Logging")); builder.AddConsole(); }); serviceCollection.AddSingleton(new BotConfiguration() { Token = configuration.GetValue("TelegramToken") ?? "" }); serviceCollection.Configure(configuration.GetSection("AutoScan")); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddHttpClient(); var serviceProvider = serviceCollection.BuildServiceProvider(); var logger = serviceProvider.GetService>()!; var botHandler = serviceProvider.GetService()!; using var cts = new CancellationTokenSource(); botHandler.Start(cts.Token); _ = SendMessageToSubscribers(cts.Token); var videoFileName = configuration.GetValue("VideoFileName") ?? "video.mp4"; if (configuration.GetValue("Fetch")) { var shinobiConnector = serviceProvider.GetService()!; await shinobiConnector.FetchLastVideo(videoFileName); } if (configuration.GetValue("Scan")) { var dvrScanner = serviceProvider.GetService()!; await dvrScanner.ScanVideos(cts.Token); } if(configuration.GetValue("Screenshot")) { var detected = "2025-02-12T07-00-02.DSME_0001.avi"; var ffmpegWrapper = serviceProvider.GetService()!; 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); } }