Skip to content

Commit

Permalink
Merge 1bf3c9e into 7052e03
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real authored Jul 21, 2020
2 parents 7052e03 + 1bf3c9e commit 74a02d8
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 65 deletions.
7 changes: 5 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
},
"postCreateCommand": "dotnet restore && .devcontainer/install_prettyprompt.sh",
// Uncomment the next line if you want to publish any ports.
"appPort": [
5001
"forwardPorts": [
5000
],
// "appPort": [
// 5000
// ],
// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "dotnet restore",
// Uncomment the next line to use a non-root user. On Linux, this will prevent
Expand Down
4 changes: 2 additions & 2 deletions exampleapps/apps/test2.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
my_app:
class: BatteryManager
# HelloWorldSecret: !secret test_secret
# dependencies:
# - global_app
dependencies:
- global_app
9 changes: 8 additions & 1 deletion src/Daemon/NetDaemon.Daemon/Daemon/NetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class NetDaemonHost : INetDaemonHost, IAsyncDisposable

private readonly ConcurrentDictionary<string, INetDaemonAppBase> _runningAppInstances =
new ConcurrentDictionary<string, INetDaemonAppBase>();
private readonly ConcurrentDictionary<string, INetDaemonAppBase> _allAppInstances =
new ConcurrentDictionary<string, INetDaemonAppBase>();

private readonly Scheduler _scheduler;

Expand Down Expand Up @@ -131,6 +133,8 @@ public IHttpHandler Http

public IEnumerable<INetDaemonAppBase> RunningAppInstances => _runningAppInstances.Values;

public IEnumerable<INetDaemonAppBase> AllAppInstances => _allAppInstances.Values;

public IScheduler Scheduler => _scheduler;

public IEnumerable<EntityState> State => InternalState.Select(n => n.Value);
Expand Down Expand Up @@ -428,6 +432,7 @@ public async Task Run(string host, short port, bool ssl, string token, Cancellat
await _hassClient.SubscribeToEvents().ConfigureAwait(false);

Connected = true;
_stopped = false;

Logger.LogInformation(
hassioToken != null
Expand Down Expand Up @@ -593,6 +598,7 @@ public async Task UnloadAllApps()
await app.Value.DisposeAsync().ConfigureAwait(false);
}
_runningAppInstances.Clear();
_allAppInstances.Clear();
}

/// <summary>
Expand Down Expand Up @@ -857,7 +863,7 @@ protected virtual async Task HandleNewEvent(HassEvent hassEvent, CancellationTok
// Todo: Make it timeout! Maybe it should be handling in it's own task like scheduler
if (tasks.Count > 0)
{

await tasks.WhenAll(token).ConfigureAwait(false);

await tasks.WhenAll(token).ConfigureAwait(false);
Expand Down Expand Up @@ -1208,6 +1214,7 @@ private async Task LoadAllApps()
{
_runningAppInstances[appInstance.Id!] = appInstance;
}
_allAppInstances[appInstance.Id!] = appInstance;
}

// Now run initialize on all sorted by dependencies
Expand Down
5 changes: 5 additions & 0 deletions src/DaemonRunner/DaemonRunner/DaemonRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@

<ItemGroup>
<PackageReference Include="JoySoftware.HassClient" Version="0.6.0-beta" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.6" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.6.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.6" />
<PackageReference Include="YamlDotNet" Version="8.1.2" />
Expand Down
18 changes: 15 additions & 3 deletions src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using System.Net;
using JoySoftware.HomeAssistant.Client;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using NetDaemon.Daemon;
using NetDaemon.Daemon.Storage;
using NetDaemon.Service;
using NetDaemon.Service.Configuration;

Expand All @@ -14,8 +21,13 @@ public static IHostBuilder UseNetDaemon(this IHostBuilder hostBuilder)
services.Configure<HomeAssistantSettings>(context.Configuration.GetSection("HomeAssistant"));
services.Configure<NetDaemonSettings>(context.Configuration.GetSection("NetDaemon"));
services.AddHttpClient();
services.AddHostedService<RunnerService>();
}).ConfigureWebHostDefaults(webbuilder =>
{
webbuilder.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5000); //HTTP port
});
webbuilder.UseStartup<ApiStartup>();
});
}
}
Expand Down
69 changes: 69 additions & 0 deletions src/DaemonRunner/DaemonRunner/Service/API/ApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetDaemon.Common;
using NetDaemon.Daemon;
using NetDaemon.Service.Configuration;

namespace NetDaemon.Service.Api
{

[ApiController]
[Route("api")]
public class ApiController : ControllerBase
{
private readonly ILogger<ApiController> _logger;
private readonly NetDaemonSettings? _netdaemonSettings;
private readonly HomeAssistantSettings? _homeassistantSettings;

private readonly NetDaemonHost? _host;
public ApiController(
IOptions<NetDaemonSettings> netDaemonSettings,
IOptions<HomeAssistantSettings> homeAssistantSettings,
ILoggerFactory? loggerFactory = null,
NetDaemonHost? host = null
)
{
_logger = loggerFactory.CreateLogger<ApiController>();
_host = host;
_netdaemonSettings = netDaemonSettings.Value;
_homeassistantSettings = homeAssistantSettings.Value;
}

[Route("settings")]
[HttpGet]
public ApiConfig? Config()
{
var tempResult = new ApiConfig
{
DaemonSettings = _netdaemonSettings,
HomeAssistantSettings = _homeassistantSettings
};
// For first release we do not expose the token
if (tempResult.HomeAssistantSettings is object)
{
tempResult.HomeAssistantSettings.Token = "";
}
return tempResult;
}

[HttpGet]
[Route("apps")]
public IEnumerable<ApiApplication>? Apps()
{
return _host?.AllAppInstances.Select(n => new ApiApplication()
{
Id = n.Id,
Dependencies = n.Dependencies,
IsEnabled = n.IsEnabled
});
}


}

}
23 changes: 23 additions & 0 deletions src/DaemonRunner/DaemonRunner/Service/API/ApiData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using NetDaemon.Service.Configuration;

namespace NetDaemon.Service.Api
{

public class ApiApplication
{
public string? Id { get; set; }
public IEnumerable<string>? Dependencies { get; set; }

public bool IsEnabled { get; set; }

}

public class ApiConfig
{
public NetDaemonSettings? DaemonSettings { get; set; }
public HomeAssistantSettings? HomeAssistantSettings { get; set; }

}

}
61 changes: 61 additions & 0 deletions src/DaemonRunner/DaemonRunner/Service/ApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Reflection;
using JoySoftware.HomeAssistant.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NetDaemon.Daemon;
using NetDaemon.Daemon.Storage;
using NetDaemon.Service.Configuration;
using NetDaemon.Service;
using Microsoft.Extensions.Options;
using System.IO;
using Microsoft.Extensions.Hosting;
using NetDaemon.Common;

namespace NetDaemon.Service
{
public class ApiStartup
{
public ApiStartup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
// services.Configure<HomeAssistantSettings>(Context.Configuration.GetSection("HomeAssistant"));
// services.Configure<NetDaemonSettings>(context.Configuration.GetSection("NetDaemon"));
services.AddHostedService<RunnerService>();
services.AddTransient<IHassClient, HassClient>();
services.AddTransient<IDataRepository>(n => new DataRepository(Path.Combine(n.GetRequiredService<IOptions<NetDaemonSettings>>().Value.SourceFolder!, ".storage")));
services.AddTransient<IHttpHandler, NetDaemon.Daemon.HttpHandler>();
services.AddSingleton<NetDaemonHost>();
services.AddHttpClient();
services.AddControllers().PartManager.ApplicationParts.Add(new AssemblyPart(Assembly.GetExecutingAssembly()));
services.AddRouting();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

// app.UseHttpsRedirection();

app.UseRouting();

// app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Loading

0 comments on commit 74a02d8

Please sign in to comment.