forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjlr.go
117 lines (93 loc) Β· 2.78 KB
/
jlr.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package vehicle
import (
"fmt"
"net/http"
"net/url"
"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/jlr"
"github.com/google/uuid"
)
// https://github.com/ardevd/jlrpy
// JLR is an api.Vehicle implementation for Jaguar LandRover cars
type JLR struct {
*embed
*jlr.Provider
}
func init() {
registry.Add("jaguar", NewJLRFromConfig)
registry.Add("landrover", NewJLRFromConfig)
}
// NewJLRFromConfig creates a new vehicle
func NewJLRFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, VIN string
DeviceID 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 := &JLR{
embed: &cc.embed,
}
log := util.NewLogger("jlr").Redact(cc.User, cc.Password, cc.VIN, cc.DeviceID)
if cc.DeviceID == "" {
uid := uuid.New()
cc.DeviceID = uid.String()
log.WARN.Println("new device id generated, add `deviceid` to config:", cc.DeviceID)
}
identity := jlr.NewIdentity(log, cc.User, cc.Password, cc.DeviceID)
token, err := identity.Login()
if err != nil {
return nil, fmt.Errorf("login failed: %w", err)
}
if err := v.RegisterDevice(log, cc.User, cc.DeviceID, token); err != nil {
return nil, fmt.Errorf("device registry failed: %w", err)
}
api := jlr.NewAPI(log, cc.DeviceID, identity)
user, err := api.User(cc.User)
if err != nil {
return nil, fmt.Errorf("login failed: %w", err)
}
cc.VIN, err = ensureVehicle(cc.VIN, func() ([]string, error) {
return api.Vehicles(user.UserId)
})
if err == nil {
v.Provider = jlr.NewProvider(api, cc.VIN, user.UserId, cc.Cache)
}
return v, err
}
func (v *JLR) RegisterDevice(log *util.Logger, user, device string, t jlr.Token) error {
c := request.NewHelper(log)
data := map[string]string{
"access_token": t.AccessToken,
"authorization_token": t.AuthToken,
"expires_in": "86400",
"deviceID": device,
}
uri := fmt.Sprintf("%s/users/%s/clients", jlr.IFOP_BASE_URL, url.PathEscape(user))
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), map[string]string{
"Authorization": "Bearer " + t.AccessToken,
"Content-type": "application/json",
"Accept": "application/json",
"X-Device-Id": device,
"x-telematicsprogramtype": "jlrpy",
"x-App-Id": "ICR_JAGUAR_ANDROID",
"x-App-Secret": "7bf6f544-1926-4714-8066-ceceb40d538d",
})
if err == nil {
_, err = c.DoBody(req)
}
return err
}