From 7916bb207708cb4c5386eccd3b5e82bf6b07ed59 Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sat, 21 Nov 2026 18:03:27 +0100 Subject: [PATCH 1/3] fix(web): say when FTW reads a charger but cannot control it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A new owner added a Zaptec Go through zaptec_cloud. The driver declares read_only = true, so FTW accepted the charger, never controlled it and never said why: Settings showed only a small "telemetry only" badge. Core states the fact and the web chooses the words, once, in web/driver-control.js: - Catalog entries carry read_only_reason: the driver's description, from the sentence that talks about reading or control, and nothing when it only names the product. - /api/loadpoints carries driver_read_only and driver_read_only_reason for each charger, from the catalog entry its driver runs. - The EV card, Settings → Devices (the device row and the cards for adding one), Settings → Chargers, the setup wizard and the advanced charger panel say "FTW reads this charger but cannot control it", with the driver's reason when it gives one. Batteries, cars and heat pumps get the same note in their words; meters and solar inverters get none. No setting changes. Signed-off-by: Fredrik Ahlgren Co-Authored-By: Claude Opus 5.5 --- .changeset/read-only-shown.md | 16 +++ go/internal/api/api_control_feedback.go | 9 ++ go/internal/api/api_driver_read_only.go | 41 +++++++ go/internal/api/api_driver_read_only_test.go | 73 +++++++++++++ go/internal/drivers/catalog.go | 42 +++++++- go/internal/drivers/catalog_test.go | 49 +++++++++ web/app.css | 9 ++ web/app.js | 15 +++ web/driver-control.js | 59 +++++++++++ web/driver-control.test.mjs | 106 +++++++++++++++++++ web/index.html | 11 +- web/loadpoints.js | 3 +- web/settings/tabs/devices.js | 35 +++++- web/settings/tabs/loadpoints.js | 19 +++- web/setup.html | 3 +- web/setup.js | 13 +++ web/setup.test.mjs | 2 +- 17 files changed, 491 insertions(+), 14 deletions(-) create mode 100644 .changeset/read-only-shown.md create mode 100644 go/internal/api/api_driver_read_only.go create mode 100644 go/internal/api/api_driver_read_only_test.go create mode 100644 web/driver-control.js create mode 100644 web/driver-control.test.mjs diff --git a/.changeset/read-only-shown.md b/.changeset/read-only-shown.md new file mode 100644 index 000000000..6916c19f5 --- /dev/null +++ b/.changeset/read-only-shown.md @@ -0,0 +1,16 @@ +--- +"ftw": patch +--- + +Say when FTW reads a charger, battery, car or heat pump but cannot control it. + +A driver that declares `read_only = true` used to show only a small +"telemetry only" badge in Settings, so an owner who added a Zaptec Go through +`zaptec_cloud` saw FTW accept the charger and never steer it, with nothing to +say why. Now the EV card, Settings → Devices, Settings → Chargers, the device +cards when adding one, the setup wizard and the advanced charger panel say +"FTW reads this charger but cannot control it", with the driver's own reason +when its description gives one. `/api/drivers/catalog` entries carry +`read_only_reason`, and `/api/loadpoints` carries `driver_read_only` and +`driver_read_only_reason`. Meters and solar inverters, where control is not +expected, get no note. No setting changes. diff --git a/go/internal/api/api_control_feedback.go b/go/internal/api/api_control_feedback.go index 8da4d47c9..acfd1e50e 100644 --- a/go/internal/api/api_control_feedback.go +++ b/go/internal/api/api_control_feedback.go @@ -739,9 +739,14 @@ type loadpointFeedbackView struct { // level, and the box does not guess one (docs/cars.md). Level *float64 `json:"current_soc,omitempty"` ControlFeedback *ControlFeedback `json:"control_feedback,omitempty"` + // DriverReadOnly says the charger's driver reads it but cannot control + // it; DriverReadOnlyReason is the driver's own reason when it gives one. + DriverReadOnly bool `json:"driver_read_only,omitempty"` + DriverReadOnlyReason string `json:"driver_read_only_reason,omitempty"` } func (s *Server) loadpointsWithFeedback(states []loadpoint.State) []loadpointFeedbackView { + readOnly := s.readOnlyDrivers() feedback := s.controlFeedback(time.Now()) byDriver := map[string]*ControlFeedback{} for i := range feedback { @@ -752,6 +757,10 @@ func (s *Server) loadpointsWithFeedback(states []loadpoint.State) []loadpointFee out := make([]loadpointFeedbackView, 0, len(states)) for _, st := range states { view := loadpointFeedbackView{State: st, ControlFeedback: byDriver[st.DriverName]} + if e, ok := readOnly[st.DriverName]; ok { + view.DriverReadOnly = true + view.DriverReadOnlyReason = e.ReadOnlyReason + } if !st.SoCUnknown { level := st.CurrentSoC view.Level = &level diff --git a/go/internal/api/api_driver_read_only.go b/go/internal/api/api_driver_read_only.go new file mode 100644 index 000000000..819428510 --- /dev/null +++ b/go/internal/api/api_driver_read_only.go @@ -0,0 +1,41 @@ +package api + +import ( + "path/filepath" + + "github.com/srcfl/ftw/go/internal/config" + "github.com/srcfl/ftw/go/internal/drivers" +) + +// readOnlyDrivers maps each configured driver name to its catalog entry when +// the driver reads its device but cannot control it. A charger added through +// such a driver is accepted and shown, and FTW never steers it; the owner +// needs to be told so where they look at the charger, not left to guess. +func (s *Server) readOnlyDrivers() map[string]drivers.CatalogEntry { + out := map[string]drivers.CatalogEntry{} + if s.deps == nil || s.deps.Cfg == nil || s.deps.CfgMu == nil { + return out + } + s.deps.CfgMu.RLock() + configured := append([]config.Driver(nil), s.deps.Cfg.Drivers...) + s.deps.CfgMu.RUnlock() + if len(configured) == 0 { + return out + } + dir := s.deps.DriverDir + if dir == "" { + dir = filepath.Join(filepath.Dir(s.deps.ConfigPath), "drivers") + } + // Local override > owner's selected signed driver > the release's own + // driver, the order the registry runs them in. + catalog, err := drivers.LoadCatalogMulti(s.deps.UserDriverDir, s.managedDriverDir(), dir) + if err != nil { + return out + } + for _, d := range configured { + if e, ok := drivers.FindCatalogEntry(catalog, d.Lua); ok && e.ReadOnly { + out[d.Name] = e + } + } + return out +} diff --git a/go/internal/api/api_driver_read_only_test.go b/go/internal/api/api_driver_read_only_test.go new file mode 100644 index 000000000..558ef578f --- /dev/null +++ b/go/internal/api/api_driver_read_only_test.go @@ -0,0 +1,73 @@ +package api + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "sync" + "testing" + + "github.com/srcfl/ftw/go/internal/config" + "github.com/srcfl/ftw/go/internal/loadpoint" +) + +// A charger whose driver only reads it says so on /api/loadpoints, with the +// driver's own reason when it gives one. A new owner added a Zaptec Go this +// way and FTW accepted it, never steered it and never said why. +func TestLoadpointsSayWhenTheChargerDriverOnlyReads(t *testing.T) { + dir := t.TempDir() + write := func(name, src string) { + t.Helper() + if err := os.WriteFile(filepath.Join(dir, name), []byte(src), 0o644); err != nil { + t.Fatal(err) + } + } + write("zaptec_cloud.lua", "DRIVER = {\n id = \"zaptec_cloud\",\n read_only = true,\n"+ + " capabilities = { \"ev\" },\n description = \"Zaptec Go via Zaptec Cloud.\",\n}\n") + write("wall.lua", "DRIVER = {\n id = \"wall\",\n read_only = true,\n"+ + " capabilities = { \"ev\" },\n description = \"Tesla Wall Connector. Observation only; no cloud account.\",\n}\n") + write("easee_cloud.lua", "DRIVER = {\n id = \"easee_cloud\",\n capabilities = { \"ev\" },\n}\n") + + mgr := loadpoint.NewManager() + mgr.Load([]loadpoint.Config{ + {ID: "drive", DriverName: "zaptec", MinChargeW: 1380, MaxChargeW: 11000}, + {ID: "garage", DriverName: "wall", MinChargeW: 1380, MaxChargeW: 11000}, + {ID: "street", DriverName: "easee", MinChargeW: 1380, MaxChargeW: 11000}, + }) + cfg := &config.Config{Drivers: []config.Driver{ + {Name: "zaptec", Lua: "drivers/zaptec_cloud.lua"}, + {Name: "wall", Lua: "drivers/wall.lua"}, + {Name: "easee", Lua: "drivers/easee_cloud.lua"}, + }} + s := New(&Deps{Loadpoints: mgr, Cfg: cfg, CfgMu: &sync.RWMutex{}, DriverDir: dir}) + + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/loadpoints", nil)) + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String()) + } + var body struct { + Loadpoints []map[string]any `json:"loadpoints"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatal(err) + } + byID := map[string]map[string]any{} + for _, lp := range body.Loadpoints { + byID[lp["id"].(string)] = lp + } + if byID["drive"]["driver_read_only"] != true { + t.Errorf("zaptec loadpoint driver_read_only = %v, want true", byID["drive"]["driver_read_only"]) + } + if _, ok := byID["drive"]["driver_read_only_reason"]; ok { + t.Errorf("a product description is not a reason, got %v", byID["drive"]["driver_read_only_reason"]) + } + if byID["garage"]["driver_read_only_reason"] != "Observation only; no cloud account." { + t.Errorf("wall connector reason = %v", byID["garage"]["driver_read_only_reason"]) + } + if _, ok := byID["street"]["driver_read_only"]; ok { + t.Errorf("a controlling charger must not say read only: %v", byID["street"]) + } +} diff --git a/go/internal/drivers/catalog.go b/go/internal/drivers/catalog.go index 7e714ac37..cbcf26e65 100644 --- a/go/internal/drivers/catalog.go +++ b/go/internal/drivers/catalog.go @@ -54,6 +54,10 @@ type CatalogEntry struct { // ReadOnly means the driver never accepts dispatch commands. The catalog // UI uses it to avoid presenting battery capacity as a control opt-in. ReadOnly bool `json:"read_only,omitempty"` + // ReadOnlyReason is why a read-only driver does not control, in the words + // of its own description. Empty when the description only says what the + // device is. See readOnlyReason. + ReadOnlyReason string `json:"read_only_reason,omitempty"` // ReadOnlyDeclared distinguishes an explicit false value from old metadata // that did not state whether the driver can control hardware. ReadOnlyDeclared bool `json:"-"` @@ -232,6 +236,9 @@ func parseCatalogEntry(path string) (CatalogEntry, error) { e.WSHosts = pickList(block, "ws_hosts") e.ConnectionDefaults = pickKVBlock(block, "connection_defaults") e.ReadOnly, e.ReadOnlyDeclared = pickOptionalBool(block, "read_only") + if e.ReadOnly { + e.ReadOnlyReason = readOnlyReason(e.Description) + } e.VerificationStatus = normalizeVerificationStatus(pickString(block, "verification_status")) e.VerifiedBy = pickList(block, "verified_by") e.VerifiedAt = pickString(block, "verified_at") @@ -290,17 +297,46 @@ func IsEVOrVehicleDriver(catalog []CatalogEntry, luaPath string) bool { // declares read_only=true. Control-pool construction uses this as a safety // boundary for read-only telemetry drivers such as Sourceful Zap. func IsReadOnlyDriver(catalog []CatalogEntry, luaPath string) bool { + e, ok := FindCatalogEntry(catalog, luaPath) + return ok && e.ReadOnly +} + +// FindCatalogEntry returns the catalog entry a configured lua path runs, +// matched by portable path first and then by file name, as +// IsEVOrVehicleDriver does. +func FindCatalogEntry(catalog []CatalogEntry, luaPath string) (CatalogEntry, bool) { if luaPath == "" { - return false + return CatalogEntry{}, false } wantPath := filepath.ToSlash(luaPath) wantFilename := filepath.Base(wantPath) for _, e := range catalog { if strings.EqualFold(e.Path, wantPath) || strings.EqualFold(e.Filename, wantFilename) { - return e.ReadOnly + return e, true } } - return false + return CatalogEntry{}, false +} + +// readOnlyWords are the words a description uses when it says why a driver +// does not control: "Read-only until a held-zero command is verified on +// hardware", "Observation only", "Does not wake or command the car". The +// device catalog page in srcfl/device-drivers uses the same words. +var readOnlyWords = regexp.MustCompile(`(?i)read[- ]?only|observ|\bcontrol(s|led|ling)?\b|\bcommand(s|ed)?\b|\bwrites?\b`) + +// readOnlyReason keeps a description only when it talks about reading or +// control, starting at the sentence that does. "Zaptec Go / Go 2 / Pro via +// Zaptec Cloud" says what the device is, not why FTW cannot steer it, so it +// gives no reason. +func readOnlyReason(description string) string { + loc := readOnlyWords.FindStringIndex(description) + if loc == nil { + return "" + } + if i := strings.LastIndex(description[:loc[0]], ". "); i >= 0 { + description = description[i+2:] + } + return strings.TrimSpace(description) } // normalizeVerificationStatus coerces the Lua string into one of the diff --git a/go/internal/drivers/catalog_test.go b/go/internal/drivers/catalog_test.go index 3135a1da2..d25f4b466 100644 --- a/go/internal/drivers/catalog_test.go +++ b/go/internal/drivers/catalog_test.go @@ -195,3 +195,52 @@ func TestCatalogMyUplinkDeclaresAuthPostPath(t *testing.T) { } t.Fatal("myuplink missing from catalog") } + +// A read-only driver's description is its reason only when it says why. The +// Zaptec Cloud description names the product, so it gives none; a Huawei +// description that explains the held-back command keeps that sentence. +func TestLoadCatalogReadOnlyReason(t *testing.T) { + dir := t.TempDir() + files := map[string]string{ + "zaptec_cloud.lua": "DRIVER = {\n id = \"zaptec_cloud\",\n read_only = true,\n" + + " description = \"Zaptec Go / Go 2 / Pro via Zaptec Cloud. Email + password; optional charger serial.\",\n}\n", + "huawei.lua": "DRIVER = {\n id = \"huawei\",\n read_only = true,\n" + + " description = \"Huawei SUN2000 via Modbus TCP. Read-only until a held-zero command is verified on hardware.\",\n}\n", + "wall.lua": "DRIVER = {\n id = \"wall\",\n read_only = true,\n" + + " description = \"Tesla Wall Connector Gen 3 on the LAN. Observation only; no cloud account.\",\n}\n", + // A controlling driver never carries a read-only reason, whatever its + // description says. + "easee_cloud.lua": "DRIVER = {\n id = \"easee_cloud\",\n" + + " description = \"Easee via Cloud. Controls the charging current.\",\n}\n", + } + for name, src := range files { + if err := os.WriteFile(filepath.Join(dir, name), []byte(src), 0644); err != nil { + t.Fatal(err) + } + } + entries, err := LoadCatalog(dir) + if err != nil { + t.Fatalf("LoadCatalog: %v", err) + } + want := map[string]string{ + "zaptec_cloud": "", + "huawei": "Read-only until a held-zero command is verified on hardware.", + "wall": "Observation only; no cloud account.", + "easee_cloud": "", + } + for _, e := range entries { + if got := e.ReadOnlyReason; got != want[e.ID] { + t.Errorf("%s ReadOnlyReason = %q, want %q", e.ID, got, want[e.ID]) + } + } + zaptec, ok := FindCatalogEntry(entries, "drivers/zaptec_cloud.lua") + if !ok || !zaptec.ReadOnly { + t.Fatalf("FindCatalogEntry(zaptec_cloud) = %+v, %v; want the read-only entry", zaptec, ok) + } + if !IsReadOnlyDriver(entries, "zaptec_cloud.lua") || IsReadOnlyDriver(entries, "easee_cloud.lua") { + t.Error("IsReadOnlyDriver must follow FindCatalogEntry") + } + if _, ok := FindCatalogEntry(entries, ""); ok { + t.Error("an empty lua path must match nothing") + } +} diff --git a/web/app.css b/web/app.css index d3708ad01..6302d3985 100644 --- a/web/app.css +++ b/web/app.css @@ -2290,6 +2290,15 @@ body.ftw-app .drv-version-detail { color: var(--fg-muted); font-size: 0.75rem; } +/* FTW reads this device but cannot control it. A plain sentence, set apart + so it is read before anyone relies on the device. */ +body.ftw-app .read-only-note { + border-left: 2px solid var(--accent-e, var(--fg-muted)); + color: var(--fg); + font-size: 0.8rem; + margin: 6px 0 10px; + padding: 2px 0 2px 8px; +} /* What changed in a version: a quiet link beside the prose, not a button. */ body.ftw-app .drv-version-changes { color: var(--accent-e, var(--fg-muted)); diff --git a/web/app.js b/web/app.js index 5b8c9ac4c..e2e463bf1 100644 --- a/web/app.js +++ b/web/app.js @@ -3184,6 +3184,21 @@ } else if (setupNote) { setupNote.remove(); } + // A charger whose driver only reads it: the plan and the charge buttons + // below cannot move it, so the modal says so first. + var readOnlyNote = evModalBody.querySelector(".ev-read-only-note"); + var readOnly = window.FTWDriverControl ? window.FTWDriverControl.loadpointNote(matched) : null; + if (readOnly) { + if (!readOnlyNote) { + readOnlyNote = document.createElement("p"); + readOnlyNote.className = "ev-read-only-note read-only-note"; + readOnlyNote.setAttribute("role", "status"); + evModalBody.insertBefore(readOnlyNote, evModalBody.firstChild); + } + readOnlyNote.textContent = window.FTWDriverControl.sentence(readOnly); + } else if (readOnlyNote) { + readOnlyNote.remove(); + } // Plan view: what the box will do with this car (one sentence + // the planned windows on a 24 h track) and the car's charge level, // which the plan is built from. Mounted once per loadpoint so the diff --git a/web/driver-control.js b/web/driver-control.js new file mode 100644 index 000000000..f97c624e6 --- /dev/null +++ b/web/driver-control.js @@ -0,0 +1,59 @@ +// Whether FTW can control a configured device, in the words every view uses. +// +// Core states the facts: a catalog entry's read_only, capabilities and +// read_only_reason, or a loadpoint's driver_read_only and +// driver_read_only_reason. This file only chooses the words, so Settings, +// setup and the EV card say the same thing. +// +// A new owner added a Zaptec Go through a read-only driver. FTW accepted the +// charger and never controlled it, and nothing on the box said why. +(function () { + 'use strict'; + + // Kinds an owner expects FTW to steer, with the word for each. A battery + // usually sits behind an inverter or a gateway such as Zap, so it is "this + // device". A meter or a solar inverter that FTW only reads is complete, so + // it gets no note. + var KINDS = [ + ['ev', 'charger'], + ['v2x_charger', 'charger'], + ['vehicle', 'car'], + ['heatpump', 'heat pump'], + ['battery', 'device'], + ]; + + // {label, text, reason} for a read-only driver of a kind FTW would + // otherwise control; null for everything else. + function readOnlyNote(entry) { + if (!entry || entry.read_only !== true) return null; + var caps = entry.capabilities || []; + for (var i = 0; i < KINDS.length; i++) { + if (caps.indexOf(KINDS[i][0]) >= 0) { + return { + label: 'reads only', + text: 'FTW reads this ' + KINDS[i][1] + ' but cannot control it.', + reason: typeof entry.read_only_reason === 'string' ? entry.read_only_reason.trim() : '', + }; + } + } + return null; + } + + // The note as one line of text, the driver's own reason last. + function sentence(note) { + if (!note) return ''; + return note.reason ? note.text + ' Its driver says: ' + note.reason : note.text; + } + + // A loadpoint carries the facts under its own names; it is always a charger. + function loadpointNote(lp) { + if (!lp) return null; + return readOnlyNote({ + read_only: lp.driver_read_only === true, + capabilities: ['ev'], + read_only_reason: lp.driver_read_only_reason, + }); + } + + window.FTWDriverControl = { readOnlyNote: readOnlyNote, loadpointNote: loadpointNote, sentence: sentence }; +})(); diff --git a/web/driver-control.test.mjs b/web/driver-control.test.mjs new file mode 100644 index 000000000..b9477fcc6 --- /dev/null +++ b/web/driver-control.test.mjs @@ -0,0 +1,106 @@ +// node --test web/driver-control.test.mjs +// +// Whether FTW can control a device, said where people choose and look at it. +// A new owner added a Zaptec Go through a read-only driver; FTW accepted the +// charger, never controlled it and never said why. + +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { describe, it } from 'node:test'; + +globalThis.window = {}; +await import('./driver-control.js'); +await import('./settings/tabs/loadpoints.js'); +const words = window.FTWDriverControl; +const S = window.FTWSettings; + +const read = path => readFileSync(new URL(path, import.meta.url), 'utf8'); +const escHtml = s => String(s).replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"'); + +describe('the words for a read-only driver', () => { + it('names a charger, a car and a heat pump, and gives the driver its own reason', () => { + const zaptec = words.readOnlyNote({ read_only: true, capabilities: ['ev'] }); + assert.equal(words.sentence(zaptec), 'FTW reads this charger but cannot control it.'); + const wall = words.readOnlyNote({ read_only: true, capabilities: ['ev'], read_only_reason: 'Observation only; no cloud account.' }); + assert.equal(words.sentence(wall), 'FTW reads this charger but cannot control it. Its driver says: Observation only; no cloud account.'); + assert.match(words.sentence(words.readOnlyNote({ read_only: true, capabilities: ['vehicle'] })), /reads this car/); + assert.match(words.sentence(words.readOnlyNote({ read_only: true, capabilities: ['heatpump'] })), /reads this heat pump/); + assert.match(words.sentence(words.readOnlyNote({ read_only: true, capabilities: ['meter', 'pv', 'battery'] })), /reads this device/); + }); + + it('says nothing for a driver that controls, or a meter or solar inverter that is only read', () => { + assert.equal(words.readOnlyNote({ read_only: false, capabilities: ['ev'] }), null); + assert.equal(words.readOnlyNote({ capabilities: ['ev'] }), null); + assert.equal(words.readOnlyNote({ read_only: true, capabilities: ['meter'] }), null); + assert.equal(words.readOnlyNote({ read_only: true, capabilities: ['meter', 'pv'] }), null); + assert.equal(words.readOnlyNote(null), null); + assert.equal(words.sentence(null), ''); + }); + + it('reads a loadpoint by its own field names', () => { + assert.equal(words.loadpointNote({ driver_read_only: false }), null); + assert.equal(words.loadpointNote(null), null); + assert.equal(words.sentence(words.loadpointNote({ driver_read_only: true, driver_read_only_reason: 'Read-only.' })), + 'FTW reads this charger but cannot control it. Its driver says: Read-only.'); + }); +}); + +describe('Settings → Chargers', () => { + const render = config => S.tabs.loadpoints.render({ config, escHtml, help: () => '' }); + const config = () => ({ + drivers: [ + { name: 'zaptec', lua: 'drivers/zaptec_cloud.lua' }, + { name: 'easee', lua: 'drivers/easee_cloud.lua' }, + ], + loadpoints: [{ id: 'drive', driver_name: 'zaptec' }, { id: 'street', driver_name: 'easee' }], + }); + + it('says on the charger card, and in the charger list, that FTW cannot control a read-only charger', () => { + S.ocppStatus = { enabled: false, chargers: [] }; + S.catalogByLua = { + 'drivers/zaptec_cloud.lua': { capabilities: ['ev'], read_only: true }, + 'drivers/easee_cloud.lua': { capabilities: ['ev'] }, + }; + const html = render(config()); + assert.match(html, /data-charger-read-only="0" role="note">FTW reads this charger but cannot control it\.zaptec \(reads only\)<\/option>/); + assert.match(html, /