Refactor services to use DI for IHaContext and TimeProvider
This commit is contained in:
@@ -7,7 +7,9 @@ using ShowerMonitorManager.Models;
|
||||
|
||||
namespace ShowerMonitorManager;
|
||||
|
||||
public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> options) : IExhaustFanControlService
|
||||
public sealed class ExhaustFanControlService(
|
||||
IHaContext haContext,
|
||||
IOptions<ShowerMonitorOptions> options) : IExhaustFanControlService
|
||||
{
|
||||
private readonly ShowerMonitorOptions _options = options.Value;
|
||||
private readonly Lock _lock = new();
|
||||
@@ -15,7 +17,7 @@ public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> opti
|
||||
private bool _fanTurnedOnByUs;
|
||||
private DateTimeOffset? _showerEndedAt;
|
||||
|
||||
public void Evaluate(ShowerState state, DateTimeOffset timestamp, IHaContext haContext)
|
||||
public void Evaluate(ShowerState state, DateTimeOffset timestamp)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
@@ -23,24 +25,24 @@ public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> opti
|
||||
{
|
||||
case ShowerState.Active:
|
||||
case ShowerState.Suspected:
|
||||
TurnOnFanIfNeeded(haContext);
|
||||
TurnOnFanIfNeeded();
|
||||
_showerEndedAt = null;
|
||||
break;
|
||||
|
||||
case ShowerState.Declining:
|
||||
case ShowerState.Completed:
|
||||
_showerEndedAt ??= timestamp;
|
||||
EvaluatePostShowerFanOff(timestamp, haContext);
|
||||
EvaluatePostShowerFanOff(timestamp);
|
||||
break;
|
||||
|
||||
case ShowerState.Idle:
|
||||
EvaluatePostShowerFanOff(timestamp, haContext);
|
||||
EvaluatePostShowerFanOff(timestamp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset(IHaContext haContext)
|
||||
public void Reset()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
@@ -58,7 +60,7 @@ public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> opti
|
||||
}
|
||||
}
|
||||
|
||||
private void TurnOnFanIfNeeded(IHaContext haContext)
|
||||
private void TurnOnFanIfNeeded()
|
||||
{
|
||||
if (_fanTurnedOnByUs)
|
||||
{
|
||||
@@ -75,7 +77,7 @@ public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> opti
|
||||
_fanTurnedOnByUs = true;
|
||||
}
|
||||
|
||||
private void EvaluatePostShowerFanOff(DateTimeOffset timestamp, IHaContext haContext)
|
||||
private void EvaluatePostShowerFanOff(DateTimeOffset timestamp)
|
||||
{
|
||||
if (!_fanTurnedOnByUs || !_showerEndedAt.HasValue)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user