Skip to content

Commit

Permalink
feat: Handle hardware reset of total energy reportings.
Browse files Browse the repository at this point in the history
  • Loading branch information
just-seba committed Sep 11, 2024
1 parent 3c462be commit 07aa1ae
Show file tree
Hide file tree
Showing 15 changed files with 690 additions and 23 deletions.
7 changes: 7 additions & 0 deletions server/Server.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "integrations", "integration
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenEMS.Integrations.Shelly.Test", "test\integrations\OpenEMS.Integrations.Shelly.Test\OpenEMS.Integrations.Shelly.Test.csproj", "{2DFA9324-C666-4D50-9EA2-F0197AFE24CC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenEMS.Domain.Test", "test\OpenEMS.Domain.Test\OpenEMS.Domain.Test.csproj", "{CD454A6C-7EE6-44B4-B00E-39498126C1B4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -71,6 +73,10 @@ Global
{2DFA9324-C666-4D50-9EA2-F0197AFE24CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DFA9324-C666-4D50-9EA2-F0197AFE24CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DFA9324-C666-4D50-9EA2-F0197AFE24CC}.Release|Any CPU.Build.0 = Release|Any CPU
{CD454A6C-7EE6-44B4-B00E-39498126C1B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD454A6C-7EE6-44B4-B00E-39498126C1B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD454A6C-7EE6-44B4-B00E-39498126C1B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD454A6C-7EE6-44B4-B00E-39498126C1B4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -87,5 +93,6 @@ Global
{BAD76F68-E1AD-41C6-8BBE-06D9FAD00C26} = {857EBC2A-8A73-4C09-8DED-D35E8C45B007}
{0114E6CC-84EB-47FD-91F5-30CD4896F903} = {79A93847-64ED-4E4D-B365-D4C3C2729058}
{2DFA9324-C666-4D50-9EA2-F0197AFE24CC} = {0114E6CC-84EB-47FD-91F5-30CD4896F903}
{CD454A6C-7EE6-44B4-B00E-39498126C1B4} = {79A93847-64ED-4E4D-B365-D4C3C2729058}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions server/src/OpenEMS.Domain/Consumers/SwitchConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SwitchConsumer : IHasOwner, IHasEvents
public Watt MaximumPowerConsumption { get; private set; } = Watt.Zero;

public bool HasReportedTotalEnergyConsumption { get; private set; }
public WattHours TotalEnergyConsumption { get; private set; } = WattHours.Zero;
public TotalEnergy TotalEnergyConsumption { get; private set; } = TotalEnergy.Zero;
public required UserId OwnedBy { get; init; }

public void ActivateSmartMode()
Expand Down Expand Up @@ -67,7 +67,7 @@ public void ReportCurrentPowerConsumption(Watt value)

public void ReportTotalEnergyConsumption(WattHours value)
{
TotalEnergyConsumption = value;
TotalEnergyConsumption = TotalEnergyConsumption.WithReported(value);
HasReportedTotalEnergyConsumption = true;
}

Expand Down
10 changes: 8 additions & 2 deletions server/src/OpenEMS.Domain/Meters/ElectricityMeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class ElectricityMeter : IHasOwner, IHasEvents
public Watt CurrentPower { get; private set; } = Watt.Zero;
public Watt MaximumPowerConsumption { get; private set; } = Watt.Zero;
public Watt MaximumPowerFeedIn { get; private set; } = Watt.Zero;
public WattHours TotalEnergyConsumption { get; set; } = WattHours.Zero;
public WattHours TotalEnergyFeedIn { get; set; } = WattHours.Zero;
public TotalEnergy TotalEnergyConsumption { get; private set; } = TotalEnergy.Zero;
public TotalEnergy TotalEnergyFeedIn { get; private set; } = TotalEnergy.Zero;
public required UserId OwnedBy { get; init; }

public void ReportCurrentPower(Watt currentPower, GridPowerDirection currentPowerDirection)
Expand Down Expand Up @@ -65,6 +65,12 @@ public void ReportCurrentPower(Watt currentPower, GridPowerDirection currentPowe
}
}

public void ReportTotalEnergyConsumption(WattHours value) =>
TotalEnergyConsumption = TotalEnergyConsumption.WithReported(value);

public void ReportTotalEnergyFeedIn(WattHours value) =>
TotalEnergyFeedIn = TotalEnergyFeedIn.WithReported(value);

public bool HasEvents() => _events.Count > 0;

public IReadOnlyList<IEvent> GetEvents() => _events.AsReadOnly();
Expand Down
4 changes: 2 additions & 2 deletions server/src/OpenEMS.Domain/Producers/Producer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Producer : IHasOwner, IHasEvents

public Watt CurrentPowerProduction { get; private set; } = Watt.Zero;
public Watt MaximumPowerProduction { get; private set; } = Watt.Zero;
public WattHours TotalEnergyProduction { get; private set; } = WattHours.Zero;
public TotalEnergy TotalEnergyProduction { get; private set; } = TotalEnergy.Zero;
public required UserId OwnedBy { get; init; }

public void ReportCurrentPowerProduction(Watt value)
Expand All @@ -32,7 +32,7 @@ public void ReportCurrentPowerProduction(Watt value)

public void ReportTotalEnergyProduction(WattHours value)
{
TotalEnergyProduction = value;
TotalEnergyProduction = TotalEnergyProduction.WithReported(value);
}

public bool HasEvents() => _events.Count > 0;
Expand Down
39 changes: 39 additions & 0 deletions server/src/OpenEMS.Domain/TotalEnergy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NMolecules.DDD;
using OpenEMS.Domain.Units;

namespace OpenEMS.Domain;

[ValueObject]
public class TotalEnergy
{
private TotalEnergy() { }

public static TotalEnergy Zero => new();

/// <summary>
/// Gets the last reported value.
/// This value may be reset to zero if the hardware has been reset.
/// </summary>
public WattHours LastReported { get; private set; } = WattHours.Zero;

/// <summary>
/// Gets the actual total energy.
/// This value is not affected by hardware resets.
/// </summary>
public WattHours Value { get; private set; } = WattHours.Zero;

public TotalEnergy WithReported(WattHours reportedValue)
{
if (reportedValue < LastReported)
{
// Hardware has been reset
return new TotalEnergy { LastReported = reportedValue, Value = Value + reportedValue };
}

return new TotalEnergy
{
LastReported = reportedValue,
Value = Value + (reportedValue - LastReported),
};
}
}
3 changes: 3 additions & 0 deletions server/src/OpenEMS.Domain/Units/WattHours.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public readonly partial record struct WattHours
public static WattHours operator +(WattHours left, WattHours right) =>
From(left.Value + right.Value);

public static WattHours operator -(WattHours left, WattHours right) =>
From(left.Value - right.Value);

private static Validation Validate(double input)
{
_ = input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ public void Configure(EntityTypeBuilder<ElectricityMeter> builder)
.IsUnique();
}
);

builder.OwnsOne(electricityMeter => electricityMeter.TotalEnergyConsumption).WithOwner();
builder.OwnsOne(electricityMeter => electricityMeter.TotalEnergyFeedIn).WithOwner();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public void Configure(EntityTypeBuilder<Producer> builder)
.IsUnique();
}
);

builder.OwnsOne(electricityMeter => electricityMeter.TotalEnergyProduction).WithOwner();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public void Configure(EntityTypeBuilder<SwitchConsumer> builder)
);

builder.OwnsOne(switchConsumer => switchConsumer.SmartModeConfiguration).WithOwner();
builder.OwnsOne(electricityMeter => electricityMeter.TotalEnergyConsumption).WithOwner();
}
}
Loading

0 comments on commit 07aa1ae

Please sign in to comment.