Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion NetDaemon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetDaemon.Runtime.Tests", "
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Debug", "Debug", "{E15D4280-7FFC-4F8B-9B8C-CF9AF2BF838C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DebugHost", "dev\DebugHost\DebugHost.csproj", "{898966EA-F814-4B7B-9A3D-5E78C38174B2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DebugHost", "src\debug\DebugHost\DebugHost.csproj", "{898966EA-F814-4B7B-9A3D-5E78C38174B2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetDaemon.Extensions.Logging", "src\Extensions\NetDaemon.Extensions.Logging\NetDaemon.Extensions.Logging.csproj", "{00333EBA-DB52-4D56-ADF7-940FB533E530}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetDaemon.Extensions.Tts", "src\Extensions\NetDaemon.Extensions.Tts\NetDaemon.Extensions.Tts.csproj", "{F4B29B77-9B92-4037-A884-288CA5EF0B78}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetDaemon.Extensions.MqttEntityManager", "src\Extensions\NetDaemon.Extensions.MqttEntityManager\NetDaemon.Extensions.MqttEntityManager.csproj", "{3EB8C461-C91E-4900-BFBD-0986CBBE87A6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DebugWebHost", "src\debug\DebugWebHost\DebugWebHost.csproj", "{AEBC7828-7C19-4A86-B6E2-58B5171347B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -271,6 +273,18 @@ Global
{F4B29B77-9B92-4037-A884-288CA5EF0B78}.Release|x64.Build.0 = Release|Any CPU
{F4B29B77-9B92-4037-A884-288CA5EF0B78}.Release|x86.ActiveCfg = Release|Any CPU
{F4B29B77-9B92-4037-A884-288CA5EF0B78}.Release|x86.Build.0 = Release|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Debug|x64.ActiveCfg = Debug|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Debug|x64.Build.0 = Debug|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Debug|x86.ActiveCfg = Debug|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Debug|x86.Build.0 = Debug|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Release|Any CPU.Build.0 = Release|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Release|x64.ActiveCfg = Release|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Release|x64.Build.0 = Release|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Release|x86.ActiveCfg = Release|Any CPU
{AEBC7828-7C19-4A86-B6E2-58B5171347B1}.Release|x86.Build.0 = Release|Any CPU
{3EB8C461-C91E-4900-BFBD-0986CBBE87A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EB8C461-C91E-4900-BFBD-0986CBBE87A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EB8C461-C91E-4900-BFBD-0986CBBE87A6}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -307,6 +321,7 @@ Global
{00333EBA-DB52-4D56-ADF7-940FB533E530} = {DFF3E7AA-7A50-4A1E-B3F8-EC01531FB83D}
{F4B29B77-9B92-4037-A884-288CA5EF0B78} = {DFF3E7AA-7A50-4A1E-B3F8-EC01531FB83D}
{3EB8C461-C91E-4900-BFBD-0986CBBE87A6} = {DFF3E7AA-7A50-4A1E-B3F8-EC01531FB83D}
{AEBC7828-7C19-4A86-B6E2-58B5171347B1} = {E15D4280-7FFC-4F8B-9B8C-CF9AF2BF838C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7C5FBB7F-654C-4CAC-964F-6D71AF3D62F8}
Expand Down
22 changes: 0 additions & 22 deletions dev/DebugHost/apps/HelloApp/HelloApp.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/AppModel/NetDaemon.AppModel.Tests/AppModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task TestGetApplicationsLocalWithDisabled()

var fakeStateManager = (FakeAppStateManager?) builder.Services.GetService<IAppStateManager>();
var appModel = builder.Services.GetService<IAppModel>();
var appModelContext = await appModel!.InitializeAsync(CancellationToken.None).ConfigureAwait(false);
var appModelContext = await appModel!.LoadNewApplicationContext(CancellationToken.None).ConfigureAwait(false);

// ACT
var apps = appModelContext.Applications;
Expand Down Expand Up @@ -105,7 +105,7 @@ public async Task TestGetApplicationsLocalWithEnabled()
.Build();

var appModel = builder.Services.GetService<IAppModel>();
var appModelContext = await appModel!.InitializeAsync(CancellationToken.None).ConfigureAwait(false);
var appModelContext = await appModel!.LoadNewApplicationContext(CancellationToken.None).ConfigureAwait(false);

// ACT
var apps = appModelContext.Applications;
Expand Down Expand Up @@ -176,7 +176,7 @@ public async Task TestGetApplicationsShouldReturnNonErrorOnes()

// ACT
var loadApps = (await appModel!
.InitializeAsync(CancellationToken.None)).Applications;
.LoadNewApplicationContext(CancellationToken.None)).Applications;


// CHECK
Expand Down Expand Up @@ -266,7 +266,7 @@ public async Task TestFocusShouldAlwaysLoadAppIfIndependentOfStateManager(Applic
.Build();

var appModel = builder.Services.GetService<IAppModel>();
var appModelContext = await appModel!.InitializeAsync(CancellationToken.None).ConfigureAwait(false);
var appModelContext = await appModel!.LoadNewApplicationContext(CancellationToken.None).ConfigureAwait(false);

// ACT
var apps = appModelContext.Applications;
Expand Down Expand Up @@ -300,7 +300,7 @@ public async Task TestInjectedClassShouldHaveCorrectValue()
.Build();

var appModel = builder.Services.GetService<IAppModel>();
var appModelContext = await appModel!.InitializeAsync(CancellationToken.None).ConfigureAwait(false);
var appModelContext = await appModel!.LoadNewApplicationContext(CancellationToken.None).ConfigureAwait(false);

// ACT
var apps = appModelContext.Applications;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public async Task TestAddYamlConfigWithTypeConverterGetsSettingsCorrectly2()
var appModel = scope.ServiceProvider.GetService<IAppModel>();

// ACT
var loadApps = (await appModel!.InitializeAsync(CancellationToken.None)).Applications;
var loadApps = (await appModel!.LoadNewApplicationContext(CancellationToken.None)).Applications;
var application = (Application)loadApps.First(n => n.Id == "LocalApps.MyAppLocalApp");
var app = (MyAppLocalApp?)application?.ApplicationContext?.Instance;
// CHECK
Expand Down
4 changes: 2 additions & 2 deletions src/AppModel/NetDaemon.AppModel.Tests/Helpers/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static async Task<IReadOnlyCollection<IApplication>> GetLocalApplicatio
})
.Build();
var appModel = builder.Services.GetService<IAppModel>();
var appModelContext = await appModel!.InitializeAsync(CancellationToken.None).ConfigureAwait(false);
var appModelContext = await appModel!.LoadNewApplicationContext(CancellationToken.None).ConfigureAwait(false);
return appModelContext.Applications;
}

Expand All @@ -43,7 +43,7 @@ internal static async Task<IReadOnlyCollection<IApplication>> GetDynamicApplicat
})
.Build();
var appModel = builder.Services.GetService<IAppModel>();
var appModelContext = await appModel!.InitializeAsync(CancellationToken.None).ConfigureAwait(false);
var appModelContext = await appModel!.LoadNewApplicationContext(CancellationToken.None).ConfigureAwait(false);
return appModelContext.Applications;
}
}
4 changes: 2 additions & 2 deletions src/AppModel/NetDaemon.AppModel/Common/IAppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace NetDaemon.AppModel;
/// <summary>
/// Application model
/// </summary>
public interface IAppModel : IAsyncDisposable
public interface IAppModel
{
/// <summary>
/// Instance and configure all applications.
Expand All @@ -15,5 +15,5 @@ public interface IAppModel : IAsyncDisposable
/// Depending on the selected compilation type it will be local or dynamically compiled apps
/// </remark>
/// <returns></returns>
Task<IAppModelContext> InitializeAsync(CancellationToken cancellationToken);
Task<IAppModelContext> LoadNewApplicationContext(CancellationToken cancellationToken);
}
2 changes: 1 addition & 1 deletion src/AppModel/NetDaemon.AppModel/Common/IAppModelContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace NetDaemon.AppModel;
/// <summary>
/// Manage AppModel state and lifecycle
/// </summary>
public interface IAppModelContext : IAsyncDisposable
public interface IAppModelContext : IAsyncInitializable, IAsyncDisposable
{
/// <summary>
/// Current instantiated and running applications
Expand Down
21 changes: 7 additions & 14 deletions src/AppModel/NetDaemon.AppModel/Internal/AppModel.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
namespace NetDaemon.AppModel.Internal;

/// <summary>
/// This class serves as a factory for creating and initializing new ApplicationContexts
/// </summary>
internal class AppModelImpl : IAppModel
{
private readonly IServiceProvider _provider;

public AppModelImpl(
IServiceProvider provider
)
public AppModelImpl(IServiceProvider provider)
{
_provider = provider;
}

internal IAppModelContext? CurrentContext { get; set; }

public async ValueTask DisposeAsync()
{
if (CurrentContext is not null)
await CurrentContext.DisposeAsync().ConfigureAwait(false);
}

public async Task<IAppModelContext> InitializeAsync(CancellationToken cancellationToken)
public async Task<IAppModelContext> LoadNewApplicationContext(CancellationToken cancellationToken)
{
// Create a new AppModelContext
var appModelContext = _provider.GetRequiredService<IAppModelContext>();
var initContext = (IAsyncInitializable)appModelContext;
await initContext.InitializeAsync(CancellationToken.None);
await appModelContext.InitializeAsync(cancellationToken);
return appModelContext;
}
}
14 changes: 8 additions & 6 deletions src/AppModel/NetDaemon.AppModel/Internal/AppModelContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Reflection;
using NetDaemon.AppModel.Internal.AppFactoryProviders;

namespace NetDaemon.AppModel.Internal;

internal class AppModelContext : IAppModelContext, IAsyncInitializable
internal class AppModelContext : IAppModelContext
{
private readonly List<Application> _applications = new();

Expand Down Expand Up @@ -37,11 +36,14 @@ public async Task InitializeAsync(CancellationToken cancellationToken)

public async ValueTask DisposeAsync()
{
if (_isDisposed)
return;
if (_isDisposed) return;
_isDisposed = true;

foreach (var appInstance in _applications) await appInstance.DisposeAsync().ConfigureAwait(false);
foreach (var appInstance in _applications)
{
await appInstance.DisposeAsync().ConfigureAwait(false);
}

_applications.Clear();
_isDisposed = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private async Task InternalRunAsync(string host, int port, bool ssl, string toke
{
if (isRetry)
{
_logger.LogDebug("Client disconnected, retrying in {seconds} seconds...", timeout.TotalSeconds);
_logger.LogDebug("Client disconnected, retrying in {Seconds} seconds...", timeout.TotalSeconds);
// This is a retry
await Task.Delay(timeout, combinedToken.Token).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static LoggerConfiguration Configure(LoggerConfiguration loggerConfigurat
.MinimumLevel.Override("System.Net.Http.HttpClient", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console(
formatProvider: CultureInfo.InvariantCulture,
theme: NetDaemonLoggingThemes.NetDaemonConsoleThemes.GetThemeByType(loggingConfiguration
.ConsoleThemeType),
applyThemeToRedirectedOutput: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private async Task ConnectAsync(MqttConfiguration mqttConfig, IMqttFactoryWrappe

private Task MqttClientOnDisconnectedAsync(MqttClientDisconnectedEventArgs arg)
{
_logger.LogDebug("MQTT disconnected: {Reason}", arg.ConnectResult.ReasonString);
_logger.LogDebug("MQTT disconnected: {Reason}", arg.ConnectResult?.ReasonString);
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class HomeAssistantRunnerMock : Mock<IHomeAssistantRunner>
public HomeAssistantClientMock ClientMock { get; }
public Subject<IHomeAssistantConnection> ConnectMock { get; }
public Subject<DisconnectReason> DisconnectMock { get; }
public HomeAssistantRunnerMock(CancellationToken cancelToken)
public HomeAssistantRunnerMock()
{
ConnectMock = new();
DisconnectMock = new();
Expand All @@ -25,12 +25,14 @@ public HomeAssistantRunnerMock(CancellationToken cancelToken)
It.IsAny<string>(),
It.IsAny<TimeSpan>(),
It.IsAny<CancellationToken>())).Returns(
async () =>
async (string _, int _, bool _, string _, string _, TimeSpan _, CancellationToken ct) =>
{
await Task.Delay(-1, cancelToken);
await Task.Delay(-1, ct);
}
);
}

public void MockConnect() => ConnectMock.OnNext(ClientMock.ConnectionMock.Object);
}

internal class HomeAssistantClientMock : Mock<IHomeAssistantClient>
Expand Down
Loading