From 2a8348df44746c86304a02453064d8faca41b656 Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Sat, 20 Jan 2024 16:56:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20DI?= =?UTF-8?q?,=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D1=8B=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0?= =?UTF-8?q?=D1=8E=D1=82=D1=81=D1=8F=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20?= =?UTF-8?q?=D0=B2=D0=B1=D1=80=D0=B0=D1=81=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B5=D0=B9=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BotService.cs | 7 +++--- NotificationReceiver.cs | 5 +++-- OpenHab/OpenHabClient.cs | 8 +++---- Program.cs | 48 +++++++++++++++++++++++++++------------- ZaoBot.csproj | 1 + config/Config.cs | 18 +++++++++++++++ config/IConfig.cs | 12 ++++++++++ 7 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 config/Config.cs create mode 100644 config/IConfig.cs diff --git a/BotService.cs b/BotService.cs index d275507..3169881 100644 --- a/BotService.cs +++ b/BotService.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Telegram.Bot; using Telegram.Bot.Args; using Telegram.Bot.Types.ReplyMarkups; +using ZaoBot.Config; namespace ZaoBot { @@ -20,15 +21,15 @@ namespace ZaoBot bool shouldRestart = false; - public BotService(JsonDocument config) + public BotService(IConfig config, OpenHab.OpenHabClient openhab) { - this.config = config; + this.config = config.Data; + this.openhab = openhab; } public void Run() { var token = config.RootElement.GetProperty("token").GetString(); - openhab = new OpenHab.OpenHabClient(config.RootElement.GetProperty("openhab_url").GetString()); while (true) { shouldRestart = false; diff --git a/NotificationReceiver.cs b/NotificationReceiver.cs index 7505d36..0496838 100644 --- a/NotificationReceiver.cs +++ b/NotificationReceiver.cs @@ -5,6 +5,7 @@ using System.Text; using System.Text.Json; using System.Threading; using Telegram.Bot; +using ZaoBot.Config; namespace ZaoBot { @@ -12,9 +13,9 @@ namespace ZaoBot { private JsonDocument config; - public NotificationReceiver(JsonDocument config) + public NotificationReceiver(IConfig config) { - this.config = config; + this.config = config.Data; } public void Run() diff --git a/OpenHab/OpenHabClient.cs b/OpenHab/OpenHabClient.cs index 1b441d4..56e19c0 100644 --- a/OpenHab/OpenHabClient.cs +++ b/OpenHab/OpenHabClient.cs @@ -5,19 +5,19 @@ using System.Net.Http.Headers; using System.Text; using System.Text.Json; using System.Threading.Tasks; +using ZaoBot.Config; namespace ZaoBot.OpenHab { class OpenHabClient { private string url; - private string token; + private string token = ""; HttpClient http = new HttpClient(); - public OpenHabClient(string url, string token = "") + public OpenHabClient(IConfig config) { - this.url = url; - this.token = token; + this.url = config.Data.RootElement.GetProperty("openhab_url").GetString(); if (!string.IsNullOrEmpty(token)) { http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token + ":"); diff --git a/Program.cs b/Program.cs index d5f59e5..5653c84 100644 --- a/Program.cs +++ b/Program.cs @@ -1,9 +1,12 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Globalization; using System.IO; using System.Reflection; using System.Text.Json; using System.Threading; +using ZaoBot.Config; +using ZaoBot.OpenHab; namespace ZaoBot { @@ -12,30 +15,45 @@ namespace ZaoBot static BotService bot; static JsonDocument config; + static ServiceCollection services; + static void Main(string[] args) { CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; config = LoadConfig(); - var receiver = new NotificationReceiver(config); - var notificationThread = new Thread(new ThreadStart(receiver.Run)); - notificationThread.Start(); - while (true) + services = new ServiceCollection(); + services.AddSingleton(CreateConfig); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + using (var provider = services.BuildServiceProvider()) { - try + var receiver = provider.GetRequiredService(); + var notificationThread = new Thread(new ThreadStart(receiver.Run)); + notificationThread.Start(); + while (true) { - bot = new BotService(config); - bot.Run(); - } - catch (Exception e) - { - // Тут ловим всё, при ошибках просто заново поднимаем бота - Console.WriteLine("Exception: " + e.Message); - Console.WriteLine(e.StackTrace); - Console.WriteLine("Restarting bot"); + try + { + bot = provider.GetRequiredService(); + bot.Run(); + } + catch (Exception e) + { + // Тут ловим всё, при ошибках просто заново поднимаем бота + Console.WriteLine("Exception: " + e.Message); + Console.WriteLine(e.StackTrace); + Console.WriteLine("Restarting bot"); + } } } } + private static Config.Config CreateConfig(IServiceProvider arg) + { + return new Config.Config(config); + } + private static JsonDocument LoadConfig() { string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); diff --git a/ZaoBot.csproj b/ZaoBot.csproj index 0b90841..601789c 100644 --- a/ZaoBot.csproj +++ b/ZaoBot.csproj @@ -6,6 +6,7 @@ + diff --git a/config/Config.cs b/config/Config.cs new file mode 100644 index 0000000..902fae6 --- /dev/null +++ b/config/Config.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; + +namespace ZaoBot.Config +{ + public class Config : IConfig + { + JsonDocument data; + public JsonDocument Data => data; + + public Config(JsonDocument data) + { + this.data = data; + } + } +} diff --git a/config/IConfig.cs b/config/IConfig.cs new file mode 100644 index 0000000..50e762b --- /dev/null +++ b/config/IConfig.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; + +namespace ZaoBot.Config +{ + interface IConfig + { + JsonDocument Data { get; } + } +}