-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprogram.cs
67 lines (57 loc) · 2.36 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
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using NetDaemon;
using Serilog;
namespace Service
{
internal class Program
{
private const string HassioConfigPath = "/data/options.json";
public static async Task Main(string[] args)
{
try
{
Log.Logger = SerilogConfigurator.Configure().CreateLogger();
if (File.Exists(HassioConfigPath))
await ReadHassioConfig();
await Host.CreateDefaultBuilder(args)
.UseSerilog(Log.Logger)
.UseNetDaemon()
.Build()
.RunAsync();
}
catch (Exception e)
{
Log.Fatal(e, "Failed to start host...");
}
finally
{
Log.CloseAndFlush();
}
}
private static async Task ReadHassioConfig()
{
try
{
var hassAddOnSettings = await JsonSerializer.DeserializeAsync<HassioConfig>(File.OpenRead(HassioConfigPath)).ConfigureAwait(false);
if (hassAddOnSettings.LogLevel is object)
SerilogConfigurator.SetMinimumLogLevel(hassAddOnSettings.LogLevel);
if (hassAddOnSettings.GenerateEntitiesOnStart is object)
Environment.SetEnvironmentVariable("NETDAEMON__GENERATEENTITIES", hassAddOnSettings.GenerateEntitiesOnStart.ToString());
if (hassAddOnSettings.LogMessages is object && hassAddOnSettings.LogMessages == true)
Environment.SetEnvironmentVariable("HASSCLIENT_MSGLOGLEVEL", "Default");
if (hassAddOnSettings.ProjectFolder is object && string.IsNullOrEmpty(hassAddOnSettings.ProjectFolder) == false)
Environment.SetEnvironmentVariable("NETDAEMON__PROJECTFOLDER", hassAddOnSettings.ProjectFolder);
// We are in Hassio so hard code the path
Environment.SetEnvironmentVariable("NETDAEMON__SOURCEFOLDER", "/config/netdaemon");
}
catch (Exception e)
{
Log.Fatal(e, "Failed to read the Home Assistant Add-on config");
}
}
}
}