forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcc.go
361 lines (293 loc) Β· 9.65 KB
/
mcc.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package charger
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"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/util/transport"
)
const (
mccAPILogin = "jwt/login"
mccAPIRefresh = "jwt/refresh"
mccAPIChargeState = "v1/api/WebServer/properties/chargeState"
mccAPICurrentSession = "v1/api/WebServer/properties/swaggerCurrentSession"
mccAPIEnergy = "v1/api/iCAN/properties/propjIcanEnergy"
mccAPISetCurrentLimit = "v1/api/SCC/properties/propHMICurrentLimit?value="
mccAPICurrentCableInformation = "v1/api/SCC/properties/json_CurrentCableInformation"
)
// MCCTokenResponse is the apiLogin response
type MCCTokenResponse struct {
Token string
Error string
}
// MCCCurrentSession is the apiCurrentSession response
type MCCCurrentSession struct {
Duration int64
EnergySumKwh float64
}
// MCCEnergyPhase is the apiEnergy response for a single phase
type MCCEnergyPhase struct {
Ampere float64
Power float64
}
// MCCEnergy is the apiEnergy response
type MCCEnergy struct {
L1, L2, L3 MCCEnergyPhase
}
// MCCCurrentCableInformation is the apiCurrentCableInformation response
type MCCCurrentCableInformation struct {
MaxValue, MinValue, Value int64
}
// MobileConnect charger supporting devices from Audi, Bentley, Porsche
type MobileConnect struct {
*request.Helper
uri string
password string
token string
tokenExpiry time.Time
cableInformation MCCCurrentCableInformation
}
func init() {
registry.Add("mcc", NewMobileConnectFromConfig)
}
// NewMobileConnectFromConfig creates a MCC charger from generic config
func NewMobileConnectFromConfig(other map[string]interface{}) (api.Charger, error) {
var cc struct {
URI, Password string
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewMobileConnect(util.DefaultScheme(cc.URI, "https"), cc.Password)
}
// NewMobileConnect creates MCC charger
func NewMobileConnect(uri, password string) (*MobileConnect, error) {
log := util.NewLogger("mcc")
mcc := &MobileConnect{
Helper: request.NewHelper(log),
uri: strings.TrimRight(uri, "/"),
password: password,
}
// ignore the self signed certificate
mcc.Client.Transport = request.NewTripper(log, transport.Insecure())
return mcc, nil
}
// construct the URL for a given api
func (mcc *MobileConnect) apiURL(api string) string {
return fmt.Sprintf("%s/%s", mcc.uri, api)
}
// process the http request to fetch the auth token for a login or refresh request
func (mcc *MobileConnect) fetchToken(request *http.Request) error {
var tr MCCTokenResponse
err := mcc.DoJSON(request, &tr)
if err == nil {
if len(tr.Token) == 0 {
return fmt.Errorf("response: %s", tr.Error)
}
mcc.token = tr.Token
// According to tests, the token is valid for 10 minutes
// but the web interface updates the token every 2 minutes, so let's enforce this
mcc.tokenExpiry = time.Now().Add(2 * time.Minute)
}
return err
}
// login as the home user with the given password
func (mcc *MobileConnect) login(password string) error {
uri := fmt.Sprintf("%s/%s", mcc.uri, mccAPILogin)
data := url.Values{
"user": []string{"user"},
"pass": []string{mcc.password},
}
req, err := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), map[string]string{
"Referer": fmt.Sprintf("%s/login", mcc.uri),
"Content-Type": "application/x-www-form-urlencoded",
})
if err != nil {
return err
}
return mcc.fetchToken(req)
}
// refresh the auth token with a new one
func (mcc *MobileConnect) refresh() error {
uri := fmt.Sprintf("%s/%s", mcc.uri, mccAPIRefresh)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return err
}
req.Header.Set("Referer", fmt.Sprintf("%s/login", mcc.uri))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", mcc.token))
return mcc.fetchToken(req)
}
// creates a http request that contains the auth token
func (mcc *MobileConnect) request(method, uri string) (*http.Request, error) {
// do we need a token refresh?
if mcc.token != "" {
// is it time to refresh the token?
if time.Until(mcc.tokenExpiry) < 10*time.Second {
if err := mcc.refresh(); err != nil {
// if refreshing the token fails it most likely is expired
// hence a new login is required, so let's enforce this
// and ignore this error
mcc.token = ""
}
}
}
// do we need to login?
if mcc.token == "" {
if err := mcc.login(mcc.password); err != nil {
return nil, err
}
}
// now lets process the request with the fetched token
req, err := http.NewRequest(method, uri, nil)
if err != nil {
return req, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", mcc.token))
req.Header.Set("Referer", fmt.Sprintf("%s/dashboard", mcc.uri))
return req, nil
}
// use http GET to fetch a non structured value from an URI and stores it in result
func (mcc *MobileConnect) getValue(uri string) ([]byte, error) {
req, err := mcc.request(http.MethodGet, uri)
if err != nil {
return nil, err
}
return mcc.DoBody(req)
}
// use http GET to fetch an escaped JSON string and unmarshal the data in result
func (mcc *MobileConnect) getEscapedJSON(uri string, result interface{}) error {
req, err := mcc.request(http.MethodGet, uri)
if err != nil {
return err
}
b, err := mcc.DoBody(req)
if err != nil {
return err
}
s, err := strconv.Unquote(strings.Trim(string(b), "\n"))
if err != nil {
return err
}
if s == "" {
return nil // empty response
}
return json.Unmarshal([]byte(s), &result)
}
// Status implements the api.Charger interface
func (mcc *MobileConnect) Status() (api.ChargeStatus, error) {
b, err := mcc.getValue(mcc.apiURL(mccAPIChargeState))
if err != nil {
return api.StatusNone, err
}
chargeState, err := strconv.ParseInt(strings.Trim(string(b), "\n"), 10, 8)
if err != nil {
return api.StatusNone, err
}
switch chargeState {
case 0: // Unplugged
return api.StatusA, nil
case 1, 3, 4, 6: // 1: Connecting, 3: Established, 4: Paused, 6: Finished
return api.StatusB, nil
case 2: // Error
return api.StatusF, nil
case 5: // Active
return api.StatusC, nil
default:
return api.StatusNone, fmt.Errorf("properties unknown result: %d", chargeState)
}
}
// Enabled implements the api.Charger interface
func (mcc *MobileConnect) Enabled() (bool, error) {
// Check if the car is connected and Paused, Active, or Finished
b, err := mcc.getValue(mcc.apiURL(mccAPIChargeState))
if err != nil {
return false, err
}
// return value is returned in the format 0\n
chargeState, err := strconv.ParseInt(strings.Trim(string(b), "\n"), 10, 8)
if err != nil {
return false, err
}
if chargeState >= 4 && chargeState <= 6 {
return true, nil
}
return false, nil
}
// Enable implements the api.Charger interface
func (mcc *MobileConnect) Enable(enable bool) error {
// As we don't know of the API to disable charging this for now always returns an error
return nil
}
// MaxCurrent implements the api.Charger interface
func (mcc *MobileConnect) MaxCurrent(current int64) error {
// The device doesn't return an error if we set a value greater than the
// current allowed max or smaller than the allowed min
// instead it will simply set it to max or min and return "OK" anyway
// Since the API here works differently, we fetch the limits
// and then return an error if the value is outside of the limits or
// otherwise set the new value
if mcc.cableInformation.MaxValue == 0 {
if err := mcc.getEscapedJSON(mcc.apiURL(mccAPICurrentCableInformation), &mcc.cableInformation); err != nil {
return err
}
}
if current < mcc.cableInformation.MinValue {
return fmt.Errorf("value is lower than the allowed minimum value %d", mcc.cableInformation.MinValue)
}
if current > mcc.cableInformation.MaxValue {
return fmt.Errorf("value is higher than the allowed maximum value %d", mcc.cableInformation.MaxValue)
}
url := fmt.Sprintf("%s%d", mcc.apiURL(mccAPISetCurrentLimit), current)
req, err := mcc.request(http.MethodPut, url)
if err != nil {
return err
}
b, err := mcc.DoBody(req)
if err != nil {
return err
}
// return value is returned in the format "OK"\n
if strings.Trim(string(b), "\n\"") != "OK" {
return fmt.Errorf("maxcurrent unexpected response: %s", string(b))
}
return nil
}
var _ api.Meter = (*MobileConnect)(nil)
// CurrentPower implements the api.Meter interface
func (mcc *MobileConnect) CurrentPower() (float64, error) {
var energy MCCEnergy
err := mcc.getEscapedJSON(mcc.apiURL(mccAPIEnergy), &energy)
return energy.L1.Power + energy.L2.Power + energy.L3.Power, err
}
var _ api.ChargeRater = (*MobileConnect)(nil)
// ChargedEnergy implements the api.ChargeRater interface
func (mcc *MobileConnect) ChargedEnergy() (float64, error) {
var currentSession MCCCurrentSession
if err := mcc.getEscapedJSON(mcc.apiURL(mccAPICurrentSession), ¤tSession); err != nil {
return 0, err
}
return currentSession.EnergySumKwh, nil
}
var _ api.ChargeTimer = (*MobileConnect)(nil)
// ChargingTime implements the api.ChargeTimer interface
func (mcc *MobileConnect) ChargingTime() (time.Duration, error) {
var currentSession MCCCurrentSession
if err := mcc.getEscapedJSON(mcc.apiURL(mccAPICurrentSession), ¤tSession); err != nil {
return 0, err
}
return time.Duration(currentSession.Duration) * time.Second, nil
}
var _ api.MeterCurrent = (*MobileConnect)(nil)
// Currents implements the api.MeterCurrent interface
func (mcc *MobileConnect) Currents() (float64, float64, float64, error) {
var energy MCCEnergy
err := mcc.getEscapedJSON(mcc.apiURL(mccAPIEnergy), &energy)
return energy.L1.Ampere, energy.L2.Ampere, energy.L3.Ampere, err
}