Improve target charging and refactor loadpoint and site apis (#1470)

This commit is contained in:
andig 2021-09-02 14:16:05 +02:00 • committed by GitHub
parent 7f2eddeab1
commit 3986b75b08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 204 additions and 180 deletions

View file

@ -11,7 +11,8 @@ import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/test"
"github.com/gorilla/handlers"
@ -61,7 +62,7 @@ func routeLogger(inner http.Handler) http.HandlerFunc {
}
}
func indexHandler(site core.SiteAPI) http.HandlerFunc {
func indexHandler(site site.API) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
@ -102,7 +103,7 @@ func jsonResponse(w http.ResponseWriter, r *http.Request, content interface{}) {
}
// HealthHandler returns current charge mode
func HealthHandler(site core.SiteAPI) http.HandlerFunc {
func HealthHandler(site site.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !site.Healthy() {
w.WriteHeader(http.StatusInternalServerError)
@ -155,15 +156,15 @@ func StateHandler(cache *util.Cache) http.HandlerFunc {
}
// CurrentChargeModeHandler returns current charge mode
func CurrentChargeModeHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func CurrentChargeModeHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res := chargeModeJSON{Mode: loadpoint.GetMode()}
res := chargeModeJSON{Mode: lp.GetMode()}
jsonResponse(w, r, res)
}
}
// ChargeModeHandler updates charge mode
func ChargeModeHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func ChargeModeHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@ -176,23 +177,23 @@ func ChargeModeHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
return
}
loadpoint.SetMode(mode)
lp.SetMode(mode)
res := chargeModeJSON{Mode: loadpoint.GetMode()}
res := chargeModeJSON{Mode: lp.GetMode()}
jsonResponse(w, r, res)
}
}
// CurrentTargetSoCHandler returns current target soc
func CurrentTargetSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func CurrentTargetSoCHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res := targetSoCJSON{TargetSoC: loadpoint.GetTargetSoC()}
res := targetSoCJSON{TargetSoC: lp.GetTargetSoC()}
jsonResponse(w, r, res)
}
}
// TargetSoCHandler updates target soc
func TargetSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func TargetSoCHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@ -200,7 +201,7 @@ func TargetSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
soc, err := strconv.ParseInt(socS, 10, 32)
if err == nil {
err = loadpoint.SetTargetSoC(int(soc))
err = lp.SetTargetSoC(int(soc))
}
if err != nil {
@ -209,21 +210,21 @@ func TargetSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
return
}
res := targetSoCJSON{TargetSoC: loadpoint.GetTargetSoC()}
res := targetSoCJSON{TargetSoC: lp.GetTargetSoC()}
jsonResponse(w, r, res)
}
}
// CurrentMinSoCHandler returns current minimum soc
func CurrentMinSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func CurrentMinSoCHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res := minSoCJSON{MinSoC: loadpoint.GetMinSoC()}
res := minSoCJSON{MinSoC: lp.GetMinSoC()}
jsonResponse(w, r, res)
}
}
// MinSoCHandler updates minimum soc
func MinSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func MinSoCHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@ -231,7 +232,7 @@ func MinSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
soc, err := strconv.ParseInt(socS, 10, 32)
if err == nil {
err = loadpoint.SetMinSoC(int(soc))
err = lp.SetMinSoC(int(soc))
}
if err != nil {
@ -240,21 +241,21 @@ func MinSoCHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
return
}
res := minSoCJSON{MinSoC: loadpoint.GetMinSoC()}
res := minSoCJSON{MinSoC: lp.GetMinSoC()}
jsonResponse(w, r, res)
}
}
// CurrentPhasesHandler returns current minimum soc
func CurrentPhasesHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func CurrentPhasesHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res := phasesJSON{Phases: loadpoint.GetPhases()}
res := phasesJSON{Phases: lp.GetPhases()}
jsonResponse(w, r, res)
}
}
// PhasesHandler updates minimum soc
func PhasesHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func PhasesHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@ -262,7 +263,7 @@ func PhasesHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
phases, err := strconv.ParseInt(phasesS, 10, 32)
if err == nil {
err = loadpoint.SetPhases(int(phases))
err = lp.SetPhases(int(phases))
}
if err != nil {
@ -271,13 +272,13 @@ func PhasesHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
return
}
res := phasesJSON{Phases: loadpoint.GetPhases()}
res := phasesJSON{Phases: lp.GetPhases()}
jsonResponse(w, r, res)
}
}
// RemoteDemandHandler updates minimum soc
func RemoteDemandHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func RemoteDemandHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@ -288,7 +289,7 @@ func RemoteDemandHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
source, ok = vars["source"]
}
demand, err := core.RemoteDemandString(demandS)
demand, err := loadpoint.RemoteDemandString(demandS)
if !ok || err != nil {
w.WriteHeader(http.StatusBadRequest)
@ -296,11 +297,11 @@ func RemoteDemandHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
return
}
loadpoint.RemoteControl(source, demand)
lp.RemoteControl(source, demand)
res := struct {
Demand core.RemoteDemand `json:"demand"`
Source string `json:"source"`
Demand loadpoint.RemoteDemand `json:"demand"`
Source string `json:"source"`
}{
Source: source,
Demand: demand,
@ -321,7 +322,7 @@ func timezone() *time.Location {
}
// TargetChargeHandler updates target soc
func TargetChargeHandler(loadpoint core.LoadPointAPI) http.HandlerFunc {
func TargetChargeHandler(loadpoint loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@ -369,7 +370,7 @@ type HTTPd struct {
}
// NewHTTPd creates HTTP server with configured routes for loadpoint
func NewHTTPd(url string, site core.SiteAPI, hub *SocketHub, cache *util.Cache) *HTTPd {
func NewHTTPd(url string, site site.API, hub *SocketHub, cache *util.Cache) *HTTPd {
routes := map[string]route{
"health": {[]string{"GET"}, "/health", HealthHandler(site)},
"state": {[]string{"GET"}, "/state", StateHandler(cache)},

View file

@ -5,7 +5,7 @@ import (
"sync"
"time"
"github.com/evcc-io/evcc/core"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/util"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
influxlog "github.com/influxdata/influxdb-client-go/v2/log"
@ -73,7 +73,7 @@ func (m *Influx) supportedType(p util.Param) bool {
}
// Run Influx publisher
func (m *Influx) Run(loadPoints []core.LoadPointAPI, in <-chan util.Param) {
func (m *Influx) Run(loadPoints []loadpoint.API, in <-chan util.Param) {
writer := m.client.WriteAPI(m.org, m.database)
// log errors

View file

@ -6,7 +6,8 @@ import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/provider/mqtt"
"github.com/evcc-io/evcc/util"
)
@ -64,7 +65,7 @@ func (m *MQTT) publish(topic string, retained bool, payload interface{}) {
m.publishSingleValue(topic, retained, payload)
}
func (m *MQTT) listenSetters(topic string, apiHandler core.LoadPointAPI) {
func (m *MQTT) listenSetters(topic string, apiHandler loadpoint.API) {
m.Handler.Listen(topic+"/mode/set", func(payload string) {
apiHandler.SetMode(api.ChargeMode(payload))
})
@ -89,7 +90,7 @@ func (m *MQTT) listenSetters(topic string, apiHandler core.LoadPointAPI) {
}
// Run starts the MQTT publisher for the MQTT API
func (m *MQTT) Run(site core.SiteAPI, in <-chan util.Param) {
func (m *MQTT) Run(site site.API, in <-chan util.Param) {
// alive
topic := fmt.Sprintf("%s/status", m.root)
m.publish(topic, true, "online")

View file

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package server
@ -7,7 +8,7 @@ import (
"net/http"
"os"
"github.com/evcc-io/evcc/core"
"github.com/evcc-io/evcc/core/site"
)
// SocketPath is the unix domain socket path
@ -26,7 +27,7 @@ func remoteIfExists(file string) {
}
// HealthListener attaches listener to unix domain socket and runs listener
func HealthListener(site core.SiteAPI) {
func HealthListener(site site.API) {
remoteIfExists(SocketPath)
l, err := net.Listen("unix", SocketPath)