forked from joeshaw/carwings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarwings.go
1204 lines (1013 loc) · 33.8 KB
/
carwings.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package carwings
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"time"
//lint:ignore SA1019 Blowfish is terrible, but that's what the Nissan API uses
"golang.org/x/crypto/blowfish"
)
const (
initialAppStrings = "9s5rfKVuMrT03RtzajWNcA"
)
var (
// ErrNotLoggedIn is returned whenever an operation is run and
// the user has not let logged in.
ErrNotLoggedIn = errors.New("not logged in")
// ErrUpdateFailed indicates an error talking to the Carwings
// service when fetching updated vehicle data.
ErrUpdateFailed = errors.New("failed to retrieve updated info from vehicle")
// ErrBatteryStatusUnavailable is returned from the
// BatteryStatus method when no data is available.
ErrBatteryStatusUnavailable = errors.New("battery status unavailable")
// ErrVehicleInfoUnavailable is returned when vehicle information is
// not available when logging in.
ErrVehicleInfoUnavailable = errors.New("vehicle info unavailable")
// Debug indiciates whether to log HTTP responses to stderr
Debug = false
// Default URL for connecting to Carwings service. This is
// changed by Nissan from time to time, so it's helpful to
// have it be configurable.
BaseURL = "https://gdcportalgw.its-mo.com/api_v200413_NE/gdc/"
// Http client used for api requests
Client = http.DefaultClient
)
func pkcs5Padding(data []byte, blocksize int) []byte {
padLen := blocksize - (len(data) % blocksize)
padding := bytes.Repeat([]byte{byte(padLen)}, padLen)
return append(data, padding...)
}
// Pads the source, does ECB Blowfish encryption on it, and returns a
// base64-encoded string.
func encrypt(s, key string) (string, error) {
cipher, err := blowfish.NewCipher([]byte(key))
if err != nil {
return "", err
}
src := []byte(s)
src = pkcs5Padding(src, cipher.BlockSize())
dst := make([]byte, len(src))
pos := 0
for pos < len(src) {
cipher.Encrypt(dst[pos:], src[pos:])
pos += cipher.BlockSize()
}
return base64.StdEncoding.EncodeToString(dst), nil
}
// MetersToMiles converts Carwings distances (in meters) to miles.
func MetersToMiles(meters int) int {
const MilesPerMeter = 0.000621371
return int(float64(meters) * MilesPerMeter)
}
const (
RegionUSA = "NNA"
RegionEurope = "NE"
RegionCanada = "NCI"
RegionAustralia = "NMA"
RegionJapan = "NML"
)
// Session defines a one or more connections to the Carwings service
type Session struct {
// Region is one of the predefined region codes where this car operates.
Region string
// Filename is an optional file to load and save an existing session to.
Filename string
username string
encpw string
VIN string
customSessionID string
tz string
loc *time.Location
cabinTemp int
}
// ClimateStatus contains information about the vehicle's climate
// control (AC or heater) status.
type ClimateStatus struct {
// Date and time this status was retrieved from the vehicle.
LastOperationTime time.Time
// The current climate control operation status.
Running bool
// Current plugged-in state
PluginState PluginState
// The amount of time the climate control system will run
// while on battery power, in seconds.
BatteryDuration int
// The amount of time the climate control system will run
// while plugged in, in seconds.
PluggedDuration int
// The climate preset temperature unit, F or C
TemperatureUnit string
// The climate preset temperature value
Temperature int
// Time the AC was stopped, or is scheduled to stop
ACStopTime time.Time
// Estimated cruising range with climate control on, in
// meters.
CruisingRangeACOn int
// Estimated cruising range with climate control off, in
// meters.
CruisingRangeACOff int
}
// BatteryStatus contains information about the vehicle's state of
// charge, current plugged-in state, charging status, and the time to
// charge the battery to full.
type BatteryStatus struct {
// Date and time this battery status was retrieved from the
// vehicle.
Timestamp time.Time
// Total capacity of the battery. Units unknown.
Capacity int
// Remaining battery level. Units unknown, but same as Capacity.
Remaining int
// Remaining battery level in Watt Hours.
RemainingWH int
// Current state of charge. In percent, should be roughly
// equivalent to Remaining / Capacity * 100.
StateOfCharge int // percent
// Estimated cruising range with climate control on, in
// meters.
CruisingRangeACOn int
// Estimated cruising range with climate control off, in
// meters.
CruisingRangeACOff int
// Current plugged-in state
PluginState PluginState
// Current charging status
ChargingStatus ChargingStatus
// Amount of time remaining until battery is fully charged,
// using different possible charging methods.
TimeToFull TimeToFull
}
// TimeToFull contains information about how long it will take to
// charge the battery to full via different charging methods.
type TimeToFull struct {
// Time to fully charge the battery using a 1.4 kW Level 1
// (120V 12A) trickle charge.
Level1 time.Duration
// Time to fully charge the battery using a 3.3 kW Level 2
// (240V ~15A) charge.
Level2 time.Duration
// Time to fully charge the battery using a 6.6 kW Level 2
// (240V ~30A) charge.
Level2At6kW time.Duration
}
// VehicleLocation indicates the vehicle's current location.
type VehicleLocation struct {
// Timestamp of the last time vehicle location was updated.
Timestamp time.Time
// Latitude of the vehicle
Latitude string
// Longitude of the vehicle
Longitude string
}
// PluginState indicates whether and how the vehicle is plugged in.
// It is separate from ChargingStatus, because the vehicle can be
// plugged in but not actively charging.
type PluginState string
const (
// Not connected to a charger
NotConnected = PluginState("NOT_CONNECTED")
// Connected to a normal J1772 Level 1 or 2 charger
Connected = PluginState("CONNECTED")
// Connected to a high voltage DC quick charger (ChaDeMo)
QCConnected = PluginState("QC_CONNECTED")
// Invalid state, when updating data from the vehicle fails.
InvalidPluginState = PluginState("INVALID")
)
func (ps PluginState) String() string {
switch ps {
case NotConnected:
return "not connected"
case Connected:
return "connected"
case QCConnected:
return "connected to quick charger"
case InvalidPluginState:
return "invalid"
default:
return string(ps)
}
}
// ChargingStatus indicates whether and how the vehicle is charging.
type ChargingStatus string
const (
// Not charging
NotCharging = ChargingStatus("NOT_CHARGING")
// Normal charging from a Level 1 or 2 EVSE
NormalCharging = ChargingStatus("NORMAL_CHARGING")
// Rapidly charging from a ChaDeMo DC quick charger
RapidlyCharging = ChargingStatus("RAPIDLY_CHARGING")
// Invalid state, when updating data from the vehicle fails.
InvalidChargingStatus = ChargingStatus("INVALID")
)
func (cs ChargingStatus) String() string {
switch cs {
case NotCharging:
return "not charging"
case NormalCharging:
return "charging"
case RapidlyCharging:
return "rapidly charging"
case InvalidChargingStatus:
return "invalid"
default:
return string(cs)
}
}
// OperationResult
const (
start = "START"
electricWaveAbnormal = "ELECTRIC_WAVE_ABNORMAL"
)
type cwTime time.Time
func (cwt *cwTime) UnmarshalJSON(data []byte) error {
if data == nil || string(data) == `""` {
return nil
}
// Carwings uses at least five different date formats! 🙄🙄🙄
t, err := time.Parse(`"2006\/01\/02 15:04"`, string(data))
if err == nil {
*cwt = cwTime(t)
return nil
}
t, err = time.Parse(`"2006-01-02 15:04:05"`, string(data))
if err == nil {
*cwt = cwTime(t)
return nil
}
// Also e.g. "UserVehicleBoundTime": "2018-08-04T15:08:33Z"
t, err = time.Parse(`"2006-01-02T15:04:05Z"`, string(data))
if err == nil {
*cwt = cwTime(t)
return nil
}
// Also e.g. "GpsDatetime": "2018-08-05T10:18:47" in monthly statistics response
t, err = time.Parse(`"2006-01-02T15:04:05"`, string(data))
if err == nil {
*cwt = cwTime(t)
return nil
}
// Also e.g. "LastScheduledTime": "2018-08-04T15:08:33Z" in ClimateControlSchedule response
t, err = time.Parse(`"Jan _2, 2006 03:04 PM"`, string(data))
if err == nil {
*cwt = cwTime(t)
return nil
}
return fmt.Errorf("cannot parse %q as carwings time", string(data))
}
// FixLocation alters the location associated with the time, without changing
// the value. This is needed since all times are parsed as if they were UTC
// when in fact some of them are in the timezone specified in the session.
func (cwt cwTime) FixLocation(location *time.Location) cwTime {
t := time.Time(cwt)
return cwTime(time.Date(
t.Year(),
t.Month(),
t.Day(),
t.Hour(),
t.Minute(),
t.Second(),
t.Nanosecond(),
location,
))
}
type response interface {
Status() int
ErrorMessage() string
}
type baseResponse struct {
StatusCode json.RawMessage `json:"status"`
Message string `json:"message"`
}
func (r *baseResponse) Status() int {
s := r.StatusCode
if s[0] == '"' {
s = s[1 : len(s)-1]
}
v, _ := strconv.Atoi(string(s))
return v
}
func (r *baseResponse) ErrorMessage() string {
return r.Message
}
func apiRequest(endpoint string, params url.Values, target response) error {
req, err := http.NewRequest("POST", BaseURL+endpoint, strings.NewReader(params.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "")
if Debug {
body, err := httputil.DumpRequestOut(req, true)
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stderr, string(body))
fmt.Fprintln(os.Stderr)
}
resp, err := Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if Debug {
body, err := httputil.DumpResponse(resp, true)
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stderr, string(body))
fmt.Fprintln(os.Stderr)
}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(target); err != nil {
return err
}
switch s := target.Status(); s {
case http.StatusOK:
return nil
case http.StatusUnauthorized, http.StatusRequestTimeout:
return ErrNotLoggedIn
default:
if e := target.ErrorMessage(); e != "" {
return fmt.Errorf("received status code %d (%s)", s, e)
}
return fmt.Errorf("received status code %d", s)
}
}
// Connect establishes a new authenticated Session with the Carwings
// service.
func (s *Session) Connect(username, password string) error {
params := url.Values{}
params.Set("initial_app_str", initialAppStrings)
var initResp struct {
baseResponse
Baseprm string `json:"baseprm"`
}
if err := apiRequest("InitialApp_v2.php", params, &initResp); err != nil {
return err
}
encpw, err := encrypt(password, initResp.Baseprm)
if err != nil {
return err
}
s.username = username
s.encpw = encpw
if s.Filename != "" {
if err := s.load(); err == nil {
return nil
} else if Debug {
fmt.Fprintf(os.Stderr, "Error loading session from %s: %v\n", s.Filename, err)
}
}
return s.Login()
}
func (s *Session) Login() error {
params := url.Values{}
params.Set("initial_app_str", initialAppStrings)
params.Set("UserId", s.username)
params.Set("Password", s.encpw)
params.Set("RegionCode", s.Region)
// Not a comprehensive representation, just what we need
type vehicleInfo struct {
VIN string `json:"vin"`
CustomSessionID string `json:"custom_sessionid"`
}
var loginResp struct {
baseResponse
// OMG this API... one of these three will be populated.
VehicleInfos []vehicleInfo `json:"vehicleInfo"`
VehicleInfoList struct {
VehicleInfos []vehicleInfo `json:"vehicleInfo"`
} `json:"vehicleInfoList"`
VehicleInfo vehicleInfo `json:"VehicleInfo"`
CustomerInfo struct {
Timezone string
VehicleInfo vehicleInfo `json:"VehicleInfo"`
}
}
if err := apiRequest("UserLoginRequest.php", params, &loginResp); err != nil {
return err
}
var vi vehicleInfo
switch {
case len(loginResp.VehicleInfos) > 0:
vi = loginResp.VehicleInfos[0]
case len(loginResp.VehicleInfoList.VehicleInfos) > 0:
vi = loginResp.VehicleInfoList.VehicleInfos[0]
case len(loginResp.CustomerInfo.VehicleInfo.VIN) > 0:
vi = loginResp.CustomerInfo.VehicleInfo
default:
vi = loginResp.VehicleInfo
}
if vi.VIN == "" {
return ErrVehicleInfoUnavailable
}
s.customSessionID = vi.CustomSessionID
s.VIN = vi.VIN
s.tz = loginResp.CustomerInfo.Timezone
loc, err := time.LoadLocation(loginResp.CustomerInfo.Timezone)
if err != nil {
loc = time.UTC
}
s.loc = loc
if s.Filename != "" {
return s.save()
}
return nil
}
func (s *Session) load() error {
if s.Filename[0] == '~' {
s.Filename = os.Getenv("HOME") + s.Filename[1:]
}
f, err := os.Open(s.Filename)
if err != nil {
return err
}
defer f.Close()
m := map[string]string{}
if err := json.NewDecoder(f).Decode(&m); err != nil {
return err
}
s.VIN = m["vin"]
s.customSessionID = m["customSessionID"]
s.tz = m["tz"]
loc, err := time.LoadLocation(m["tz"])
if err != nil {
loc = time.UTC
}
s.loc = loc
return nil
}
func (s *Session) save() error {
if s.Filename[0] == '~' {
s.Filename = os.Getenv("HOME") + s.Filename[1:]
}
f, err := os.OpenFile(s.Filename, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
m := map[string]string{
"vin": s.VIN,
"customSessionID": s.customSessionID,
"tz": s.tz,
}
if err := json.NewEncoder(f).Encode(m); err != nil {
f.Close()
os.Remove(s.Filename)
return err
}
return f.Close()
}
func (s *Session) apiRequest(endpoint string, params url.Values, target response) error {
params = s.setCommonParams(params)
err := apiRequest(endpoint, params, target)
if err == ErrNotLoggedIn {
if err := s.Login(); err != nil {
return err
}
params = s.setCommonParams(params)
return apiRequest(endpoint, params, target)
}
return err
}
func (s *Session) setCommonParams(params url.Values) url.Values {
if params == nil {
params = url.Values{}
}
params.Set("RegionCode", s.Region)
params.Set("VIN", s.VIN)
params.Set("custom_sessionid", s.customSessionID)
params.Set("tz", s.tz)
return params
}
// UpdateStatus asks the Carwings service to request an update from
// the vehicle. This is an asynchronous operation: it returns a
// "result key" that must be used to poll for status with the
// CheckUpdate method.
func (s *Session) UpdateStatus() (string, error) {
var resp struct {
baseResponse
ResultKey string `json:"resultKey"`
}
if err := s.apiRequest("BatteryStatusCheckRequest.php", nil, &resp); err != nil {
return "", err
}
return resp.ResultKey, nil
}
// CheckUpdate returns whether the update corresponding to the
// provided result key has finished.
func (s *Session) CheckUpdate(resultKey string) (bool, error) {
params := url.Values{}
params.Set("resultKey", resultKey)
var resp struct {
baseResponse
ResponseFlag int `json:"responseFlag,string"`
OperationResult string `json:"operationResult"`
}
if err := s.apiRequest("BatteryStatusCheckResultRequest.php", params, &resp); err != nil {
return false, err
}
var err error
if resp.OperationResult == electricWaveAbnormal {
err = ErrUpdateFailed
}
return resp.ResponseFlag == 1, err
}
// BatteryStatus returns the most recent battery status from the
// Carwings service. Note that this data is not real-time: it is
// cached from the last time the vehicle data was updated. Use
// UpdateStatus method to update vehicle data.
func (s *Session) BatteryStatus() (BatteryStatus, error) {
type batteryStatusRecord struct {
BatteryStatus struct {
BatteryChargingStatus string
BatteryCapacity int `json:",string"`
BatteryRemainingAmount string
BatteryRemainingAmountWH string
BatteryRemainingAmountKWH string
SOC struct {
Value int `json:",string"`
}
}
PluginState string
CruisingRangeAcOn json.Number `json:",string"`
CruisingRangeAcOff json.Number `json:",string"`
TimeRequiredToFull struct {
HourRequiredToFull int `json:",string"`
MinutesRequiredToFull int `json:",string"`
}
TimeRequiredToFull200 struct {
HourRequiredToFull int `json:",string"`
MinutesRequiredToFull int `json:",string"`
}
TimeRequiredToFull200_6kW struct {
HourRequiredToFull int `json:",string"`
MinutesRequiredToFull int `json:",string"`
}
NotificationDateAndTime cwTime
}
var resp struct {
baseResponse
BatteryStatusRecords json.RawMessage
}
if err := s.apiRequest("BatteryStatusRecordsRequest.php", nil, &resp); err != nil {
return BatteryStatus{}, err
}
if string(resp.BatteryStatusRecords) == "[]" {
return BatteryStatus{}, ErrBatteryStatusUnavailable
}
var batrec batteryStatusRecord
if err := json.Unmarshal(resp.BatteryStatusRecords, &batrec); err != nil {
return BatteryStatus{}, err
}
remaining, _ := strconv.Atoi(batrec.BatteryStatus.BatteryRemainingAmount)
remainingWH, _ := strconv.Atoi(batrec.BatteryStatus.BatteryRemainingAmountWH)
acOn, _ := batrec.CruisingRangeAcOn.Float64()
acOff, _ := batrec.CruisingRangeAcOff.Float64()
soc := batrec.BatteryStatus.SOC.Value
if soc == 0 {
soc = int(math.Round(float64(remaining) / float64(batrec.BatteryStatus.BatteryCapacity)))
}
bs := BatteryStatus{
Timestamp: time.Time(batrec.NotificationDateAndTime).In(s.loc),
Capacity: batrec.BatteryStatus.BatteryCapacity,
Remaining: remaining,
RemainingWH: remainingWH,
StateOfCharge: soc,
CruisingRangeACOn: int(acOn),
CruisingRangeACOff: int(acOff),
PluginState: PluginState(batrec.PluginState),
ChargingStatus: ChargingStatus(batrec.BatteryStatus.BatteryChargingStatus),
TimeToFull: TimeToFull{
Level1: time.Duration(batrec.TimeRequiredToFull.HourRequiredToFull)*time.Hour + time.Duration(batrec.TimeRequiredToFull.MinutesRequiredToFull)*time.Minute,
Level2: time.Duration(batrec.TimeRequiredToFull200.HourRequiredToFull)*time.Hour + time.Duration(batrec.TimeRequiredToFull200.MinutesRequiredToFull)*time.Minute,
Level2At6kW: time.Duration(batrec.TimeRequiredToFull200_6kW.HourRequiredToFull)*time.Hour + time.Duration(batrec.TimeRequiredToFull200_6kW.MinutesRequiredToFull)*time.Minute,
},
}
return bs, nil
}
// ClimateControlStatus returns the most recent climate control status
// from the Carwings service.
func (s *Session) ClimateControlStatus() (ClimateStatus, error) {
type remoteACRecords struct {
OperationResult string
OperationDateAndTime cwTime
RemoteACOperation string
ACStartStopDateAndTime cwTime
ACStartStopURL string
CruisingRangeAcOn json.Number `json:",string"`
CruisingRangeAcOff json.Number `json:",string"`
PluginState string
ACDurationBatterySec int `json:",string"`
ACDurationPluggedSec int `json:",string"`
PreAC_unit string
PreAC_temp int `json:",string"`
}
var resp struct {
baseResponse
RemoteACRecords json.RawMessage
}
if err := s.apiRequest("RemoteACRecordsRequest.php", nil, &resp); err != nil {
return ClimateStatus{}, err
}
// Sometimes the RemoteACRecords field is an empty array
// instead of a struct value. This API... ¯\_(ツ)_/¯
if string(resp.RemoteACRecords) == "[]" {
return ClimateStatus{}, errors.New("climate status not available")
}
var racr remoteACRecords
if err := json.Unmarshal(resp.RemoteACRecords, &racr); err != nil {
return ClimateStatus{}, err
}
acOn, _ := racr.CruisingRangeAcOn.Float64()
acOff, _ := racr.CruisingRangeAcOff.Float64()
running := racr.RemoteACOperation == "START"
acStopTime := time.Time(racr.ACStartStopDateAndTime).In(s.loc)
if running {
if NotConnected == PluginState(racr.PluginState) {
acStopTime = acStopTime.Add(time.Second * time.Duration(racr.ACDurationBatterySec))
} else {
acStopTime = acStopTime.Add(time.Second * time.Duration(racr.ACDurationPluggedSec))
}
}
cs := ClimateStatus{
LastOperationTime: time.Time(racr.OperationDateAndTime.FixLocation(s.loc)),
Running: running,
PluginState: PluginState(racr.PluginState),
BatteryDuration: racr.ACDurationBatterySec,
PluggedDuration: racr.ACDurationPluggedSec,
TemperatureUnit: racr.PreAC_unit,
Temperature: racr.PreAC_temp,
ACStopTime: acStopTime,
CruisingRangeACOn: int(acOn),
CruisingRangeACOff: int(acOff),
}
return cs, nil
}
// ClimateOffRequest sends a request to turn off the climate control
// system. This is an asynchronous operation: it returns a "result
// key" that can be used to poll for status with the
// CheckClimateOffRequest method.
func (s *Session) ClimateOffRequest() (string, error) {
var resp struct {
baseResponse
ResultKey string `json:"resultKey"`
}
if err := s.apiRequest("ACRemoteOffRequest.php", nil, &resp); err != nil {
return "", err
}
return resp.ResultKey, nil
}
// CheckClimateOffRequest returns whether the ClimateOffRequest has
// finished.
func (s *Session) CheckClimateOffRequest(resultKey string) (bool, error) {
var resp struct {
baseResponse
ResponseFlag int `json:"responseFlag,string"` // 0 or 1
OperationResult string `json:"operationResult"`
TimeStamp cwTime `json:"timeStamp"`
HVACStatus string `json:"hvacStatus"`
}
params := url.Values{}
params.Set("resultKey", resultKey)
if err := s.apiRequest("ACRemoteOffResult.php", params, &resp); err != nil {
return false, err
}
return resp.ResponseFlag == 1, nil
}
// ClimateOnRequest sends a request to turn on the climate control
// system. This is an asynchronous operation: it returns a "result
// key" that can be used to poll for status with the
// CheckClimateOnRequest method.
func (s *Session) ClimateOnRequest() (string, error) {
var resp struct {
baseResponse
ResultKey string `json:"resultKey"`
}
if err := s.apiRequest("ACRemoteRequest.php", nil, &resp); err != nil {
return "", err
}
return resp.ResultKey, nil
}
// CheckClimateOnRequest returns whether the ClimateOnRequest has
// finished.
func (s *Session) CheckClimateOnRequest(resultKey string) (bool, error) {
var resp struct {
baseResponse
ResponseFlag int `json:"responseFlag,string"` // 0 or 1
OperationResult string `json:"operationResult"`
ACContinueTime string `json:"acContinueTime"`
TimeStamp cwTime `json:"timeStamp"`
HVACStatus string `json:"hvacStatus"`
}
params := url.Values{}
params.Set("resultKey", resultKey)
if err := s.apiRequest("ACRemoteResult.php", params, &resp); err != nil {
return false, err
}
return resp.ResponseFlag == 1, nil
}
// ChargingRequest begins charging a plugged-in vehicle.
func (s *Session) ChargingRequest() error {
var resp struct {
baseResponse
}
params := url.Values{}
params.Set("ExecuteTime", time.Now().In(s.loc).Format("2006-01-02"))
if err := s.apiRequest("BatteryRemoteChargingRequest.php", params, &resp); err != nil {
return err
}
return nil
}
// CabinTempRequest sends a request to get the cabin temperature. This is an
// asynchronous operation: it returns a "result key" that can be used
// to poll for status with the CheckCabinTempRequest method.
func (s *Session) CabinTempRequest() (string, error) {
var resp struct {
baseResponse
ResultKey string `json:"resultKey"`
}
if err := s.apiRequest("GetInteriorTemperatureRequestForNsp.php", nil, &resp); err != nil {
return "", err
}
return resp.ResultKey, nil
}
// CheckCabinTempRequest returns whether the CabinTempRequest has finished.
func (s *Session) CheckCabinTempRequest(resultKey string) (bool, error) {
var resp struct {
baseResponse
ResponseFlag int `json:"responseFlag,string"` // 0 or 1
Temperature int `json:"Inc_temp"`
}
params := url.Values{}
params.Set("resultKey", resultKey)
if err := s.apiRequest("GetInteriorTemperatureResultForNsp.php", params, &resp); err != nil {
return false, err
}
s.cabinTemp = resp.Temperature
return resp.ResponseFlag == 1, nil
}
// GetCabinTemp returns the latest cached cabin temperature result.
func (s *Session) GetCabinTemp() int {
return s.cabinTemp
}
// TripDetail holds the details of each trip. All of the parsed detail is
// used in both the response and the MonthlyStatistics.
type TripDetail struct {
// "PriceSimulatorDetailInfoTrip": [
// {
// "TripId": "1",
// "PowerConsumptTotal": "2461.12",
// "PowerConsumptMoter": "3812.22",
// "PowerConsumptMinus": "1351.1",
// "TravelDistance": "17841",
// "ElectricMileage": "13.8",
// "CO2Reduction": "3",
// "MapDisplayFlg": "NONACTIVE",
// "GpsDatetime": "2018-08-05T10:18:47"
// },
TripId int `json:",string"`
PowerConsumedTotal float64 `json:"PowerConsumptTotal,string"`
PowerConsumedMotor float64 `json:"PowerConsumptMoter,string"`
PowerRegenerated float64 `json:"PowerConsumptMinus,string"`
Meters int `json:"TravelDistance,string"`
Efficiency float64 `json:"ElectricMileage,string"`
CO2Reduction int `json:",string"`
MapDisplayFlag string `json:"MapDisplayFlg"`
GPSDateTime cwTime `json:"GpsDatetime"`
Started time.Time `json:",omitempty"`
}
// DateDetail is the detail for a single date
type DateDetail struct {
TargetDate string
Trips []TripDetail
}
// MonthlyTotals holds the various totals of things for the whole month
type MonthlyTotals struct {
Trips int `json:"TotalNumberOfTrips,string"`
PowerConsumed float64 `json:"TotalPowerConsumptTotal,string"`
PowerConsumedMotor float64 `json:"TotalPowerConsumptMoter,string"`
PowerRegenerated float64 `json:"TotalPowerConsumptMinus,string"`
MetersTravelled int `json:"TotalTravelDistance,string"`
Efficiency float64 `json:"TotalElectricMileage,string"`
CO2Reduction int `json:"TotalCO2Reductiont,string"`
}
// MonthlyStatistics is the structure returned which includes
// all of the trips and all of the totals as well as the electricity rate
// informtion that has been supplied to CarWings.
type MonthlyStatistics struct {
EfficiencyScale string
ElectricityRate float64
ElectricityBill float64
Dates []DateDetail
Total MonthlyTotals
}
// GetMonthlyStatistics gets the statistics for a particular month
func (s *Session) GetMonthlyStatistics(month time.Time) (MonthlyStatistics, error) {
// {
// "status": 200,
// "PriceSimulatorDetailInfoResponsePersonalData": {
// "TargetMonth": "201808",
// "TotalPowerConsumptTotal": "55.88882",
// "TotalPowerConsumptMoter": "71.44184",
// "TotalPowerConsumptMinus": "15.55302",
// "ElectricPrice": "0.15",
// "ElectricBill": "8.3833230",