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/ocpp-dispatch-allowlist-http-timeouts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ftw": patch
---

Route periodic EV dispatch through OCPP, keep Lua loadpoint names off the OCPP allowlist, and bound HTTP read/write/idle time.
7 changes: 1 addition & 6 deletions go/cmd/ftw/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"strings"
"syscall"
"time"

"github.com/srcfl/ftw/go/internal/api"
"github.com/srcfl/ftw/go/internal/config"
Expand Down Expand Up @@ -172,11 +171,7 @@ func runBootstrap(configPath, webDir, driverDir string) {
}()
})

srv := &http.Server{
Addr: ":8080",
Handler: secureBootstrapMutations(mux),
ReadHeaderTimeout: 10 * time.Second,
}
srv := newHTTPServer(":8080", secureBootstrapMutations(mux))
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("bootstrap server", "err", err)
os.Exit(1)
Expand Down
111 changes: 111 additions & 0 deletions go/cmd/ftw/ev_send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"context"

"github.com/srcfl/ftw/go/internal/config"
"github.com/srcfl/ftw/go/internal/loadpoint"
"github.com/srcfl/ftw/go/internal/ocpp"
)

// ocppApprovedIDs is the OCPP allowlist. Lua loadpoint names are not charge-point
// identities: anyone holding the shared basic-auth secret could otherwise connect
// as "easee" and steal DerEV plus commands. Adopted OCPP loadpoints (driver names
// that are not Lua drivers) and ids listed under ocpp.chargers stay approved.
func ocppApprovedIDs(cfg *config.Config) []string {
if cfg == nil {
return nil
}
lua := make(map[string]struct{}, len(cfg.Drivers))
for _, d := range cfg.Drivers {
if d.Name != "" {
lua[d.Name] = struct{}{}
}
}
seen := make(map[string]struct{})
var out []string
add := func(id string) {
if id == "" {
return
}
if _, ok := seen[id]; ok {
return
}
seen[id] = struct{}{}
out = append(out, id)
}
if cfg.OCPP != nil {
for _, c := range cfg.OCPP.Chargers {
add(c.ID)
}
}
for _, lp := range cfg.Loadpoints {
if _, isLua := lua[lp.DriverName]; isLua {
continue
}
add(lp.DriverName)
}
return out
}

// evCommandRouter sends EV commands to an online, adopted OCPP charger, or falls
// through to the Lua registry. Periodic dispatch uses SendWithOutcome / SendCycle;
// those must take the same route as the API's SenderFunc or planner ticks never
// reach a charger that has no Lua driver.
type evCommandRouter struct {
ocpp *ocpp.Server
send loadpoint.SenderFunc
sendOutcome loadpoint.OutcomeSenderFunc
sendCycle loadpoint.CycleSenderFunc
}

func newEVCommandRouter(
srv *ocpp.Server,
send loadpoint.SenderFunc,
sendOutcome loadpoint.OutcomeSenderFunc,
sendCycle loadpoint.CycleSenderFunc,
) evCommandRouter {
return evCommandRouter{ocpp: srv, send: send, sendOutcome: sendOutcome, sendCycle: sendCycle}
}

func (r evCommandRouter) ocppRoute(name string) bool {
return r.ocpp != nil && r.ocpp.Handler().IsOnline(name) && r.ocpp.Handler().IsApproved(name)
}

func (r evCommandRouter) Send(ctx context.Context, name string, payload []byte) error {
if r.ocppRoute(name) {
return r.ocpp.Command(ctx, name, payload)
}
if r.send == nil {
return nil
}
return r.send(ctx, name, payload)
}

func (r evCommandRouter) SendWithOutcome(ctx context.Context, name string, payload []byte, outcome func(error)) error {
if r.ocppRoute(name) {
err := r.ocpp.Command(ctx, name, payload)
if outcome != nil {
outcome(err)
}
return err
}
if r.sendOutcome != nil {
return r.sendOutcome(ctx, name, payload, outcome)
}
err := r.Send(ctx, name, payload)
if outcome != nil {
outcome(err)
}
return err
}

func (r evCommandRouter) SendCycle(ctx context.Context, name string, payload []byte, cycleID uint64) error {
if r.ocppRoute(name) {
return r.ocpp.Command(ctx, name, payload)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve OCPP cycle supersession

Preserve the cycleID sequencing when routing OCPP pause/resume commands instead of calling ocpp.Command directly. If a wallbox cycle pauses and a subsequent tick sends 0 W during the three-second gap—particularly because the site meter became stale—the later ev_resume still runs and restores Handler.LastAmps; unlike Registry.SendEVContinuation, this path cannot detect that the intervening command superseded the cycle, so charging can restart after the safety standdown.

AGENTS.md reference: AGENTS.md:L34-L35

Useful? React with 👍 / 👎.

}
if r.sendCycle != nil {
return r.sendCycle(ctx, name, payload, cycleID)
}
return r.Send(ctx, name, payload)
}
Loading