forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseat-cupra.go
82 lines (66 loc) · 1.88 KB
/
seat-cupra.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package vehicle
import (
"context"
"fmt"
"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/seat"
"github.com/evcc-io/evcc/vehicle/seat/cupra"
"github.com/evcc-io/evcc/vehicle/vag/tokenrefreshservice"
"github.com/evcc-io/evcc/vehicle/vag/vwidentity"
"golang.org/x/oauth2"
)
// Cupra is an api.Vehicle implementation for Seat Cupra cars
type Cupra struct {
*embed
*cupra.Provider // provides the api implementations
}
func init() {
registry.Add("cupra", NewCupraFromConfig)
}
// NewCupraFromConfig creates a new vehicle
func NewCupraFromConfig(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 := &Cupra{
embed: &cc.embed,
}
log := util.NewLogger("cupra").Redact(cc.User, cc.Password, cc.VIN)
// get initial VW identity id_token
q, err := vwidentity.Login(log, seat.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
trs := tokenrefreshservice.New(log, seat.TRSParams)
token, err := trs.Exchange(q)
if err != nil {
return nil, err
}
ts := trs.TokenSource(token)
// get OIDC user information
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))
ui, err := vwidentity.Config.NewProvider(ctx).UserInfo(ctx, ts)
if err != nil {
return nil, fmt.Errorf("failed getting user information: %w", err)
}
api := cupra.NewAPI(log, ts)
cc.VIN, err = ensureVehicle(cc.VIN, func() ([]string, error) {
return api.Vehicles(ui.Subject)
})
if err == nil {
v.Provider = cupra.NewProvider(api, ui.Subject, cc.VIN, cc.Cache)
}
return v, err
}