Добавил DI, основные классы создаются через вбрасывание зависимостей через конструктор
This commit is contained in:
+4
-3
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
|||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Args;
|
using Telegram.Bot.Args;
|
||||||
using Telegram.Bot.Types.ReplyMarkups;
|
using Telegram.Bot.Types.ReplyMarkups;
|
||||||
|
using ZaoBot.Config;
|
||||||
|
|
||||||
namespace ZaoBot
|
namespace ZaoBot
|
||||||
{
|
{
|
||||||
@@ -20,15 +21,15 @@ namespace ZaoBot
|
|||||||
|
|
||||||
bool shouldRestart = false;
|
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()
|
public void Run()
|
||||||
{
|
{
|
||||||
var token = config.RootElement.GetProperty("token").GetString();
|
var token = config.RootElement.GetProperty("token").GetString();
|
||||||
openhab = new OpenHab.OpenHabClient(config.RootElement.GetProperty("openhab_url").GetString());
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
shouldRestart = false;
|
shouldRestart = false;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Text;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
|
using ZaoBot.Config;
|
||||||
|
|
||||||
namespace ZaoBot
|
namespace ZaoBot
|
||||||
{
|
{
|
||||||
@@ -12,9 +13,9 @@ namespace ZaoBot
|
|||||||
{
|
{
|
||||||
private JsonDocument config;
|
private JsonDocument config;
|
||||||
|
|
||||||
public NotificationReceiver(JsonDocument config)
|
public NotificationReceiver(IConfig config)
|
||||||
{
|
{
|
||||||
this.config = config;
|
this.config = config.Data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Run()
|
public void Run()
|
||||||
|
|||||||
@@ -5,19 +5,19 @@ using System.Net.Http.Headers;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using ZaoBot.Config;
|
||||||
|
|
||||||
namespace ZaoBot.OpenHab
|
namespace ZaoBot.OpenHab
|
||||||
{
|
{
|
||||||
class OpenHabClient
|
class OpenHabClient
|
||||||
{
|
{
|
||||||
private string url;
|
private string url;
|
||||||
private string token;
|
private string token = "";
|
||||||
HttpClient http = new HttpClient();
|
HttpClient http = new HttpClient();
|
||||||
|
|
||||||
public OpenHabClient(string url, string token = "")
|
public OpenHabClient(IConfig config)
|
||||||
{
|
{
|
||||||
this.url = url;
|
this.url = config.Data.RootElement.GetProperty("openhab_url").GetString();
|
||||||
this.token = token;
|
|
||||||
if (!string.IsNullOrEmpty(token))
|
if (!string.IsNullOrEmpty(token))
|
||||||
{
|
{
|
||||||
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token + ":");
|
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token + ":");
|
||||||
|
|||||||
+21
-3
@@ -1,9 +1,12 @@
|
|||||||
using System;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using ZaoBot.Config;
|
||||||
|
using ZaoBot.OpenHab;
|
||||||
|
|
||||||
namespace ZaoBot
|
namespace ZaoBot
|
||||||
{
|
{
|
||||||
@@ -12,18 +15,27 @@ namespace ZaoBot
|
|||||||
static BotService bot;
|
static BotService bot;
|
||||||
static JsonDocument config;
|
static JsonDocument config;
|
||||||
|
|
||||||
|
static ServiceCollection services;
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||||
config = LoadConfig();
|
config = LoadConfig();
|
||||||
var receiver = new NotificationReceiver(config);
|
services = new ServiceCollection();
|
||||||
|
services.AddSingleton<IConfig, Config.Config>(CreateConfig);
|
||||||
|
services.AddSingleton<BotService, BotService>();
|
||||||
|
services.AddSingleton<OpenHabClient, OpenHabClient>();
|
||||||
|
services.AddSingleton<NotificationReceiver, NotificationReceiver>();
|
||||||
|
using (var provider = services.BuildServiceProvider())
|
||||||
|
{
|
||||||
|
var receiver = provider.GetRequiredService<NotificationReceiver>();
|
||||||
var notificationThread = new Thread(new ThreadStart(receiver.Run));
|
var notificationThread = new Thread(new ThreadStart(receiver.Run));
|
||||||
notificationThread.Start();
|
notificationThread.Start();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
bot = new BotService(config);
|
bot = provider.GetRequiredService<BotService>();
|
||||||
bot.Run();
|
bot.Run();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -35,6 +47,12 @@ namespace ZaoBot
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Config.Config CreateConfig(IServiceProvider arg)
|
||||||
|
{
|
||||||
|
return new Config.Config(config);
|
||||||
|
}
|
||||||
|
|
||||||
private static JsonDocument LoadConfig()
|
private static JsonDocument LoadConfig()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||||
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
|
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user