forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathford-connect.go
67 lines (52 loc) · 1.38 KB
/
ford-connect.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
package vehicle
import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/ford/connect"
)
// https://developer.ford.com/apis/fordconnect
// FordConnect is an api.Vehicle implementation for Ford cars
type FordConnect struct {
*embed
*connect.Provider
}
func init() {
registry.Add("ford-connect", NewFordConnectFromConfig)
}
// NewFordConnectFromConfig creates a new vehicle
func NewFordConnectFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
Credentials ClientCredentials
Tokens Tokens
VIN string
Cache time.Duration
}{
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
v := &FordConnect{
embed: &cc.embed,
}
if err := cc.Credentials.Error(); err != nil {
return nil, err
}
token, err := cc.Tokens.Token()
if err != nil {
return nil, err
}
log := util.NewLogger("ford").Redact(cc.VIN)
identity := connect.NewIdentity(log, cc.Credentials.ID, cc.Credentials.Secret, token)
api := connect.NewAPI(log, identity)
vehicle, err := ensureVehicleEx(cc.VIN, api.Vehicles, func(v connect.Vehicle) (string, error) {
return api.VIN(v.VehicleID)
})
if err == nil {
v.fromVehicle(vehicle.NickName, 0)
v.Provider = connect.NewProvider(api, vehicle.VehicleID, cc.Cache)
}
return v, err
}