|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6" |
| 12 | + "github.com/lorenzodonini/ocpp-go/ocpp1.6/core" |
| 13 | + "github.com/srcfl/ftw/go/internal/events" |
| 14 | + "github.com/srcfl/ftw/go/internal/loadpoint" |
| 15 | + "github.com/srcfl/ftw/go/internal/ocpp" |
| 16 | + "github.com/srcfl/ftw/go/internal/ocpp/chargersocket" |
| 17 | + "github.com/srcfl/ftw/go/internal/telemetry" |
| 18 | +) |
| 19 | + |
| 20 | +// A Wi-Fi blip drops the charger's socket while Charge now runs. The same |
| 21 | +// charger comes back still plugged in, and no unplug was seen. |
| 22 | +func TestChargeNowSurvivesOCPPSocketBlip(t *testing.T) { |
| 23 | + for _, tc := range []struct { |
| 24 | + name string |
| 25 | + serial string |
| 26 | + seeUnknown bool // a control tick runs while the socket is down |
| 27 | + unplugAfter bool // the charger reports Available after it returns |
| 28 | + wantW float64 |
| 29 | + }{ |
| 30 | + {name: "same charger", serial: "A", seeUnknown: true, wantW: 11040}, |
| 31 | + {name: "same charger, blip between ticks", serial: "A", wantW: 11040}, |
| 32 | + {name: "other charger", serial: "B", seeUnknown: true, wantW: 0}, |
| 33 | + {name: "unplug reported after return", serial: "A", seeUnknown: true, unplugAfter: true, wantW: 0}, |
| 34 | + } { |
| 35 | + t.Run(tc.name, func(t *testing.T) { |
| 36 | + listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 37 | + if err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + port := listener.Addr().(*net.TCPAddr).Port |
| 41 | + listener.Close() |
| 42 | + tel := telemetry.NewStore() |
| 43 | + srv, err := ocpp.Start(context.Background(), &ocpp.Config{Enabled: true, Bind: "127.0.0.1", Port: port, ApprovedIDs: []string{"garage"}}, tel) |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + defer srv.Stop() |
| 48 | + await := func(fn func() bool) { |
| 49 | + t.Helper() |
| 50 | + deadline := time.Now().Add(2 * time.Second) |
| 51 | + for !fn() { |
| 52 | + if time.Now().After(deadline) { |
| 53 | + t.Fatal("OCPP state did not settle") |
| 54 | + } |
| 55 | + time.Sleep(time.Millisecond) |
| 56 | + } |
| 57 | + } |
| 58 | + addr := fmt.Sprintf("127.0.0.1:%d", port) |
| 59 | + await(func() bool { |
| 60 | + c, e := net.DialTimeout("tcp", addr, 10*time.Millisecond) |
| 61 | + if e != nil { |
| 62 | + return false |
| 63 | + } |
| 64 | + c.Close() |
| 65 | + return true |
| 66 | + }) |
| 67 | + connect := func(serial string) ocpp16.ChargePoint { |
| 68 | + t.Helper() |
| 69 | + cp := chargersocket.ChargePoint16("garage", nil) |
| 70 | + if e := cp.Start("ws://" + addr); e != nil { |
| 71 | + t.Fatal(e) |
| 72 | + } |
| 73 | + if _, e := cp.BootNotification("Home", "Easee", func(r *core.BootNotificationRequest) { r.ChargePointSerialNumber = serial }); e != nil { |
| 74 | + t.Fatal(e) |
| 75 | + } |
| 76 | + return cp |
| 77 | + } |
| 78 | + status := func(cp ocpp16.ChargePoint, s core.ChargePointStatus) { |
| 79 | + t.Helper() |
| 80 | + if _, e := cp.StatusNotification(1, core.NoError, s); e != nil { |
| 81 | + t.Fatal(e) |
| 82 | + } |
| 83 | + } |
| 84 | + cp := connect("A") |
| 85 | + status(cp, core.ChargePointStatusCharging) |
| 86 | + manager := loadpoint.NewManager() |
| 87 | + manager.Load([]loadpoint.Config{{ID: "garage", DriverName: "garage", MinChargeW: 1380, MaxChargeW: 11040, VehicleCapacityWh: 60000}}) |
| 88 | + now := time.Now() |
| 89 | + manager.SetNowFn(func() time.Time { return now }) |
| 90 | + bus := events.NewBus() |
| 91 | + manager.SetBus(bus) |
| 92 | + plugEdges := 0 |
| 93 | + bus.Subscribe(events.KindChargingConnected, func(events.Event) { plugEdges++ }) |
| 94 | + var commands []float64 |
| 95 | + ctrl := loadpoint.NewController(manager, func(time.Time) (loadpoint.Directive, bool) { return loadpoint.Directive{}, false }, func(name string) (loadpoint.EVSample, bool) { |
| 96 | + return currentEVSample(tel.Get(name, telemetry.DerEV), tel.DriverHealth(name), time.Minute, time.Now(), srv.Handler().IsOnline(name), currentOCPPDeviceID(srv.Handler(), name)) |
| 97 | + }, func(_ context.Context, _ string, p []byte) error { |
| 98 | + var d struct { |
| 99 | + Power float64 `json:"power_w"` |
| 100 | + Action string `json:"action"` |
| 101 | + } |
| 102 | + if e := json.Unmarshal(p, &d); e != nil { |
| 103 | + return e |
| 104 | + } |
| 105 | + if d.Action == "ev_set_current" { |
| 106 | + commands = append(commands, d.Power) |
| 107 | + } |
| 108 | + return nil |
| 109 | + }) |
| 110 | + ctrl.SetSiteFuse(loadpoint.SiteFuse{MaxAmps: 25, Voltage: 230, PhaseCnt: 3}) |
| 111 | + tick := func() { ctrl.Tick(context.Background(), now); now = now.Add(3 * time.Second) } |
| 112 | + tick() |
| 113 | + ctrl.SetManualHold("garage", loadpoint.ManualHold{Persistent: true, PowerW: 11040}) |
| 114 | + tick() |
| 115 | + if len(commands) == 0 || commands[len(commands)-1] != 11040 { |
| 116 | + t.Fatalf("Charge now did not start: %v", commands) |
| 117 | + } |
| 118 | + sent := len(commands) |
| 119 | + |
| 120 | + cp.Stop() |
| 121 | + await(func() bool { return !srv.Handler().IsOnline("garage") }) |
| 122 | + if tc.seeUnknown { |
| 123 | + tick() |
| 124 | + } |
| 125 | + cp = connect(tc.serial) |
| 126 | + defer cp.Stop() |
| 127 | + status(cp, core.ChargePointStatusCharging) |
| 128 | + if tc.unplugAfter { |
| 129 | + tick() |
| 130 | + status(cp, core.ChargePointStatusAvailable) |
| 131 | + } |
| 132 | + tick() |
| 133 | + tick() |
| 134 | + |
| 135 | + after := commands[sent:] |
| 136 | + hold, held := ctrl.GetManualHold("garage", now) |
| 137 | + st, _ := manager.State("garage") |
| 138 | + t.Logf("after the blip: commands=%v hold=%+v held=%v unconfirmed=%v plugged=%v", after, hold, held, st.ManualRestoreUnconfirmed, st.PluggedIn) |
| 139 | + if !tc.unplugAfter && (!st.PluggedIn || plugEdges != 0) { |
| 140 | + t.Fatalf("the blip looked like an unplug: plugged=%v plug edges=%d", st.PluggedIn, plugEdges) |
| 141 | + } |
| 142 | + if tc.wantW > 0 { |
| 143 | + if len(after) == 0 { |
| 144 | + t.Fatal("no command after the blip") |
| 145 | + } |
| 146 | + for _, w := range after { |
| 147 | + if w != tc.wantW { |
| 148 | + t.Fatalf("the blip changed Charge now: commands after it %v, want only %v", after, tc.wantW) |
| 149 | + } |
| 150 | + } |
| 151 | + if !held || hold.PowerW != tc.wantW || st.ManualRestoreUnconfirmed { |
| 152 | + t.Fatalf("Charge now did not survive: hold=%+v held=%v unconfirmed=%v", hold, held, st.ManualRestoreUnconfirmed) |
| 153 | + } |
| 154 | + return |
| 155 | + } |
| 156 | + if tc.unplugAfter { |
| 157 | + // An unplug ends Charge now; an empty charger gets no command. |
| 158 | + if held || st.PluggedIn { |
| 159 | + t.Fatalf("unplug did not end Charge now: hold=%+v held=%v plugged=%v", hold, held, st.PluggedIn) |
| 160 | + } |
| 161 | + return |
| 162 | + } |
| 163 | + if !held || hold.PowerW != 0 || !st.ManualRestoreUnconfirmed || len(after) == 0 || after[len(after)-1] != 0 { |
| 164 | + t.Fatalf("old Charge now followed %s: commands=%v hold=%+v unconfirmed=%v", tc.name, after, hold, st.ManualRestoreUnconfirmed) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
0 commit comments