-
-
Notifications
You must be signed in to change notification settings - Fork 524
/
bmw.go
136 lines (112 loc) Β· 3.35 KB
/
bmw.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package vehicle
import (
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
)
const (
bmwAuth = "https://customer.bmwgroup.com/gcdm/oauth/authenticate"
bmwAPI = "https://www.bmw-connecteddrive.com/api"
)
type bmwDynamicResponse struct {
AttributesMap struct {
ChargingLevelHv float64 `json:"chargingLevelHv,string"`
}
}
// BMW is an api.Vehicle implementation for BMW cars
type BMW struct {
*embed
*api.HTTPHelper
user, password, vin string
token string
tokenValid time.Time
chargeStateG provider.FloatGetter
}
// NewBMWFromConfig creates a new vehicle
func NewBMWFromConfig(log *api.Logger, other map[string]interface{}) api.Vehicle {
cc := struct {
Title string
Capacity int64
User, Password, VIN string
Cache time.Duration
}{}
api.DecodeOther(log, other, &cc)
v := &BMW{
embed: &embed{cc.Title, cc.Capacity},
HTTPHelper: api.NewHTTPHelper(api.NewLogger("bmw ")),
user: cc.User,
password: cc.Password,
vin: cc.VIN,
}
v.chargeStateG = provider.NewCached(log, v.chargeState, cc.Cache).FloatGetter()
return v
}
func (v *BMW) login(user, password string) error {
data := url.Values{
"username": []string{user},
"password": []string{password},
"client_id": []string{"dbf0a542-ebd1-4ff0-a9a7-55172fbfce35"},
"redirect_uri": []string{"https://www.bmw-connecteddrive.com/app/default/static/external-dispatch.html"},
"response_type": []string{"token"},
"scope": []string{"authenticate_user fupo"},
"state": []string{"eyJtYXJrZXQiOiJkZSIsImxhbmd1YWdlIjoiZGUiLCJkZXN0aW5hdGlvbiI6ImxhbmRpbmdQYWdlIn0"},
"locale": []string{"DE-de"},
}
req, err := http.NewRequest(http.MethodPost, bmwAuth, strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, // don't follow redirects
}
resp, err := client.Do(req)
if err != nil {
return err
}
query, err := url.ParseQuery(resp.Header.Get("Location"))
if err != nil {
return err
}
token := query.Get("access_token")
expires, err := strconv.Atoi(query.Get("expires_in"))
if err != nil || token == "" || expires == 0 {
return errors.New("could not obtain token")
}
v.token = token
v.tokenValid = time.Now().Add(time.Duration(expires) * time.Second)
return nil
}
func (v *BMW) request(uri string) (*http.Request, error) {
if v.token == "" || time.Since(v.tokenValid) > 0 {
if err := v.login(v.user, v.password); err != nil {
return nil, err
}
}
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err == nil {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", v.token))
}
return req, nil
}
// chargeState implements the Vehicle.ChargeState interface
func (v *BMW) chargeState() (float64, error) {
uri := fmt.Sprintf("%s/vehicle/dynamic/v1/%s", bmwAPI, v.vin)
req, err := v.request(uri)
if err != nil {
return 0, err
}
var br bmwDynamicResponse
_, err = v.RequestJSON(req, &br)
return br.AttributesMap.ChargingLevelHv, err
}
// ChargeState implements the Vehicle.ChargeState interface
func (v *BMW) ChargeState() (float64, error) {
return v.chargeStateG()
}