From 2413095464723ca344fc27a1bc4e3c193e172648 Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Wed, 21 Oct 2026 14:30:14 +0200 Subject: [PATCH] fix(loadpoint): Charge now keeps charging after a restart A restart restored Charge now as a pause. The saved choice needs proof that the same car is still on the charger, and the restore asked for a session ID that most chargers never report (OCPP and every bundled driver except easee_cloud). It answered "unconfirmed" and paused. A saved choice without a session ID is now checked by hardware identity alone; an observed unplug still ends it. A record with a session ID waits for the charger to report one instead of pausing, and a socket reconnect during that wait no longer saves a pause over it. A different session still pauses as before. Co-Authored-By: Claude Opus 5.5 --- .changeset/charge-now-survives-restart.md | 5 + .../loadpoint/controller_hold_restart_test.go | 120 ++++++++++++++++++ .../loadpoint/controller_hold_restore.go | 5 +- go/internal/loadpoint/manual_hold_state.go | 17 ++- .../loadpoint/manual_hold_state_test.go | 2 +- .../loadpoint/manual_unbound_state_test.go | 9 +- 6 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 .changeset/charge-now-survives-restart.md create mode 100644 go/internal/loadpoint/controller_hold_restart_test.go diff --git a/.changeset/charge-now-survives-restart.md b/.changeset/charge-now-survives-restart.md new file mode 100644 index 000000000..fdb0c6a3c --- /dev/null +++ b/.changeset/charge-now-survives-restart.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +Charge now keeps charging after an update or restart. Before, a restart turned it into a pause until someone pressed Start again. It still ends on Stop, when the car is unplugged or full, and a different car on the charger does not inherit it. diff --git a/go/internal/loadpoint/controller_hold_restart_test.go b/go/internal/loadpoint/controller_hold_restart_test.go new file mode 100644 index 000000000..b2035d25a --- /dev/null +++ b/go/internal/loadpoint/controller_hold_restart_test.go @@ -0,0 +1,120 @@ +package loadpoint + +import ( + "context" + "testing" + "time" +) + +// bootController is one Core process over a state.db that outlives it. +func bootController(t *testing.T, store *sessionMemory, samples map[string]EVSample) (*Controller, *fakeSender) { + t.Helper() + m := NewManager() + m.Load([]Config{holdLoadpoint()}) + m.SetSessionStore(store) + sender := &fakeSender{} + plan := PlanFunc(func(time.Time) (Directive, bool) { return Directive{}, false }) + tel := TelemetryFunc(func(driver string) (EVSample, bool) { s, ok := samples[driver]; return s, ok }) + c := NewController(m, plan, tel, sender.Send) + c.SetSiteFuse(SiteFuse{MaxAmps: 16, Voltage: 230, PhaseCnt: 3}) + c.SetManualHoldSaver(func(id string, h ManualHold, cleared bool) { + if err := m.PersistManualHold(id, h, cleared); err != nil { + t.Error(err) + } + }) + return c, sender +} + +func tickWants(t *testing.T, c *Controller, sender *fakeSender, now time.Time, power float64, unconfirmed bool) { + t.Helper() + c.Tick(context.Background(), now) + if last, ok := lastSetCurrent(sender.calls); !ok || last.power != power { + t.Fatalf("commands=%+v, want latest power %v", sender.calls, power) + } + if st, _ := c.manager.State("garage"); st.ManualRestoreUnconfirmed != unconfirmed { + t.Fatalf("manual_restore_unconfirmed = %v, want %v", st.ManualRestoreUnconfirmed, unconfirmed) + } +} + +// Charge now ends on Stop, unplug or a full car. A restart is none of them, +// even when the charger reports its identity before its session. +func TestChargeNowWaitsForSessionAfterRestart(t *testing.T) { + for _, reconnect := range []bool{false, true} { + store := &sessionMemory{data: map[string]string{}} + sample := EVSample{Connected: true, RequestActive: true, ConnectionGeneration: 1, DeviceID: "easee:A", SessionID: "session-1"} + samples := map[string]EVSample{"easee": sample} + c, sender := bootController(t, store, samples) + now := time.Now() + c.SetManualHold("garage", ManualHold{PowerW: 4140, Persistent: true}) + tickWants(t, c, sender, now, 4140, false) + + c, sender = bootController(t, store, samples) + sample.SessionID = "" + samples["easee"] = sample + tickWants(t, c, sender, now.Add(20*time.Second), 0, true) + if reconnect { + // A new socket is not a new car; only the session can tell. + sample.ConnectionGeneration = 2 + samples["easee"] = sample + tickWants(t, c, sender, now.Add(21*time.Second), 0, true) + } + sample.SessionID = "session-1" + samples["easee"] = sample + tickWants(t, c, sender, now.Add(22*time.Second), 4140, false) + } +} + +// Most chargers, OCPP included, never report a session ID. The same +// hardware still plugged in resumes at once; an unplug still ends it. +func TestChargeNowWithoutSessionIDResumesAfterRestart(t *testing.T) { + store := &sessionMemory{data: map[string]string{}} + sample := EVSample{Connected: true, RequestActive: true, ConnectionGeneration: 1, DeviceID: "rigco:OCPPSIM02"} + samples := map[string]EVSample{"easee": sample} + c, sender := bootController(t, store, samples) + now := time.Now() + c.SetManualHold("garage", ManualHold{PowerW: 4140, Persistent: true}) + tickWants(t, c, sender, now, 4140, false) + + // After a restart the charger connects before it reports its cable. + samples["easee"] = EVSample{ConnectionUnknown: true, ConnectionGeneration: 1} + c, sender = bootController(t, store, samples) + c.Tick(context.Background(), now.Add(20*time.Second)) + samples["easee"] = sample + tickWants(t, c, sender, now.Add(21*time.Second), 4140, false) + + sample.Connected = false + samples["easee"] = sample + c.Tick(context.Background(), now.Add(22*time.Second)) + sample.Connected = true + samples["easee"] = sample + tickWants(t, c, sender, now.Add(23*time.Second), 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) + if h, ok := c.GetManualHold("garage", now); ok { + t.Fatalf("Charge now returned after unplug and restart: %+v", h) + } +} + +// A different session after a restart means the car changed while Core +// was down. It must not inherit the earlier Charge now. +func TestChargeNowDoesNotFollowAnotherSessionAfterRestart(t *testing.T) { + store := &sessionMemory{data: map[string]string{}} + sample := EVSample{Connected: true, RequestActive: true, DeviceID: "easee:A", SessionID: "session-1"} + samples := map[string]EVSample{"easee": sample} + c, sender := bootController(t, store, samples) + now := time.Now() + c.SetManualHold("garage", ManualHold{PowerW: 4140, Persistent: true}) + tickWants(t, c, sender, now, 4140, false) + + c, sender = bootController(t, store, samples) + sample.SessionID = "" + samples["easee"] = sample + tickWants(t, c, sender, now.Add(20*time.Second), 0, true) + sample.SessionID = "session-2" + samples["easee"] = sample + tickWants(t, c, sender, now.Add(21*time.Second), 0, true) + tickWants(t, c, sender, now.Add(22*time.Second), 0, true) +} diff --git a/go/internal/loadpoint/controller_hold_restore.go b/go/internal/loadpoint/controller_hold_restore.go index 04ac994c5..45299c329 100644 --- a/go/internal/loadpoint/controller_hold_restore.go +++ b/go/internal/loadpoint/controller_hold_restore.go @@ -61,8 +61,11 @@ func (c *Controller) restoreManualHoldForSession(id string) { } // A Start before the first hardware reading binds here. Once bound, a // different hardware/session/config generation must not inherit its power. + // A restore still waiting for proof has no power to inherit; the saved + // record is checked against the next identity instead of overwritten. c.manualBindings[id] = current - if changed { + restored, seen := c.manualRestored[id] + if pending := seen && !restored; changed && !pending { if hold, held := c.holds[id]; held { if hold.PowerW > 0 { c.holds[id] = ManualHold{Persistent: true} diff --git a/go/internal/loadpoint/manual_hold_state.go b/go/internal/loadpoint/manual_hold_state.go index 36cd4d482..71562f9cc 100644 --- a/go/internal/loadpoint/manual_hold_state.go +++ b/go/internal/loadpoint/manual_hold_state.go @@ -153,10 +153,12 @@ func (m *Manager) setManualPersistenceError(id string, err error) { } } -// RestoreManualHold returns pending until hardware identity is known. A -// verified same-session positive hold or a hardware-bound pause is restored. -// Unknown/changed sessions and legacy records return a persistent zero-W hold -// plus unconfirmed: failed restoration must not start automatic charging. +// RestoreManualHold returns pending until the record's proof is known: the +// hardware identity, and the session ID when the record has one. A record +// without a session ID is checked by hardware alone; most chargers never +// report one, and an observed unplug still ends the hold. Changed hardware or +// sessions and legacy records return a persistent zero-W hold plus +// unconfirmed: failed restoration must not start automatic charging. func (m *Manager) RestoreManualHold(id string) (ManualHold, string) { m.sessionMu.Lock() defer m.sessionMu.Unlock() @@ -227,9 +229,14 @@ func (m *Manager) RestoreManualHold(id string) (ManualHold, string) { !record.Hold.ExpiresAt.IsZero() || !finite(record.Hold.PowerW) || record.Hold.PowerW < 0 { return ManualHold{Persistent: true}, "unconfirmed" } - if record.Hold.PowerW == 0 || (sessionID != "" && sessionID == record.SessionID) { + if record.Hold.PowerW == 0 || record.SessionID == "" || sessionID == record.SessionID { return record.Hold, "restored" } + if sessionID == "" { + // A charger can report its session after its identity. Deciding now + // would turn Charge now into a pause on every restart. + return ManualHold{Persistent: true}, "pending" + } return ManualHold{Persistent: true}, "unconfirmed" } diff --git a/go/internal/loadpoint/manual_hold_state_test.go b/go/internal/loadpoint/manual_hold_state_test.go index eadeb728a..f042bc0f8 100644 --- a/go/internal/loadpoint/manual_hold_state_test.go +++ b/go/internal/loadpoint/manual_hold_state_test.go @@ -141,7 +141,7 @@ func TestManualHoldRestoreRequiresHardwareAndActiveSession(t *testing.T) { }{ {"easee:ABC", "session-1", "restored", 4140}, {"easee:ABC", "session-2", "unconfirmed", 0}, - {"easee:ABC", "", "unconfirmed", 0}, + {"easee:ABC", "", "pending", 0}, {"easee:OTHER", "session-1", "unconfirmed", 0}, } { m := sessionManager(store, "garage", "charger") diff --git a/go/internal/loadpoint/manual_unbound_state_test.go b/go/internal/loadpoint/manual_unbound_state_test.go index da51da1f1..34148b4b8 100644 --- a/go/internal/loadpoint/manual_unbound_state_test.go +++ b/go/internal/loadpoint/manual_unbound_state_test.go @@ -18,11 +18,16 @@ func TestManualRestrictionSurvivesRestartWithoutHardwareIdentity(t *testing.T) { restarted.ObserveSession("garage", true, 0, 1000, true, device, "") c := NewController(restarted, nil, nil, nil) c.restoreManualHoldForSession("garage") - if h, ok := c.GetManualHold("garage", time.Now()); !ok || !h.Persistent || h.PowerW != 0 { + // Only hardware identity can carry power across a restart. + want := 0.0 + if device == "easee:ABC" { + want = watts + } + if h, ok := c.GetManualHold("garage", time.Now()); !ok || !h.Persistent || h.PowerW != want { t.Fatalf("%q/%v: restart lost restriction: %+v %v", device, watts, h, ok) } st, _ := restarted.State("garage") - if st.ManualRestoreUnconfirmed != (watts > 0) { + if st.ManualRestoreUnconfirmed != (watts != want) { t.Fatalf("%q/%v: confirmation=%v", device, watts, st.ManualRestoreUnconfirmed) } if err := restarted.PersistManualHold("garage", ManualHold{}, true); err != nil {