using Evergreen.Health.Application.Health.Devices.Contracts; using Evergreen.Health.Application.Health.Integrations.HomeAssistant; using Microsoft.Extensions.Options; namespace Evergreen.Health.Application.Health.Scenes; public interface IDeviceActionSafetyRule { Task ValidateAsync(DeviceCommand command, CancellationToken cancellationToken); void Accepted(DeviceCommand command, DateTimeOffset now); } public sealed class SceneLightSafetyState { private readonly object _lock = new(); private DateTimeOffset? _offAt; public DateTimeOffset? OffAt { get { lock (_lock) return _offAt; } } public void MarkOff(DateTimeOffset now) { lock (_lock) _offAt = now; } public void MarkOn() { lock (_lock) _offAt = null; } } /// 记录主灯的已发送开关状态,供晨起渐亮处理滞后的云端回读。 public sealed class PrimaryLightStateRule(SceneLightSafetyState state) : IDeviceActionSafetyRule { public Task ValidateAsync(DeviceCommand command, CancellationToken cancellationToken) => Task.CompletedTask; public void Accepted(DeviceCommand command, DateTimeOffset now) { if (command.Intent == DeviceCommandIntent.TurnRelaxationLightOff) state.MarkOff(now); else if (command.Intent == DeviceCommandIntent.TurnRelaxationLightOn) state.MarkOn(); } }