Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion docs/user/extensions/scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,20 @@ Install-Package NetDaemon.Extensions.Scheduling
.AddNetDaemonScheduler()
```

* If you want to schedule based on Sun Events (e.g. trigger at sunrise/sunset), then also register the SunEventsScheduler:

```csharp
.ConfigureServices((_, services) =>
services
.AddAppsFromAssembly(Assembly.GetExecutingAssembly())
.AddNetDaemonStateManager()
.AddNetDaemonScheduler()
.AddSunEventScheduler(latitude, longitude)
```

### Injecting the scheduler

You can get an instance of the `IScheduler` interface bye simply injecting it into your apps constructor:
You can get an instance of the `IScheduler` interface by simply injecting it into your apps constructor:

```csharp
using System.Reactive.Concurrency.Scheduler;
Expand All @@ -49,6 +60,16 @@ __IScheduler.Now always returns UTC. Use `Now.LocalDateTime` to get the current

:::

For Sun Events, inject the `ISunEventScheduler` instance into your app.

```csharp
using System.Reactive.Concurrency.Scheduler;

[NetDaemonApp]
class MyApp(ISunEventScheduler sunEventScheduler)
{ }
```

### Using the Scheduler

The `System.Reactive.Concurrency.Scheduler` namespace provides several extension methods for `IScheduler` that allow you to schedule tasks at a specific time, after a specific `TimeSpan`, or periodically. You can use these framework provided methods directly on the scheduler you received via the constructor and they will be scheduled using the cancellation and logging behavior.
Expand Down Expand Up @@ -81,6 +102,19 @@ The first argument of this method is a [Cron expression](https://en.wikipedia.or

The `ScheduleCron()` extension method uses [Cronos](https://github.com/HangfireIO/Cronos) to parse your Cron expression. See its docs for the exact specification.

### Sun Events

The `ISunEventScheduler` enables triggering an event at Dawn, Sunrise, Sunset and Dusk. The specific times are calculated everyday based on the coordinates you provide when registering the service.

```csharp
public SunriseSchedulingApp(IHaContext ha, IScheduler scheduler)
{
var entities = new Entities(ha);
scheduler.RunAtSunrise(() => entities.Light.Patio.TurnOff());
}
```


## Unit testing scheduling apps

Timing in some apps can be pretty complicated. It can therefore be useful to create unit tests for your schedules. For this purpose there is a special version of the `IScheduler` interface implemented by `Microsoft.Reactive.Testing.TestScheduler` in the `Microsoft.Reactive.Testing` nuget package.
Expand Down