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/charge-now-survives-restart.md
Original file line number Diff line number Diff line change
@@ -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.
120 changes: 120 additions & 0 deletions go/internal/loadpoint/controller_hold_restart_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 4 additions & 1 deletion go/internal/loadpoint/controller_hold_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
17 changes: 12 additions & 5 deletions go/internal/loadpoint/manual_hold_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
}

Expand Down
2 changes: 1 addition & 1 deletion go/internal/loadpoint/manual_hold_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 7 additions & 2 deletions go/internal/loadpoint/manual_unbound_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading