Http: add min/max current api (#1698)

This commit is contained in:
andig 2021-10-04 08:46:53 +02:00 • committed by GitHub
parent f53b215ce5
commit 006fb56d95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 16 deletions

View file

@ -607,6 +607,8 @@ Loadpoint ids for REST API are starting at `0:
- `/api/loadpoints/<id>/mode`: loadpoint charge mode (writable)
- `/api/loadpoints/<id>/minsoc`: loadpoint minimum SoC (writable)
- `/api/loadpoints/<id>/targetsoc`: loadpoint target SoC (writable)
- `/api/loadpoints/<id>/mincurrent`: loadpoint minimum current (writable)
- `/api/loadpoints/<id>/maxcurrent`: loadpoint maximum current (writable)
- `/api/loadpoints/<id>/phases`: loadpoint enabled phases (writable)
Note: to modify writable settings perform a `POST` request appending the value as path segment.

View file

@ -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)},
}