Skip to content

Commit

Permalink
top level function refactor (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 6, 2020
1 parent 667a54b commit 9db6ab4
Showing 1 changed file with 44 additions and 47 deletions.
91 changes: 44 additions & 47 deletions src/Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,60 @@
using Microsoft.Extensions.Hosting;
using NetDaemon;
using Serilog;
using Service;

namespace Service
const string HassioConfigPath = "/data/options.json";

try
{
internal class Program
{
private const string HassioConfigPath = "/data/options.json";

public static async Task Main(string[] args)
{
try
{
Log.Logger = SerilogConfigurator.Configure().CreateLogger();
Log.Logger = SerilogConfigurator.Configure().CreateLogger();

if (File.Exists(HassioConfigPath))
await ReadHassioConfig();
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();
}
}
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);
/// <summary>
/// Reads the Home Assistant (hassio) configuration file
/// </summary>
/// <returns></returns>
static async Task ReadHassioConfig()
{
try
{
var hassAddOnSettings = await JsonSerializer.DeserializeAsync<HassioConfig>(File.OpenRead(HassioConfigPath)).ConfigureAwait(false);

if (hassAddOnSettings?.LogLevel is not null)
SerilogConfigurator.SetMinimumLogLevel(hassAddOnSettings.LogLevel);
if (hassAddOnSettings?.LogLevel is not null)
SerilogConfigurator.SetMinimumLogLevel(hassAddOnSettings.LogLevel);

if (hassAddOnSettings?.GenerateEntitiesOnStart is not null)
Environment.SetEnvironmentVariable("NETDAEMON__GENERATEENTITIES", hassAddOnSettings.GenerateEntitiesOnStart.ToString());
if (hassAddOnSettings?.GenerateEntitiesOnStart is not null)
Environment.SetEnvironmentVariable("NETDAEMON__GENERATEENTITIES", hassAddOnSettings.GenerateEntitiesOnStart.ToString());

if (hassAddOnSettings?.LogMessages is not null && hassAddOnSettings.LogMessages == true)
Environment.SetEnvironmentVariable("HASSCLIENT_MSGLOGLEVEL", "Default");
if (hassAddOnSettings?.LogMessages is not null && hassAddOnSettings.LogMessages == true)
Environment.SetEnvironmentVariable("HASSCLIENT_MSGLOGLEVEL", "Default");

if (hassAddOnSettings?.ProjectFolder is not null && string.IsNullOrEmpty(hassAddOnSettings.ProjectFolder) == false)
Environment.SetEnvironmentVariable("NETDAEMON__PROJECTFOLDER", hassAddOnSettings.ProjectFolder);
if (hassAddOnSettings?.ProjectFolder is not null && 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");
}
}
// 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");
}
}

0 comments on commit 9db6ab4

Please sign in to comment.