Skip to content

Commit

Permalink
Support for events
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 10, 2020
1 parent 2839e1a commit 10adc3a
Show file tree
Hide file tree
Showing 6 changed files with 660 additions and 570 deletions.
18 changes: 11 additions & 7 deletions exampleapps/apps/test1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ 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 =>
//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 =>
{
Log("{entity} ({state})", x.New.EntityId, x.New.State);
Log("event: {domain}.{event} - {data}", f?.Domain??"none", f.Event, f?.Data);
});


return Task.CompletedTask;
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/App/NetDaemon.App/Common/INetDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Reactive.Linq;
using JoySoftware.HomeAssistant.NetDaemon.Common.Reactive;

namespace JoySoftware.HomeAssistant.NetDaemon.Common
{
Expand Down Expand Up @@ -101,10 +102,18 @@ public interface INetDaemonAppBase : INetDaemonInitialableApp, IDisposable, IEqu
/// <summary>
/// Interface that all NetDaemon apps needs to implement
/// </summary>
public interface INetDaemon : INetDaemonCommon,
//IObservable<EntityState> //,
IObservable<(EntityState Old, EntityState New)>
public interface INetDaemon : INetDaemonCommon
{
/// <summary>
/// The observable implementation for state changes
/// </summary>
IRxStateChange StateChanges { get; }

/// <summary>
/// The observable implementation for event changes
/// </summary>
IRxEvent EventChanges { get; }

/// <summary>
/// Selects one or more camera entities to do action on
/// </summary>
Expand Down
44 changes: 37 additions & 7 deletions src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Reactive.Concurrency;

namespace JoySoftware.HomeAssistant.NetDaemon.Common.Reactive
{
Expand All @@ -29,16 +28,15 @@ public static IObservable<(EntityState Old, EntityState New)> NDSameStateFor(thi
/// </summary>
public abstract class NetDaemonRxApp : NetDaemonAppBase, INetDaemonReactive
{
private ReactiveEvent? _reactiveEvent = null;
private ReactiveState? _reactiveState = null;

public NetDaemonRxApp()
{
}

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

public IRxEvent EventChanges => _reactiveEvent;
public IRxStateChange StateAllChanges => _reactiveState;

public IObservable<(EntityState Old, EntityState New)> StateChanges => _reactiveState.Where(e => e.New.State != e.Old.State);
public IEnumerable<EntityState> States =>
_daemon?.State ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");

Expand Down Expand Up @@ -112,11 +110,27 @@ public async override Task StartUpAsync(INetDaemon daemon)
await base.StartUpAsync(daemon);
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_reactiveState = new ReactiveState(_daemon);
_reactiveEvent = new ReactiveEvent(_daemon);
}

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

public class ReactiveEvent : IRxEvent
{
private readonly INetDaemon _daemon;

public ReactiveEvent(INetDaemon daemon)
{
_daemon = daemon;
}

public IDisposable Subscribe(IObserver<RxEvent> observer)
{
return _daemon!.EventChanges.Subscribe(observer);
}
}

public class ReactiveState : IRxStateChange
{
private readonly INetDaemon _daemon;
Expand All @@ -128,7 +142,23 @@ public ReactiveState(INetDaemon daemon)

public IDisposable Subscribe(IObserver<(EntityState, EntityState)> observer)
{
return _daemon!.Subscribe(observer);
return _daemon!.StateChanges.Subscribe(observer);
}
}
public class RxEvent
{
private readonly dynamic? _data;
private readonly string? _domain;
private readonly string _eventName;
public RxEvent(string eventName, string? domain, dynamic? data)
{
_eventName = eventName;
_domain = domain;
_data = data;
}

public dynamic? Data => _data;
public dynamic? Domain => _domain;
public string Event => _eventName;
}
}
59 changes: 32 additions & 27 deletions src/App/NetDaemon.App/Common/Reactive/INetDaemonReactive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ public interface ICallService
public interface INetDaemonReactive : INetDaemonAppBase, ICallService, IRxEntity
{
/// <summary>
/// The observable statestream state change
/// The observable events
/// </summary>
/// <remarks>
/// Old state != New state
/// </remarks>
public IObservable<(EntityState Old, EntityState New)> StateChanges { get; }
public IRxEvent EventChanges { get; }

/// <summary>
/// The observable statestream, all changes inkluding attributes
/// </summary>
public IRxStateChange StateAllChanges { get; }

/// <summary>
/// The observable statestream state change
/// </summary>
/// <remarks>
/// Old state != New state
/// </remarks>
public IObservable<(EntityState Old, EntityState New)> StateChanges { get; }
/// <summary>
/// Enuberable of current states
/// </summary>
Expand All @@ -48,28 +52,6 @@ public interface INetDaemonReactive : INetDaemonAppBase, ICallService, IRxEntity

public interface IRxAppHelpers
{

}

public interface IRxSchedule
{
/// <summary>
/// Shedules an action every (timespan)
/// </summary>
/// <param name="timespan">The timespan to schedule</param>
IObservable<long> RunEvery(TimeSpan timespan);

/// <summary>
/// Run daily at a specific time
/// </summary>
/// <param name="time">The time in "hh:mm:ss" format</param>
IObservable<long> RunDaily(string time);

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

public interface IRxEntity
Expand Down Expand Up @@ -99,6 +81,29 @@ public interface IRxEntity
RxEntity Entity(string entityId);
}

public interface IRxEvent : IObservable<RxEvent>
{
}

public interface IRxSchedule
{
/// <summary>
/// Run daily at a specific time
/// </summary>
/// <param name="time">The time in "hh:mm:ss" format</param>
IObservable<long> RunDaily(string time);

/// <summary>
/// Shedules an action every (timespan)
/// </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);
}
public interface IRxStateChange : IObservable<(EntityState Old, EntityState New)>
{
}
Expand Down
Loading

0 comments on commit 10adc3a

Please sign in to comment.