Skip to content

Commit

Permalink
GUI: Description on app from code comment (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Jul 24, 2020
1 parent fd258a1 commit 18b21d2
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 121 deletions.
4 changes: 4 additions & 0 deletions exampleapps/apps/test1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using System.Reactive.Linq;
using System.Runtime.Serialization;
// using Netdaemon.Generated.Extensions;

/// <summary>
/// Does some awesome stuff
/// </summary>
public class GlobalApp : NetDaemonApp
{
// private ISchedulerResult _schedulerResult;
Expand Down
232 changes: 119 additions & 113 deletions exampleapps/apps/test2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,125 +6,131 @@
using Netdaemon.Generated.Reactive;
using NetDaemon.Common;

public class BatteryManager : GeneratedAppBase //NetDaemonRxApp
// public class BatteryManager : NetDaemonRxApp
namespace NStest
{
// private ISchedulerResult _schedulerResult;
private int numberOfRuns = 0;

public string? HelloWorldSecret { get; set; }
public override async Task InitializeAsync()
/// <summary> cool multiple lines </summary>
public class BatteryManager : GeneratedAppBase //NetDaemonRxApp
// public class BatteryManager : NetDaemonRxApp
{
// Remote.Tvrummet.TurnOn(new {activity="TV"});
// Log(Remote.Tvrummet.State);
// Log(Remote.Tvrummet.Area);

// SetState("sensor.testing", "on", new { attributeobject = new { aobject = "hello" } });
// RunEvery(TimeSpan.FromSeconds(5), () => Log("Hello world!"));
// RunDaily("13:00:00", () => Log("Hello world!"));
// RunIn(TimeSpan.FromSeconds(5), () => Entity("light.tomas_rum").TurnOn());
// Entity("light.tomas_rum")
// .StateChanges
// .Subscribe(s => Log("Chanche {entity}", s.New.State));

// StateChanges
// .Where(e => e.New.EntityId.StartsWith("light."))
// .Subscribe(e =>
// {
// Log("Hello!");
// });
// EventChanges
// // .Where(e => e.Domain == "scene" && e.Event == "turn_on")
// .Subscribe(e =>
// {
// Log("Hello!");
// },
// err => LogError(err, "Ohh nooo!"),
// () => Log("Ending event"));



// Event("TEST_EVENT").Call(async (ev, data) => { Log("EVENT2!"); }).Execute();

// Scheduler.RunEvery(5000, () => { var x = 0; var z = 4 / x; return Task.CompletedTask; });
// Entity("sun.sun").WhenStateChange(allChanges: true).Call((entityid, to, from) => throw new Exception("Test")).Execute();
// var app = (GlobalApp)GetApp("global_app");

// Log($"The global app shows {app.SharedThing}");
// int? test = null;
// bool testa = test.HasValue;

// await this.LightEx().JulbelysningVardagsrumH.TurnOn().ExecuteAsync();
// await this.MediaPlayerEx().PlexChromecast.Play().ExecuteAsync();
// Scheduler.RunIn(TimeSpan.FromSeconds(10), async () => await DoTheMagic("s").ConfigureAwait(false));

// private ISchedulerResult _schedulerResult;
private int numberOfRuns = 0;

public string? HelloWorldSecret { get; set; }
public override async Task InitializeAsync()
{
// Remote.Tvrummet.TurnOn(new {activity="TV"});
// Log(Remote.Tvrummet.State);
// Log(Remote.Tvrummet.Area);

// SetState("sensor.testing", "on", new { attributeobject = new { aobject = "hello" } });
// RunEvery(TimeSpan.FromSeconds(5), () => Log("Hello world!"));
// RunDaily("13:00:00", () => Log("Hello world!"));
// RunIn(TimeSpan.FromSeconds(5), () => Entity("light.tomas_rum").TurnOn());
// Entity("light.tomas_rum")
// .StateChanges
// .Subscribe(s => Log("Chanche {entity}", s.New.State));

// StateChanges
// .Where(e => e.New.EntityId.StartsWith("light."))
// .Subscribe(e =>
// {
// Log("Hello!");
// });
// EventChanges
// // .Where(e => e.Domain == "scene" && e.Event == "turn_on")
// .Subscribe(e =>
// {
// Log("Hello!");
// },
// err => LogError(err, "Ohh nooo!"),
// () => Log("Ending event"));



// Event("TEST_EVENT").Call(async (ev, data) => { Log("EVENT2!"); }).Execute();

// Scheduler.RunEvery(5000, () => { var x = 0; var z = 4 / x; return Task.CompletedTask; });
// Entity("sun.sun").WhenStateChange(allChanges: true).Call((entityid, to, from) => throw new Exception("Test")).Execute();
// var app = (GlobalApp)GetApp("global_app");

// Log($"The global app shows {app.SharedThing}");
// int? test = null;
// bool testa = test.HasValue;

// await this.LightEx().JulbelysningVardagsrumH.TurnOn().ExecuteAsync();
// await this.MediaPlayerEx().PlexChromecast.Play().ExecuteAsync();
// Scheduler.RunIn(TimeSpan.FromSeconds(10), async () => await DoTheMagic("s").ConfigureAwait(false));

}

// async Task DoTheMagic()
// {
// Log("WAITING FOR STATE");
// // await Entity("binary_sensor.vardagsrum_pir").DelayUntilStateChange(to: "on").Task;

// var task = DelayUntilStateChange(new string[] { "binary_sensor.vardagsrum_pir" }, to: "on", from: "off");

// await task.Task;
// Log("STATE IS COOL DAMN IT!!!");

// }
// async Task DoTheMagic(string test)
// {
// Log("WAITING FOR STATE");
// // await Entity("binary_sensor.vardagsrum_pir").DelayUntilStateChange(to: "on").Task;

// var task = DelayUntilStateChange(new string[] { "binary_sensor.vardagsrum_pir" }, to: "on", from: "off");

// await task.Task;
// Log("STATE IS COOL DAMN IT!!!");

// }
// bool doingWork = false;
// private async Task DoWork()
// {
// if (doingWork)
// return;

// // Time to do work
// Entity(Wh)
// }

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

// async Task DoTheMagic()
// public static class NotifyExtensions
// {
// Log("WAITING FOR STATE");
// // await Entity("binary_sensor.vardagsrum_pir").DelayUntilStateChange(to: "on").Task;

// var task = DelayUntilStateChange(new string[] { "binary_sensor.vardagsrum_pir" }, to: "on", from: "off");

// await task.Task;
// Log("STATE IS COOL DAMN IT!!!");

// public static async Task NotifyMobile(this NetDaemonApp app, string mobileId, string title, string message)
// {
// var data = new Obj
// {
// ["title"] = title,
// ["message"] = message
// };
// await app.CallService("notify", mobileId, data, false);
// }

// public static async Task NotifyMobileThread(this NetDaemonApp app, string mobileId, string title, string message, string threadId)
// {
// var data = new Obj
// {
// ["title"] = title,
// ["message"] = message,
// ["data"] = new Obj
// {
// ["push"] = new Obj
// {
// ["thread-id"] = threadId
// }
// }
// };
// await app.CallService("notify", mobileId, data, false);
// }
// }
// async Task DoTheMagic(string test)
// {
// Log("WAITING FOR STATE");
// // await Entity("binary_sensor.vardagsrum_pir").DelayUntilStateChange(to: "on").Task;

// var task = DelayUntilStateChange(new string[] { "binary_sensor.vardagsrum_pir" }, to: "on", from: "off");

// await task.Task;
// Log("STATE IS COOL DAMN IT!!!");

// }
// bool doingWork = false;
// private async Task DoWork()
// {
// if (doingWork)
// return;

// // Time to do work
// Entity(Wh)
// }

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

// public static class NotifyExtensions
// {
// public static async Task NotifyMobile(this NetDaemonApp app, string mobileId, string title, string message)
// {
// var data = new Obj
// {
// ["title"] = title,
// ["message"] = message
// };
// await app.CallService("notify", mobileId, data, false);
// }

// public static async Task NotifyMobileThread(this NetDaemonApp app, string mobileId, string title, string message, string threadId)
// {
// var data = new Obj
// {
// ["title"] = title,
// ["message"] = message,
// ["data"] = new Obj
// {
// ["push"] = new Obj
// {
// ["thread-id"] = threadId
// }
// }
// };
// await app.CallService("notify", mobileId, data, false);
// }
// }
2 changes: 1 addition & 1 deletion exampleapps/apps/test2.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
my_app:
class: BatteryManager
class: NStest.BatteryManager
# HelloWorldSecret: !secret test_secret
dependencies:
- global_app
11 changes: 8 additions & 3 deletions src/App/NetDaemon.App/Common/INetDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ public interface INetDaemonAppBase :
/// </summary>
public string? Id { get; set; }

/// <summary>
/// Returns the description, is the decorating comment of app class
/// </summary>
public string Description { get; }

/// <summary>
/// Gets or sets a flag indicating whether this app is enabled.
/// This property property can be controlled from Home Assistant.
Expand Down Expand Up @@ -410,7 +415,7 @@ public interface IDoLogging
/// <param name="message">The message to log</param>
/// <param name="param">Params</param>
void Log(Exception exception, string message, params object[] param);

/// <summary>
/// Logs an informational message
/// </summary>
Expand Down Expand Up @@ -438,7 +443,7 @@ public interface IDoLogging
/// <param name="message">The message to log</param>
/// <param name="param">Params</param>
void LogInformation(Exception exception, string message, params object[] param);

/// <summary>
/// Logs a debug message
/// </summary>
Expand Down Expand Up @@ -479,7 +484,7 @@ public interface IDoLogging
/// <param name="exception">A exception</param>
/// <param name="message">The message to log</param>
void LogError(Exception exception, string message);

/// <summary>
/// Logs an error message
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions src/App/NetDaemon.App/Common/NetDaemonAppBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ namespace NetDaemon.Common
/// </summary>
public abstract class NetDaemonAppBase : INetDaemonAppBase
{
/// <summary>
/// A set of properties found in static analysis of code for each app
/// </summary>
public static Dictionary<string, Dictionary<string, string>> CompileTimeProperties { get; set; } = new Dictionary<string, Dictionary<string, string>>();

/// <summary>
/// The NetDaemonHost instance
/// </summary>
Expand Down Expand Up @@ -82,6 +87,28 @@ public IHttpHandler Http
/// <inheritdoc/>
public bool IsEnabled { get; set; } = true;

/// <inheritdoc/>
public string Description
{
get
{
var app_key = this.GetType().FullName;

if (app_key is null)
return "";

if (CompileTimeProperties.ContainsKey(app_key))
{
if (CompileTimeProperties[app_key].ContainsKey("description"))
{
return CompileTimeProperties[app_key]["description"];
}
}
return "";
}
}


/// <inheritdoc/>
public ILogger? Logger { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions src/DaemonRunner/DaemonRunner/Service/API/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public class ApiController : ControllerBase
[Route("apps")]
public IEnumerable<ApiApplication>? Apps()
{

return _host?.AllAppInstances.Select(n => new ApiApplication()
{
Id = n.Id,
Dependencies = n.Dependencies,
IsEnabled = n.IsEnabled
IsEnabled = n.IsEnabled,
Description = n.Description
});
}


}

}
2 changes: 2 additions & 0 deletions src/DaemonRunner/DaemonRunner/Service/API/ApiData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class ApiApplication

public bool IsEnabled { get; set; }

public string? Description { get; set; }

}

public class ApiConfig
Expand Down
Loading

0 comments on commit 18b21d2

Please sign in to comment.