-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeLightTransitionToggleConfigurator.cs
More file actions
170 lines (143 loc) · 7.4 KB
/
Copy pathCompositeLightTransitionToggleConfigurator.cs
File metadata and controls
170 lines (143 loc) · 7.4 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System.Reactive.Concurrency;
using CodeCasa.AutomationPipelines.Lights.Extensions;
using CodeCasa.AutomationPipelines.Lights.Nodes;
using CodeCasa.AutomationPipelines.Lights.Timeline;
using CodeCasa.Lights;
using Microsoft.Extensions.DependencyInjection;
using Occurify;
namespace CodeCasa.AutomationPipelines.Lights.Toggle;
internal class CompositeLightTransitionToggleConfigurator<TLight>(
Dictionary<string, LightTransitionToggleConfigurator<TLight>> activeConfigurators,
Dictionary<string, LightTransitionToggleConfigurator<TLight>> inactiveConfigurators) : ILightTransitionToggleConfigurator<TLight>
where TLight : ILight
{
public ILightTransitionToggleConfigurator<TLight> SetToggleTimeout(TimeSpan timeout)
{
activeConfigurators.Values.ForEach(c => c.SetToggleTimeout(timeout));
inactiveConfigurators.Values.ForEach(c => c.SetToggleTimeout(timeout));
return this;
}
public ILightTransitionToggleConfigurator<TLight> IncludeOffInToggleCycle()
{
activeConfigurators.Values.ForEach(c => c.IncludeOffInToggleCycle());
inactiveConfigurators.Values.ForEach(c => c.IncludeOffInToggleCycle());
return this;
}
public ILightTransitionToggleConfigurator<TLight> ExcludeOffFromToggleCycle()
{
activeConfigurators.Values.ForEach(c => c.ExcludeOffFromToggleCycle());
inactiveConfigurators.Values.ForEach(c => c.ExcludeOffFromToggleCycle());
return this;
}
public ILightTransitionToggleConfigurator<TLight> SetGracePeriod(TimeSpan gracePeriod)
{
activeConfigurators.Values.ForEach(c => c.SetGracePeriod(gracePeriod));
inactiveConfigurators.Values.ForEach(c => c.SetGracePeriod(gracePeriod));
return this;
}
public ILightTransitionToggleConfigurator<TLight> AddOff()
{
return Add<TurnOffThenPassThroughNode>();
}
public ILightTransitionToggleConfigurator<TLight> AddOn()
{
return Add(LightTransition.On());
}
public ILightTransitionToggleConfigurator<TLight> Add(LightParameters lightParameters)
{
return Add(lightParameters.AsTransition());
}
public ILightTransitionToggleConfigurator<TLight> Add(Func<IServiceProvider, LightParameters?> lightParametersFactory)
{
return Add(c => lightParametersFactory(c)?.AsTransition());
}
public ILightTransitionToggleConfigurator<TLight> Add(Func<IServiceProvider, LightTransition?, LightParameters?> lightParametersFactory)
{
return Add((c, t) => lightParametersFactory(c, t)?.AsTransition());
}
public ILightTransitionToggleConfigurator<TLight> Add(LightTransition lightTransition)
{
return Add(_ => lightTransition);
}
public ILightTransitionToggleConfigurator<TLight> Add(Func<IServiceProvider, LightTransition?> lightTransitionFactory)
{
return Add(c => new StaticLightTransitionNode(lightTransitionFactory(c), c.GetRequiredService<IScheduler>()));
}
public ILightTransitionToggleConfigurator<TLight> Add(Func<IServiceProvider, LightTransition?, LightTransition?> lightTransitionFactory)
{
return Add(c => new FactoryNode<LightTransition>(t => lightTransitionFactory(c, t)));
}
public ILightTransitionToggleConfigurator<TLight> Add<TNode>() where TNode : IPipelineNode<LightTransition>
{
activeConfigurators.Values.ForEach(c => c.Add<TNode>());
inactiveConfigurators.Values.ForEach(c => c.AddPassThrough());
return this;
}
public ILightTransitionToggleConfigurator<TLight> Add(Func<IServiceProvider, IPipelineNode<LightTransition>> nodeFactory)
{
activeConfigurators.Values.ForEach(c => c.Add(nodeFactory));
inactiveConfigurators.Values.ForEach(c => c.AddPassThrough());
return this;
}
public ILightTransitionToggleConfigurator<TLight> AddPassThrough()
{
activeConfigurators.Values.ForEach(c => c.AddPassThrough());
inactiveConfigurators.Values.ForEach(c => c.AddPassThrough());
return this;
}
public ILightTransitionToggleConfigurator<TLight> AddTimeline(Dictionary<ITimeline, LightParameters> timeline, TimeSpan? transitionTimeForTimelineState = null)
{
activeConfigurators.Values.ForEach(c => c.AddTimeline(timeline, transitionTimeForTimelineState));
inactiveConfigurators.Values.ForEach(c => c.AddPassThrough());
return this;
}
public ILightTransitionToggleConfigurator<TLight> AddTimeline(Func<IServiceProvider, Dictionary<ITimeline, LightParameters>> timelineFactory, TimeSpan? transitionTimeForTimelineState = null)
{
activeConfigurators.Values.ForEach(c => c.AddTimeline(timelineFactory, transitionTimeForTimelineState));
inactiveConfigurators.Values.ForEach(c => c.AddPassThrough());
return this;
}
public ILightTransitionToggleConfigurator<TLight> AddTimeline(Action<ITimelineConfigurator> configure)
{
var configurator = new TimelineConfigurator();
configure(configurator);
return AddTimeline(configurator.TimelineFactory, configurator.TransitionTime);
}
public ILightTransitionToggleConfigurator<TLight> ForLight(string lightId, Action<ILightTransitionToggleConfigurator<TLight>> configure, ExcludedLightBehaviours excludedLightBehaviour = ExcludedLightBehaviours.None) => ForLights([lightId], configure, excludedLightBehaviour);
public ILightTransitionToggleConfigurator<TLight> ForLight(TLight light, Action<ILightTransitionToggleConfigurator<TLight>> configure, ExcludedLightBehaviours excludedLightBehaviour = ExcludedLightBehaviours.None) => ForLights([light], configure, excludedLightBehaviour);
public ILightTransitionToggleConfigurator<TLight> ForLights(IEnumerable<string> lightIds,
Action<ILightTransitionToggleConfigurator<TLight>> configure,
ExcludedLightBehaviours excludedLightBehaviour = ExcludedLightBehaviours.None)
{
var lightIdsArray =
CompositeHelper.ValidateLightsSupported(lightIds, activeConfigurators.Keys);
if (lightIdsArray.Length == activeConfigurators.Count)
{
configure(this);
return this;
}
if (excludedLightBehaviour == ExcludedLightBehaviours.None)
{
if (lightIdsArray.Length == 1)
{
configure(activeConfigurators[lightIdsArray.First()]);
return this;
}
configure(new CompositeLightTransitionToggleConfigurator<TLight>(
activeConfigurators.Where(kvp => lightIdsArray.Contains(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value), []));
return this;
}
configure(new CompositeLightTransitionToggleConfigurator<TLight>(
activeConfigurators.Where(kvp => lightIdsArray.Contains(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
activeConfigurators.Where(kvp => !lightIdsArray.Contains(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)));
return this;
}
public ILightTransitionToggleConfigurator<TLight> ForLights(IEnumerable<TLight> lights, Action<ILightTransitionToggleConfigurator<TLight>> configure, ExcludedLightBehaviours excludedLightBehaviour = ExcludedLightBehaviours.None)
{
var lightIds = CompositeHelper.ResolveGroupsAndValidateLightsSupported(lights, activeConfigurators.Keys);
return ForLights(lightIds, configure, excludedLightBehaviour);
}
}