66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
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
|
|
{
|
|
class Program
|
|
{
|
|
static BotService bot;
|
|
static JsonDocument config;
|
|
|
|
static ServiceCollection services;
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
|
config = LoadConfig();
|
|
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));
|
|
notificationThread.Start();
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
bot = provider.GetRequiredService<BotService>();
|
|
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);
|
|
var config = Path.Combine(assemblyFolder, "zaobot.json");
|
|
string configStr = File.ReadAllText(config);
|
|
return JsonDocument.Parse(configStr);
|
|
}
|
|
}
|
|
}
|