Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/d3-unplug-grace-keeps-charge-now.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions go/cmd/ftw/ev_ocpp_blip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions go/cmd/ftw/ev_ocpp_reconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions go/internal/cars/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 10 additions & 3 deletions go/internal/loadpoint/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions go/internal/loadpoint/controller_hold_restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
62 changes: 62 additions & 0 deletions go/internal/loadpoint/controller_hold_restore.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package loadpoint

import "time"

type manualSessionBinding struct {
deviceID, sessionID string
generation uint64
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
10 changes: 8 additions & 2 deletions go/internal/loadpoint/controller_manual_override_surplus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
1 change: 1 addition & 0 deletions go/internal/loadpoint/controller_missing_reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
116 changes: 116 additions & 0 deletions go/internal/loadpoint/controller_unplug_grace_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 4 additions & 1 deletion go/internal/ocpp/inbound_socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Loading