From b437433976a1cefc06bb68ed8825ba2facab7428 Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 18 May 2021 13:37:51 +0200 Subject: [PATCH] SoC: add /api/vehicles (#1048) --- internal/vehicle/config.go | 9 ++++++++ soc/server/ui/ui.go | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/internal/vehicle/config.go b/internal/vehicle/config.go index c6bb15d2c..05b06e983 100644 --- a/internal/vehicle/config.go +++ b/internal/vehicle/config.go @@ -29,6 +29,15 @@ func (r vehicleRegistry) Get(name string) (func(map[string]interface{}) (api.Veh var registry vehicleRegistry = make(map[string]func(map[string]interface{}) (api.Vehicle, error)) +// Types returns the list of vehicle types +func Types() []string { + var res []string + for typ := range registry { + res = append(res, typ) + } + return res +} + // NewFromConfig creates vehicle from configuration func NewFromConfig(typ string, other map[string]interface{}) (v api.Vehicle, err error) { factory, err := registry.Get(strings.ToLower(typ)) diff --git a/soc/server/ui/ui.go b/soc/server/ui/ui.go index 87d840f03..e8074b87b 100644 --- a/soc/server/ui/ui.go +++ b/soc/server/ui/ui.go @@ -12,13 +12,16 @@ import ( "net/http" "net/http/pprof" "os" + "sort" "strings" "time" + "github.com/andig/evcc/internal/vehicle" "github.com/andig/evcc/soc/server/auth" "github.com/andig/evcc/util" "github.com/dgrijalva/jwt-go" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/thoas/go-funk" "golang.org/x/crypto/acme/autocert" "golang.org/x/oauth2" "golang.org/x/oauth2/github" @@ -209,12 +212,53 @@ func getUserInfo(state string, code string) (*User, error) { return &user, nil } +var typeFilter = []string{"custom", "cloud", "niu", "tesla"} + +var typeMap = map[string]string{ + "bmw": "BMW", + "citroen": "Citroën", + "skoda": "Škoda", + "enyaq": "Škoda Enyaq", + "vw": "VW", + "id": "VW ID", +} + +type typeDesc struct { + ID string `json:"id"` + Name string `json:"name"` +} + +func handleVehicles(w http.ResponseWriter, r *http.Request) { + types := make([]typeDesc, 0) + + for _, id := range vehicle.Types() { + if funk.ContainsString(typeFilter, id) { + continue + } + + vt := typeDesc{ID: id, Name: strings.Title(id)} + if name, ok := typeMap[id]; ok { + vt.Name = name + } + + types = append(types, vt) + } + + sort.Slice(types, func(i, j int) bool { + return strings.Compare(types[i].Name, types[j].Name) < 0 + }) + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + _ = json.NewEncoder(w).Encode(types) +} + func Run() { mux := &http.ServeMux{} mux.HandleFunc("/", handleMain) mux.HandleFunc("/privacy", handlePrivacy) mux.HandleFunc("/login", handleLogin) mux.HandleFunc("/callback", handleCallback) + mux.HandleFunc("/api/vehicles", handleVehicles) mux.Handle("/metrics", promhttp.Handler())