Repository navigation
fix(loadpoint): accept newer delayed car charge readings #1648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "ftw": patch | ||
| --- | ||
|
|
||
| Compare delayed car charge readings by measurement time. A newer car reading can now replace an older one even when both arrive late. Keep newer manual corrections and count energy delivered since the measurement once. Keep the measurement time when restart or trimmed history requires using the raw reading. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package loadpoint | ||
|
|
||
| import ( | ||
| "math" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // Cloud delivery order must not replace the car's measurement times. Both | ||
| // readings arrive late; the second was measured after the first. | ||
| func TestDelayedVehicleSoCUsesMeasurementOrder(t *testing.T) { | ||
| m := NewManager() | ||
| m.Load([]Config{{ID: "car", VehicleCapacityWh: 86500, PluginSoC: .60}}) | ||
| start := time.Date(2026, 10, 15, 10, 0, 0, 0, time.UTC) | ||
| now := start | ||
| m.SetNowFn(func() time.Time { return now }) | ||
| m.Observe("car", true, 0, 0, true) | ||
| if !m.SetCurrentSoC("car", .60) { | ||
| t.Fatal("manual starting level refused") | ||
| } | ||
| for i := 1; i <= 8; i++ { | ||
| now = start.Add(time.Duration(i) * 5 * time.Minute) | ||
| m.Observe("car", true, 6000, float64(i)*500, true) | ||
| } | ||
| first := start.Add(10 * time.Minute) | ||
| if !m.VehicleSoCSupersedes("car", first) || !m.AnchorVehicleSoCAt("car", .62, first) { | ||
| t.Fatal("first delayed car reading refused") | ||
| } | ||
| now = start.Add(45 * time.Minute) | ||
| m.Observe("car", true, 6000, 4500, true) | ||
| second := start.Add(20 * time.Minute) | ||
| if !m.VehicleSoCSupersedes("car", second) { | ||
| t.Fatal("newer car measurement lost to the first reading's receipt time") | ||
| } | ||
| want := .68 + 2500*DefaultChargeEfficiency/86500 | ||
| if got, fromVehicle := m.PlanningStartSoC("car", .68, second, true); !fromVehicle || math.Abs(got-want) > 1e-9 { | ||
| t.Fatalf("planner start %.12f fromVehicle=%t, want %.12f", got, fromVehicle, want) | ||
| } | ||
| if !m.AnchorVehicleSoCAt("car", .68, second) { | ||
| t.Fatal("second delayed car reading refused") | ||
| } | ||
| st, _ := m.State("car") | ||
| if math.Abs(st.CurrentSoC-want) > 1e-9 || st.UpdatedAtMs != now.UnixMilli() { | ||
| t.Fatalf("wrong corrected level or receipt time: %+v", st) | ||
| } | ||
| for _, observed := range []time.Time{time.Time{}, first, second} { | ||
| if m.VehicleSoCSupersedes("car", observed) { | ||
| t.Fatalf("old or repeated measurement became newer: %v", observed) | ||
| } | ||
| if got, fromVehicle := m.PlanningStartSoC("car", .68, observed, true); fromVehicle || math.Abs(got-want) > 1e-9 { | ||
| t.Fatalf("old reading changed the estimate or counted energy twice: %.12f, %t", got, fromVehicle) | ||
| } | ||
| } | ||
| // A later explicit user correction still takes precedence over a car | ||
| // measurement taken before it, even when that reading arrives afterwards. | ||
| now = start.Add(46 * time.Minute) | ||
| if !m.SetCurrentSoC("car", .71) { | ||
| t.Fatal("manual correction refused") | ||
| } | ||
| olderThanManual := start.Add(44 * time.Minute) | ||
| if m.VehicleSoCSupersedes("car", olderThanManual) { | ||
| t.Fatal("a delayed car reading replaced a newer manual correction") | ||
| } | ||
| if got, fromVehicle := m.PlanningStartSoC("car", .68, olderThanManual, true); fromVehicle || math.Abs(got-.71) > 1e-9 { | ||
| t.Fatalf("manual correction lost: %.12f, %t", got, fromVehicle) | ||
| } | ||
| now = now.Add(2 * time.Minute) | ||
| if !m.VehicleSoCSupersedes("car", now.Add(-time.Minute)) { | ||
| t.Fatal("a car reading measured after the manual correction cannot replace it") | ||
| } | ||
| } | ||
|
|
||
| func TestDelayedVehicleSoCWithoutHistoryKeepsMeasurementOrder(t *testing.T) { | ||
| for _, age := range []time.Duration{2 * time.Minute, 10 * time.Minute} { | ||
| t.Run(age.String(), func(t *testing.T) { | ||
| m := NewManager() | ||
| m.Load([]Config{{ID: "car", VehicleCapacityWh: 86500, PluginSoC: .60}}) | ||
| received := time.Date(2026, 10, 15, 10, 0, 0, 0, time.UTC) | ||
| now := received | ||
| m.SetNowFn(func() time.Time { return now }) | ||
| // A restart begins mid-session: no energy history before receipt. | ||
| m.Observe("car", true, 6000, 4000, true) | ||
| first, second := received.Add(-age), received.Add(-age/2) | ||
| if !m.AnchorVehicleSoCAt("car", .62, first) { | ||
| t.Fatal("qualified delayed reading refused") | ||
| } | ||
| now = now.Add(time.Minute) | ||
| m.Observe("car", true, 6000, 4500, true) | ||
| if !m.VehicleSoCSupersedes("car", second) { | ||
| t.Fatal("newer car measurement lost to raw fallback receipt time") | ||
| } | ||
| if got, fromVehicle := m.PlanningStartSoC("car", .68, second, true); !fromVehicle || math.Abs(got-.68) > 1e-9 { | ||
| t.Fatalf("unknown earlier energy changed planning SoC: %.12f, %t", got, fromVehicle) | ||
| } | ||
| if !m.AnchorVehicleSoCAt("car", .68, second) { | ||
| t.Fatal("newer delayed reading refused") | ||
| } | ||
| if st, _ := m.State("car"); math.Abs(st.CurrentSoC-.68) > 1e-9 || st.UpdatedAtMs != now.UnixMilli() { | ||
| t.Fatalf("raw fallback changed receipt time or added unknown energy: %+v", st) | ||
| } | ||
| if m.VehicleSoCSupersedes("car", first) || m.VehicleSoCSupersedes("car", second) { | ||
| t.Fatal("old or repeated cloud reading can replace the newer level") | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a mid-session restart, a delayed measurement from before the first retained energy sample makes
deliveredAtfail, sogo/cmd/ftw/main.go:2986-2987falls back toAnchorVehicleSoC, which passes a zero timestamp and records the receipt time instead. A later cloud reading that was measured after the first but before that receipt is then rejected byVehicleSoCSupersedes, leaving the ordering bug in place for cold-start/recovery sessions; preserve the originalobservedAteven when anchoring must fall back to the current energy value.AGENTS.md reference: AGENTS.md:L24-L25
Useful? React with 👍 / 👎.