forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ford.go
65 lines (50 loc) · 1.26 KB
/
ford.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
package vehicle
import (
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/ford"
)
// https://github.com/d4v3y0rk/ffpass-module
// Ford is an api.Vehicle implementation for Ford cars
type Ford struct {
*embed
*ford.Provider
}
func init() {
registry.Add("ford", NewFordFromConfig)
}
// NewFordFromConfig creates a new vehicle
func NewFordFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, VIN string
Expiry time.Duration
Cache time.Duration
}{
Expiry: expiry,
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.User == "" || cc.Password == "" {
return nil, api.ErrMissingCredentials
}
v := &Ford{
embed: &cc.embed,
}
log := util.NewLogger("ford").Redact(cc.User, cc.Password, cc.VIN)
identity := ford.NewIdentity(log, cc.User, cc.Password)
err := identity.Login()
if err != nil {
return nil, fmt.Errorf("login failed: %w", err)
}
api := ford.NewAPI(log, identity)
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
if err == nil {
v.Provider = ford.NewProvider(api, cc.VIN, cc.Expiry, cc.Cache)
}
return v, err
}