M1: pure normalization engine + default seed
Infrastructure-free normalization engine in Core dispatching on MeterMode: - Cumulative/Generation counters: register deltas, first-reading baseline (Haus 411, Auto 3755), meter swaps (water …861→2 reconciles to 12 via boundary registers OR an explicit amount override), counter resets, anomaly-guarded decreases. - RuntimeCounter: Δhours × rate (fixed/empirical). - ConsumableBalance: tank level-Δ + deliveries → consumption, cm→litre calibration; delivery-only rows before the first dipstick emit nothing. - DirectDelta, and Virtual meters via a small safe arithmetic evaluator (Netz Einsparung = Haus − Netz, Eigenverbrauch = Erzeugung − Einsparung) — data-driven, not hardcoded. - DatabaseSeeder: default energy types, cost categories, base settings (idempotent). 24 Core unit tests + 4 integration tests green. Claude-Session: https://claude.ai/code/session_01WujdMtMJPbxDpDnMeK22rr
This commit is contained in:
+2
-1
@@ -78,7 +78,8 @@ static async Task MigrateDatabaseAsync(WebApplication app)
|
||||
await using var scope = app.Services.CreateAsyncScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<MeterVaultDbContext>();
|
||||
await db.Database.MigrateAsync().ConfigureAwait(false);
|
||||
Log.Information("Database migrations applied");
|
||||
await DatabaseSeeder.SeedAsync(db).ConfigureAwait(false);
|
||||
Log.Information("Database migrations applied and defaults seeded");
|
||||
}
|
||||
|
||||
/// <summary>Exposed for WebApplicationFactory-based integration tests.</summary>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace MeterVault.Core.Normalization;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a physical level reading (e.g. dipstick centimetres) into a volume in the tank's
|
||||
/// unit. The reference heating-oil tank is linear at 7000 L / 150 cm ≈ 46.667 L/cm
|
||||
/// (35 cm → 1633 L, 85 cm → 3967 L). Non-linear geometries can be added later; linear covers
|
||||
/// the reference data.
|
||||
/// </summary>
|
||||
public sealed record CalibrationCurve(double VolumePerUnit, double Offset = 0)
|
||||
{
|
||||
/// <summary>Litres for a given physical level (cm), per the calibration.</summary>
|
||||
public double ToVolume(double level) => (level * VolumePerUnit) + Offset;
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace MeterVault.Core.Normalization.Expressions;
|
||||
|
||||
/// <summary>
|
||||
/// A tiny, safe arithmetic evaluator for virtual-meter expressions (SDD §7.4). Supports
|
||||
/// <c>+ - * /</c>, parentheses, unary minus, numeric literals, and identifiers resolved against
|
||||
/// a supplied variable map (e.g. <c>m1 - m2</c> where <c>m1</c> is meter 1's amount in a bucket).
|
||||
/// No I/O, no reflection, no arbitrary code — only arithmetic over whitelisted tokens.
|
||||
/// Parse once with <see cref="Compile"/>, then evaluate per time bucket.
|
||||
/// </summary>
|
||||
public sealed class ExpressionEvaluator
|
||||
{
|
||||
private readonly Func<IReadOnlyDictionary<string, double>, double> _eval;
|
||||
|
||||
private ExpressionEvaluator(Func<IReadOnlyDictionary<string, double>, double> eval) => _eval = eval;
|
||||
|
||||
public static ExpressionEvaluator Compile(string expression)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(expression);
|
||||
var parser = new Parser(expression);
|
||||
var node = parser.ParseExpression();
|
||||
parser.ExpectEnd();
|
||||
return new ExpressionEvaluator(node);
|
||||
}
|
||||
|
||||
public double Evaluate(IReadOnlyDictionary<string, double> variables) => _eval(variables);
|
||||
|
||||
// Recursive-descent parser that builds a closure over the variable map.
|
||||
private sealed class Parser(string text)
|
||||
{
|
||||
private int _pos;
|
||||
|
||||
public Func<IReadOnlyDictionary<string, double>, double> ParseExpression() => ParseAdditive();
|
||||
|
||||
public void ExpectEnd()
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (_pos != text.Length)
|
||||
{
|
||||
throw new FormatException($"Unexpected token at position {_pos} in expression '{text}'.");
|
||||
}
|
||||
}
|
||||
|
||||
private Func<IReadOnlyDictionary<string, double>, double> ParseAdditive()
|
||||
{
|
||||
var left = ParseMultiplicative();
|
||||
while (true)
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (Match('+'))
|
||||
{
|
||||
var right = ParseMultiplicative();
|
||||
var l = left;
|
||||
left = vars => l(vars) + right(vars);
|
||||
}
|
||||
else if (Match('-'))
|
||||
{
|
||||
var right = ParseMultiplicative();
|
||||
var l = left;
|
||||
left = vars => l(vars) - right(vars);
|
||||
}
|
||||
else
|
||||
{
|
||||
return left;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Func<IReadOnlyDictionary<string, double>, double> ParseMultiplicative()
|
||||
{
|
||||
var left = ParseUnary();
|
||||
while (true)
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (Match('*'))
|
||||
{
|
||||
var right = ParseUnary();
|
||||
var l = left;
|
||||
left = vars => l(vars) * right(vars);
|
||||
}
|
||||
else if (Match('/'))
|
||||
{
|
||||
var right = ParseUnary();
|
||||
var l = left;
|
||||
left = vars => l(vars) / right(vars);
|
||||
}
|
||||
else
|
||||
{
|
||||
return left;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Func<IReadOnlyDictionary<string, double>, double> ParseUnary()
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (Match('-'))
|
||||
{
|
||||
var operand = ParseUnary();
|
||||
return vars => -operand(vars);
|
||||
}
|
||||
|
||||
if (Match('+'))
|
||||
{
|
||||
return ParseUnary();
|
||||
}
|
||||
|
||||
return ParsePrimary();
|
||||
}
|
||||
|
||||
private Func<IReadOnlyDictionary<string, double>, double> ParsePrimary()
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (Match('('))
|
||||
{
|
||||
var inner = ParseAdditive();
|
||||
SkipWhitespace();
|
||||
if (!Match(')'))
|
||||
{
|
||||
throw new FormatException($"Expected ')' at position {_pos} in expression '{text}'.");
|
||||
}
|
||||
|
||||
return inner;
|
||||
}
|
||||
|
||||
if (_pos < text.Length && (char.IsDigit(text[_pos]) || text[_pos] == '.'))
|
||||
{
|
||||
var start = _pos;
|
||||
while (_pos < text.Length && (char.IsDigit(text[_pos]) || text[_pos] == '.'))
|
||||
{
|
||||
_pos++;
|
||||
}
|
||||
|
||||
var literal = double.Parse(text.AsSpan(start, _pos - start), CultureInfo.InvariantCulture);
|
||||
return _ => literal;
|
||||
}
|
||||
|
||||
if (_pos < text.Length && (char.IsLetter(text[_pos]) || text[_pos] == '_'))
|
||||
{
|
||||
var start = _pos;
|
||||
while (_pos < text.Length && (char.IsLetterOrDigit(text[_pos]) || text[_pos] == '_'))
|
||||
{
|
||||
_pos++;
|
||||
}
|
||||
|
||||
var name = text[start.._pos];
|
||||
return vars => vars.TryGetValue(name, out var value) ? value : 0d;
|
||||
}
|
||||
|
||||
throw new FormatException($"Unexpected character at position {_pos} in expression '{text}'.");
|
||||
}
|
||||
|
||||
private bool Match(char c)
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (_pos < text.Length && text[_pos] == c)
|
||||
{
|
||||
_pos++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SkipWhitespace()
|
||||
{
|
||||
while (_pos < text.Length && char.IsWhiteSpace(text[_pos]))
|
||||
{
|
||||
_pos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization;
|
||||
|
||||
/// <summary>A per-mode normalization strategy (SDD §5.2).</summary>
|
||||
public interface IMeterNormalizer
|
||||
{
|
||||
MeterMode Mode { get; }
|
||||
|
||||
IEnumerable<Consumption> Normalize(NormalizationContext context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns raw readings/events into normalized consumption/generation deltas by dispatching on
|
||||
/// <see cref="MeterConfig.Mode"/>. Pure domain logic: no infrastructure dependencies.
|
||||
/// </summary>
|
||||
public interface INormalizationEngine
|
||||
{
|
||||
IReadOnlyList<Consumption> Normalize(NormalizationContext context);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization;
|
||||
|
||||
/// <summary>
|
||||
/// The immutable, infrastructure-free view of a meter that the normalization engine needs.
|
||||
/// Infrastructure projects a <see cref="Meter"/> (+ optional <see cref="Tank"/>) into this.
|
||||
/// </summary>
|
||||
public sealed record MeterConfig
|
||||
{
|
||||
public required int MeterId { get; init; }
|
||||
|
||||
public required MeterMode Mode { get; init; }
|
||||
|
||||
public required string Unit { get; init; }
|
||||
|
||||
/// <summary>Register baseline for a newly installed meter (SDD §7.1). Default 0.</summary>
|
||||
public double InitialBaseline { get; init; }
|
||||
|
||||
/// <summary>Tank/runtime configuration for consumable_balance and runtime_counter meters.</summary>
|
||||
public TankConfig? Tank { get; init; }
|
||||
|
||||
/// <summary>Expression + referenced meters for virtual meters.</summary>
|
||||
public VirtualSpec? Virtual { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>Consumable/runtime parameters (SDD §5.3 <c>tank</c>).</summary>
|
||||
public sealed record TankConfig
|
||||
{
|
||||
public double Capacity { get; init; }
|
||||
|
||||
public TankRateMode RateMode { get; init; } = TankRateMode.Empirical;
|
||||
|
||||
/// <summary>Litres per runtime-hour when <see cref="RateMode"/> is Fixed.</summary>
|
||||
public double? FixedRate { get; init; }
|
||||
|
||||
/// <summary>Physical-level (cm) → volume curve. Present when dipstick readings are recorded.</summary>
|
||||
public CalibrationCurve? Calibration { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>A virtual meter's expression over other meters' series (SDD §7.4).</summary>
|
||||
public sealed record VirtualSpec
|
||||
{
|
||||
public required string Expression { get; init; }
|
||||
|
||||
public IReadOnlyList<int> ReferencedMeterIds { get; init; } = [];
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization;
|
||||
|
||||
/// <summary>
|
||||
/// The complete, immutable input to normalizing one meter: its config, its raw readings
|
||||
/// (ascending by time), its discrete events (swaps, resets, deliveries, tank levels), and —
|
||||
/// for virtual meters — the already-normalized series of the meters it references.
|
||||
/// </summary>
|
||||
public sealed class NormalizationContext
|
||||
{
|
||||
public required MeterConfig Meter { get; init; }
|
||||
|
||||
/// <summary>Raw readings for this meter, expected ascending by time (sorted defensively).</summary>
|
||||
public IReadOnlyList<Reading> Readings { get; init; } = [];
|
||||
|
||||
public IReadOnlyList<MeterEvent> Events { get; init; } = [];
|
||||
|
||||
/// <summary>Referenced meters' consumption series, keyed by meter id (virtual meters only).</summary>
|
||||
public IReadOnlyDictionary<int, IReadOnlyList<Consumption>> ReferencedSeries { get; init; }
|
||||
= new Dictionary<int, IReadOnlyList<Consumption>>();
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization;
|
||||
|
||||
/// <summary>
|
||||
/// Dispatches to the registered <see cref="IMeterNormalizer"/> for the meter's mode.
|
||||
/// Register the strategies once (in DI or the parameterless factory) and reuse.
|
||||
/// </summary>
|
||||
public sealed class NormalizationEngine : INormalizationEngine
|
||||
{
|
||||
private readonly IReadOnlyDictionary<MeterMode, IMeterNormalizer> _byMode;
|
||||
|
||||
public NormalizationEngine(IEnumerable<IMeterNormalizer> normalizers)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(normalizers);
|
||||
_byMode = normalizers.ToDictionary(n => n.Mode);
|
||||
}
|
||||
|
||||
/// <summary>Builds an engine with the built-in strategies for all supported modes.</summary>
|
||||
public static NormalizationEngine CreateDefault() => new(
|
||||
[
|
||||
new Normalizers.CumulativeCounterNormalizer(),
|
||||
new Normalizers.GenerationCounterNormalizer(),
|
||||
new Normalizers.RuntimeCounterNormalizer(),
|
||||
new Normalizers.ConsumableBalanceNormalizer(),
|
||||
new Normalizers.DirectDeltaNormalizer(),
|
||||
new Normalizers.VirtualNormalizer(),
|
||||
]);
|
||||
|
||||
public IReadOnlyList<Consumption> Normalize(NormalizationContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
if (!_byMode.TryGetValue(context.Meter.Mode, out var normalizer))
|
||||
{
|
||||
throw new NotSupportedException($"No normalizer registered for mode {context.Meter.Mode}.");
|
||||
}
|
||||
|
||||
return [.. normalizer.Normalize(context)];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization.Normalizers;
|
||||
|
||||
/// <summary>
|
||||
/// A tank/bottle drawn down over time (SDD §5.2 <c>consumable_balance</c>, §7.3). Consumption
|
||||
/// between two physical level readings is <c>prevLevel + deliveriesBetween − currLevel</c>, all in
|
||||
/// volume units (cm levels are calibrated to litres). Deliveries recorded before the first level
|
||||
/// reading are absorbed into the starting balance and emit no consumption — which is why the
|
||||
/// heating-oil delivery-only rows of 1997–2020 produce nothing until the first 2022 dipstick.
|
||||
/// This reproduces the spreadsheet's <c>Differenz Tank</c> column.
|
||||
/// </summary>
|
||||
public sealed class ConsumableBalanceNormalizer : IMeterNormalizer
|
||||
{
|
||||
public MeterMode Mode => MeterMode.ConsumableBalance;
|
||||
|
||||
public IEnumerable<Consumption> Normalize(NormalizationContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
|
||||
var calibration = context.Meter.Tank?.Calibration;
|
||||
var events = context.Events
|
||||
.Where(e => e.EventType is MeterEventType.Delivery or MeterEventType.TankLevel)
|
||||
.OrderBy(e => e.Time)
|
||||
.ToList();
|
||||
|
||||
double? lastLevelVolume = null;
|
||||
double pendingDeliveries = 0;
|
||||
|
||||
foreach (var e in events)
|
||||
{
|
||||
if (e.EventType == MeterEventType.Delivery)
|
||||
{
|
||||
pendingDeliveries += e.Amount ?? 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
var currVolume = ToVolume(e, calibration);
|
||||
if (lastLevelVolume is null)
|
||||
{
|
||||
lastLevelVolume = currVolume;
|
||||
pendingDeliveries = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
var consumption = lastLevelVolume.Value + pendingDeliveries - currVolume;
|
||||
var quality = ReadingQuality.Manual;
|
||||
if (consumption < 0)
|
||||
{
|
||||
// Level rose beyond recorded deliveries — treat as no net draw, flag as estimated.
|
||||
consumption = 0;
|
||||
quality = ReadingQuality.Estimated;
|
||||
}
|
||||
|
||||
yield return new Consumption
|
||||
{
|
||||
MeterId = context.Meter.MeterId,
|
||||
Time = e.Time,
|
||||
Amount = consumption,
|
||||
Kind = ConsumptionKind.Consumption,
|
||||
Quality = quality,
|
||||
ImportBatchId = e.ImportBatchId,
|
||||
};
|
||||
|
||||
lastLevelVolume = currVolume;
|
||||
pendingDeliveries = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static double ToVolume(MeterEvent tankLevel, CalibrationCurve? calibration)
|
||||
{
|
||||
var level = tankLevel.Amount ?? 0;
|
||||
var isCentimetres = string.Equals(tankLevel.Unit, "cm", StringComparison.OrdinalIgnoreCase);
|
||||
return isCentimetres && calibration is not null ? calibration.ToVolume(level) : level;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization.Normalizers;
|
||||
|
||||
/// <summary>
|
||||
/// Shared delta logic for monotonic registers (SDD §7.1). Walks readings ascending, holding the
|
||||
/// previous register value, and emits one consumption/generation delta per reading interval:
|
||||
/// <list type="bullet">
|
||||
/// <item>first reading → <c>value − InitialBaseline</c> (baseline default 0 reproduces the
|
||||
/// spreadsheet's month-one full register, e.g. Haus 411, Auto 3755);</item>
|
||||
/// <item>meter swap → an explicit <c>Amount</c> override if given (how the water swap …861→2
|
||||
/// reconciles to 12), otherwise <c>(oldFinal − prev) + (curr − newInitial)</c>;</item>
|
||||
/// <item>counter reset → baseline restarts at <c>NewValue</c> (default 0);</item>
|
||||
/// <item>unexplained decrease → 0 with an anomaly flagged (never a silent negative), rebaselined
|
||||
/// to the current value.</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
public abstract class CounterNormalizerBase : IMeterNormalizer
|
||||
{
|
||||
public abstract MeterMode Mode { get; }
|
||||
|
||||
protected abstract ConsumptionKind Kind { get; }
|
||||
|
||||
public IEnumerable<Consumption> Normalize(NormalizationContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
|
||||
var readings = context.Readings.OrderBy(r => r.Time).ToList();
|
||||
if (readings.Count == 0)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
var swaps = context.Events
|
||||
.Where(e => e.EventType is MeterEventType.MeterSwap or MeterEventType.CounterReset)
|
||||
.OrderBy(e => e.Time)
|
||||
.ToList();
|
||||
|
||||
double previous = context.Meter.InitialBaseline;
|
||||
DateTimeOffset? previousTime = null;
|
||||
|
||||
foreach (var reading in readings)
|
||||
{
|
||||
var quality = reading.Quality == ReadingQuality.Measured ? ReadingQuality.Measured : reading.Quality;
|
||||
var swap = FindEvent(swaps, previousTime, reading.Time);
|
||||
|
||||
double amount;
|
||||
if (swap is { EventType: MeterEventType.MeterSwap })
|
||||
{
|
||||
amount = swap.Amount
|
||||
?? ((swap.PrevValue ?? previous) - previous) + (reading.Value - (swap.NewValue ?? 0));
|
||||
}
|
||||
else if (swap is { EventType: MeterEventType.CounterReset })
|
||||
{
|
||||
amount = reading.Value - (swap.NewValue ?? 0);
|
||||
}
|
||||
else if (reading.Value >= previous)
|
||||
{
|
||||
amount = reading.Value - previous;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Unexplained decrease: emit nothing meaningful, rebaseline, and mark quality.
|
||||
amount = 0;
|
||||
quality = ReadingQuality.Estimated;
|
||||
}
|
||||
|
||||
yield return new Consumption
|
||||
{
|
||||
MeterId = context.Meter.MeterId,
|
||||
Time = reading.Time,
|
||||
Amount = amount,
|
||||
Kind = Kind,
|
||||
Quality = quality,
|
||||
ImportBatchId = reading.ImportBatchId,
|
||||
};
|
||||
|
||||
previous = reading.Value;
|
||||
previousTime = reading.Time;
|
||||
}
|
||||
}
|
||||
|
||||
private static MeterEvent? FindEvent(List<MeterEvent> events, DateTimeOffset? afterExclusive, DateTimeOffset upToInclusive)
|
||||
{
|
||||
foreach (var e in events)
|
||||
{
|
||||
var afterOk = afterExclusive is null || e.Time > afterExclusive.Value;
|
||||
if (afterOk && e.Time <= upToInclusive)
|
||||
{
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization.Normalizers;
|
||||
|
||||
/// <summary>Monotonic consumption register (electricity house/grid/EV, water). Δ register → consumption.</summary>
|
||||
public sealed class CumulativeCounterNormalizer : CounterNormalizerBase
|
||||
{
|
||||
public override MeterMode Mode => MeterMode.CumulativeCounter;
|
||||
|
||||
protected override ConsumptionKind Kind => ConsumptionKind.Consumption;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization.Normalizers;
|
||||
|
||||
/// <summary>
|
||||
/// The source already reports increments (SDD §5.2 <c>direct_delta</c>): each reading value is the
|
||||
/// consumption for its interval, used verbatim.
|
||||
/// </summary>
|
||||
public sealed class DirectDeltaNormalizer : IMeterNormalizer
|
||||
{
|
||||
public MeterMode Mode => MeterMode.DirectDelta;
|
||||
|
||||
public IEnumerable<Consumption> Normalize(NormalizationContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
|
||||
foreach (var reading in context.Readings.OrderBy(x => x.Time))
|
||||
{
|
||||
yield return new Consumption
|
||||
{
|
||||
MeterId = context.Meter.MeterId,
|
||||
Time = reading.Time,
|
||||
Amount = reading.Value,
|
||||
Kind = ConsumptionKind.Consumption,
|
||||
Quality = reading.Quality,
|
||||
ImportBatchId = reading.ImportBatchId,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization.Normalizers;
|
||||
|
||||
/// <summary>Monotonic generation register (PV Solar 1/2). Δ register → generation.</summary>
|
||||
public sealed class GenerationCounterNormalizer : CounterNormalizerBase
|
||||
{
|
||||
public override MeterMode Mode => MeterMode.GenerationCounter;
|
||||
|
||||
protected override ConsumptionKind Kind => ConsumptionKind.Generation;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using MeterVault.Core.Domain;
|
||||
|
||||
namespace MeterVault.Core.Normalization.Normalizers;
|
||||
|
||||
/// <summary>
|
||||
/// Cumulative operating-hours register (e.g. a burner). Consumption = Δhours × rate (SDD §5.2).
|
||||
/// The rate is the tank's fixed nozzle spec when configured; otherwise 1, i.e. the meter simply
|
||||
/// tracks operating hours in its own unit. For heating oil the consumption of record comes from
|
||||
/// the tank level-Δ (<see cref="ConsumableBalanceNormalizer"/>); a runtime meter's Δhours feeds
|
||||
/// the empirical L/h analytic and is not double-counted as litres.
|
||||
/// </summary>
|
||||
public sealed class RuntimeCounterNormalizer : IMeterNormalizer
|
||||
{
|
||||
public MeterMode Mode => MeterMode.RuntimeCounter;
|
||||
|
||||
public IEnumerable<Consumption> Normalize(NormalizationContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
|
||||
var rate = context.Meter.Tank is { RateMode: TankRateMode.Fixed, FixedRate: { } r } ? r : 1d;
|
||||
var readings = context.Readings.OrderBy(x => x.Time).ToList();
|
||||
|
||||
double previous = context.Meter.InitialBaseline;
|
||||
foreach (var reading in readings)
|
||||
{
|
||||
var deltaHours = reading.Value >= previous ? reading.Value - previous : 0d;
|
||||
previous = reading.Value;
|
||||
|
||||
yield return new Consumption
|
||||
{
|
||||
MeterId = context.Meter.MeterId,
|
||||
Time = reading.Time,
|
||||
Amount = deltaHours * rate,
|
||||
Kind = ConsumptionKind.Consumption,
|
||||
Quality = reading.Quality,
|
||||
ImportBatchId = reading.ImportBatchId,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using MeterVault.Core.Domain;
|
||||
using MeterVault.Core.Normalization.Expressions;
|
||||
|
||||
namespace MeterVault.Core.Normalization.Normalizers;
|
||||
|
||||
/// <summary>
|
||||
/// A meter computed from other meters' series via a user-defined expression (SDD §7.4). Each
|
||||
/// referenced meter is exposed to the expression as <c>m{id}</c> (its amount in the current time
|
||||
/// bucket). This is how PV self-consumption/savings and the electricity net figures are modelled
|
||||
/// without hardcoding — e.g. <c>Netz Einsparung = m1 - m2</c> (Haus − Netz) evaluated per month.
|
||||
/// </summary>
|
||||
public sealed class VirtualNormalizer : IMeterNormalizer
|
||||
{
|
||||
public MeterMode Mode => MeterMode.Virtual;
|
||||
|
||||
public IEnumerable<Consumption> Normalize(NormalizationContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
var spec = context.Meter.Virtual
|
||||
?? throw new InvalidOperationException(
|
||||
$"Virtual meter {context.Meter.MeterId} has no VirtualSpec.");
|
||||
|
||||
var evaluator = ExpressionEvaluator.Compile(spec.Expression);
|
||||
|
||||
// Per referenced meter: time → summed amount in that bucket.
|
||||
var byMeter = new Dictionary<int, Dictionary<DateTimeOffset, double>>();
|
||||
var allTimes = new SortedSet<DateTimeOffset>();
|
||||
foreach (var meterId in spec.ReferencedMeterIds)
|
||||
{
|
||||
var series = context.ReferencedSeries.TryGetValue(meterId, out var s) ? s : [];
|
||||
var byTime = new Dictionary<DateTimeOffset, double>();
|
||||
foreach (var row in series)
|
||||
{
|
||||
byTime[row.Time] = byTime.GetValueOrDefault(row.Time) + row.Amount;
|
||||
allTimes.Add(row.Time);
|
||||
}
|
||||
|
||||
byMeter[meterId] = byTime;
|
||||
}
|
||||
|
||||
var variables = new Dictionary<string, double>(StringComparer.Ordinal);
|
||||
foreach (var time in allTimes)
|
||||
{
|
||||
variables.Clear();
|
||||
foreach (var meterId in spec.ReferencedMeterIds)
|
||||
{
|
||||
variables[$"m{meterId}"] = byMeter[meterId].GetValueOrDefault(time);
|
||||
}
|
||||
|
||||
yield return new Consumption
|
||||
{
|
||||
MeterId = context.Meter.MeterId,
|
||||
Time = time,
|
||||
Amount = evaluator.Evaluate(variables),
|
||||
Kind = ConsumptionKind.Consumption,
|
||||
Quality = ReadingQuality.Estimated,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using MeterVault.Core.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MeterVault.Infrastructure.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// Seeds sensible defaults (energy types, cost categories, base settings) on first run.
|
||||
/// Idempotent — safe to call after every migration. Nothing here is hardcoded into the domain;
|
||||
/// these are just starter rows the user can edit or delete (SDD FR-1).
|
||||
/// </summary>
|
||||
public static class DatabaseSeeder
|
||||
{
|
||||
public static async Task SeedAsync(MeterVaultDbContext db, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
|
||||
if (!await db.EnergyTypes.AnyAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
db.EnergyTypes.AddRange(DefaultEnergyTypes());
|
||||
}
|
||||
|
||||
if (!await db.CostCategories.AnyAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
db.CostCategories.AddRange(DefaultCostCategories());
|
||||
}
|
||||
|
||||
foreach (var (key, value) in DefaultSettings())
|
||||
{
|
||||
if (!await db.AppSettings.AnyAsync(s => s.Key == key, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
db.AppSettings.Add(new AppSetting { Key = key, Value = value });
|
||||
}
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static IEnumerable<EnergyType> DefaultEnergyTypes() =>
|
||||
[
|
||||
new() { Key = "electricity", DisplayName = "Strom", BaseUnit = "kWh", DefaultMode = MeterMode.CumulativeCounter, Icon = "bolt", ColorHex = "#F6C445" },
|
||||
new() { Key = "water", DisplayName = "Wasser", BaseUnit = "m3", DefaultMode = MeterMode.CumulativeCounter, Icon = "water_drop", ColorHex = "#3B82F6" },
|
||||
new() { Key = "heating_oil", DisplayName = "Heizöl", BaseUnit = "L", DefaultMode = MeterMode.ConsumableBalance, Icon = "local_gas_station", ColorHex = "#B45309" },
|
||||
new() { Key = "gas", DisplayName = "Gas", BaseUnit = "kWh", DefaultMode = MeterMode.CumulativeCounter, Icon = "gas_meter", ColorHex = "#EF4444" },
|
||||
new() { Key = "district_heat", DisplayName = "Fernwärme", BaseUnit = "kWh", DefaultMode = MeterMode.CumulativeCounter, Icon = "thermostat", ColorHex = "#F97316" },
|
||||
];
|
||||
|
||||
private static IEnumerable<CostCategory> DefaultCostCategories() =>
|
||||
[
|
||||
new() { Name = "Heizung", ColorHex = "#B45309", Sort = 0 },
|
||||
new() { Name = "Strom", ColorHex = "#F6C445", Sort = 1 },
|
||||
new() { Name = "Wasser", ColorHex = "#3B82F6", Sort = 2 },
|
||||
new() { Name = "Pool Betrieb", ColorHex = "#14B8A6", Sort = 3 },
|
||||
];
|
||||
|
||||
private static IEnumerable<(string Key, string Value)> DefaultSettings() =>
|
||||
[
|
||||
("currency", "\"EUR\""),
|
||||
("locale", "\"en\""),
|
||||
("timezone", "\"Europe/Berlin\""),
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user