HEMS: published detailed config including max power (#25869)
This commit is contained in:
parent
f365e2099a
commit
c25bf05086
50 changed files with 364 additions and 177 deletions
|
|
@ -25,6 +25,8 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type publisher func(string, any)
|
||||
|
||||
type route struct {
|
||||
Method string
|
||||
Pattern string
|
||||
|
|
@ -221,7 +223,7 @@ func (s *HTTPd) RegisterSiteHandlers(site site.API, valueChan chan<- util.Param)
|
|||
}
|
||||
|
||||
// RegisterSystemHandler provides system level handlers
|
||||
func (s *HTTPd) RegisterSystemHandler(site *core.Site, valueChan chan<- util.Param, cache *util.ParamCache, auth auth.Auth, shutdown func(), configFile string) {
|
||||
func (s *HTTPd) RegisterSystemHandler(site *core.Site, pub publisher, cache *util.ParamCache, auth auth.Auth, shutdown func(), configFile string) {
|
||||
router := s.Server.Handler.(*mux.Router)
|
||||
|
||||
// api
|
||||
|
|
@ -288,8 +290,8 @@ func (s *HTTPd) RegisterSystemHandler(site *core.Site, valueChan chan<- util.Par
|
|||
"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},
|
||||
"updatesponsortoken": {"POST", "/sponsortoken", updateSponsortokenHandler(pub)},
|
||||
"deletesponsortoken": {"DELETE", "/sponsortoken", deleteSponsorTokenHandler(pub)},
|
||||
}
|
||||
|
||||
// yaml handlers
|
||||
|
|
@ -314,8 +316,8 @@ func (s *HTTPd) RegisterSystemHandler(site *core.Site, valueChan chan<- util.Par
|
|||
keys.Shm: func() any { return new(shm.Config) },
|
||||
keys.Influx: func() any { return new(globalconfig.Influx) },
|
||||
} {
|
||||
routes["update"+key] = route{Method: "POST", Pattern: "/" + key, HandlerFunc: settingsSetJsonHandler(key, valueChan, fun)}
|
||||
routes["delete"+key] = route{Method: "DELETE", Pattern: "/" + key, HandlerFunc: settingsDeleteJsonHandler(key, valueChan, fun())}
|
||||
routes["update"+key] = route{Method: "POST", Pattern: "/" + key, HandlerFunc: settingsSetJsonHandler(key, pub, fun)}
|
||||
routes["delete"+key] = route{Method: "DELETE", Pattern: "/" + key, HandlerFunc: settingsDeleteJsonHandler(key, pub, fun())}
|
||||
}
|
||||
|
||||
for _, r := range routes {
|
||||
|
|
|
|||
|
|
@ -9,33 +9,53 @@ import (
|
|||
"github.com/evcc-io/evcc/util/sponsor"
|
||||
)
|
||||
|
||||
func updateSponsortokenHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
func updateSponsortokenHandler(pub publisher) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(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
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if req.Token != "" {
|
||||
if err := sponsor.ConfigureSponsorship(req.Token); err != nil {
|
||||
jsonError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
pub(keys.Sponsor, struct {
|
||||
Status sponsor.Status `json:"status"`
|
||||
FromYaml bool `json:"fromYaml"`
|
||||
}{
|
||||
Status: sponsor.GetStatus(),
|
||||
FromYaml: false,
|
||||
})
|
||||
}
|
||||
|
||||
// TODO find better place
|
||||
settings.SetString(keys.SponsorToken, req.Token)
|
||||
setConfigDirty()
|
||||
|
||||
jsonWrite(w, sponsor.GetStatus())
|
||||
}
|
||||
|
||||
// TODO find better place
|
||||
settings.SetString(keys.SponsorToken, req.Token)
|
||||
setConfigDirty()
|
||||
|
||||
jsonWrite(w, sponsor.Status())
|
||||
}
|
||||
|
||||
func deleteSponsorTokenHandler(w http.ResponseWriter, r *http.Request) {
|
||||
settings.SetString(keys.SponsorToken, "")
|
||||
setConfigDirty()
|
||||
jsonWrite(w, true)
|
||||
func deleteSponsorTokenHandler(pub publisher) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
settings.SetString(keys.SponsorToken, "")
|
||||
|
||||
pub(keys.Sponsor, struct {
|
||||
Status sponsor.Status `json:"status"`
|
||||
FromYaml bool `json:"fromYaml"`
|
||||
}{
|
||||
Status: sponsor.Status{},
|
||||
FromYaml: false,
|
||||
})
|
||||
|
||||
setConfigDirty()
|
||||
jsonWrite(w, true)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/server/db/settings"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/redact"
|
||||
"github.com/gorilla/mux"
|
||||
"go.yaml.in/yaml/v4"
|
||||
|
|
@ -79,7 +78,7 @@ func settingsSetYamlHandler(key string, other, struc any) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func settingsSetJsonHandler(key string, valueChan chan<- util.Param, newStruc func() any) http.HandlerFunc {
|
||||
func settingsSetJsonHandler(key string, pub publisher, newStruc func() any) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
struc := newStruc()
|
||||
dec := json.NewDecoder(r.Body)
|
||||
|
|
@ -103,18 +102,18 @@ func settingsSetJsonHandler(key string, valueChan chan<- util.Param, newStruc fu
|
|||
settings.SetJson(key, struc)
|
||||
setConfigDirty()
|
||||
|
||||
valueChan <- util.Param{Key: key, Val: struc}
|
||||
pub(key, struc)
|
||||
|
||||
jsonWrite(w, true)
|
||||
}
|
||||
}
|
||||
|
||||
func settingsDeleteJsonHandler(key string, valueChan chan<- util.Param, struc any) http.HandlerFunc {
|
||||
func settingsDeleteJsonHandler(key string, pub publisher, struc any) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
settings.SetString(key, "")
|
||||
setConfigDirty()
|
||||
|
||||
valueChan <- util.Param{Key: key, Val: struc}
|
||||
pub(key, struc)
|
||||
|
||||
jsonWrite(w, true)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue