Переделал всё на DI
This commit is contained in:
+20
-16
@@ -1,13 +1,10 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using ZaoBot.Config;
|
||||
|
||||
namespace ZaoBot
|
||||
@@ -18,42 +15,49 @@ namespace ZaoBot
|
||||
static ITelegramBotClient botClient;
|
||||
Dictionary<long, IUserState> userStates = new Dictionary<long, IUserState>();
|
||||
private OpenHab.OpenHabClient openhab;
|
||||
private IServiceProvider services;
|
||||
|
||||
bool shouldRestart = false;
|
||||
|
||||
public BotService(IConfig config, OpenHab.OpenHabClient openhab)
|
||||
public BotService(IServiceProvider services, IConfig config, OpenHab.OpenHabClient openhab)
|
||||
{
|
||||
this.services = services;
|
||||
this.config = config.Data;
|
||||
this.openhab = openhab;
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
var token = config.RootElement.GetProperty("token").GetString();
|
||||
while (true)
|
||||
shouldRestart = false;
|
||||
|
||||
var botClient = services.GetRequiredService<ITelegramBotClient>();
|
||||
|
||||
using CancellationTokenSource cts = new CancellationTokenSource();
|
||||
try
|
||||
{
|
||||
shouldRestart = false;
|
||||
botClient = new TelegramBotClient(token);
|
||||
|
||||
using CancellationTokenSource cts = new CancellationTokenSource();
|
||||
|
||||
var me = botClient.GetMeAsync().Result;
|
||||
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
|
||||
botClient.StartReceiving(
|
||||
updateHandler: OnBotUpdateAsync,
|
||||
pollingErrorHandler: OnPollingError
|
||||
pollingErrorHandler: OnPollingError,
|
||||
cancellationToken: cts.Token
|
||||
);
|
||||
while (!shouldRestart)
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
cts.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private Task OnPollingError(ITelegramBotClient bot, Exception exception, CancellationToken cts)
|
||||
{
|
||||
Console.WriteLine("Error on polling: " + exception.Message);
|
||||
Console.WriteLine("Will restart");
|
||||
shouldRestart = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -72,7 +76,7 @@ namespace ZaoBot
|
||||
}
|
||||
if (state == null)
|
||||
{
|
||||
state = new MainMenuUserState(botClient, config, openhab);
|
||||
state = services.GetService<MainMenuUserState>();
|
||||
}
|
||||
state = await state.ProcessMessage(state, updateInfo);
|
||||
if (state == null)
|
||||
@@ -81,7 +85,7 @@ namespace ZaoBot
|
||||
chatId: updateInfo.Message.Chat,
|
||||
text: "Я запутался, давайте сначала"
|
||||
);
|
||||
state = new MainMenuUserState(botClient, config, openhab);
|
||||
state = services.GetService<MainMenuUserState>();
|
||||
state = await state.ProcessMessage(state, updateInfo);
|
||||
}
|
||||
userStates[id] = state;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
@@ -7,6 +8,7 @@ using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using ZaoBot.Config;
|
||||
using ZaoBot.OpenHab;
|
||||
|
||||
namespace ZaoBot
|
||||
@@ -25,14 +27,16 @@ namespace ZaoBot
|
||||
private JsonDocument config;
|
||||
List<CameraConfigData> cameras;
|
||||
OpenHabClient openhab;
|
||||
IServiceProvider services;
|
||||
|
||||
public CamerasMenuUserState(ITelegramBotClient botClient, JsonDocument config, OpenHabClient openhab)
|
||||
public CamerasMenuUserState(IServiceProvider services, ITelegramBotClient botClient, IConfig config, OpenHabClient openhab)
|
||||
{
|
||||
this.botClient = botClient;
|
||||
this.config = config;
|
||||
this.config = config.Data;
|
||||
this.openhab = openhab;
|
||||
this.services = services;
|
||||
cameras = new List<CameraConfigData>();
|
||||
foreach (var element in config.RootElement.GetProperty("cameras").EnumerateObject())
|
||||
foreach (var element in this.config.RootElement.GetProperty("cameras").EnumerateObject())
|
||||
{
|
||||
var cam = new CameraConfigData()
|
||||
{
|
||||
@@ -47,7 +51,7 @@ namespace ZaoBot
|
||||
{
|
||||
if (e.Message.Text == CMD_BACK)
|
||||
{
|
||||
var newState = new MainMenuUserState(botClient, config, openhab);
|
||||
var newState = services.GetRequiredService<MainMenuUserState>();
|
||||
await newState.ProcessMessage(this, e);
|
||||
return newState;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using ZaoBot.Config;
|
||||
using ZaoBot.OpenHab;
|
||||
|
||||
namespace ZaoBot
|
||||
@@ -23,11 +23,13 @@ namespace ZaoBot
|
||||
private JsonDocument config;
|
||||
HttpClient http = new HttpClient();
|
||||
OpenHabClient openhab;
|
||||
IServiceProvider services;
|
||||
|
||||
public MainMenuUserState(ITelegramBotClient botClient, JsonDocument config, OpenHabClient openhab)
|
||||
public MainMenuUserState(IServiceProvider services, ITelegramBotClient botClient, IConfig config, OpenHabClient openhab)
|
||||
{
|
||||
this.services = services;
|
||||
this.botClient = botClient;
|
||||
this.config = config;
|
||||
this.config = config.Data;
|
||||
this.openhab = openhab;
|
||||
}
|
||||
|
||||
@@ -61,13 +63,13 @@ namespace ZaoBot
|
||||
}
|
||||
else if (e.Message.Text == CMD_STATUS)
|
||||
{
|
||||
var newState = new StatusMenuUserState(botClient, config, openhab);
|
||||
var newState = services.GetRequiredService<StatusMenuUserState>();
|
||||
await newState.ProcessMessage(this, e);
|
||||
return newState;
|
||||
}
|
||||
else if (e.Message.Text == CMD_CAMERAS)
|
||||
{
|
||||
var newState = new CamerasMenuUserState(botClient, config, openhab);
|
||||
var newState = services.GetRequiredService<CamerasMenuUserState>();
|
||||
await newState.ProcessMessage(this, e);
|
||||
return newState;
|
||||
}
|
||||
|
||||
+20
-3
@@ -5,6 +5,7 @@ using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using Telegram.Bot;
|
||||
using ZaoBot.Config;
|
||||
using ZaoBot.OpenHab;
|
||||
|
||||
@@ -23,9 +24,16 @@ namespace ZaoBot
|
||||
config = LoadConfig();
|
||||
services = new ServiceCollection();
|
||||
services.AddSingleton<IConfig, Config.Config>(CreateConfig);
|
||||
services.AddSingleton<BotService, BotService>();
|
||||
services.AddSingleton<OpenHabClient, OpenHabClient>();
|
||||
services.AddSingleton<NotificationReceiver, NotificationReceiver>();
|
||||
|
||||
services.AddScoped<BotService, BotService>();
|
||||
services.AddScoped<IServiceProvider, IServiceProvider>(p => p);
|
||||
services.AddScoped<ITelegramBotClient, TelegramBotClient>(CreateTelegramBot);
|
||||
|
||||
services.AddTransient<MainMenuUserState, MainMenuUserState>();
|
||||
services.AddTransient<StatusMenuUserState, StatusMenuUserState>();
|
||||
services.AddTransient<CamerasMenuUserState, CamerasMenuUserState>();
|
||||
using (var provider = services.BuildServiceProvider())
|
||||
{
|
||||
var receiver = provider.GetRequiredService<NotificationReceiver>();
|
||||
@@ -35,8 +43,11 @@ namespace ZaoBot
|
||||
{
|
||||
try
|
||||
{
|
||||
bot = provider.GetRequiredService<BotService>();
|
||||
bot.Run();
|
||||
using (var scope = provider.CreateScope())
|
||||
{
|
||||
bot = scope.ServiceProvider.GetRequiredService<BotService>();
|
||||
bot.Run();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -49,6 +60,12 @@ namespace ZaoBot
|
||||
}
|
||||
}
|
||||
|
||||
private static TelegramBotClient CreateTelegramBot(IServiceProvider arg)
|
||||
{
|
||||
var token = config.RootElement.GetProperty("token").GetString();
|
||||
return new TelegramBotClient(token);
|
||||
}
|
||||
|
||||
private static Config.Config CreateConfig(IServiceProvider arg)
|
||||
{
|
||||
return new Config.Config(config);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
@@ -7,6 +8,7 @@ using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using ZaoBot.Config;
|
||||
using ZaoBot.OpenHab;
|
||||
|
||||
namespace ZaoBot
|
||||
@@ -19,12 +21,14 @@ namespace ZaoBot
|
||||
private JsonDocument config;
|
||||
HttpClient http = new HttpClient();
|
||||
OpenHabClient openhab;
|
||||
IServiceProvider services;
|
||||
|
||||
public StatusMenuUserState(ITelegramBotClient botClient, JsonDocument config, OpenHabClient openhab)
|
||||
public StatusMenuUserState(IServiceProvider services, ITelegramBotClient botClient, IConfig config, OpenHabClient openhab)
|
||||
{
|
||||
this.botClient = botClient;
|
||||
this.config = config;
|
||||
this.config = config.Data;
|
||||
this.openhab = openhab;
|
||||
this.services = services;
|
||||
}
|
||||
|
||||
public async Task<IUserState> ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e)
|
||||
@@ -47,7 +51,7 @@ namespace ZaoBot
|
||||
}
|
||||
else if (e.Message.Text == CMD_BACK)
|
||||
{
|
||||
var newState = new MainMenuUserState(botClient, config, openhab);
|
||||
var newState = services.GetRequiredService<MainMenuUserState>();
|
||||
await newState.ProcessMessage(this, e);
|
||||
return newState;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user