Skip to content

Commit 265bde6

Browse files
authored
Implemented methods to configure logging in the light automation pipeline. (#187)
1 parent 648c134 commit 265bde6

10 files changed

Lines changed: 107 additions & 28 deletions

src/CodeCasa.AutomationPipelines.Lights/Pipeline/CompositeLightTransitionPipelineConfigurator.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using CodeCasa.AutomationPipelines.Lights.Extensions;
44
using CodeCasa.AutomationPipelines.Lights.ReactiveNode;
55
using CodeCasa.Lights;
6+
using System.Collections.Generic;
7+
using System.Xml.Linq;
68

79
namespace CodeCasa.AutomationPipelines.Lights.Pipeline
810
{
@@ -20,9 +22,16 @@ internal partial class CompositeLightTransitionPipelineConfigurator(
2022
public Dictionary<string, LightTransitionPipelineConfigurator> NodeContainers { get; } = nodeContainers;
2123

2224
/// <inheritdoc/>
23-
public ILightTransitionPipelineConfigurator SetName(string name)
25+
public ILightTransitionPipelineConfigurator EnableLogging(string? pipelineName = null)
2426
{
25-
NodeContainers.Values.ForEach(b => b.SetName(name));
27+
NodeContainers.Values.ForEach(b => b.EnableLogging(pipelineName));
28+
return this;
29+
}
30+
31+
/// <inheritdoc/>
32+
public ILightTransitionPipelineConfigurator DisableLogging()
33+
{
34+
NodeContainers.Values.ForEach(b => b.DisableLogging());
2635
return this;
2736
}
2837

@@ -45,7 +54,16 @@ public ILightTransitionPipelineConfigurator AddReactiveNode(
4554
Action<ILightTransitionReactiveNodeConfigurator> configure)
4655
{
4756
var nodes = reactiveNodeFactory.CreateReactiveNodes(NodeContainers.Select(nc => nc.Value.Light),
48-
configure);
57+
c =>
58+
{
59+
var firstContainer = NodeContainers.Values.First();
60+
if (firstContainer.Log ?? false)
61+
{
62+
// If logging is enabled, enable it on the reactive node configurator by default.
63+
c.EnableLogging($"Reactive Node in {firstContainer.Name ?? "light group"}");
64+
}
65+
configure(c);
66+
});
4967
NodeContainers.ForEach(kvp => kvp.Value.AddNode(nodes[kvp.Key]));
5068
return this;
5169
}

src/CodeCasa.AutomationPipelines.Lights/Pipeline/ILightTransitionPipelineConfigurator.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ namespace CodeCasa.AutomationPipelines.Lights.Pipeline;
1212
public partial interface ILightTransitionPipelineConfigurator
1313
{
1414
/// <summary>
15-
/// Sets the name for the current pipeline configuration.
15+
/// Enables logging for the pipeline configuration.
1616
/// </summary>
17-
/// <param name="name">The name to assign to the pipeline.</param>
17+
/// <param name="pipelineName">The optional name of the pipeline to include in logs.</param>
1818
/// <returns>The configurator instance for method chaining.</returns>
19-
ILightTransitionPipelineConfigurator SetName(string name);
19+
ILightTransitionPipelineConfigurator EnableLogging(string? pipelineName = null);
20+
21+
/// <summary>
22+
/// Disables logging for the pipeline configuration.
23+
/// </summary>
24+
/// <returns>The configurator instance for method chaining.</returns>
25+
ILightTransitionPipelineConfigurator DisableLogging();
2026

2127
/// <summary>
2228
/// Adds a pipeline node of type <typeparamref name="TNode"/> to the pipeline.

src/CodeCasa.AutomationPipelines.Lights/Pipeline/LightPipelineFactory.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,20 @@ internal Dictionary<string, IPipeline<LightTransition>> CreateLightPipelines(IEn
6868
return configurators.ToDictionary(kvp => kvp.Key, kvp =>
6969
{
7070
var conf = kvp.Value;
71+
if (conf.Log ?? false)
72+
{
73+
return new Pipeline<LightTransition>(
74+
conf.Name ?? conf.Light.Id,
75+
LightTransition.Off(),
76+
conf.Nodes,
77+
conf.Light.ApplyTransition,
78+
logger);
79+
}
80+
7181
return (IPipeline<LightTransition>)new Pipeline<LightTransition>(
72-
conf.Name ?? conf.Light.Id,
7382
LightTransition.Off(),
7483
conf.Nodes,
75-
conf.Light.ApplyTransition,
76-
logger);
84+
conf.Light.ApplyTransition);
7785
});
7886
}
7987
}

src/CodeCasa.AutomationPipelines.Lights/Pipeline/LightTransitionPipelineConfigurator.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal partial class LightTransitionPipelineConfigurator(
2121

2222
internal ILight Light { get; } = light;
2323
internal string? Name { get; private set; }
24+
internal bool? Log { get; private set; }
2425

2526
public IReadOnlyCollection<IPipelineNode<LightTransition>> Nodes => _nodes.AsReadOnly();
2627

@@ -31,9 +32,17 @@ public ILightTransitionPipelineConfigurator
3132
}
3233

3334
/// <inheritdoc/>
34-
public ILightTransitionPipelineConfigurator SetName(string name)
35+
public ILightTransitionPipelineConfigurator EnableLogging(string? pipelineName = null)
3536
{
36-
Name = name;
37+
Name = pipelineName;
38+
Log = true;
39+
return this;
40+
}
41+
42+
/// <inheritdoc/>
43+
public ILightTransitionPipelineConfigurator DisableLogging()
44+
{
45+
Log = false;
3746
return this;
3847
}
3948

@@ -66,7 +75,15 @@ public ILightTransitionPipelineConfigurator AddNode(Func<ILightPipelineContext,
6675
public ILightTransitionPipelineConfigurator AddReactiveNode(
6776
Action<ILightTransitionReactiveNodeConfigurator> configure)
6877
{
69-
return AddNode(reactiveNodeFactory.CreateReactiveNode(Light, configure));
78+
return AddNode(reactiveNodeFactory.CreateReactiveNode(Light, c =>
79+
{
80+
if (Log ?? false)
81+
{
82+
// If logging is enabled, enable it on the reactive node configurator by default.
83+
c.EnableLogging($"Reactive Node in {Name ?? Light.Id}");
84+
}
85+
configure(c);
86+
}));
7087
}
7188

7289
/// <inheritdoc/>

src/CodeCasa.AutomationPipelines.Lights/ReactiveNode/CompositeLightTransitionReactiveNodeConfigurator.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ internal partial class CompositeLightTransitionReactiveNodeConfigurator(
2020
: ILightTransitionReactiveNodeConfigurator
2121
{
2222
/// <inheritdoc/>
23-
public ILightTransitionReactiveNodeConfigurator SetName(string name)
23+
public ILightTransitionReactiveNodeConfigurator EnableLogging(string? name = null)
2424
{
25-
configurators.Values.ForEach(c => c.SetName(name));
25+
configurators.Values.ForEach(b => b.EnableLogging(name));
26+
return this;
27+
}
28+
29+
/// <inheritdoc/>
30+
public ILightTransitionReactiveNodeConfigurator DisableLogging()
31+
{
32+
configurators.Values.ForEach(b => b.DisableLogging());
2633
return this;
2734
}
2835

src/CodeCasa.AutomationPipelines.Lights/ReactiveNode/ILightTransitionReactiveNodeConfigurator.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ namespace CodeCasa.AutomationPipelines.Lights.ReactiveNode;
1111
public partial interface ILightTransitionReactiveNodeConfigurator
1212
{
1313
/// <summary>
14-
/// Sets the name for the current reactive node configuration.
14+
/// Enables logging for the reactive node configuration.
1515
/// </summary>
16-
/// <param name="name">The name to assign to the reactive node.</param>
16+
/// <param name="name">The optional name of the reactive node to include in logs.</param>
1717
/// <returns>The configurator instance for method chaining.</returns>
18-
ILightTransitionReactiveNodeConfigurator SetName(string name);
18+
ILightTransitionReactiveNodeConfigurator EnableLogging(string? name = null);
19+
20+
/// <summary>
21+
/// Disables logging for the reactive node configuration.
22+
/// </summary>
23+
/// <returns>The configurator instance for method chaining.</returns>
24+
ILightTransitionReactiveNodeConfigurator DisableLogging();
1925

2026
/// <summary>
2127
/// Adds a reactive dimmer control that will be reset when the reactive node activates a new node.

src/CodeCasa.AutomationPipelines.Lights/ReactiveNode/LightTransitionReactiveNodeConfigurator.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using System.Reactive;
88
using System.Reactive.Concurrency;
99
using System.Reactive.Linq;
10-
using System.Xml.Linq;
11-
using CodeCasa.AutomationPipelines.Lights.Utils;
1210
using Microsoft.Extensions.DependencyInjection;
1311

1412
namespace CodeCasa.AutomationPipelines.Lights.ReactiveNode;
@@ -30,14 +28,23 @@ internal partial class LightTransitionReactiveNodeConfigurator(
3028
public ILight Light { get; } = light;
3129

3230
internal string? Name { get; private set; }
31+
internal bool? Log { get; private set; }
3332
internal List<IObservable<IPipelineNode<LightTransition>?>> NodeObservables { get; } = new();
3433
internal List<IDimmer> Dimmers { get; } = new();
3534
internal DimmerOptions DimmerOptions { get; private set; } = new ();
3635

3736
/// <inheritdoc/>
38-
public ILightTransitionReactiveNodeConfigurator SetName(string name)
37+
public ILightTransitionReactiveNodeConfigurator EnableLogging(string? name = null)
3938
{
4039
Name = name;
40+
Log = true;
41+
return this;
42+
}
43+
44+
/// <inheritdoc/>
45+
public ILightTransitionReactiveNodeConfigurator DisableLogging()
46+
{
47+
Log = false;
4148
return this;
4249
}
4350

src/CodeCasa.AutomationPipelines.Lights/ReactiveNode/ReactiveNode.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public class ReactiveNode : PipelineNode<LightTransition>
2020
private IPipelineNode<LightTransition>? _activeNode;
2121
private IDisposable? _activeNodeSubscription;
2222

23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="ReactiveNode"/> class.
25+
/// </summary>
26+
/// <param name="nodeObservable">An observable that emits the pipeline nodes to activate. Null values deactivate the current node.</param>
27+
public ReactiveNode(IObservable<IPipelineNode<LightTransition>?> nodeObservable) :
28+
this(null, nodeObservable, null!)
29+
{
30+
}
31+
2332
/// <summary>
2433
/// Initializes a new instance of the <see cref="ReactiveNode"/> class.
2534
/// </summary>

src/CodeCasa.AutomationPipelines.Lights/ReactiveNode/ReactiveNodeFactory.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,14 @@ internal Dictionary<string, IPipelineNode<LightTransition>> CreateReactiveNodes(
130130

131131
private ReactiveNode CreateReactiveNode(LightTransitionReactiveNodeConfigurator reactiveNodeConfigurator)
132132
{
133-
return new ReactiveNode(
134-
reactiveNodeConfigurator.Name,
135-
reactiveNodeConfigurator.NodeObservables.Merge(),
136-
serviceProvider.GetRequiredService<ILogger<ReactiveNode>>());
133+
if (reactiveNodeConfigurator.Log ?? false)
134+
{
135+
return new ReactiveNode(
136+
reactiveNodeConfigurator.Name,
137+
reactiveNodeConfigurator.NodeObservables.Merge(),
138+
serviceProvider.GetRequiredService<ILogger<ReactiveNode>>());
139+
}
140+
return new ReactiveNode(reactiveNodeConfigurator.NodeObservables.Merge());
137141
}
138142

139143
private void SubscribeToPulses(

tests/CodeCasa.AutomationPipelines.Lights.Tests/ReactiveNodeTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ public void Initialize()
7878
public void CreateNode()
7979
{
8080
// Act
81-
var node = _reactiveNodeFactory.CreateReactiveNode(_lightMock.Object, config =>
82-
{
83-
config.SetName("TestNode");
84-
});
81+
var node = _reactiveNodeFactory.CreateReactiveNode(_lightMock.Object, _ => { });
8582

8683
// Assert
8784
Assert.IsNotNull(node);

0 commit comments

Comments
 (0)