Skip to content

Commit

Permalink
Connect on state RUNNING
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Aug 15, 2020
1 parent 7b46e6e commit e6c7d50
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN wget -qO /s6 \
\
&& git clone https://github.com/net-daemon/admin.git /admin \
&& cd /admin \
&& git checkout tags/1.2.3 \
&& git checkout tags/1.2.4 \
&& make deploy \
\
&& rm -fr /var/lib/apt/lists/* \
Expand Down
2 changes: 1 addition & 1 deletion src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async override ValueTask DisposeAsync()
_reactiveState = null;

await base.DisposeAsync().ConfigureAwait(false);
Log("RxApp {app} is Disposed", Id!);
LogDebug("RxApp {app} is Disposed", Id!);
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/App/NetDaemon.App/NetDaemon.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JoySoftware.HassClient" Version="0.6.2-beta" />
<PackageReference Include="JoySoftware.HassClient" Version="0.7.0-beta" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.7" />
<PackageReference Include="System.Reactive" Version="4.4.1" />
</ItemGroup>
Expand Down
13 changes: 12 additions & 1 deletion src/Daemon/NetDaemon.Daemon/Daemon/NetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ public async Task Run(string host, short port, bool ssl, string token, Cancellat
if (!connectResult)
{
Connected = false;
await _hassClient.CloseAsync().ConfigureAwait(false);
return;
}

var hassConfig = await _hassClient.GetConfig().ConfigureAwait(false);
if (hassConfig.State != "RUNNING")
{
Logger.LogInformation("Home Assistant is not ready yet, state: {state} ..", hassConfig.State);
await _hassClient.CloseAsync();
return;
}

Expand Down Expand Up @@ -567,7 +576,7 @@ public async Task Stop()
{
return;
}
Logger.LogInformation("Try stopping Instance NetDaemonHost");
Logger.LogDebug("Try stopping Instance NetDaemonHost");

await UnloadAllApps().ConfigureAwait(false);

Expand All @@ -579,6 +588,8 @@ public async Task Stop()

_stopped = true;

Connected = false;

Logger.LogInformation("Stopped Instance NetDaemonHost");
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion src/Daemon/NetDaemon.Daemon/NetDaemon.Daemon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JoySoftware.HassClient" Version="0.6.2-beta" />
<PackageReference Include="JoySoftware.HassClient" Version="0.7.0-beta" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.7" />
<PackageReference Include="YamlDotNet" Version="8.1.2" />
Expand Down
2 changes: 1 addition & 1 deletion src/DaemonRunner/DaemonRunner/DaemonRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JoySoftware.HassClient" Version="0.6.2-beta" />
<PackageReference Include="JoySoftware.HassClient" Version="0.7.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" />
Expand Down
14 changes: 9 additions & 5 deletions src/DaemonRunner/DaemonRunner/Service/RunnerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class RunnerService : BackgroundService
/// <summary>
/// The interval used when disconnected
/// </summary>
private const int ReconnectInterval = 40000;
private const int ReconnectInterval = 10000;
private const string Version = "dev";

private readonly HomeAssistantSettings _homeAssistantSettings;
Expand Down Expand Up @@ -102,7 +102,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
stoppingToken
);

await WaitForDaemonToConnect(daemonHost, stoppingToken).ConfigureAwait(false);
if (await WaitForDaemonToConnect(daemonHost, stoppingToken).ConfigureAwait(false) == false)
{
continue;
}

if (!stoppingToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -132,7 +135,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
if (!stoppingToken.IsCancellationRequested)
{
// It is disconnected, wait
_logger.LogWarning($"Home assistant is unavailable, retrying in {ReconnectInterval / 1000} seconds...");
// _logger.LogWarning($"Home assistant is unavailable, retrying in {ReconnectInterval / 1000} seconds...");
}
}
catch (TaskCanceledException)
Expand Down Expand Up @@ -230,16 +233,17 @@ private void EnsureApplicationDirectoryExists(NetDaemonSettings settings)
Directory.CreateDirectory(appDirectory);
}

private async Task WaitForDaemonToConnect(NetDaemonHost daemonHost, CancellationToken stoppingToken)
private async Task<bool> WaitForDaemonToConnect(NetDaemonHost daemonHost, CancellationToken stoppingToken)
{
var nrOfTimesCheckForConnectedState = 0;

while (!daemonHost.Connected && !stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken).ConfigureAwait(false);
await Task.Delay(500, stoppingToken).ConfigureAwait(false);
if (nrOfTimesCheckForConnectedState++ > 5)
break;
}
return daemonHost.Connected;
}
}
}

0 comments on commit e6c7d50

Please sign in to comment.