This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vw.go
70 lines (55 loc) · 1.57 KB
/
vw.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
package vehicle
import (
"time"
"github.com/connectorjs/evcm/api"
"github.com/connectorjs/evcm/util"
"github.com/connectorjs/evcm/util/request"
"github.com/connectorjs/evcm/vehicle/vag/service"
"github.com/connectorjs/evcm/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
}
if cc.User == "" || cc.Password == "" {
return nil, api.ErrMissingCredentials
}
v := &VW{
embed: &cc.embed,
}
log := util.NewLogger("vw").Redact(cc.User, cc.Password, cc.VIN)
trs, err := service.TokenRefreshServiceTokenSource(log, vw.TRSParams, vw.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
ts := service.MbbTokenSource(log, trs, vw.AuthClientID)
api := vw.NewAPI(log, ts, vw.Brand, vw.Country)
api.Client.Timeout = cc.Timeout
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
if err == nil {
if err = api.HomeRegion(cc.VIN); err == nil {
v.Provider = vw.NewProvider(api, cc.VIN, cc.Cache)
}
}
return v, err
}