Skip to content
Closed
Show file tree
Hide file tree
Changes from 11 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
6 changes: 3 additions & 3 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ func (s *HTTPd) RegisterSystemHandler(site *core.Site, valueChan chan<- util.Par
"deletedevice": {"DELETE", "/devices/{class:[a-z]+}/{id:[0-9.]+}", deleteDeviceHandler(site)},
"testconfig": {"POST", "/test/{class:[a-z]+}", testConfigHandler},
"testmerged": {"POST", "/test/{class:[a-z]+}/merge/{id:[0-9.]+}", testConfigHandler},
"interval": {"POST", "/interval/{value:[0-9.]+}", settingsSetDurationHandler(keys.Interval)},
"updatesponsortoken": {"POST", "/sponsortoken", updateSponsortokenHandler},
"deletesponsortoken": {"DELETE", "/sponsortoken", deleteSponsorTokenHandler},
"interval": {"POST", "/interval/{value:[0-9.]+}", settingsSetDurationHandler(keys.Interval, valueChan)},
"updatesponsortoken": {"POST", "/sponsortoken", updateSponsortokenHandler(valueChan)},
"deletesponsortoken": {"DELETE", "/sponsortoken", deleteSponsorTokenHandler(valueChan)},
}

// yaml handlers
Expand Down
54 changes: 33 additions & 21 deletions server/http_config_site_other_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,48 @@ import (

"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/sponsor"
)

func updateSponsortokenHandler(w http.ResponseWriter, r *http.Request) {
var req struct {
Token string `json:"token"`
}

if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
func updateSponsortokenHandler(valueChan chan<- util.Param) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Token string `json:"token"`
}

if req.Token != "" {
if err := sponsor.ConfigureSponsorship(req.Token); err != nil {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
sponsor.SetFromYaml(false)
}

// TODO find better place
settings.SetString(keys.SponsorToken, req.Token)
setConfigDirty()
if req.Token != "" {
if err := sponsor.ConfigureSponsorship(req.Token); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
sponsor.SetFromYaml(false)
}

// TODO find better place
settings.SetString(keys.SponsorToken, req.Token)
Copy link
Member

Choose a reason for hiding this comment

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

Imho we should move this and the channel handling into a sponsor function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can refactor this into a helper function. Would you prefer a function in the sponsor package that handles both the database save and websocket publish, or a local helper in the handler file?

Copy link
Member

Choose a reason for hiding this comment

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

@copilot move this saving and publishing logic to the sponsor package. Pass uiChan to package on init.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 5aaa62d. Moved sponsor token saving and publishing logic to the sponsor package:

  • Added sponsor.Init(valueChan) called in RegisterSystemHandler
  • Created sponsor.SaveToken() and sponsor.DeleteToken() functions that handle database persistence and websocket publishing
  • Updated handlers to use these new functions
  • Removed valueChan parameter from sponsor handlers

setConfigDirty()

status := sponsor.Status()
valueChan <- util.Param{Key: keys.Sponsor, Val: status}

jsonWrite(w, sponsor.Status())
jsonWrite(w, status)
}
}

func deleteSponsorTokenHandler(w http.ResponseWriter, r *http.Request) {
settings.SetString(keys.SponsorToken, "")
setConfigDirty()
jsonWrite(w, true)
func deleteSponsorTokenHandler(valueChan chan<- util.Param) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
settings.SetString(keys.SponsorToken, "")
setConfigDirty()

status := sponsor.Status()
valueChan <- util.Param{Key: keys.Sponsor, Val: status}

jsonWrite(w, true)
}
}
4 changes: 3 additions & 1 deletion server/http_global_settings_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func settingsDeleteHandler(key string) http.HandlerFunc {
}
}

func settingsSetDurationHandler(key string) http.HandlerFunc {
func settingsSetDurationHandler(key string, valueChan chan<- util.Param) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

Expand All @@ -41,6 +41,8 @@ func settingsSetDurationHandler(key string) http.HandlerFunc {
settings.SetInt(key, int64(time.Second*time.Duration(val)))
setConfigDirty()

valueChan <- util.Param{Key: key, Val: val}

jsonWrite(w, val)
}
}
Expand Down
68 changes: 68 additions & 0 deletions tests/config-control.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { test, expect } from "@playwright/test";
import { start, stop, baseUrl } from "./evcc";
import { enableExperimental, expectModalVisible, expectModalHidden } from "./utils";

test.use({ baseURL: baseUrl() });

test.afterEach(async () => {
await stop();
});

test.describe("control settings (interval)", () => {
test("interval is immediately visible after save without restart", async ({ page }) => {
await start();
await page.goto("/#/config");
await enableExperimental(page);

const entry = page.getByTestId("generalconfig-control");
await expect(entry).toContainText("10 s");

await entry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("control-modal");
await expectModalVisible(modal);

const intervalInput = modal.getByLabel("Interval");
await expect(intervalInput).toHaveValue("10");
await intervalInput.fill("20");

await modal.getByRole("button", { name: "Save" }).click();

await expectModalHidden(modal);

await expect(page.getByTestId("restart-needed")).toBeVisible();

await expect(entry).toContainText("20 s");
await expect(entry).not.toContainText("10 s");

await entry.getByRole("button", { name: "Edit" }).click();
await expectModalVisible(modal);
await expect(intervalInput).toHaveValue("20");
});

test("residual power is immediately visible after save", async ({ page }) => {
await start();
await page.goto("/#/config");
await enableExperimental(page);

const entry = page.getByTestId("generalconfig-control");
await entry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("control-modal");
await expectModalVisible(modal);

const residualPowerInput = modal.getByLabel("Residual power");
const initialValue = await residualPowerInput.inputValue();

await residualPowerInput.fill("200");

await modal.getByRole("button", { name: "Save" }).click();

await expectModalHidden(modal);

await entry.getByRole("button", { name: "Edit" }).click();
await expectModalVisible(modal);
await expect(residualPowerInput).toHaveValue("200");
await expect(residualPowerInput).not.toHaveValue(initialValue);
});
});
16 changes: 7 additions & 9 deletions tests/sponsor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ test.describe("sponsor token", () => {
await expect(page.getByTestId("fatal-error")).toContainText("sponsorship");

// Open sponsor modal
const sponsorEntry = page.getByTestId("generalconfig-sponsoring");
await expect(sponsorEntry).toContainText("invalid");
await sponsorEntry.getByRole("button", { name: "Edit" }).click();
const entry = page.getByTestId("generalconfig-sponsoring");
await expect(entry).toContainText("invalid");
await entry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("sponsor-modal");
const tokenInput = modal.getByRole("textbox", { name: "Your token" });
Expand Down Expand Up @@ -63,17 +63,15 @@ test.describe("sponsor token", () => {
await page.goto("/#/config");
await enableExperimental(page);

// Open sponsor modal and enter token
await page
.getByTestId("generalconfig-sponsoring")
.getByRole("button", { name: "Edit" })
.click();
const entry = page.getByTestId("generalconfig-sponsoring");
await expect(entry).toContainText("---");

await entry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("sponsor-modal");
const textarea = modal.getByRole("textbox", { name: "Enter your token" });

await textarea.fill(EXPIRED_TOKEN);
// Try to save to trigger validation
await modal.getByRole("button", { name: "Save" }).click();
await expect(modal).toContainText("token is expired");
});
Expand Down
Loading