From 006fb56d95fdab955ff561c0ecae63df1163f636 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 4 Oct 2021 08:46:53 +0200 Subject: [PATCH] Http: add min/max current api (#1698) --- README.md | 2 ++ server/http.go | 90 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 750989d0f..ddb8c7d20 100644 --- a/README.md +++ b/README.md @@ -607,6 +607,8 @@ Loadpoint ids for REST API are starting at `0: - `/api/loadpoints//mode`: loadpoint charge mode (writable) - `/api/loadpoints//minsoc`: loadpoint minimum SoC (writable) - `/api/loadpoints//targetsoc`: loadpoint target SoC (writable) +- `/api/loadpoints//mincurrent`: loadpoint minimum current (writable) +- `/api/loadpoints//maxcurrent`: loadpoint maximum current (writable) - `/api/loadpoints//phases`: loadpoint enabled phases (writable) Note: to modify writable settings perform a `POST` request appending the value as path segment. diff --git a/server/http.go b/server/http.go index 6ac4e37f6..d78f6ed39 100644 --- a/server/http.go +++ b/server/http.go @@ -38,6 +38,14 @@ type minSoCJSON struct { MinSoC int `json:"minSoC"` } +type minCurrentJSON struct { + MinCurrent float64 `json:"minCurrent"` +} + +type maxCurrentJSON struct { + MaxCurrent float64 `json:"maxCurrent"` +} + type phasesJSON struct { Phases int `json:"phases"` } @@ -168,9 +176,7 @@ func ChargeModeHandler(lp loadpoint.API) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - modeS := vars["mode"] - - mode, err := api.ChargeModeString(modeS) + mode, err := api.ChargeModeString(vars["value"]) if err != nil { w.WriteHeader(http.StatusBadRequest) jsonResponse(w, r, errorJSON{Error: err.Error()}) @@ -197,9 +203,7 @@ func TargetSoCHandler(lp loadpoint.API) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - socS := vars["soc"] - soc, err := strconv.ParseInt(socS, 10, 32) - + soc, err := strconv.ParseInt(vars["value"], 10, 32) if err == nil { err = lp.SetTargetSoC(int(soc)) } @@ -228,9 +232,7 @@ func MinSoCHandler(lp loadpoint.API) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - socS := vars["soc"] - soc, err := strconv.ParseInt(socS, 10, 32) - + soc, err := strconv.ParseInt(vars["value"], 10, 32) if err == nil { err = lp.SetMinSoC(int(soc)) } @@ -246,6 +248,60 @@ func MinSoCHandler(lp loadpoint.API) http.HandlerFunc { } } +// CurrentMinCurrentHandler returns current minimum current +func CurrentMinCurrentHandler(lp loadpoint.API) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + res := minCurrentJSON{MinCurrent: lp.GetMinCurrent()} + jsonResponse(w, r, res) + } +} + +// MinCurrentHandler updates minimum current +func MinCurrentHandler(lp loadpoint.API) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + current, err := strconv.ParseFloat(vars["value"], 64) + if err == nil { + lp.SetMinCurrent(current) + } else { + w.WriteHeader(http.StatusBadRequest) + jsonResponse(w, r, errorJSON{Error: err.Error()}) + return + } + + res := minCurrentJSON{MinCurrent: lp.GetMinCurrent()} + jsonResponse(w, r, res) + } +} + +// CurrentMaxCurrentHandler returns current maximum current +func CurrentMaxCurrentHandler(lp loadpoint.API) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + res := maxCurrentJSON{MaxCurrent: lp.GetMaxCurrent()} + jsonResponse(w, r, res) + } +} + +// MaxCurrentHandler updates maximum current +func MaxCurrentHandler(lp loadpoint.API) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + current, err := strconv.ParseFloat(vars["value"], 64) + if err == nil { + lp.SetMaxCurrent(current) + } else { + w.WriteHeader(http.StatusBadRequest) + jsonResponse(w, r, errorJSON{Error: err.Error()}) + return + } + + res := maxCurrentJSON{MaxCurrent: lp.GetMaxCurrent()} + jsonResponse(w, r, res) + } +} + // CurrentPhasesHandler returns current minimum soc func CurrentPhasesHandler(lp loadpoint.API) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -259,9 +315,7 @@ func PhasesHandler(lp loadpoint.API) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - phasesS := vars["phases"] - phases, err := strconv.ParseInt(phasesS, 10, 32) - + phases, err := strconv.ParseInt(vars["value"], 10, 32) if err == nil { err = lp.SetPhases(int(phases)) } @@ -412,13 +466,17 @@ func NewHTTPd(url string, site site.API, hub *SocketHub, cache *util.Cache) *HTT routes := map[string]route{ "getmode": {[]string{"GET"}, "/mode", CurrentChargeModeHandler(lp)}, - "setmode": {[]string{"POST", "OPTIONS"}, "/mode/{mode:[a-z]+}", ChargeModeHandler(lp)}, + "setmode": {[]string{"POST", "OPTIONS"}, "/mode/{value:[a-z]+}", ChargeModeHandler(lp)}, "gettargetsoc": {[]string{"GET"}, "/targetsoc", CurrentTargetSoCHandler(lp)}, - "settargetsoc": {[]string{"POST", "OPTIONS"}, "/targetsoc/{soc:[0-9]+}", TargetSoCHandler(lp)}, + "settargetsoc": {[]string{"POST", "OPTIONS"}, "/targetsoc/{value:[0-9]+}", TargetSoCHandler(lp)}, "getminsoc": {[]string{"GET"}, "/minsoc", CurrentMinSoCHandler(lp)}, - "setminsoc": {[]string{"POST", "OPTIONS"}, "/minsoc/{soc:[0-9]+}", MinSoCHandler(lp)}, + "setminsoc": {[]string{"POST", "OPTIONS"}, "/minsoc/{value:[0-9]+}", MinSoCHandler(lp)}, + "getmincurrent": {[]string{"GET"}, "/mincurrent", CurrentMinCurrentHandler(lp)}, + "setmincurrent": {[]string{"POST", "OPTIONS"}, "/mincurrent/{value:[0-9]+}", MinCurrentHandler(lp)}, + "getmaxcurrent": {[]string{"GET"}, "/maxcurrent", CurrentMaxCurrentHandler(lp)}, + "setmaxcurrent": {[]string{"POST", "OPTIONS"}, "/maxcurrent/{value:[0-9]+}", MaxCurrentHandler(lp)}, "getphases": {[]string{"GET"}, "/phases", CurrentPhasesHandler(lp)}, - "setphases": {[]string{"POST", "OPTIONS"}, "/phases/{phases:[0-9]+}", PhasesHandler(lp)}, + "setphases": {[]string{"POST", "OPTIONS"}, "/phases/{value:[0-9]+}", PhasesHandler(lp)}, "settargetcharge": {[]string{"POST", "OPTIONS"}, "/targetcharge/{soc:[0-9]+}/{time:[0-9TZ:-]+}", TargetChargeHandler(lp)}, "remotedemand": {[]string{"POST", "OPTIONS"}, "/remotedemand/{demand:[a-z]+}/{source::[0-9a-zA-Z_-]+}", RemoteDemandHandler(lp)}, }