forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
skoda-enyaq.go
85 lines (67 loc) · 1.86 KB
/
skoda-enyaq.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
83
84
85
package vehicle
import (
"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/skoda/myskoda"
"github.com/evcc-io/evcc/vehicle/skoda/myskoda/service"
)
// https://gitlab.com/prior99/skoda
// Enyaq is an api.Vehicle implementation for Skoda Enyaq cars
type Enyaq struct {
*embed
*myskoda.Provider // provides the api implementations
}
func init() {
registry.Add("enyaq", NewEnyaqFromConfig)
}
// NewEnyaqFromConfig creates a new vehicle
func NewEnyaqFromConfig(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 := &Enyaq{
embed: &cc.embed,
}
var err error
log := util.NewLogger("enyaq").Redact(cc.User, cc.Password, cc.VIN)
// use Skoda api to resolve list of vehicles
ts, err := service.TokenRefreshServiceTokenSource(log, myskoda.TRSParams, myskoda.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
api := myskoda.NewAPI(log, ts)
api.Client.Timeout = cc.Timeout
vehicle, err := ensureVehicleEx(
cc.VIN, api.Vehicles,
func(v myskoda.Vehicle) (string, error) {
return v.VIN, nil
},
)
if err == nil {
vehicle, err = api.VehicleDetails(vehicle.VIN)
}
if err == nil {
v.fromVehicle(vehicle.Name, float64(vehicle.Specification.Battery.CapacityInKWh))
}
// reuse tokenService to build provider
if err == nil {
api := myskoda.NewAPI(log, ts)
api.Client.Timeout = cc.Timeout
v.Provider = myskoda.NewProvider(api, vehicle.VIN, cc.Cache)
}
return v, err
}