Skip to content

Commit

Permalink
Http: add min/max current api (evcc-io#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Oct 4, 2021
1 parent f53b215 commit 006fb56
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
90 changes: 74 additions & 16 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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()})
Expand All @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand All @@ -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) {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)},
}
Expand Down

0 comments on commit 006fb56

Please sign in to comment.