|
| 1 | +using CodeCasa.Lights; |
| 2 | +using System.Reactive.Concurrency; |
| 3 | +using CodeCasa.AutomationPipelines.Lights.Context; |
| 4 | +using CodeCasa.AutomationPipelines.Lights.Nodes; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | + |
| 7 | +namespace CodeCasa.AutomationPipelines.Lights.Extensions |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Extension methods for <see cref="ILightPipelineContext"/>. |
| 11 | + /// </summary> |
| 12 | + public static class LightPipelineContextExtensions |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Creates a pipeline node that applies the specified light parameters and automatically turns off the light after a specified duration. |
| 16 | + /// The timeout is not reset by any external events. |
| 17 | + /// </summary> |
| 18 | + /// <param name="context">The light pipeline context.</param> |
| 19 | + /// <param name="lightParameters">The light parameters to apply as a transition.</param> |
| 20 | + /// <param name="timeSpan">The duration after which the light should turn off.</param> |
| 21 | + /// <returns>A pipeline node that applies the light parameters and handles the turn-off behavior.</returns> |
| 22 | + public static IPipelineNode<LightTransition> LightParametersThatTurnsOffAfter(this ILightPipelineContext context, |
| 23 | + LightParameters lightParameters, |
| 24 | + TimeSpan timeSpan) |
| 25 | + { |
| 26 | + var scheduler = context.ServiceProvider.GetRequiredService<IScheduler>(); |
| 27 | + var innerNode = new StaticLightTransitionNode(lightParameters.AsTransition(), scheduler); |
| 28 | + return innerNode.TurnOffAfter(timeSpan, scheduler); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Creates a pipeline node that applies the specified light parameters and automatically turns off the light after a specified duration. |
| 33 | + /// The timeout can be reset when the observable emits a value. |
| 34 | + /// </summary> |
| 35 | + /// <typeparam name="T">The type of elements emitted by the reset timer observable.</typeparam> |
| 36 | + /// <param name="context">The light pipeline context.</param> |
| 37 | + /// <param name="lightParameters">The light parameters to apply as a transition.</param> |
| 38 | + /// <param name="timeSpan">The duration after which the light should turn off.</param> |
| 39 | + /// <param name="resetTimerObservable">An observable that resets the turn-off timer when it emits.</param> |
| 40 | + /// <returns>A pipeline node that applies the light parameters and handles the turn-off behavior.</returns> |
| 41 | + public static IPipelineNode<LightTransition> LightParametersThatTurnsOffAfter<T>(this ILightPipelineContext context, |
| 42 | + LightParameters lightParameters, |
| 43 | + TimeSpan timeSpan, IObservable<T> resetTimerObservable) |
| 44 | + { |
| 45 | + var scheduler = context.ServiceProvider.GetRequiredService<IScheduler>(); |
| 46 | + var innerNode = new StaticLightTransitionNode(lightParameters.AsTransition(), scheduler); |
| 47 | + return innerNode.TurnOffAfter(timeSpan, resetTimerObservable, scheduler); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments