evcc-io/vehicle/vw.go
2021-10-22 20:20:16 +02:00

80 lines
1.8 KiB
Go

package vehicle
import (
"fmt"
"net/url"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/vehicle/vw"
)
// https://github.com/trocotronic/weconnect
// https://github.com/TA2k/ioBroker.vw-connect
// VW is an api.Vehicle implementation for VW cars
type VW struct {
*embed
*vw.Provider // provides the api implementations
}
func init() {
registry.Add("vw", NewVWFromConfig)
}
// NewVWFromConfig creates a new vehicle
func NewVWFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, VIN string
Cache time.Duration
Timeout time.Duration
}{
Cache: interval,
Timeout: request.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
v := &VW{
embed: &cc.embed,
}
log := util.NewLogger("vw")
identity := vw.NewIdentity(log)
query := url.Values(map[string][]string{
"response_type": {"id_token token"},
"client_id": {"9496332b-ea03-4091-a224-8c746b885068@apps_vw-dilab_com"},
"redirect_uri": {"carnet://identity-kit/login"},
"scope": {"openid profile mbb"}, // cars birthdate nickname address phone
})
err := identity.LoginVAG("38761134-34d0-41f3-9a73-c4be88d7d337", query, cc.User, cc.Password)
if err != nil {
return v, fmt.Errorf("login failed: %w", err)
}
api := vw.NewAPI(log, identity, "VW", "DE")
api.Client.Timeout = cc.Timeout
if cc.VIN == "" {
cc.VIN, err = findVehicle(api.Vehicles())
if err == nil {
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
}
}
if err == nil {
if err = api.HomeRegion(strings.ToUpper(cc.VIN)); err == nil {
v.Provider = vw.NewProvider(api, strings.ToUpper(cc.VIN), cc.Cache)
}
}
return v, err
}