Skip to content

Commit

Permalink
Fix debugging story
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 21, 2020
1 parent 920a2c3 commit 3a4adfd
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 197 deletions.
3 changes: 0 additions & 3 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,3 @@ ENV TZ=Europe/Stockholm
# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=noninteractive
ENV TEST=AK

# We do not load local assemblies
ENV NETDAEMON__APPSOURCE=/workspaces/netdaemon/exampleapps
18 changes: 18 additions & 0 deletions DEV.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Developing NetDaemon
These instructions are for developing NetDaemon. For apps please use [the docs](https://netdaemon.xyz).

## Setup the environment vars
Easiest is to setup environment varables for your Home Assistant instance

| Environment variable | Description |
| ------ | ------ |
| HOMEASSISTANT__TOKEN | Token secret to access the HA instance
|
| HOMEASSISTANT__HOST | The ip or hostname of HA
| HOMEASSISTANT__PORT | The port of home assistant (defaults to 8123 if not specified) |
| NETDAEMON__GENERATEENTITIES | Generate entities, recommed set false unless debugging|
| NETDAEMON__SOURCEFOLDER | The folder where it will find the source code. Set this to empty `""` to debug apps local. If needed to debug the dynamic source compilation, set to `/workspaces/netdaemon/Service/apps`

Use `src/Service/apps` as starting point to debug your stuff!

Good luck
1 change: 0 additions & 1 deletion exampleapps/.storage/theapp_the_app_store.json

This file was deleted.

2 changes: 0 additions & 2 deletions exampleapps/README.md

This file was deleted.

30 changes: 0 additions & 30 deletions exampleapps/apps/test1.cs

This file was deleted.

2 changes: 0 additions & 2 deletions exampleapps/apps/test1.yaml

This file was deleted.

137 changes: 0 additions & 137 deletions exampleapps/apps/test2.cs

This file was deleted.

5 changes: 0 additions & 5 deletions exampleapps/apps/test2.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class NetDaemonSettings
/// </summary>
public string GetAppSourceDirectory()
{
var source = AppSource?.Trim() ?? throw new NullReferenceException("AppSource cannot be null!");
var source = AppSource?.Trim() ?? Directory.GetCurrentDirectory();

if (source.EndsWith(".csproj") || source.EndsWith(".dll"))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace Service.Infrastructure
namespace NetDaemon.Infrastructure.Config
{
public class HassioConfig
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;

namespace NetDaemon.Service.Infrastructure
namespace NetDaemon.Infrastructure.Config
{
internal class NetDaemonTheme : Serilog.Sinks.SystemConsole.Themes.ConsoleTheme
{
Expand Down
File renamed without changes.
18 changes: 10 additions & 8 deletions src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
using NetDaemon.Daemon.Config;
using NetDaemon.Service;
using NetDaemon.Service.App;
using NetDaemon.Service.Infrastructure;
using Serilog;
using Service.Infrastructure;
using NetDaemon.Infrastructure.Config;

namespace NetDaemon
{
Expand Down Expand Up @@ -57,7 +56,7 @@ public static void CleanupNetDaemon()

private static void RegisterNetDaemonAssembly(IServiceCollection services)
{
if (!BypassLocalAssemblyLoading())
if (UseLocalAssemblyLoading())
services.AddSingleton<IDaemonAppCompiler, LocalDaemonAppCompiler>();
else
services.AddSingleton<IDaemonAppCompiler, DaemonAppCompiler>();
Expand All @@ -68,16 +67,19 @@ private static void RegisterNetDaemonAssembly(IServiceCollection services)
/// This is typically when running in container. When running in dev
/// you want the local loading
/// </summary>
private static bool BypassLocalAssemblyLoading()
private static bool UseLocalAssemblyLoading()
{

var appSource = Environment.GetEnvironmentVariable("NETDAEMON__APPSOURCE") ??
throw new NullReferenceException("NETDAEMON__APPSOURCE cannot be null!");

var appSource = Environment.GetEnvironmentVariable("NETDAEMON__APPSOURCE");

if (string.IsNullOrEmpty(appSource))
return true;

if (appSource.EndsWith(".csproj") || appSource.EndsWith(".dll"))
return false;
else
return true;
else
return false;
}

/// <summary>
Expand Down
13 changes: 7 additions & 6 deletions src/DaemonRunner/DaemonRunner/Service/App/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ public class CodeGenerator
{{
public string EntityId => EntityIds.First();
public string? Area => DaemonRxApp.State(EntityId)?.Area;
public string? Area => DaemonRxApp?.State(EntityId)?.Area;
public dynamic? Attribute => DaemonRxApp.State(EntityId)?.Attribute;
public dynamic? Attribute => DaemonRxApp?.State(EntityId)?.Attribute;
public DateTime LastChanged => DaemonRxApp.State(EntityId).LastChanged;
public DateTime? LastChanged => DaemonRxApp?.State(EntityId)?.LastChanged;
public DateTime LastUpdated => DaemonRxApp.State(EntityId).LastUpdated;
public DateTime? LastUpdated => DaemonRxApp?.State(EntityId)?.LastUpdated;
public dynamic? State => DaemonRxApp.State(EntityId)?.State;
public dynamic? State => DaemonRxApp?.State(EntityId)?.State;
public {domain.ToCamelCase()}Entity(INetDaemonReactive daemon, IEnumerable<string> entityIds) : base(daemon, entityIds)
{{
Expand Down Expand Up @@ -211,7 +211,8 @@ public class CodeGenerator
else if (data is not null)
{{
var expObject = ((object)data).ToExpandoObject();
serviceData.CopyFrom(expObject);
if (expObject is not null)
serviceData.CopyFrom(expObject);
}}
{entityAssignmentStatement}
DaemonRxApp.CallService(""{domain}"", ""{s.Service}"", serviceData);
Expand Down
28 changes: 28 additions & 0 deletions src/Service/apps/DebugApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using System.Linq;
using System;
using System.Reactive.Linq;
using System.Collections.Generic;
using NetDaemon.Common.Reactive;
using NetDaemon.Common;

namespace Debug
{

/// <summary> Use this class as startingpoint for debugging </summary>
public class DebugApp : NetDaemonRxApp
{

public override void Initialize()
{
RunEvery(TimeSpan.FromSeconds(5), () => Log("Hello world!"));
}

[HomeAssistantServiceCall]
public void CallMeFromHass(dynamic data)
{
Log("A call from hass! {data}", data);
}
}

}
2 changes: 2 additions & 0 deletions src/Service/apps/DebugApp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
debug_app:
class: Debug.DebugApp
4 changes: 4 additions & 0 deletions src/Service/apps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Service project
This is the startup project for NetDaemon service. You can use this as debugger of NetDaemon. Provided in `apps` folder there are apps that can be used as starting point for debugging. These are not moved to output folder or publish folder.

See `Dev.md` for details how to setup debugging of this project

0 comments on commit 3a4adfd

Please sign in to comment.