26 lines
1.0 KiB
C#
26 lines
1.0 KiB
C#
namespace Evergreen.Health.Application.Health.Scenes;
|
|
|
|
public interface ILightOffHandler
|
|
{
|
|
Task HandleAsync(CancellationToken cancellationToken);
|
|
}
|
|
|
|
/// <summary>跨设备联动由场景层分发;灯具不需要知道其他家具实体。</summary>
|
|
public sealed class LightOffDispatcher
|
|
{
|
|
private readonly IReadOnlyList<ILightOffHandler> _handlers;
|
|
public LightOffDispatcher(IEnumerable<ILightOffHandler> handlers) => _handlers = handlers.ToArray();
|
|
public async Task NotifyAsync(CancellationToken cancellationToken)
|
|
{
|
|
var failures = new List<Exception>();
|
|
foreach (var handler in _handlers)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
try { await handler.HandleAsync(cancellationToken); }
|
|
catch (Exception exception) when (exception is not OperationCanceledException) { failures.Add(exception); }
|
|
}
|
|
if (failures.Count > 0)
|
|
throw new AggregateException("灯光已关闭,但部分安全联动未完成", failures);
|
|
}
|
|
}
|