diff --git a/.changeset/d3-unplug-grace-keeps-charge-now.md b/.changeset/d3-unplug-grace-keeps-charge-now.md new file mode 100644 index 000000000..714998990 --- /dev/null +++ b/.changeset/d3-unplug-grace-keeps-charge-now.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +Charge now no longer ends on a single "not connected" reading. The cable must read out for 30 seconds, the same grace the car sessions use, before Charge now or Pause is cleared. A different session or charger within that time still ends it. diff --git a/go/cmd/ftw/ev_ocpp_blip_test.go b/go/cmd/ftw/ev_ocpp_blip_test.go index 9653a614b..f46f4469e 100644 --- a/go/cmd/ftw/ev_ocpp_blip_test.go +++ b/go/cmd/ftw/ev_ocpp_blip_test.go @@ -131,6 +131,11 @@ func TestChargeNowSurvivesOCPPSocketBlip(t *testing.T) { } tick() tick() + if tc.unplugAfter { + // The cable stays out for UnplugGrace. + now = now.Add(loadpoint.UnplugGrace) + tick() + } after := commands[sent:] hold, held := ctrl.GetManualHold("garage", now) diff --git a/go/cmd/ftw/ev_ocpp_reconnect_test.go b/go/cmd/ftw/ev_ocpp_reconnect_test.go index bb0d7a91d..fc56e51bb 100644 --- a/go/cmd/ftw/ev_ocpp_reconnect_test.go +++ b/go/cmd/ftw/ev_ocpp_reconnect_test.go @@ -194,6 +194,9 @@ func TestOCPPReconnectIsNotAnUnplug(t *testing.T) { } status(cp, false) tick() + // The cable stays out for UnplugGrace. + now = now.Add(loadpoint.UnplugGrace) + tick() if _, ok := ctrl.GetManualHold("garage", now); ok { t.Fatal("physical unplug did not clear hold") } diff --git a/go/internal/cars/tick.go b/go/internal/cars/tick.go index 8c9abe825..27136e0a1 100644 --- a/go/internal/cars/tick.go +++ b/go/internal/cars/tick.go @@ -177,8 +177,8 @@ func (r *Runtime) resumeSessionLocked(st loadpoint.State, s *session) { const bootWindow = 30 * time.Second // UnplugGrace is how long a charger must say the cable is out before the -// session ends. -const UnplugGrace = 30 * time.Second +// session ends. The loadpoint clears Charge now on the same grace. +const UnplugGrace = loadpoint.UnplugGrace // PrePlugMaxAge is how much older than the plug-in a level may be and still // start the session. An older one may predate a drive. diff --git a/go/internal/loadpoint/controller.go b/go/internal/loadpoint/controller.go index 1987b8b2c..9ca3488c1 100644 --- a/go/internal/loadpoint/controller.go +++ b/go/internal/loadpoint/controller.go @@ -42,6 +42,7 @@ type Controller struct { commandTimeout time.Duration wallboxCycleSeq atomic.Uint64 resumeOffers map[string]*resumeOffer // owned by the dispatch tick + unpluggedSince map[string]time.Time // owned by the dispatch tick energySamples map[string]*meteredEnergy // fuseEVMax is this tick's EV share of the site fuse. nil means no cap. @@ -1342,7 +1343,9 @@ func (c *Controller) observeTick(now time.Time, cfg Config, sample EVSample, dis c.manager.SetSurplusWithheld(cfg.ID, wasSurplus && enteringSurplusPaused) c.manager.ObserveSample(cfg.ID, sample) c.observePhases(cfg, sample, now) - c.restoreManualHoldForSession(cfg.ID) + if !c.holdInUnplugGrace(cfg.ID, sample.Connected, now) { + c.restoreManualHoldForSession(cfg.ID) + } c.evaluateBatteryBoost(cfg.ID, now, sample.Connected, dispatchAllowed && !sample.PowerUnavailable) } @@ -1432,8 +1435,12 @@ func (c *Controller) tickOne(ctx context.Context, now time.Time, lpCfg Config, s // Release any manual override when the vehicle unplugs. A // persistent "Start" hold (zero ExpiresAt) would otherwise // survive the session and silently re-apply to the next car; - // "until Stop or unplug" is the operator's mental model. - c.ClearManualHold(lpCfg.ID) + // "until Stop or unplug" is the operator's mental model. One + // reading is not an unplug: the cable must stay out for + // UnplugGrace, as the cars runtime counts it. + if since, seen := c.unpluggedSince[lpCfg.ID]; !seen || now.Sub(since) >= UnplugGrace { + c.ClearManualHold(lpCfg.ID) + } return } if !wasPlugged { diff --git a/go/internal/loadpoint/controller_hold_restart_test.go b/go/internal/loadpoint/controller_hold_restart_test.go index b2035d25a..c5eccf1b1 100644 --- a/go/internal/loadpoint/controller_hold_restart_test.go +++ b/go/internal/loadpoint/controller_hold_restart_test.go @@ -82,17 +82,19 @@ func TestChargeNowWithoutSessionIDResumesAfterRestart(t *testing.T) { samples["easee"] = sample tickWants(t, c, sender, now.Add(21*time.Second), 4140, false) + // The cable stays out for UnplugGrace. sample.Connected = false samples["easee"] = sample c.Tick(context.Background(), now.Add(22*time.Second)) + c.Tick(context.Background(), now.Add(22*time.Second+UnplugGrace)) sample.Connected = true samples["easee"] = sample - tickWants(t, c, sender, now.Add(23*time.Second), 0, false) + tickWants(t, c, sender, now.Add(23*time.Second+UnplugGrace), 0, false) if h, ok := c.GetManualHold("garage", now); ok { t.Fatalf("the next car inherited Charge now: %+v", h) } c, sender = bootController(t, store, samples) - tickWants(t, c, sender, now.Add(30*time.Second), 0, false) + tickWants(t, c, sender, now.Add(30*time.Second+UnplugGrace), 0, false) if h, ok := c.GetManualHold("garage", now); ok { t.Fatalf("Charge now returned after unplug and restart: %+v", h) } diff --git a/go/internal/loadpoint/controller_hold_restore.go b/go/internal/loadpoint/controller_hold_restore.go index 80bff11be..3623a3f3f 100644 --- a/go/internal/loadpoint/controller_hold_restore.go +++ b/go/internal/loadpoint/controller_hold_restore.go @@ -1,5 +1,7 @@ package loadpoint +import "time" + type manualSessionBinding struct { deviceID, sessionID string generation uint64 @@ -84,6 +86,11 @@ func (c *Controller) restoreManualHoldForSession(id string) { // A changed generation therefore means a counter reset or a new // session, even when the earlier session ID was still unknown. changed = changed || previous.generation != current.generation + if !changed && current.sessionID == "" { + // Keep the last proven session while the charger names none, + // as after an unplug, so a later proof is checked against it. + current.sessionID = previous.sessionID + } } } // A Start before the first hardware reading binds here. Once bound, a @@ -137,3 +144,58 @@ func (c *Controller) restoreManualHoldForSession(id string) { c.manager.SetManualRestoreUnconfirmed(id, false) } } + +// UnplugGrace is how long a charger must say the cable is out before the +// session ends and Charge now is cleared. The cars runtime uses it too. +const UnplugGrace = 30 * time.Second + +// holdInUnplugGrace times the unplug and reports whether a hold waits out +// UnplugGrace. An unplugged reading says nothing about the next car, so the +// caller skips the session check. A cable back within the grace keeps the +// hold for the same car; another device or session clears it. +func (c *Controller) holdInUnplugGrace(id string, connected bool, now time.Time) bool { + if connected { + if _, pending := c.unpluggedSince[id]; pending { + delete(c.unpluggedSince, id) + c.rebindAfterUnplug(id) + } + return false + } + if c.unpluggedSince == nil { + c.unpluggedSince = map[string]time.Time{} + } + since, seen := c.unpluggedSince[id] + if !seen { + since = now + c.unpluggedSince[id] = now + } + c.holdMu.Lock() + _, held := c.holds[id] + c.holdMu.Unlock() + return held && now.Sub(since) < UnplugGrace +} + +// rebindAfterUnplug compares the first reading after a short unplug with +// the hardware the hold was bound to before it. +func (c *Controller) rebindAfterUnplug(id string) { + current := c.manager.manualSessionBinding(id) + c.holdMu.Lock() + previous, bound := c.manualBindings[id] + _, held := c.holds[id] + other := bound && ((previous.deviceID != "" && current.deviceID != "" && previous.deviceID != current.deviceID) || + otherSession(previous, current)) + if held && bound && !other && current.deviceID != "" { + // The unplug may have moved the session generation. The same + // hardware and session keep the hold. restoreManualHoldForSession + // keeps the carried session while the charger names none, so a + // later session proof is checked against the one before the unplug. + if current.sessionID == "" { + current.sessionID = previous.sessionID + } + c.manualBindings[id] = current + } + c.holdMu.Unlock() + if held && other { + c.ClearManualHold(id) + } +} diff --git a/go/internal/loadpoint/controller_manual_override_surplus_test.go b/go/internal/loadpoint/controller_manual_override_surplus_test.go index beded3f35..763292815 100644 --- a/go/internal/loadpoint/controller_manual_override_surplus_test.go +++ b/go/internal/loadpoint/controller_manual_override_surplus_test.go @@ -108,11 +108,17 @@ func TestPersistentManualHoldClearedOnUnplug(t *testing.T) { t.Fatalf("hold not installed") } - // Car unplugs. + // Car unplugs. One reading is not yet an unplug. samples[cfg.DriverName] = EVSample{Connected: false} c.Tick(context.Background(), now.Add(5*time.Second)) + if _, active := c.GetManualHold(cfg.ID, now.Add(5*time.Second)); !active { + t.Fatalf("persistent hold cleared by one unplugged reading; want kept for UnplugGrace") + } - if _, active := c.GetManualHold(cfg.ID, now.Add(5*time.Second)); active { + // The cable stays out for UnplugGrace. + at := now.Add(5*time.Second + UnplugGrace) + c.Tick(context.Background(), at) + if _, active := c.GetManualHold(cfg.ID, at); active { t.Errorf("persistent hold survived unplug; want cleared") } } diff --git a/go/internal/loadpoint/controller_missing_reading_test.go b/go/internal/loadpoint/controller_missing_reading_test.go index f65aa3ac5..3265a6005 100644 --- a/go/internal/loadpoint/controller_missing_reading_test.go +++ b/go/internal/loadpoint/controller_missing_reading_test.go @@ -37,6 +37,7 @@ func TestMissingReadingDoesNotEndManualSession(t *testing.T) { } samples[cfg.DriverName] = EVSample{Connected: false} c.Tick(context.Background(), now.Add(3*time.Second)) + c.Tick(context.Background(), now.Add(3*time.Second+UnplugGrace)) if _, ok := c.GetManualHold(cfg.ID, now); ok { t.Fatal("a confirmed unplug must clear the hold") } diff --git a/go/internal/loadpoint/controller_unplug_grace_test.go b/go/internal/loadpoint/controller_unplug_grace_test.go new file mode 100644 index 000000000..ddf9302eb --- /dev/null +++ b/go/internal/loadpoint/controller_unplug_grace_test.go @@ -0,0 +1,116 @@ +package loadpoint + +import ( + "context" + "testing" + "time" +) + +var graceCar = EVSample{Connected: true, RequestActive: true, PowerW: 4140, SessionWh: 6000, DeviceID: "dev-1", SessionID: "sess-1"} + +// unplugFlap runs Charge now at 4140 W, then one unplugged reading, then +// the given connected reading 5 s later. It returns whether the hold is +// still there and the last order. +func unplugFlap(t *testing.T, unplugged, back EVSample) (bool, float64) { + t.Helper() + now := time.Now() + cfg := holdLoadpoint() + samples := map[string]EVSample{} + sender := &fakeSender{} + c := newTestController(t, []Config{cfg}, &Directive{SlotStart: now, SlotEnd: now.Add(time.Hour)}, samples, sender) + samples[cfg.DriverName] = graceCar + c.Tick(context.Background(), now) + c.SetManualHold(cfg.ID, ManualHold{PowerW: 4140, PhaseMode: "3p", Persistent: true}) + c.Tick(context.Background(), now.Add(5*time.Second)) + if n := len(sender.calls); n == 0 || sender.calls[n-1].power != 4140 { + t.Fatalf("Charge now not dispatched: %+v", sender.calls) + } + samples[cfg.DriverName] = unplugged + c.Tick(context.Background(), now.Add(10*time.Second)) + samples[cfg.DriverName] = back + c.Tick(context.Background(), now.Add(15*time.Second)) + _, held := c.GetManualHold(cfg.ID, now.Add(15*time.Second)) + last := -1.0 + if n := len(sender.calls); n > 0 { + last = sender.calls[n-1].power + } + return held, last +} + +// One Connected=false reading between two readings of the same device and +// session does not end Charge now; cars.UnplugGrace keeps that session 30 s. +func TestOneUnpluggedReadingKeepsChargeNow(t *testing.T) { + held, last := unplugFlap(t, EVSample{Connected: false, DeviceID: "dev-1", SessionID: "sess-1"}, graceCar) + if !held || last != 4140 { + t.Fatalf("Charge now gone after one 5 s unplug reading of the same session: held=%v last order=%v W", held, last) + } +} + +// A charger that drops the session id while it reads unplugged still +// keeps Charge now when the same session comes back. +func TestUnpluggedReadingWithoutSessionKeepsChargeNow(t *testing.T) { + held, last := unplugFlap(t, EVSample{Connected: false, DeviceID: "dev-1"}, graceCar) + if !held || last != 4140 { + t.Fatalf("Charge now gone after one unplug reading without a session id: held=%v last order=%v W", held, last) + } +} + +// Another session within the grace is another car: Charge now ends and the +// plan decides, as an unplug did before. +func TestOtherSessionAfterShortUnplugClearsChargeNow(t *testing.T) { + other := graceCar + other.SessionID = "sess-2" + held, last := unplugFlap(t, EVSample{Connected: false, DeviceID: "dev-1"}, other) + if held || last != 0 { + t.Fatalf("a new session inherited Charge now: held=%v last order=%v W", held, last) + } +} + +// lateSessionFlap is unplugFlap for a charger that names the session one +// reading after the cable, as Easee Cloud does. It returns the hold and the +// last order once the session is named. +func lateSessionFlap(t *testing.T, back EVSample) (ManualHold, bool, float64) { + t.Helper() + now := time.Now() + cfg := holdLoadpoint() + samples := map[string]EVSample{} + sender := &fakeSender{} + c := newTestController(t, []Config{cfg}, &Directive{SlotStart: now, SlotEnd: now.Add(time.Hour)}, samples, sender) + samples[cfg.DriverName] = graceCar + c.Tick(context.Background(), now) + c.SetManualHold(cfg.ID, ManualHold{PowerW: 4140, PhaseMode: "3p", Persistent: true}) + c.Tick(context.Background(), now.Add(5*time.Second)) + samples[cfg.DriverName] = EVSample{Connected: false, DeviceID: "dev-1"} + c.Tick(context.Background(), now.Add(10*time.Second)) + unnamed := graceCar + unnamed.SessionID = "" + samples[cfg.DriverName] = unnamed + c.Tick(context.Background(), now.Add(12*time.Second)) + samples[cfg.DriverName] = back + c.Tick(context.Background(), now.Add(14*time.Second)) + h, held := c.GetManualHold(cfg.ID, now.Add(14*time.Second)) + last := -1.0 + if n := len(sender.calls); n > 0 { + last = sender.calls[n-1].power + } + return h, held, last +} + +// Another session named one reading after the cable came back is still +// another car: it must not inherit Charge now. +func TestLateOtherSessionAfterShortUnplugEndsChargeNow(t *testing.T) { + other := graceCar + other.SessionID = "sess-2" + h, held, last := lateSessionFlap(t, other) + if last == 4140 || (held && h.PowerW > 0) { + t.Fatalf("late other session inherited Charge now: last=%v W hold=%+v", last, h) + } +} + +// The same session named one reading late keeps Charge now. +func TestLateSameSessionAfterShortUnplugKeepsChargeNow(t *testing.T) { + h, held, last := lateSessionFlap(t, graceCar) + if !held || h.PowerW != 4140 || last != 4140 { + t.Fatalf("Charge now lost when the same session was named late: held=%v hold=%+v last=%v W", held, h, last) + } +} diff --git a/go/internal/ocpp/inbound_socket_test.go b/go/internal/ocpp/inbound_socket_test.go index a4f8de00f..6215f37bf 100644 --- a/go/internal/ocpp/inbound_socket_test.go +++ b/go/internal/ocpp/inbound_socket_test.go @@ -156,7 +156,10 @@ func TestOldSocketStatusCannotClearPauseAfterReconnect(t *testing.T) { if err := status(false); err != nil { t.Fatal(err) } - ctrl.Tick(context.Background(), time.Now()) + unplugged := time.Now() + ctrl.Tick(context.Background(), unplugged) + // The cable stays out for UnplugGrace. + ctrl.Tick(context.Background(), unplugged.Add(loadpoint.UnplugGrace)) if _, ok := ctrl.GetManualHold("garage", time.Now()); ok { t.Fatal("fresh physical unplug did not clear Pause") }