-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightTransitionToggleConfigurator.cs
More file actions
136 lines (110 loc) · 5.52 KB
/
Copy pathLightTransitionToggleConfigurator.cs
File metadata and controls
136 lines (110 loc) · 5.52 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
using System.Reactive.Concurrency;
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 LightTransitionToggleConfigurator<TLight>(TLight light, IScheduler scheduler) : ILightTransitionToggleConfigurator<TLight>
where TLight : ILight
{
public TLight Light { get; } = light;
internal TimeSpan? ToggleTimeout { get; private set; }
internal bool? IncludeOffValue { get; private set; }
internal TimeSpan? GracePeriod { get; private set; }
internal List<Func<IServiceProvider, IPipelineNode<LightTransition>>> NodeFactories
{
get;
} = [];
public ILightTransitionToggleConfigurator<TLight> SetToggleTimeout(TimeSpan timeout)
{
ToggleTimeout = timeout;
return this;
}
public ILightTransitionToggleConfigurator<TLight> IncludeOffInToggleCycle()
{
IncludeOffValue = true;
return this;
}
public ILightTransitionToggleConfigurator<TLight> ExcludeOffFromToggleCycle()
{
IncludeOffValue = false;
return this;
}
public ILightTransitionToggleConfigurator<TLight> SetGracePeriod(TimeSpan gracePeriod)
{
GracePeriod = 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(sp => lightParametersFactory(sp)?.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(_ => new StaticLightTransitionNode(lightTransition, scheduler));
}
public ILightTransitionToggleConfigurator<TLight> Add(Func<IServiceProvider, LightTransition?> lightTransitionFactory)
{
return Add(sp => new StaticLightTransitionNode(lightTransitionFactory(sp), sp.GetRequiredService<IScheduler>()));
}
public ILightTransitionToggleConfigurator<TLight> Add(Func<IServiceProvider, LightTransition?, LightTransition?> lightTransitionFactory)
{
return Add(sp => new FactoryNode<LightTransition>(t => lightTransitionFactory(sp, t)));
}
public ILightTransitionToggleConfigurator<TLight> Add<TNode>() where TNode : IPipelineNode<LightTransition>
{
return Add(sp => ActivatorUtilities.CreateInstance<TNode>(sp));
}
public ILightTransitionToggleConfigurator<TLight> Add(Func<IServiceProvider, IPipelineNode<LightTransition>> nodeFactory)
{
NodeFactories.Add(nodeFactory);
return this;
}
public ILightTransitionToggleConfigurator<TLight> AddPassThrough()
{
return Add(_ => new PassThroughNode<LightTransition>());
}
public ILightTransitionToggleConfigurator<TLight> AddTimeline(Dictionary<ITimeline, LightParameters> timeline, TimeSpan? transitionTimeForTimelineState = null)
{
return Add(sp => new TimelineNode(timeline, sp.GetRequiredService<IScheduler>(), transitionTimeForTimelineState));
}
public ILightTransitionToggleConfigurator<TLight> AddTimeline(Func<IServiceProvider, Dictionary<ITimeline, LightParameters>> timelineFactory, TimeSpan? transitionTimeForTimelineState = null)
{
return Add(sp => new TimelineNode(timelineFactory(sp), sp.GetRequiredService<IScheduler>(), transitionTimeForTimelineState));
}
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)
{
CompositeHelper.ValidateLightSupported(lightIds, Light.Id);
return this;
}
public ILightTransitionToggleConfigurator<TLight> ForLights(IEnumerable<TLight> lights, Action<ILightTransitionToggleConfigurator<TLight>> configure, ExcludedLightBehaviours excludedLightBehaviour = ExcludedLightBehaviours.None)
{
CompositeHelper.ResolveGroupsAndValidateLightSupported(lights, Light.Id);
return this;
}
}