Skip to content

Commit

Permalink
Documentation and remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 11, 2020
1 parent 707c13c commit bcb6858
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 33 deletions.
11 changes: 3 additions & 8 deletions exampleapps/apps/test1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@ public class GlobalApp : NetDaemonRxApp
//public string? SharedThing { get; set; }
public override Task InitializeAsync()
{
//Entities("binary_sensor.tomas_rum_pir", "binary_sensor.vardagsrum_pir")
// .Merge()
// .Where(e => e.New.State == "off")
// .Subscribe(x =>
// {
// Log("{entity} ({state})", x.New.EntityId, x.New.State);
// });





EventChanges
.Subscribe(f =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/App/NetDaemon.App/Common/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static dynamic ToDynamic(this (string name, object val)[] attributeNameVa
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static ExpandoObject ToExpandoObject(this object obj)
internal static ExpandoObject ToExpandoObject(this object obj)
{
// Null-check

Expand Down
6 changes: 6 additions & 0 deletions src/App/NetDaemon.App/Common/INetDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ public interface INetDaemon : INetDaemonCommon
/// <param name="data">Any data that the service requires</param>
void CallService(string domain, string service, dynamic? data = null);

/// <summary>
/// Set entity state
/// </summary>
/// <param name="entityId">Entity unique id</param>
/// <param name="state">The state that being set, only primitives are supported</param>
/// <param name="attributes">Attributes, use anonomous types and lowercase letters</param>
void SetState(string entityId, dynamic state, dynamic? attributes = null);
}

Expand Down
8 changes: 8 additions & 0 deletions src/App/NetDaemon.App/Common/NetDaemonAppBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,23 @@ public bool Equals([AllowNull] INetDaemonAppBase other)
return false;
}

/// <summary>
/// Initializes the app, is virtual and overridden
/// </summary>
public virtual void Initialize()
{
// do nothing
}

/// <summary>
/// Initializes the app async, is virtual and overridden
/// </summary>
public virtual Task InitializeAsync()
{
// Do nothing
return Task.CompletedTask;
}

/// <inheritdoc/>
public async Task RestoreAppStateAsync()
{
Expand Down
76 changes: 70 additions & 6 deletions src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace JoySoftware.HomeAssistant.NetDaemon.Common.Reactive
{
/// <summary>
/// Extension methods for Observables
/// </summary>
public static class ObservableExtensionMethods
{
/// <summary>
Expand All @@ -30,22 +33,30 @@ public abstract class NetDaemonRxApp : NetDaemonAppBase, INetDaemonReactive
{
private ReactiveEvent? _reactiveEvent = null;
private ReactiveState? _reactiveState = null;
public NetDaemonRxApp()
{
}

public IRxEvent EventChanges => _reactiveEvent;
public IRxStateChange StateAllChanges => _reactiveState;
/// <inheritdoc/>
public IRxEvent EventChanges =>
_reactiveEvent ?? throw new ApplicationException("Application not initialized correctly");

/// <inheritdoc/>
public IRxStateChange StateAllChanges =>
_reactiveState ?? throw new ApplicationException("Application not initialized correctly");

/// <inheritdoc/>
public IObservable<(EntityState Old, EntityState New)> StateChanges => _reactiveState.Where(e => e.New.State != e.Old.State);

/// <inheritdoc/>
public IEnumerable<EntityState> States =>
_daemon?.State ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");

public void CallService(string domain, string service, dynamic data)
/// <inheritdoc/>
public void CallService(string domain, string service, dynamic? data)
{
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_daemon.CallService(domain, service, data);
}

/// <inheritdoc/>
public RxEntity Entities(Func<IEntityProperties, bool> func)
{
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
Expand All @@ -63,20 +74,24 @@ public RxEntity Entities(Func<IEntityProperties, bool> func)
}
}

/// <inheritdoc/>
public RxEntity Entities(params string[] entityIds) => Entities((IEnumerable<string>)entityIds);

/// <inheritdoc/>
public RxEntity Entities(IEnumerable<string> entityIds)
{
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return new RxEntity(_daemon, entityIds);
}

/// <inheritdoc/>
public RxEntity Entity(string entityId)
{
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return new RxEntity(_daemon, new string[] { entityId });
}

/// <inheritdoc/>
public IObservable<long> RunDaily(string time)
{
DateTime timeOfDayToTrigger;
Expand All @@ -89,22 +104,26 @@ public IObservable<long> RunDaily(string time)
return Observable.Timer(timeOfDayToTrigger, TimeSpan.FromDays(1), TaskPoolScheduler.Default);
}

/// <inheritdoc/>
public IObservable<long> RunEvery(TimeSpan timespan)
{
return Observable.Interval(timespan, TaskPoolScheduler.Default);
}

/// <inheritdoc/>
public IObservable<long> RunIn(TimeSpan timespan)
{
return Observable.Timer(timespan, TaskPoolScheduler.Default);
}

/// <inheritdoc/>
public void SetState(string entityId, dynamic state, dynamic? attributes = null)
{
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_daemon.SetState(entityId, state, attributes);
}

/// <inheritdoc/>
public async override Task StartUpAsync(INetDaemon daemon)
{
await base.StartUpAsync(daemon);
Expand All @@ -113,52 +132,97 @@ public async override Task StartUpAsync(INetDaemon daemon)
_reactiveEvent = new ReactiveEvent(_daemon);
}

/// <inheritdoc/>
public EntityState? State(string entityId) => _daemon?.GetState(entityId);
}

/// <summary>
/// Implements Observable RxEvent
/// </summary>
public class ReactiveEvent : IRxEvent
{
private readonly INetDaemon _daemon;

/// <summary>
/// Constructor
/// </summary>
/// <param name="daemon">The NetDaemon host object</param>
public ReactiveEvent(INetDaemon daemon)
{
_daemon = daemon;
}

/// <summary>
/// Implements IObservable ReactiveEvent
/// </summary>
/// <param name="observer">Observer</param>
public IDisposable Subscribe(IObserver<RxEvent> observer)
{
return _daemon!.EventChanges.Subscribe(observer);
}
}

/// <summary>
/// Implements the IObservable state changes
/// </summary>
public class ReactiveState : IRxStateChange
{
private readonly INetDaemon _daemon;

/// <summary>
/// Constructor
/// </summary>
/// <param name="daemon">The NetDaemon host object</param>
public ReactiveState(INetDaemon daemon)
{
_daemon = daemon;
}

/// <summary>
/// Implements IObservable ReactivState
/// </summary>
/// <param name="observer">Observer</param>
public IDisposable Subscribe(IObserver<(EntityState, EntityState)> observer)
{
return _daemon!.StateChanges.Subscribe(observer);
}
}

/// <summary>
/// Represent an event from eventstream
/// </summary>
public class RxEvent
{
private readonly dynamic? _data;
private readonly string? _domain;
private readonly string _eventName;

/// <summary>
/// Constructor
/// </summary>
/// <param name="eventName">Event</param>
/// <param name="domain">Domain</param>
/// <param name="data">Data</param>
public RxEvent(string eventName, string? domain, dynamic? data)
{
_eventName = eventName;
_domain = domain;
_data = data;
}

/// <summary>
/// Data from event
/// </summary>
public dynamic? Data => _data;

/// <summary>
/// Domain (call service event)
/// </summary>
public dynamic? Domain => _domain;

/// <summary>
/// The event being sent
/// </summary>
public string Event => _eventName;
}
}
30 changes: 27 additions & 3 deletions src/App/NetDaemon.App/Common/Reactive/INetDaemonReactive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@

namespace JoySoftware.HomeAssistant.NetDaemon.Common.Reactive
{
/// <summary>
/// Interface for objects that implements CallService
/// </summary>
public interface ICallService
{
/// <summary>
/// Calls service in Home Assistant
/// </summary>
/// <param name="domain">Domain of sevice</param>
/// <param name="service">Service name</param>
/// <param name="data">Data provided to service. Use anonomous type</param>
void CallService(string domain, string service, dynamic? data);
}

Expand All @@ -30,6 +39,7 @@ public interface INetDaemonReactive : INetDaemonAppBase, ICallService, IRxEntity
/// Old state != New state
/// </remarks>
public IObservable<(EntityState Old, EntityState New)> StateChanges { get; }

/// <summary>
/// Enuberable of current states
/// </summary>
Expand All @@ -50,10 +60,13 @@ public interface INetDaemonReactive : INetDaemonAppBase, ICallService, IRxEntity
EntityState? State(string entityId);
}

public interface IRxAppHelpers
{
}
//public interface IRxAppHelpers
//{
//}

/// <summary>
/// Interface for entities in Rx API
/// </summary>
public interface IRxEntity
{
/// <summary>
Expand Down Expand Up @@ -81,10 +94,16 @@ public interface IRxEntity
RxEntity Entity(string entityId);
}

/// <summary>
/// Interface for Observable Events
/// </summary>
public interface IRxEvent : IObservable<RxEvent>
{
}

/// <summary>
/// Interface for scheduling
/// </summary>
public interface IRxSchedule
{
/// <summary>
Expand All @@ -98,12 +117,17 @@ public interface IRxSchedule
/// </summary>
/// <param name="timespan">The timespan to schedule</param>
IObservable<long> RunEvery(TimeSpan timespan);

/// <summary>
/// Delays excecution of an action (timespan) time
/// </summary>
/// <param name="timespan">Timespan to delay</param>
IObservable<long> RunIn(TimeSpan timespan);
}

/// <summary>
/// Interface for observable state changes
/// </summary>
public interface IRxStateChange : IObservable<(EntityState Old, EntityState New)>
{
}
Expand Down

0 comments on commit bcb6858

Please sign in to comment.