-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightTransitionReactiveNodeConfigurator.Toggle.cs
More file actions
109 lines (96 loc) · 4.7 KB
/
Copy pathLightTransitionReactiveNodeConfigurator.Toggle.cs
File metadata and controls
109 lines (96 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using CodeCasa.AutomationPipelines.Lights.Extensions;
using CodeCasa.AutomationPipelines.Lights.Nodes;
using CodeCasa.AutomationPipelines.Lights.Timeline;
using CodeCasa.AutomationPipelines.Lights.Toggle;
using CodeCasa.Lights;
using CodeCasa.Lights.Extensions;
using Occurify;
namespace CodeCasa.AutomationPipelines.Lights.ReactiveNode;
internal partial class LightTransitionReactiveNodeConfigurator<TLight>
{
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable, IEnumerable<LightParameters> lightParameters)
=> AddToggle(triggerObservable, lightParameters.ToArray());
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable,
params LightParameters[] lightParameters)
{
return AddToggle(triggerObservable, configure =>
{
foreach (var lightParameter in lightParameters)
{
configure.Add(lightParameter);
}
});
}
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable, IEnumerable<LightTransition> lightTransitions)
=> AddToggle(triggerObservable, lightTransitions.ToArray());
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable,
params LightTransition[] lightTransitions)
{
return AddToggle(triggerObservable, configure =>
{
foreach (var lightTransition in lightTransitions)
{
configure.Add(lightTransition);
}
});
}
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable, IEnumerable<Func<IServiceProvider, IPipelineNode<LightTransition>>> nodeFactories)
=> AddToggle(triggerObservable, nodeFactories.ToArray());
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable, params Func<IServiceProvider, IPipelineNode<LightTransition>>[] nodeFactories)
{
return AddToggle(triggerObservable, configure =>
{
foreach (var fact in nodeFactories)
{
configure.Add(fact);
}
});
}
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable, Action<ILightTransitionToggleConfigurator<TLight>> configure)
{
var toggleConfigurator = new LightTransitionToggleConfigurator<TLight>(Light, _scheduler);
configure(toggleConfigurator);
var gracePeriod = toggleConfigurator.GracePeriod ?? TimeSpan.FromSeconds(1);
AddNodeSource(triggerObservable.ToToggleObservable(
lastActivationTime =>
{
var utcNow = DateTime.UtcNow;
if (utcNow - Light.LastChangedUtc <= gracePeriod &&
(!lastActivationTime.HasValue || utcNow - lastActivationTime > gracePeriod))
{
return !Light.IsOn();
}
return Light.IsOn();
},
() => new TurnOffThenPassThroughNode(),
toggleConfigurator.NodeFactories.Select(fact =>
{
return new Func<IPipelineNode<LightTransition>>(() =>
fact.CreateScopedNode(ServiceProvider) // Note: This service provider already has the light registered. We scope it further for node lifetime.
);
}),
toggleConfigurator.ToggleTimeout ?? TimeSpan.FromMilliseconds(1000),
toggleConfigurator.IncludeOffValue));
return this;
}
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable,
Dictionary<ITimeline, LightParameters> timeline, TimeSpan? transitionTimeForTimelineState = null)
=> AddToggle(triggerObservable, c => c.AddTimeline(timeline, transitionTimeForTimelineState));
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable,
Func<IServiceProvider, Dictionary<ITimeline, LightParameters>> timelineFactory,
TimeSpan? transitionTimeForTimelineState = null)
=> AddToggle(triggerObservable, c => c.AddTimeline(timelineFactory, transitionTimeForTimelineState));
/// <inheritdoc/>
public ILightTransitionReactiveNodeConfigurator<TLight> AddToggle<T>(IObservable<T> triggerObservable,
Action<ITimelineConfigurator> configure)
=> AddToggle(triggerObservable, c => c.AddTimeline(configure));
}