24729ee267
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
57 lines
1.3 KiB
C#
57 lines
1.3 KiB
C#
using ShowerMonitorManager.Interface;
|
|
|
|
namespace ShowerMonitorManager;
|
|
|
|
public sealed class HumidityRateCalculator(int windowSize) : IHumidityRateCalculator
|
|
{
|
|
private readonly Lock _lock = new();
|
|
private readonly Queue<(double Humidity, DateTimeOffset Timestamp)> _readings = new();
|
|
private double? _previousReading;
|
|
|
|
public double? PreviousReading
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _previousReading;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RecordReading(double humidity, DateTimeOffset timestamp)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
_readings.Enqueue((humidity, timestamp));
|
|
_previousReading = humidity;
|
|
|
|
while (_readings.Count > windowSize) _readings.Dequeue();
|
|
}
|
|
}
|
|
|
|
public double GetRatePerMinute()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_readings.Count < 2) return 0.0;
|
|
|
|
var oldest = _readings.Peek();
|
|
var newest = _readings.Last();
|
|
var elapsedMinutes = (newest.Timestamp - oldest.Timestamp).TotalMinutes;
|
|
|
|
if (elapsedMinutes <= 0) return 0.0;
|
|
|
|
return (newest.Humidity - oldest.Humidity) / elapsedMinutes;
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
_readings.Clear();
|
|
_previousReading = null;
|
|
}
|
|
}
|
|
} |