-
Notifications
You must be signed in to change notification settings - Fork 11
fix(mpc): preserve charging periods across replans #1344
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 | ||
| --- | ||
|
|
||
| Plan charging in periods at allowed current steps, using fresh charging state across replans. Favor runs of at least five minutes after departure energy and safety needs, allow a short final top-up, and avoid extra starts for tiny savings. Update the bundled Energyplan worker with joint EV cost decisions and faster tariff planning. |
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,46 @@ | ||
| package loadpoint | ||
|
|
||
| import "time" | ||
|
|
||
| // ChargingPeriod returns fresh observed charging, with the duration of the | ||
| // current uninterrupted run. Commanded power is not evidence of charging. | ||
| func (m *Manager) ChargingPeriod(id string) (bool, time.Duration) { | ||
| m.mu.RLock() | ||
| defer m.mu.RUnlock() | ||
| lp, ok := m.byID[id] | ||
| if !ok { | ||
| return false, 0 | ||
| } | ||
| now := m.now() | ||
| window := lp.powerWindow | ||
| if window <= 0 { | ||
| window = (EVSample{}).PowerWindow() | ||
| } | ||
| if !lp.pluggedIn || lp.powerUnavailable || lp.currentPowerW < steadyChargeFloorW || | ||
| lp.powerAt.IsZero() || lp.powerAt.After(now.Add(time.Second)) || now.Sub(lp.powerAt) > window || | ||
| lp.chargingPeriodSince.IsZero() || lp.chargingPeriodSince.After(now) { | ||
| return false, 0 | ||
| } | ||
| return true, now.Sub(lp.chargingPeriodSince) | ||
| } | ||
|
|
||
| // An observation gap cannot prove uninterrupted charging. Keep this planning | ||
| // history separate from the manager's interruption notification hysteresis. | ||
| func observeChargingPeriod(lp *loadpointRuntime, sample EVSample, now time.Time) { | ||
| at := sample.PowerAt | ||
| if at.IsZero() { | ||
| at = now | ||
| } | ||
| window := lp.powerWindow | ||
| if window <= 0 { | ||
| window = (EVSample{}).PowerWindow() | ||
| } | ||
| if !sample.Connected || sample.ConnectionUnknown || sample.PowerUnavailable || !finite(sample.PowerW) || sample.PowerW < steadyChargeFloorW || | ||
| at.After(now.Add(time.Second)) || now.Sub(at) > sample.PowerWindow() { | ||
| lp.chargingPeriodSince = time.Time{} | ||
| return | ||
| } | ||
| if lp.chargingPeriodSince.IsZero() || !lp.pluggedIn || at.Before(lp.powerAt) || at.Sub(lp.powerAt) > window { | ||
| lp.chargingPeriodSince = now | ||
| } | ||
| } |
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,102 @@ | ||
| package loadpoint | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestChargingPeriodUsesFreshObservedSession(t *testing.T) { | ||
| m := NewManager() | ||
| now := time.Unix(1700000000, 0) | ||
| m.SetNowFn(func() time.Time { return now }) | ||
| cfg := []Config{{ID: "car", DriverName: "test", MaxChargeW: 11000}} | ||
| m.Load(cfg) | ||
| check := func(want bool, duration time.Duration) { | ||
| t.Helper() | ||
| got, elapsed := m.ChargingPeriod("car") | ||
| if got != want || elapsed != duration { | ||
| t.Fatalf("charging=%v elapsed=%s, want %v/%s", got, elapsed, want, duration) | ||
| } | ||
| } | ||
| check(false, 0) | ||
| m.Observe("car", true, 4140, 0, true) | ||
| now = now.Add(20 * time.Second) | ||
| m.Observe("car", true, 4140, 23, true) | ||
| check(true, 20*time.Second) | ||
| m.Load(cfg) | ||
| check(true, 20*time.Second) | ||
| now = now.Add(31 * time.Second) | ||
| check(false, 0) | ||
| m.Observe("car", true, 4140, 23, true) | ||
| check(true, 0) // the observation gap cannot prove continuous charging | ||
| m.Observe("car", true, 0, 23, true) | ||
| check(false, 0) | ||
| now = now.Add(time.Minute) | ||
| m.Observe("car", true, 4140, 23, true) | ||
| check(true, 0) | ||
| m.ObserveSample("car", EVSample{Connected: true, PowerUnavailable: true, PowerAt: now}) | ||
| check(false, 0) | ||
| m.Observe("car", false, 0, 0, false) | ||
| check(false, 0) | ||
| m.Observe("car", true, 4140, 0, true) | ||
| check(true, 0) | ||
| } | ||
|
|
||
| func TestChargingPeriodHonorsSourceCadence(t *testing.T) { | ||
| m := NewManager() | ||
| now := time.Unix(1700000000, 0) | ||
| m.SetNowFn(func() time.Time { return now }) | ||
| m.Load([]Config{{ID: "car"}}) | ||
| m.ObserveSample("car", EVSample{Connected: true, PowerW: 4140, PowerAt: now, PowerMaxAge: 2 * time.Minute}) | ||
| now = now.Add(time.Minute) | ||
| if on, _ := m.ChargingPeriod("car"); !on { | ||
| t.Fatal("fresh slow source lost") | ||
| } | ||
| now = now.Add(61 * time.Second) | ||
| if on, _ := m.ChargingPeriod("car"); on { | ||
| t.Fatal("stale source credited") | ||
| } | ||
| } | ||
|
|
||
| func TestChargingPeriodResetsOnLostConnectionProof(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| identified bool | ||
| generation uint64 | ||
| unknown bool | ||
| }{ | ||
| {"unknown identified connection", true, 1, true}, | ||
| {"unknown unidentified connection", false, 1, true}, | ||
| {"new connection", true, 2, false}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| m := NewManager() | ||
| now := time.Unix(1700000000, 0) | ||
| m.SetNowFn(func() time.Time { return now }) | ||
| m.Load([]Config{{ID: "car"}}) | ||
| m.observeConnectionProof("car", 1, false) | ||
| m.Observe("car", true, 4140, 0, true) | ||
| if tc.identified { | ||
| m.byID["car"].sessionDeviceID = "charger" | ||
| m.byID["car"].sessionID = "session" | ||
| } | ||
| now = now.Add(10 * time.Second) | ||
| if on, elapsed := m.ChargingPeriod("car"); !on || elapsed != 10*time.Second { | ||
| t.Fatalf("before connection loss: charging=%v elapsed=%s", on, elapsed) | ||
| } | ||
| m.observeConnectionProof("car", tc.generation, tc.unknown) | ||
| if on, elapsed := m.ChargingPeriod("car"); on || elapsed != 0 { | ||
| t.Fatalf("cached power survived connection loss: charging=%v elapsed=%s", on, elapsed) | ||
| } | ||
| m.observeConnectionProof("car", tc.generation, false) | ||
| if on, _ := m.ChargingPeriod("car"); on { | ||
| t.Fatal("connection proof alone restored charging") | ||
| } | ||
| now = now.Add(time.Second) | ||
| m.Observe("car", true, 4140, 0, true) | ||
| if on, elapsed := m.ChargingPeriod("car"); !on || elapsed != 0 { | ||
| t.Fatalf("fresh observation retained pre-gap duration: charging=%v elapsed=%s", on, elapsed) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,51 @@ | ||
| package mpc | ||
|
|
||
| import "context" | ||
|
|
||
| // ChargingPeriods describes a preference, after deadline energy and physical | ||
| // reserves. It never delays Core safety, manual stop, or charge completion. | ||
| // StartCostOre is part of the optimizer preference, not the electricity bill. | ||
| type ChargingPeriods struct { | ||
| MinChargeSeconds float64 `json:"min_charge_seconds"` | ||
| StartCostOre float64 `json:"start_cost_ore"` | ||
| InitialCharging bool `json:"initial_charging"` | ||
| InitialChargeSeconds float64 `json:"initial_charge_seconds"` | ||
| } | ||
|
|
||
| // DefaultChargingPeriods favors runs of at least five minutes and avoids an | ||
| // extra start for less than five ore. A final top-up may be shorter. | ||
| func DefaultChargingPeriods(charging bool, seconds float64) ChargingPeriods { | ||
| return ChargingPeriods{MinChargeSeconds: 300, StartCostOre: 5, | ||
| InitialCharging: charging, InitialChargeSeconds: min(300, max(0, seconds))} | ||
| } | ||
|
|
||
| func (o *EnergyplanOptimizer) addChargingPeriods(ctx context.Context, request *externalRequest, p Params) error { | ||
| loads := p.activeLoadpoints() | ||
| needed := false | ||
| for _, lp := range loads { | ||
| needed = needed || lp.Charging.MinChargeSeconds > 0 || lp.Charging.StartCostOre > 0 | ||
| } | ||
| if !needed { | ||
| return nil | ||
| } | ||
| // Negotiate against the running worker on every request. Restart or a | ||
| // binary rollback must not inherit a feature learned from another process. | ||
| probeCtx, cancel := context.WithTimeout(ctx, o.cfg.Timeout) | ||
| defer cancel() | ||
| info, err := o.Health(probeCtx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !optimizerHasFeature(info, "charging_periods") { | ||
| return nil | ||
| } | ||
| byID := make(map[string]ChargingPeriods, len(loads)) | ||
| for _, lp := range loads { | ||
| byID[lp.ID] = lp.Charging | ||
| } | ||
| for i := range request.FlexLoads { | ||
| c := byID[request.FlexLoads[i].ID] | ||
| request.FlexLoads[i].Charging = &c | ||
| } | ||
| return nil | ||
| } |
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,99 @@ | ||
| package mpc | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "math" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| type chargingTransport struct{ feature bool } | ||
|
|
||
| func (c *chargingTransport) RoundTrip(context.Context, []byte) ([]byte, error) { | ||
| features := []string{"champion", "ev_duty"} | ||
| if c.feature { | ||
| features = append(features, "charging_periods") | ||
| } | ||
| return json.Marshal(map[string]any{"name": "ftw-solver", "version": "test", "protocol_version": 1, "features": features}) | ||
| } | ||
| func (*chargingTransport) Health(context.Context) (OptimizerRuntimeInfo, error) { | ||
| return OptimizerRuntimeInfo{}, fmt.Errorf("native handshake required") | ||
| } | ||
| func (*chargingTransport) Close() error { return nil } | ||
|
|
||
| func TestChargingPeriodsNegotiatesEachWorkerAndKeepsLegacyWire(t *testing.T) { | ||
| slots, p := externalTestFixture() | ||
| p.Loadpoint = &LoadpointSpec{ID: "car", PluggedIn: true, CapacityWh: 60000, Levels: 11, SoCMax: 1, MaxChargeW: 11000, Charging: DefaultChargingPeriods(true, 120)} | ||
| transport := &chargingTransport{feature: true} | ||
| external, err := NewExternalOptimizer(ExternalOptimizerConfig{Transport: transport}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| o := &EnergyplanOptimizer{ExternalOptimizer: external} | ||
| for _, supported := range []bool{true, false, true} { | ||
| transport.feature = supported | ||
| r := external.buildRequest(slots, p) | ||
| if r.FlexLoads[0].Charging != nil { | ||
| t.Fatal("generic sidecar received an unnegotiated field") | ||
| } | ||
| if err := o.addChargingPeriods(context.Background(), &r, p); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| c := r.FlexLoads[0].Charging | ||
| if (c != nil) != supported { | ||
| t.Fatalf("supported %v, charging %+v", supported, c) | ||
| } | ||
| if supported && (c.MinChargeSeconds != 300 || c.StartCostOre != 5 || !c.InitialCharging || c.InitialChargeSeconds != 120) { | ||
| t.Fatalf("state lost: %+v", c) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestChargingPeriodsRejectsInvalidPreference(t *testing.T) { | ||
| for _, bad := range []float64{-1, math.Inf(1), math.NaN(), 86401} { | ||
| lp := &LoadpointSpec{ID: "car", PluggedIn: true, CapacityWh: 60000, Levels: 11, SoCMax: 1, MaxChargeW: 11000, Charging: ChargingPeriods{MinChargeSeconds: bad}} | ||
| if err := validateLoadpointSpecs([]*LoadpointSpec{lp}, map[string]string{}); err == nil { | ||
| t.Fatalf("accepted %v", bad) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestNativeChargingPeriodsContinueMeasuredRun(t *testing.T) { | ||
| template := nativeWorker(t, 500*time.Millisecond) | ||
| defer template.Close() | ||
| o, err := NewEnergyplanOptimizer(template.cfg.Command[0]) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer o.Close() | ||
| start := time.Now().Add(time.Minute).Truncate(time.Minute) | ||
| slots := make([]Slot, 12) | ||
| for i := range slots { | ||
| slots[i] = Slot{StartMs: start.Add(time.Duration(i) * time.Minute).UnixMilli(), LenMin: 1, Confidence: 1, PriceOre: 100, Limits: PowerLimits{MaxImportW: 1000}} | ||
| } | ||
| p := Params{Mode: ModeArbitrage, CapacityWh: 10000, SoCMin: .1, SoCMax: .9, InitialSoC: .5, ChargeEfficiency: 1, DischargeEfficiency: 1, | ||
| Loadpoint: &LoadpointSpec{ID: "car", CapacityWh: 1000, Levels: 11, SoCMax: .1, TargetSoC: .1, TargetSlotIdx: 11, PluggedIn: true, | ||
| ChargeEfficiency: 1, MaxChargeW: 1000, AllowedStepsW: []float64{0, 1000}, Charging: DefaultChargingPeriods(true, 180)}} | ||
| plan, err := o.Optimize(context.Background(), slots, p) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := ValidatePlan(slots, p, &plan); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var req externalRequest | ||
| if err := json.Unmarshal(plan.OptimizerInput, &req); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(req.FlexLoads) != 1 || req.FlexLoads[0].Charging == nil || !req.FlexLoads[0].Charging.InitialCharging { | ||
| t.Fatal("measured run missing from request") | ||
| } | ||
| if plan.Actions[0].LoadpointPowerW["car"] <= 0 { | ||
| t.Fatal("current charging was postponed") | ||
| } | ||
| if math.Abs(plan.TotalCostOre-10) > 1e-5 { | ||
| t.Fatalf("preference changed the bill: %g", plan.TotalCostOre) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
When
Controller.tickOnereceivesConnectionUnknown, it callsobserveConnectionProofand returns before reaching this observation, but that path never clearschargingPeriodSince,powerAt, or the cached power. A replan during the remaining power-freshness window therefore reportsInitialCharging=true, and a fresh sample after a brief reconnect can continue the pre-gap duration, even though the socket gap means uninterrupted charging was not observed. Clear the charging-period evidence when connection proof becomes unknown or changes.AGENTS.md reference: AGENTS.md:L22-L23
Useful? React with 👍 / 👎.