-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (71 loc) · 3.48 KB
/
Copy pathProgram.cs
File metadata and controls
87 lines (71 loc) · 3.48 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Runtime.Loader;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace HuaweiSolar
{
public class Program
{
private static ServiceProvider ServiceProvider { get; set; }
public static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();
// Share the cancellation token source
var cts = new CancellationTokenSource();
// Initialse the poller passing it the Cancellation Token Source
var huaweiSolarPoller = ServiceProvider.GetRequiredService<HuaweiSolarPoller>()
.InitialiseAsync(cts).GetAwaiter().GetResult();
// Start the poller
huaweiSolarPoller.Start();
// Wait until the app unloads or is cancelled
AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
Console.CancelKeyPress += (sender, cpe) => cts.Cancel();
WhenCancelled(cts.Token).Wait();
// Stop the poller as the service is stopping
huaweiSolarPoller.Stop();
Log.Logger.Information("Huawei Solar Poller has exited.");
}
/// <summary>
/// <c>WhenCancelled</c> - Handles cleanup operations when app is cancelled or unloads
/// </summary>
public static Task WhenCancelled(CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).SetResult(true), tcs);
return tcs.Task;
}
/// <summary
/// <c>ConfigureServices</c> - Sets up the dependency injection of services
/// </summary>
private static void ConfigureServices(ServiceCollection serviceCollection)
{
string configPath = Environment.GetEnvironmentVariable("ConfigPath");
if (string.IsNullOrWhiteSpace(configPath))
{
configPath = "/etc/huaweisolar";
}
Console.WriteLine("Reading appsettings.json from: {0}", configPath);
Console.WriteLine("ConfigureServices called");
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(configPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddEnvironmentVariables()
.Build();
serviceCollection.AddSingleton(configuration);
serviceCollection.AddSingleton<ChargeHQSender>();
serviceCollection.AddSingleton<HuaweiSolarPoller>();
// Configure Logger
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
serviceCollection.AddLogging((builder) => {
builder.AddSerilog();
});
Log.Logger.Debug("Configured Logger");
}
}
}