diff --git a/src/CasaBot/CasaBotApp/Program.cs b/src/CasaBot/CasaBotApp/Program.cs index bfdbc70..9e94acb 100644 --- a/src/CasaBot/CasaBotApp/Program.cs +++ b/src/CasaBot/CasaBotApp/Program.cs @@ -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("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(); -if (botHandler is null) -{ - Console.WriteLine("Bot is not initialized"); - return; -} +var botHandler = serviceProvider.GetService()!; using var cts = new CancellationTokenSource(); @@ -40,8 +46,38 @@ 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); +} -Console.WriteLine($"bot is running... Press Enter to terminate"); +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; diff --git a/src/CasaBot/CasaBotApp/ShinobiConnector.cs b/src/CasaBot/CasaBotApp/ShinobiConnector.cs new file mode 100644 index 0000000..a80790a --- /dev/null +++ b/src/CasaBot/CasaBotApp/ShinobiConnector.cs @@ -0,0 +1,61 @@ +using Microsoft.Extensions.Logging; + +namespace CasaBotApp; + +public class ShinobiConnector +{ + private readonly ILogger _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 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(); + } + } + + +} \ No newline at end of file