f919d6b25b
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
70 lines
1.6 KiB
C#
70 lines
1.6 KiB
C#
#region
|
|
|
|
using ShowerMonitorManager.Interface;
|
|
|
|
#endregion
|
|
|
|
namespace ShowerMonitorManager;
|
|
|
|
public sealed class HumidityRateCalculator(int windowSize) : IHumidityRateCalculator
|
|
{
|
|
private readonly Lock _lock = new Lock();
|
|
private readonly Queue<(double Humidity, DateTimeOffset Timestamp)> _readings = new Queue<(double Humidity, DateTimeOffset Timestamp)>();
|
|
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;
|
|
}
|
|
}
|
|
} |