Skip to content

Commit

Permalink
Websocket support (#195)
Browse files Browse the repository at this point in the history
* Websocket support

* Fix tests

* bump gui
  • Loading branch information
helto4real committed Aug 17, 2020
1 parent 0fe2ec0 commit 10ad07b
Show file tree
Hide file tree
Showing 21 changed files with 692 additions and 141 deletions.
6 changes: 3 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
},
"postCreateCommand": "dotnet restore && .devcontainer/install_prettyprompt.sh",
// Uncomment the next line if you want to publish or forward any ports.
// "forwardPorts": [
// 5000
// ],
"forwardPorts": [
5000
],
// "appPort": [
// 5000
// ],
Expand Down
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.5 \
&& git checkout tags/1.3.0 \
&& make deploy \
\
&& rm -fr /var/lib/apt/lists/* \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace NetDaemon.Common.Configuration
{
/// <summary>
/// Home Assistant related settings
/// </summary>
public class HomeAssistantSettings
{
/// <summary>
/// Home Assistant address
/// </summary>
public string Host { get; set; } = "localhost";
/// <summary>
/// Home Assistant port
/// </summary>
public short Port { get; set; } = 8123;
/// <summary>
/// Connect using ssl
/// </summary>
public bool Ssl { get; set; } = false;
/// <summary>
/// Token to authorize
/// </summary>
public string Token { get; set; } = string.Empty;
}
}
25 changes: 25 additions & 0 deletions src/App/NetDaemon.App/Common/Configuration/NetDaemonSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace NetDaemon.Common.Configuration
{
/// <summary>
/// Settings related to NetDaemon instance
/// </summary>
public class NetDaemonSettings
{
/// <summary>
/// Set true to generate entieies from Home Assistant
/// </summary>
public bool? GenerateEntities { get; set; } = false;
/// <summary>
/// If Admin gui would be used
/// </summary>
public bool? Admin { get; set; } = false;
/// <summary>
/// Where the apps are found
/// </summary>
public string? SourceFolder { get; set; } = null;
/// <summary>
/// Points to non default csproj file
/// </summary>
public string? ProjectFolder { get; set; } = string.Empty;
}
}
77 changes: 77 additions & 0 deletions src/App/NetDaemon.App/Common/ExternalEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

using System;
using System.Collections.Generic;
using NetDaemon.Common.Configuration;

namespace NetDaemon.Common
{

/// <summary>
/// Base class for all external events
/// </summary>
public class ExternalEventBase
{

}

/// <summary>
/// Sent when app information are changed in the netdaemon
/// </summary>
public class AppsInformationEvent : ExternalEventBase
{

}


/// <summary>
/// Information about the application
/// </summary>
public class ApplicationInfo
{
/// <summary>
/// Unique id
/// </summary>
public string? Id { get; set; }
/// <summary>
/// All application dependencies
/// </summary>
public IEnumerable<string>? Dependencies { get; set; }

/// <summary>
/// If app is enabled or disabled
/// </summary>
public bool IsEnabled { get; set; }

/// <summary>
/// Application description
/// </summary>
public string? Description { get; set; }

/// <summary>
/// Next scheduled event
/// </summary>
public DateTime? NextScheduledEvent { get; set; }

/// <summary>
/// Last known error message
/// </summary>
public string? LastErrorMessage { get; set; }

}

/// <summary>
/// All config information
/// </summary>
public class ConfigInfo
{
/// <summary>
/// Settings for NetDaemon
/// </summary>
public NetDaemonSettings? DaemonSettings { get; set; }
/// <summary>
/// Settings Home Assistant related
/// </summary>
public HomeAssistantSettings? HomeAssistantSettings { get; set; }

}
}
1 change: 0 additions & 1 deletion src/App/NetDaemon.App/Common/NetDaemonApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ public async override ValueTask DisposeAsync()
_stateCallbacks.Clear();
_eventCallbacks.Clear();
_eventFunctionSelectorCallbacks.Clear();

await base.DisposeAsync().ConfigureAwait(false);
}

Expand Down
3 changes: 0 additions & 3 deletions src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public async override ValueTask DisposeAsync()
if (_stateObservables is object)
_stateObservables!.Clear();

_eventObservables = null;
_stateObservables = null;

// Make sure we release all references so the apps can be
// unloaded correctly
_reactiveEvent = null;
Expand Down
6 changes: 6 additions & 0 deletions src/Daemon/NetDaemon.Daemon/Daemon/INetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,11 @@ public interface INetDaemonHost : INetDaemon
/// Clears all app instances registered
/// </summary>
Task UnloadAllApps();

/// <summary>
/// To subscribe to external event, typical the API does this
/// </summary>
/// <param name="func">callback function</param>
void SubscribeToExternalEvents(Func<ExternalEventBase, Task> func);
}
}
33 changes: 29 additions & 4 deletions src/Daemon/NetDaemon.Daemon/Daemon/NetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace NetDaemon.Daemon
{
public class NetDaemonHost : INetDaemonHost, IAsyncDisposable
{
internal readonly ConcurrentBag<Func<ExternalEventBase, Task>> _externalEventCallSubscribers =
new ConcurrentBag<Func<ExternalEventBase, Task>>();
internal readonly ConcurrentDictionary<string, HassArea> _hassAreas =
new ConcurrentDictionary<string, HassArea>();

Expand Down Expand Up @@ -72,6 +74,11 @@ public class NetDaemonHost : INetDaemonHost, IAsyncDisposable
private readonly ConcurrentDictionary<string, INetDaemonAppBase> _allAppInstances =
new ConcurrentDictionary<string, INetDaemonAppBase>();

/// <summary>
/// Used for testing
/// </summary>
internal ConcurrentDictionary<string, INetDaemonAppBase> InternalAllAppInstances => _allAppInstances;

private readonly Scheduler _scheduler;

private readonly List<string> _supportedDomainsForTurnOnOff = new List<string>
Expand Down Expand Up @@ -1278,13 +1285,10 @@ async Task SetStateOnDaemonAppSwitch(string state, dynamic? data)
if (!entityId.StartsWith("switch.netdaemon_"))
return; // We only want app switches


await SetDependentState(entityId, state);



await ReloadAllApps();

await PostExternalEvent(new AppsInformationEvent());
}
}

Expand Down Expand Up @@ -1361,5 +1365,26 @@ private async Task<bool> RestoreAppState(INetDaemonAppBase appInstance)
// Error return false
return false;
}

/// <inheritdoc/>
public void SubscribeToExternalEvents(Func<ExternalEventBase, Task> func)
{
_externalEventCallSubscribers.Add(func);
}

private async Task PostExternalEvent(ExternalEventBase ev)
{
if (_externalEventCallSubscribers.Count == 0)
return;

var callbackTaskList = new List<Task>(_externalEventCallSubscribers.Count);

foreach (var callback in _externalEventCallSubscribers)
{
callbackTaskList.Add(Task.Run(() => callback(ev)));
}

await callbackTaskList.WhenAll(_cancelToken);
}
}
}
2 changes: 1 addition & 1 deletion src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using NetDaemon.Common.Configuration;
using NetDaemon.Daemon;
using NetDaemon.Daemon.Storage;
using NetDaemon.Service;
using NetDaemon.Service.Configuration;

namespace NetDaemon
{
Expand Down
93 changes: 0 additions & 93 deletions src/DaemonRunner/DaemonRunner/Service/API/ApiController.cs

This file was deleted.

4 changes: 1 addition & 3 deletions src/DaemonRunner/DaemonRunner/Service/API/ApiData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using NetDaemon.Service.Configuration;
using NetDaemon.Common.Configuration;

namespace NetDaemon.Service.Api
{
Expand All @@ -17,14 +17,12 @@ public class ApiApplication
public DateTime? NextScheduledEvent { get; set; }

public string? LastErrorMessage { get; set; }

}

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

}

}
Loading

0 comments on commit 10ad07b

Please sign in to comment.