diff --git a/exampleapps/apps/test1.cs b/exampleapps/apps/test1.cs index a953be2ef..e748f14b2 100644 --- a/exampleapps/apps/test1.cs +++ b/exampleapps/apps/test1.cs @@ -19,9 +19,10 @@ public override Task InitializeAsync() var rand = new Random(); var randNumber = rand.Next(); - RunEvery(TimeSpan.FromSeconds(5)).Subscribe(s => Log("Hello world {rand}", randNumber)); + RunEvery(TimeSpan.FromSeconds(5), () => Log("Hello world {rand}", randNumber)); + RunEvery(TimeSpan.FromSeconds(15), () => throw new Exception("Ohh noo man!")); // EventChanges // .Subscribe(f => diff --git a/src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs b/src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs index 0285bcdbb..122e3d823 100644 --- a/src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs +++ b/src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs @@ -92,7 +92,7 @@ public RxEntity Entity(string entityId) } /// - public IObservable RunDaily(string time) + public IDisposable RunDaily(string time, Action action) { DateTime timeOfDayToTrigger; @@ -100,19 +100,77 @@ public IObservable RunDaily(string time) { throw new FormatException($"{time} is not a valid time for the current locale"); } - return Observable.Timer(timeOfDayToTrigger, TimeSpan.FromDays(1), TaskPoolScheduler.Default).TakeWhile(x => this.IsEnabled); + + return Observable.Timer( + timeOfDayToTrigger, + TimeSpan.FromDays(1), + TaskPoolScheduler.Default) + .TakeWhile(x => this.IsEnabled) + .Subscribe( + s => + { + try + { + action(); + } + catch (Exception e) + { + LogError(e, "Error, RunDaily APP: {app}", Id ?? "unknown"); + } + + }, + ex => + { + LogError(ex, "Error, RunDaily_ex APP: {app}", Id ?? "unknown"); + }); } /// - public IObservable RunEvery(TimeSpan timespan) + public IDisposable RunEvery(TimeSpan timespan, Action action) { - return Observable.Interval(timespan, TaskPoolScheduler.Default).TakeWhile(x => this.IsEnabled); + return Observable.Interval(timespan, TaskPoolScheduler.Default) + .TakeWhile(x => this.IsEnabled) + .Subscribe( + s => + { + try + { + action(); + } + catch (Exception e) + { + LogError(e, "Error, RunEvery APP: {app}", Id ?? "unknown"); + } + + }, + ex => + { + LogError(ex, "Error, RunEvery_ex APP: {app}", Id ?? "unknown"); + }); } /// - public IObservable RunIn(TimeSpan timespan) + public IDisposable RunIn(TimeSpan timespan, Action action) { - return Observable.Timer(timespan, TaskPoolScheduler.Default).TakeWhile(x => this.IsEnabled); + return Observable.Timer(timespan, TaskPoolScheduler.Default) + .TakeWhile(x => this.IsEnabled) + .Subscribe( + s => + { + try + { + action(); + } + catch (Exception e) + { + LogError(e, "Error, RunIn APP: {app}", Id ?? "unknown"); + } + + }, + ex => + { + LogError(ex, "Error, RunIn_ex APP: {app}", Id ?? "unknown"); + }); } ///