Repository navigation
fix(modbus): give external clients exclusive write ownership #1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "ftw": patch | ||
| --- | ||
|
|
||
| Keep one Modbus writer per device. The proxy defaults to read only. When external writes are enabled, FTW keeps reading but stops all device writes until the owner changes the setting and restarts Core. Restrict proxy requests to configured unit IDs, give driver traffic priority, and close idle clients on shutdown. | ||
|
|
||
| Rename the Home Assistant settings tab to Integrations. Home Assistant MQTT and the Modbus proxy each keep their own section. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/srcfl/ftw/go/internal/config" | ||
| ) | ||
|
|
||
| func TestExternalModbusOwnerKeepsTelemetryButLeavesPlannerAndDispatch(t *testing.T) { | ||
| cfg := &config.Config{ | ||
| ModbusProxy: &config.ModbusProxy{Enabled: true, AllowWrite: true}, | ||
| Drivers: []config.Driver{{Name: "sungrow", Lua: "drivers/sungrow.lua", BatteryCapacityWh: 10000, | ||
| Capabilities: config.Capabilities{Modbus: &config.ModbusConfig{Host: "127.0.0.1", Port: 502, UnitID: 1}}}}, | ||
| } | ||
| cfg.PinModbusProxyOwnership() | ||
| if len(driverCapacitiesFrom(cfg.ModbusControlDrivers(), nil, evCatalog(), true)) != 0 { | ||
| t.Fatal("external battery remains in planner pool") | ||
| } | ||
| if got := driverCapacitiesFrom(cfg.Drivers, nil, evCatalog(), false); got["sungrow"] != 10000 { | ||
| t.Fatalf("telemetry capacity lost: %v", got) | ||
| } | ||
| if len(driverLimitsFrom(cfg.ModbusControlDrivers(), cfg.Batteries)) != 0 { | ||
| t.Fatal("external battery remains in dispatch limits") | ||
| } | ||
| if !config.ObserveOnlyDriverSet(cfg)["sungrow"] { | ||
| t.Fatal("watchdog does not see read-only owner") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/srcfl/ftw/go/internal/config" | ||
| ) | ||
|
|
||
| func TestDriverControlReportsExternalModbusOwnership(t *testing.T) { | ||
| srv, _ := controlServerWithLuaConfig(t, controlProbeLua, config.Driver{ | ||
| Name: "heat", ObserveOnly: true, | ||
| Capabilities: config.Capabilities{Modbus: &config.ModbusConfig{Host: "127.0.0.1", Port: 502, UnitID: 1}}, | ||
| }) | ||
| // The setting belongs to the endpoint; it is not a saved observe_only | ||
| // flag on one driver that another driver could omit. | ||
| srv.deps.Cfg.Drivers[0].ObserveOnly = false | ||
| srv.deps.Cfg.ModbusProxy = &config.ModbusProxy{Enabled: true, AllowWrite: true} | ||
| srv.deps.Cfg.PinModbusProxyOwnership() | ||
| rec := post(t, srv, "/api/drivers/heat/control", `{"control":"set_offset","value":2,"duration_s":600}`) | ||
| if rec.Code != http.StatusForbidden { | ||
| t.Fatalf("external owner POST = %d: %s", rec.Code, rec.Body.String()) | ||
| } | ||
| srv.deps.Cfg.ModbusProxy.AllowWrite = false | ||
| rec = post(t, srv, "/api/drivers/heat/control", `{"control":"set_offset","value":2,"duration_s":600}`) | ||
| if rec.Code != http.StatusForbidden { | ||
| t.Fatalf("save returned ownership before restart: %d", rec.Code) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func proxyOwnershipConfig() *Config { | ||
| return &Config{ | ||
| ModbusProxy: &ModbusProxy{Enabled: true, AllowWrite: true}, | ||
| Drivers: []Driver{ | ||
| {Name: "battery", BatteryCapacityWh: 10000, Capabilities: Capabilities{Modbus: &ModbusConfig{Host: "10.0.0.1", UnitID: 1}}}, | ||
| {Name: "meter", Capabilities: Capabilities{Modbus: &ModbusConfig{Host: "10.0.0.1", Port: 502, UnitID: 2}}}, | ||
| {Name: "mqtt", Capabilities: Capabilities{MQTT: &MQTTConfig{Host: "10.0.0.2"}}}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestModbusProxyOwnershipSurvivesSavedModeChanges(t *testing.T) { | ||
| cfg := proxyOwnershipConfig() | ||
| cfg.PinModbusProxyOwnership() | ||
| cfg.ModbusProxy.AllowWrite = false | ||
| if got := ObserveOnlyDriverSet(cfg); !got["battery"] || !got["meter"] || got["mqtt"] { | ||
| t.Fatalf("active ownership changed before restart: %v", got) | ||
| } | ||
| runtime := cfg.ModbusControlDrivers() | ||
| if !runtime[0].ObserveOnly || !runtime[1].ObserveOnly || runtime[2].ObserveOnly { | ||
| t.Fatalf("runtime drivers: %+v", runtime) | ||
| } | ||
| if cfg.Drivers[0].ObserveOnly || cfg.Drivers[1].ObserveOnly { | ||
| t.Fatal("derived flags changed saved settings") | ||
| } | ||
| incoming := proxyOwnershipConfig() | ||
| incoming.ModbusProxy.Enabled = false | ||
| incoming.PreserveMaskedSecrets(cfg) | ||
| if !incoming.ModbusProxyOwnsWrites(incoming.Drivers[0]) { | ||
| t.Fatal("settings save lost active ownership") | ||
| } | ||
| incoming.PinModbusProxyOwnership() | ||
| if len(ObserveOnlyDriverSet(incoming)) != 0 { | ||
| t.Fatal("restart did not return ownership to FTW") | ||
| } | ||
| } | ||
|
|
||
| func TestModbusProxyReadOnlyDoesNotTakeControlOnSave(t *testing.T) { | ||
| cfg := proxyOwnershipConfig() | ||
| cfg.ModbusProxy.AllowWrite = false | ||
| cfg.PinModbusProxyOwnership() | ||
| cfg.ModbusProxy.AllowWrite = true | ||
| if len(ObserveOnlyDriverSet(cfg)) != 0 { | ||
| t.Fatal("write ownership changed before restart") | ||
| } | ||
| cfg.PinModbusProxyOwnership() | ||
| if !cfg.ModbusProxyOwnsWrites(cfg.Drivers[0]) { | ||
| t.Fatal("restart did not apply external ownership") | ||
| } | ||
| } | ||
|
|
||
| func TestModbusProxyAllowlistAndRestartFollowEffectiveEndpoints(t *testing.T) { | ||
| cfg := proxyOwnershipConfig() | ||
| binds, err := cfg.ModbusProxyBinds() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(binds) != 1 || !reflect.DeepEqual(binds[0].UnitIDs, []uint8{1, 2}) { | ||
| t.Fatalf("binds: %+v", binds) | ||
| } | ||
| other := proxyOwnershipConfig() | ||
| other.Drivers[0], other.Drivers[1] = other.Drivers[1], other.Drivers[0] | ||
| if got := RestartRequiredFor(cfg, other); len(got) != 0 { | ||
| t.Fatalf("driver order changed effective listener: %v", got) | ||
| } | ||
| other.Drivers[0].Capabilities.Modbus.UnitID = 3 | ||
| if got := RestartRequiredFor(cfg, other); len(got) == 0 { | ||
| t.Fatal("unit allowlist change needs restart") | ||
| } | ||
| } | ||
|
|
||
| func TestUnusedModbusProxySettingsDoNotRequireRestart(t *testing.T) { | ||
| old := &Config{} | ||
| newCfg := &Config{ModbusProxy: &ModbusProxy{Listen: ":1502"}} | ||
| if got := RestartRequiredFor(old, newCfg); len(got) != 0 { | ||
| t.Fatalf("disabled defaults require restart: %v", got) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an externally owned Modbus inverter also has
supports_pv_curtail, onlyDriverLimitsuses the ownership-filtered drivers;SupportsPVCurtailstill advertises that inverter as controllable, and the hot-reload path repeats the same pattern. During an export-ceiling breach,computePVExportGuardtherefore counts the proposed cap as a real reduction and can report the residual as cleared, while the registry rejects the command as observe-only and nothing reaches the device. Exclude externally owned drivers when building the PV-curtail map so the guard reports the export as uncontrollable rather than hiding the unmet clamp.AGENTS.md reference: AGENTS.md:L22-L23
Useful? React with 👍 / 👎.