-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
70 lines (68 loc) · 3.14 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using DomoticzToRouterSmsBot.Adapters;
using DomoticzToRouterSmsBot.Loader;
using DomoticzToRouterSmsBot.Proccessor;
using DomoticzToRouterSmsBot.Proccessor.Commands;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Polly;
namespace DomoticzToRouterSmsBot
{
internal class Program
{
public static async Task Main(string[] args)
{
var builder = new HostBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true);
config.AddJsonFile("lastProccessedSMS.json", optional: true, reloadOnChange: true);
})
.ConfigureServices((hostingContext, services) =>
{
services.AddOptions();
services.AddSingleton<ILoggerFactory, LoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
services.AddLogging((builder) => builder.SetMinimumLevel(LogLevel.Trace));
services.AddScoped<ISmsParser, SmsParser>()
.AddScoped<ISmsRunner, SmsRunner>()
.AddScoped<ISmsUpdater, TpLinkRouter>()
.AddScoped<ToggleSwitch>()
.AddScoped<MarkAsRead>()
.AddScoped<ICommand>(provider =>
{
var c1 = provider.GetService<ToggleSwitch>();
((Command)c1).Use(provider.GetService<MarkAsRead>());
return c1;
});
if (String.IsNullOrEmpty(hostingContext.Configuration["DataFilePath"]))
{
services.AddScoped<IDomoticz, Domoticz>();
services.AddScoped<ISmsLoader, TpLinkRouter>();
services.AddHttpClient("router").AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.RetryAsync(3));
services.AddHttpClient("domoticz").AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.RetryAsync(3));
}
else
{
services.AddScoped<IDomoticz, Domoticz>();
services.AddHttpClient("domoticz").AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.RetryAsync(3));
services.AddScoped<ISmsLoader, File>();
}
services.AddHostedService<WorkerService>();
})
.ConfigureLogging((hostingContext, logging) => {
logging.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
NLog.LogManager.LoadConfiguration("nlog.config");
});
await builder.RunConsoleAsync();
}
}
public class Proccessed
{
public DateTime LastProccessedTime { get; set; }
}
}