feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
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();
|
||||
|
||||
public void RecordReading(double humidity, DateTimeOffset timestamp)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_readings.Enqueue((humidity, timestamp));
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user