Files
Evergreen-Health/Evergreen.Health.Application/Health/Scenes/DeviceActionSafety.cs
T
2026-11-09 16:10:05 +08:00

32 lines
1.3 KiB
C#

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; }
}
/// <summary>记录主灯的已发送开关状态,供晨起渐亮处理滞后的云端回读。</summary>
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();
}
}