feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -9,15 +9,19 @@ public class PowerCostCalculationServiceTests
|
||||
{
|
||||
private readonly PowerCostCalculationService _sut = new();
|
||||
|
||||
private const double DefaultTotalWatts = 341.97;
|
||||
private const double DefaultSmoothedOtherWatts = 131.04;
|
||||
private const double DefaultPricePerKwh = 0.1791;
|
||||
|
||||
private static PowerCalculationResult CreateResult(
|
||||
double totalWatts = 1000,
|
||||
double smoothedOtherWatts = 200,
|
||||
double totalWatts = DefaultTotalWatts,
|
||||
double smoothedOtherWatts = DefaultSmoothedOtherWatts,
|
||||
Dictionary<string, double>? deviceReadings = null)
|
||||
{
|
||||
var readings = deviceReadings ?? new Dictionary<string, double>
|
||||
{
|
||||
["sensor.device_a_power"] = 500,
|
||||
["sensor.device_b_power"] = 300
|
||||
["sensor.sonoff_100215c534_power"] = 101.0,
|
||||
["sensor.sonoff_10018909cb_power"] = 109.93
|
||||
};
|
||||
|
||||
return new PowerCalculationResult
|
||||
@@ -37,12 +41,12 @@ public class PowerCostCalculationServiceTests
|
||||
public void CalculateCostIncrement_ShouldComputeCorrectCost()
|
||||
{
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(
|
||||
powerWatts: 1000,
|
||||
pricePerKwh: 0.10,
|
||||
powerWatts: 341.97,
|
||||
pricePerKwh: 0.1791,
|
||||
pollIntervalSeconds: PowerMonitorConsts.DefaultPollIntervalSeconds);
|
||||
|
||||
var expected = (1000.0 / PowerMonitorConsts.WattsPerKilowatt)
|
||||
* 0.10
|
||||
var expected = (341.97 / PowerMonitorConsts.WattsPerKilowatt)
|
||||
* 0.1791
|
||||
* ((double)PowerMonitorConsts.DefaultPollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
|
||||
cost.Should().BeApproximately(expected, 0.000001);
|
||||
}
|
||||
@@ -50,14 +54,14 @@ public class PowerCostCalculationServiceTests
|
||||
[Fact]
|
||||
public void CalculateCostIncrement_WithZeroPower_ShouldReturnZero()
|
||||
{
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(0, 0.10, 15);
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(0, DefaultPricePerKwh, 15);
|
||||
cost.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateCostIncrement_WithZeroPrice_ShouldReturnZero()
|
||||
{
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(1000, 0, 15);
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(341.97, 0, 15);
|
||||
cost.Should().Be(0);
|
||||
}
|
||||
|
||||
@@ -71,7 +75,7 @@ public class PowerCostCalculationServiceTests
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
|
||||
_sut.RecordCosts(result, 0.10, 15, now);
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
|
||||
var totalSnapshot = _sut.GetTotalCostSnapshot();
|
||||
totalSnapshot.CostDaily.Should().BeGreaterThan(0);
|
||||
@@ -86,22 +90,22 @@ public class PowerCostCalculationServiceTests
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
|
||||
_sut.RecordCosts(result, 0.10, 15, now);
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
|
||||
var deviceA = _sut.GetDeviceCostSnapshot("sensor.device_a_power");
|
||||
deviceA.CostDaily.Should().BeGreaterThan(0);
|
||||
var ac = _sut.GetDeviceCostSnapshot("sensor.sonoff_100215c534_power");
|
||||
ac.CostDaily.Should().BeGreaterThan(0);
|
||||
|
||||
var deviceB = _sut.GetDeviceCostSnapshot("sensor.device_b_power");
|
||||
deviceB.CostDaily.Should().BeGreaterThan(0);
|
||||
var induction = _sut.GetDeviceCostSnapshot("sensor.sonoff_10018909cb_power");
|
||||
induction.CostDaily.Should().BeGreaterThan(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_TotalCostShouldBeHigherThanOtherCost()
|
||||
{
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult(totalWatts: 1000, smoothedOtherWatts: 200);
|
||||
var result = CreateResult(totalWatts: DefaultTotalWatts, smoothedOtherWatts: DefaultSmoothedOtherWatts);
|
||||
|
||||
_sut.RecordCosts(result, 0.10, 15, now);
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
|
||||
var total = _sut.GetTotalCostSnapshot();
|
||||
var other = _sut.GetOtherCostSnapshot();
|
||||
@@ -114,10 +118,10 @@ public class PowerCostCalculationServiceTests
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
|
||||
_sut.RecordCosts(result, 0.10, 15, now);
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
var firstCost = _sut.GetTotalCostSnapshot().CostDaily;
|
||||
|
||||
_sut.RecordCosts(result, 0.10, 15, now.AddSeconds(15));
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now.AddSeconds(15));
|
||||
var secondCost = _sut.GetTotalCostSnapshot().CostDaily;
|
||||
|
||||
secondCost.Should().BeApproximately(firstCost * 2, 0.000001);
|
||||
@@ -129,19 +133,19 @@ public class PowerCostCalculationServiceTests
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result1 = CreateResult(deviceReadings: new Dictionary<string, double>
|
||||
{
|
||||
["sensor.device_a_power"] = 500
|
||||
["sensor.sonoff_100215c534_power"] = 101.0
|
||||
});
|
||||
|
||||
_sut.RecordCosts(result1, 0.10, 15, now);
|
||||
_sut.RecordCosts(result1, DefaultPricePerKwh, 15, now);
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(1);
|
||||
|
||||
var result2 = CreateResult(deviceReadings: new Dictionary<string, double>
|
||||
{
|
||||
["sensor.device_a_power"] = 500,
|
||||
["sensor.device_b_power"] = 300
|
||||
["sensor.sonoff_100215c534_power"] = 101.0,
|
||||
["sensor.sonoff_10018909cb_power"] = 109.93
|
||||
});
|
||||
|
||||
_sut.RecordCosts(result2, 0.10, 15, now.AddSeconds(15));
|
||||
_sut.RecordCosts(result2, DefaultPricePerKwh, 15, now.AddSeconds(15));
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(2);
|
||||
}
|
||||
|
||||
@@ -155,9 +159,9 @@ public class PowerCostCalculationServiceTests
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
|
||||
_sut.RecordCosts(result, 0.15, 15, now);
|
||||
_sut.RecordCosts(result, 0.2492, 15, now);
|
||||
|
||||
_sut.LastKnownPrice.Should().Be(0.15);
|
||||
_sut.LastKnownPrice.Should().Be(0.2492);
|
||||
_sut.PriceSource.Should().Be("live");
|
||||
}
|
||||
|
||||
@@ -167,19 +171,19 @@ public class PowerCostCalculationServiceTests
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
|
||||
_sut.RecordCosts(result, 0.10, 15, now);
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
_sut.RecordCosts(result, 0, 15, now.AddSeconds(15));
|
||||
|
||||
_sut.LastKnownPrice.Should().Be(0.10);
|
||||
_sut.LastKnownPrice.Should().Be(DefaultPricePerKwh);
|
||||
_sut.PriceSource.Should().Be("live");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetPrice_ShouldUpdatePriceAndMarkAsStale()
|
||||
{
|
||||
_sut.SetPrice(0.20);
|
||||
_sut.SetPrice(0.1891);
|
||||
|
||||
_sut.LastKnownPrice.Should().Be(0.20);
|
||||
_sut.LastKnownPrice.Should().Be(0.1891);
|
||||
_sut.PriceSource.Should().Be("stale");
|
||||
}
|
||||
|
||||
@@ -229,7 +233,7 @@ public class PowerCostCalculationServiceTests
|
||||
public async Task RecordCosts_ConcurrentAccess_ShouldNotLoseData()
|
||||
{
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult(totalWatts: 1000, smoothedOtherWatts: 200);
|
||||
var result = CreateResult(totalWatts: DefaultTotalWatts, smoothedOtherWatts: DefaultSmoothedOtherWatts);
|
||||
const int callsPerThread = 100;
|
||||
const int threadCount = 10;
|
||||
|
||||
@@ -238,7 +242,7 @@ public class PowerCostCalculationServiceTests
|
||||
{
|
||||
for (var i = 0; i < callsPerThread; i++)
|
||||
{
|
||||
_sut.RecordCosts(result, 0.10, 15, now.AddSeconds(1));
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now.AddSeconds(1));
|
||||
}
|
||||
}))
|
||||
.ToArray();
|
||||
|
||||
Reference in New Issue
Block a user