189 lines
7.4 KiB
C#
189 lines
7.4 KiB
C#
using HomeAssistant.HistoryReplay.Configuration;
|
|
using HomeAssistant.HistoryReplay.HistoryData;
|
|
using HomeAssistant.HistoryReplay.Replay;
|
|
using HomeAssistant.HistoryReplay.Reporting;
|
|
using LightMonitor.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using LightMonitorManager.Models;
|
|
|
|
namespace HomeAssistant.HistoryReplay.Runners;
|
|
|
|
public sealed class LightReplayRunner : IMonitorReplayRunner
|
|
{
|
|
private readonly HistoryTimeline _timeline;
|
|
private readonly ReplayTimeProvider _timeProvider;
|
|
private readonly ReplayTimeline _replayTimeline;
|
|
private readonly ReplayDataSeries _dataSeries;
|
|
private readonly ILogger<LightReplayRunner> _logger;
|
|
private readonly ReplayOptions _replayOptions;
|
|
private readonly LightMonitorOptions _lightOptions;
|
|
|
|
public string MonitorName => "Light";
|
|
|
|
public LightReplayRunner(
|
|
HistoryTimeline timeline,
|
|
ReplayTimeProvider timeProvider,
|
|
ReplayTimeline replayTimeline,
|
|
ReplayDataSeries dataSeries,
|
|
ILogger<LightReplayRunner> logger,
|
|
IOptions<ReplayOptions> replayOptions,
|
|
IOptions<LightMonitorOptions> lightOptions)
|
|
{
|
|
_timeline = timeline;
|
|
_timeProvider = timeProvider;
|
|
_replayTimeline = replayTimeline;
|
|
_dataSeries = dataSeries;
|
|
_logger = logger;
|
|
_replayOptions = replayOptions.Value;
|
|
_lightOptions = lightOptions.Value;
|
|
}
|
|
|
|
public Task RunAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
_logger.LogInformation("Starting Light replay");
|
|
|
|
ReplaySunEvents(cancellationToken);
|
|
ReplayPersonArrivals(cancellationToken);
|
|
|
|
_logger.LogInformation("Light replay complete");
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private void ReplaySunEvents(CancellationToken cancellationToken)
|
|
{
|
|
var sunHistory = _timeline.GetEntityHistory(_lightOptions.SunEntityId);
|
|
string? previousSunState = null;
|
|
|
|
foreach (var entry in sunHistory)
|
|
{
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (entry.LastChanged < _replayOptions.StartTime || entry.LastChanged > _replayOptions.EndTime)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var isBelowHorizon = string.Equals(entry.State, LightMonitorConsts.SunBelowHorizon, StringComparison.OrdinalIgnoreCase);
|
|
_dataSeries.AddPoint(MonitorName, "SunBelowHorizon", entry.LastChanged, isBelowHorizon ? 1.0 : 0.0);
|
|
|
|
if (isBelowHorizon &&
|
|
!string.Equals(previousSunState, LightMonitorConsts.SunBelowHorizon, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
_timeProvider.AdvanceTo(entry.LastChanged);
|
|
|
|
var anyoneHome = IsAnyoneHome(entry.LastChanged);
|
|
var lightState = _timeline.GetStateAt(_lightOptions.LightEntityId, entry.LastChanged)?.State;
|
|
var lightAlreadyOn = string.Equals(lightState, LightMonitorConsts.LightOnState, StringComparison.OrdinalIgnoreCase);
|
|
|
|
if (anyoneHome && !lightAlreadyOn)
|
|
{
|
|
_dataSeries.AddPoint(MonitorName, "LightOn", entry.LastChanged, 1.0);
|
|
_replayTimeline.AddEvent(new ReplayEvent
|
|
{
|
|
Timestamp = entry.LastChanged,
|
|
Monitor = MonitorName,
|
|
EventType = ReplayEventType.Decision,
|
|
Message = "TURN ON light (sun below horizon + someone home)"
|
|
});
|
|
}
|
|
else if (!anyoneHome)
|
|
{
|
|
_replayTimeline.AddEvent(new ReplayEvent
|
|
{
|
|
Timestamp = entry.LastChanged,
|
|
Monitor = MonitorName,
|
|
EventType = ReplayEventType.Info,
|
|
Message = "Sun below horizon but nobody home -- light stays off"
|
|
});
|
|
}
|
|
else
|
|
{
|
|
_replayTimeline.AddEvent(new ReplayEvent
|
|
{
|
|
Timestamp = entry.LastChanged,
|
|
Monitor = MonitorName,
|
|
EventType = ReplayEventType.Info,
|
|
Message = "Sun below horizon but light already on"
|
|
});
|
|
}
|
|
}
|
|
|
|
previousSunState = entry.State;
|
|
}
|
|
}
|
|
|
|
private void ReplayPersonArrivals(CancellationToken cancellationToken)
|
|
{
|
|
foreach (var personEntityId in _lightOptions.PersonEntityIds)
|
|
{
|
|
var personHistory = _timeline.GetEntityHistory(personEntityId);
|
|
string? previousState = null;
|
|
|
|
foreach (var entry in personHistory)
|
|
{
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (entry.LastChanged < _replayOptions.StartTime || entry.LastChanged > _replayOptions.EndTime)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (string.Equals(entry.State, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase) &&
|
|
!string.Equals(previousState, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
_timeProvider.AdvanceTo(entry.LastChanged);
|
|
|
|
var sunEntry = _timeline.GetStateAt(_lightOptions.SunEntityId, entry.LastChanged);
|
|
var isDark = string.Equals(sunEntry?.State, LightMonitorConsts.SunBelowHorizon, StringComparison.OrdinalIgnoreCase);
|
|
var lightState = _timeline.GetStateAt(_lightOptions.LightEntityId, entry.LastChanged)?.State;
|
|
var lightAlreadyOn = string.Equals(lightState, LightMonitorConsts.LightOnState, StringComparison.OrdinalIgnoreCase);
|
|
|
|
if (isDark && !lightAlreadyOn)
|
|
{
|
|
_dataSeries.AddPoint(MonitorName, "LightOn", entry.LastChanged, 1.0);
|
|
_replayTimeline.AddEvent(new ReplayEvent
|
|
{
|
|
Timestamp = entry.LastChanged,
|
|
Monitor = MonitorName,
|
|
EventType = ReplayEventType.Decision,
|
|
Message = $"TURN ON light ({personEntityId} arrived home + dark outside)"
|
|
});
|
|
}
|
|
else if (isDark)
|
|
{
|
|
_replayTimeline.AddEvent(new ReplayEvent
|
|
{
|
|
Timestamp = entry.LastChanged,
|
|
Monitor = MonitorName,
|
|
EventType = ReplayEventType.Info,
|
|
Message = $"{personEntityId} arrived home (dark) but light already on"
|
|
});
|
|
}
|
|
}
|
|
|
|
previousState = entry.State;
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool IsAnyoneHome(DateTimeOffset timestamp)
|
|
{
|
|
foreach (var personEntityId in _lightOptions.PersonEntityIds)
|
|
{
|
|
var entry = _timeline.GetStateAt(personEntityId, timestamp);
|
|
if (string.Equals(entry?.State, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|