127 lines
4.8 KiB
C#
127 lines
4.8 KiB
C#
using System.Globalization;
|
|
using HomeAssistant.HistoryReplay.Configuration;
|
|
using HomeAssistant.HistoryReplay.HistoryData;
|
|
using HomeAssistant.HistoryReplay.Replay;
|
|
using HomeAssistant.HistoryReplay.Reporting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using ShowerMonitor.Models.Enums;
|
|
using ShowerMonitorManager.Interface;
|
|
using ShowerMonitorManager.Models;
|
|
|
|
namespace HomeAssistant.HistoryReplay.Runners;
|
|
|
|
public sealed class ShowerReplayRunner : IMonitorReplayRunner
|
|
{
|
|
private readonly HistoryTimeline _timeline;
|
|
private readonly ReplayTimeProvider _timeProvider;
|
|
private readonly ReplayTimeline _replayTimeline;
|
|
private readonly IShowerDetectionService _detectionService;
|
|
private readonly IExhaustFanControlService _fanControlService;
|
|
private readonly ILogger<ShowerReplayRunner> _logger;
|
|
private readonly ReplayOptions _replayOptions;
|
|
private readonly ShowerMonitorOptions _showerOptions;
|
|
|
|
public string MonitorName => "Shower";
|
|
|
|
public ShowerReplayRunner(
|
|
HistoryTimeline timeline,
|
|
ReplayTimeProvider timeProvider,
|
|
ReplayTimeline replayTimeline,
|
|
IShowerDetectionService detectionService,
|
|
IExhaustFanControlService fanControlService,
|
|
ILogger<ShowerReplayRunner> logger,
|
|
IOptions<ReplayOptions> replayOptions,
|
|
IOptions<ShowerMonitorOptions> showerOptions)
|
|
{
|
|
_timeline = timeline;
|
|
_timeProvider = timeProvider;
|
|
_replayTimeline = replayTimeline;
|
|
_detectionService = detectionService;
|
|
_fanControlService = fanControlService;
|
|
_logger = logger;
|
|
_replayOptions = replayOptions.Value;
|
|
_showerOptions = showerOptions.Value;
|
|
}
|
|
|
|
public Task RunAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
_logger.LogInformation("Starting Shower replay");
|
|
|
|
var currentTime = _replayOptions.StartTime;
|
|
var pollInterval = TimeSpan.FromSeconds(_showerOptions.PollIntervalSeconds);
|
|
var previousState = ShowerState.Idle;
|
|
|
|
while (currentTime <= _replayOptions.EndTime && !cancellationToken.IsCancellationRequested)
|
|
{
|
|
_timeProvider.AdvanceTo(currentTime);
|
|
|
|
var bathroomEntry = _timeline.GetStateAt(_showerOptions.BathroomHumidityEntityId, currentTime);
|
|
var homeEntry = _timeline.GetStateAt(_showerOptions.MainHomeHumidityEntityId, currentTime);
|
|
|
|
var bathroomHumidity = ParseDouble(bathroomEntry?.State);
|
|
var homeHumidity = ParseDouble(homeEntry?.State);
|
|
|
|
if (bathroomHumidity.HasValue && homeHumidity.HasValue)
|
|
{
|
|
var state = _detectionService.Evaluate(bathroomHumidity.Value, homeHumidity.Value, currentTime);
|
|
var differential = bathroomHumidity.Value - homeHumidity.Value;
|
|
|
|
_fanControlService.Evaluate(state, currentTime);
|
|
|
|
if (state != previousState)
|
|
{
|
|
var eventType = state switch
|
|
{
|
|
ShowerState.Active => ReplayEventType.Decision,
|
|
ShowerState.Completed => ReplayEventType.StateChange,
|
|
_ => ReplayEventType.Info
|
|
};
|
|
|
|
_replayTimeline.AddEvent(new ReplayEvent
|
|
{
|
|
Timestamp = currentTime,
|
|
Monitor = MonitorName,
|
|
EventType = eventType,
|
|
Message = $"State: {previousState} → {state} (bathroom: {bathroomHumidity.Value:F1}%, home: {homeHumidity.Value:F1}%, diff: {differential:F1}%)",
|
|
Details = GetStateChangeDetails(state)
|
|
});
|
|
|
|
previousState = state;
|
|
}
|
|
}
|
|
|
|
currentTime = currentTime.Add(pollInterval);
|
|
}
|
|
|
|
_replayTimeline.AddEvent(new ReplayEvent
|
|
{
|
|
Timestamp = _replayOptions.EndTime,
|
|
Monitor = MonitorName,
|
|
EventType = ReplayEventType.Info,
|
|
Message = $"Final: {_detectionService.TodaySessionCount} session(s) detected"
|
|
});
|
|
|
|
_logger.LogInformation("Shower replay complete");
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private string? GetStateChangeDetails(ShowerState state) =>
|
|
state switch
|
|
{
|
|
ShowerState.Completed when _detectionService.LastCompletedSession is not null =>
|
|
$"Duration: {_detectionService.LastCompletedSession.Duration:mm\\:ss}, Peak humidity: {_detectionService.LastCompletedSession.PeakHumidity:F1}%",
|
|
_ => null
|
|
};
|
|
|
|
private static double? ParseDouble(string? value)
|
|
{
|
|
if (value is null or "unavailable" or "unknown")
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return double.TryParse(value, CultureInfo.InvariantCulture, out var result) ? result : null;
|
|
}
|
|
}
|