generated from theriverman/go-cli-project
/
vehicles.go
793 lines (733 loc) · 28.8 KB
/
vehicles.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
package vocdriver
import (
"fmt"
"log"
"time"
)
var ServiceTypeMap map[string]string = map[string]string{
"RDL": "Lock Vehicle",
"RDU": "Unlock Vehicle",
"RHBLF": "Blink Lights",
}
type VehiclesService struct {
client *Client
Endpoint string
}
/*
Low-level Functions
*/
func (v *VehiclesService) GetVehicleByVIN(vin string) (vehicle *Vehicle, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin)
if _, err = v.client.Request.Get(url, &vehicle); err != nil {
return nil, err
}
vehicle.client = v.client
err = vehicle.RetrieveHyperlinks()
return
}
func (v *VehiclesService) GetVehicleByHyperlink(url string) (vehicle *Vehicle, err error) {
if url == "" {
return nil, fmt.Errorf("url must not be empty")
}
if _, err = v.client.Request.Get(url, &vehicle); err != nil {
return nil, err
}
vehicle.client = v.client
err = vehicle.RetrieveHyperlinks()
return
}
func (v *VehiclesService) GetVehicleAttributesByVIN(vin string) (attributes *VehicleAttributes, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "attributes")
if _, err = v.client.Request.Get(url, &attributes); err != nil {
return nil, err
}
attributes.client = v.client
return
}
func (v *VehiclesService) GetVehicleStatusByVIN(vin string) (status *VehicleStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "status")
if _, err = v.client.Request.Get(url, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) GetVehiclePositionByVIN(vin string) (position *VehiclePosition, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "position")
if _, err = v.client.Request.Get(url, &position); err != nil {
return nil, err
}
position.client = v.client
return
}
func (v *VehiclesService) GetVehicleTripsByVIN(vin string) (trips *VehicleTrips, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "trips")
if _, err = v.client.Request.Get(url, &trips); err != nil {
return nil, err
}
trips.client = v.client
return
}
// GetServiceStatus retrieves the current status of an async operation (typically an action sent to a vehicle)
func (v *VehiclesService) GetServiceStatus(url string) (vss *VehicleServiceStatus, err error) {
if _, err = v.client.Request.Get(url, &vss); err != nil {
return nil, err
}
vss.client = v.client
return
}
// EvaluateServiceStatusAuto hangs the main application waiting for the requested operation to finish
// During this time the Service API is polled every second and the response is evaluated
// - if the request timeouts (default: 30s), an error is returned
// - if the request fails, an error is returned
func (v *VehiclesService) EvaluateServiceStatusAuto(vss *VehicleServiceStatus) (err error) {
timeoutSeconds := 30
if ServiceTypeMap[vss.ServiceType] == "Unlock Vehicle" {
vehicle, err := v.client.Vehicles.GetVehicleByVIN(vss.VehicleID)
if err != nil {
return fmt.Errorf("failed to retrieve vehicle details for %s", vss.VehicleID)
}
timeoutSeconds = vehicle.Attributes.UnlockTimeFrame
log.Printf("value of timeoutSeconds increased to %d to match the vehicle's unlockTimeFrame value", timeoutSeconds)
}
return v.EvaluateServiceStatus(vss, timeoutSeconds)
}
func (v *VehiclesService) EvaluateServiceStatus(vss *VehicleServiceStatus, timeoutSeconds int) (err error) {
c := 0
for {
if c == timeoutSeconds {
return fmt.Errorf("request timeout (%ds)", timeoutSeconds)
}
if c > 0 {
if err = vss.Refresh(); err != nil {
return
}
}
switch vss.Status {
case "Started":
time.Sleep(1 * time.Second)
c++
continue
case "MessageDelivered":
time.Sleep(1 * time.Second)
c++
continue
case "Successful":
return nil
case "Failed":
return fmt.Errorf("request (%s) failed: %s", ServiceTypeMap[vss.ServiceType], vss.FailureReason)
default:
return fmt.Errorf("request (%s) failed with status (%s): %s", ServiceTypeMap[vss.ServiceType], vss.Status, vss.FailureReason)
}
}
}
// BlinkLights blinks the lights on the car without sounding the horn
//
// This API requires sending the client's position. You have two options:
// - Share your position by passing a valid *Position struct.
// - Pass in `nil` and the actual own position of the car will be sent used
func (v *VehiclesService) BlinkLights(vin string, position *Position) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "honk_blink", "lights")
if position == nil {
position = &Position{}
vehiclePosition, err := v.GetVehiclePositionByVIN(vin)
if err != nil {
return nil, err
}
position.Longitude = vehiclePosition.Position.Longitude
position.Latitude = vehiclePosition.Position.Latitude
}
payload := map[string]float64{
"clientAccuracy": 0.0,
"clientLatitude": position.Latitude,
"clientLongitude": position.Longitude,
}
if _, err = v.client.Request.Post(url, payload, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
// HonkAndBlink honks and blinks the car
//
// This API requires sending the client's position. You have two options:
// - Share your position by passing a valid *Position struct.
// - Pass in `nil` and the actual own position of the car will be sent used
func (v *VehiclesService) HonkAndBlink(vin string, position *Position) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "honkAndBlink")
if position == nil {
position = &Position{}
vehiclePosition, err := v.GetVehiclePositionByVIN(vin)
if err != nil {
return nil, err
}
position.Longitude = vehiclePosition.Position.Longitude
position.Latitude = vehiclePosition.Position.Latitude
}
payload := map[string]float64{
"clientAccuracy": 0.0,
"clientLatitude": position.Latitude,
"clientLongitude": position.Longitude,
}
if _, err = v.client.Request.Post(url, payload, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) LockVehicle(vin string) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "lock")
if _, err = v.client.Request.Post(url, nil, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) UnlockVehicle(vin string) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "unlock")
if _, err = v.client.Request.Post(url, nil, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) StartEngine(vin string) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "engine", "start")
if _, err = v.client.Request.Post(url, map[string]int{"runtime": 15}, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) StopEngine(vin string) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "engine", "stop")
if _, err = v.client.Request.Post(url, nil, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) StartHeater(vin string) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "heater", "start")
if _, err = v.client.Request.Post(url, nil, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) StopHeater(vin string) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "heater", "stop")
if _, err = v.client.Request.Post(url, nil, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) StartPreclimatization(vin string) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "preclimatization", "start")
if _, err = v.client.Request.Post(url, nil, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
func (v *VehiclesService) StopPreclimatization(vin string) (status *VehicleServiceStatus, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "preclimatization", "stop")
if _, err = v.client.Request.Post(url, nil, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
/*
Charge Locations
*/
func (v *VehiclesService) GetChargingLocations(vin string) (chargingLocations *ChargingLocations, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "chargeLocations")
if _, err = v.client.Request.Get(url, &chargingLocations); err != nil {
return nil, err
}
chargingLocations.client = v.client
for i := 0; i < len(chargingLocations.ChargingLocations); i++ {
chargingLocations.ChargingLocations[i].client = v.client
}
return
}
func (v *VehiclesService) GetChargingLocation(vin, chargingId string) (chargingLocation *ChargingLocation, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
if chargingId == "" {
return nil, fmt.Errorf("chargingId must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "chargeLocations", chargingId)
if _, err = v.client.Request.Get(url, &chargingLocation); err != nil {
return nil, err
}
chargingLocation.client = v.client
return
}
func (v *VehiclesService) UpdateChargingLocation(vin, chargingId string, chargingLocation *ChargingLocation) (chargingLocationResponse *ChargingLocation, err error) {
if vin == "" {
return nil, fmt.Errorf("vin must not be empty")
}
if chargingId == "" {
return nil, fmt.Errorf("chargingId must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "chargeLocations", chargingId)
if _, err = v.client.Request.Put(url, &chargingLocation, &chargingLocationResponse); err != nil {
return nil, err
}
chargingLocationResponse.client = v.client
return
}
/*
Utils
*/
func (v *VehiclesService) RetrieveServiceStatus(vin, customerServiceId string) (status *VehicleServiceStatus, err error) {
if vin == "" || customerServiceId == "" {
return nil, fmt.Errorf("vin and customerServiceId must not be empty")
}
url := v.client.MakeURL(v.Endpoint, vin, "services", customerServiceId)
if _, err = v.client.Request.Get(url, &status); err != nil {
return nil, err
}
status.client = v.client
return
}
type Vehicle struct {
Attributes *VehicleAttributes
Status *VehicleStatus
VehicleAccountRelations []AccountVehicleRelation
HyperlinkAttributes string `json:"attributes"` // url
HyperlinkStatus string `json:"status"` // url
HyperlinkVehicleAccountRelations []string `json:"vehicleAccountRelations"` // url
VehicleID string `json:"vehicleId"` // vin
vehicleAccountRelationsRetrieved bool
client *Client // added for interface simplification
}
func (v *Vehicle) RetrieveHyperlinks() (err error) {
if v.Attributes == nil {
if v.Attributes, err = v.client.Vehicles.GetVehicleAttributesByVIN(v.VehicleID); err != nil {
return
}
}
if v.Status == nil {
if v.Status, err = v.client.Vehicles.GetVehicleStatusByVIN(v.VehicleID); err != nil {
return
}
}
if !v.vehicleAccountRelationsRetrieved {
for _, url := range v.HyperlinkVehicleAccountRelations {
relation, err := v.client.AccountVehicleRelation.GetByHyperlink(url)
if err != nil {
return err
}
v.VehicleAccountRelations = append(v.VehicleAccountRelations, *relation)
}
v.vehicleAccountRelationsRetrieved = true
}
return
}
/*
Actions/Operations
*/
func (v Vehicle) BlinkLights(position *Position) (status *VehicleServiceStatus, err error) {
return v.client.Vehicles.BlinkLights(v.VehicleID, position)
}
func (v *Vehicle) GetPosition() (position *VehiclePosition, err error) {
return v.client.Vehicles.GetVehiclePositionByVIN(v.VehicleID)
}
func (v *Vehicle) GetTrips() (trips *VehicleTrips, err error) {
return v.client.Vehicles.GetVehicleTripsByVIN(v.VehicleID)
}
func (v *Vehicle) Lock() (status *VehicleServiceStatus, err error) {
if !v.IsLockSupported() {
return nil, fmt.Errorf("lock/unlock is not supported by %s [%s]", v.Attributes.RegistrationNumber, v.Attributes.Vin)
}
return v.client.Vehicles.LockVehicle(v.VehicleID)
}
func (v Vehicle) UnlockVehicle() (status *VehicleServiceStatus, err error) {
if !v.IsUnlockSupported() {
return nil, fmt.Errorf("lock/unlock is not supported by %s [%s]", v.Attributes.RegistrationNumber, v.Attributes.Vin)
}
return v.client.Vehicles.UnlockVehicle(v.VehicleID)
}
func (v Vehicle) StartEngine() (status *VehicleServiceStatus, err error) {
if !v.IsEngineStartSupported() {
return nil, fmt.Errorf("engine start/stop is not supported by %s [%s]", v.Attributes.RegistrationNumber, v.Attributes.Vin)
}
return v.client.Vehicles.StartEngine(v.VehicleID)
}
func (v Vehicle) StopEngine() (status *VehicleServiceStatus, err error) {
if !v.IsEngineStartSupported() {
return nil, fmt.Errorf("engine start/stop is not supported by %s [%s]", v.Attributes.RegistrationNumber, v.Attributes.Vin)
}
return v.client.Vehicles.StopEngine(v.VehicleID)
}
func (v Vehicle) StartHeater() (status *VehicleServiceStatus, err error) {
switch {
case v.IsHeaterSupported():
return v.client.Vehicles.StartHeater(v.VehicleID)
case v.IsPreclimatizationSupported():
return v.client.Vehicles.StartPreclimatization(v.VehicleID)
default:
return nil, fmt.Errorf("heater is not supported by %s [%s]", v.Attributes.RegistrationNumber, v.Attributes.Vin)
}
}
func (v Vehicle) StopHeater() (status *VehicleServiceStatus, err error) {
switch {
case v.IsHeaterSupported():
return v.client.Vehicles.StopHeater(v.VehicleID)
case v.IsPreclimatizationSupported():
return v.client.Vehicles.StopPreclimatization(v.VehicleID)
default:
return nil, fmt.Errorf("heater is not supported by %s [%s]", v.Attributes.RegistrationNumber, v.Attributes.Vin)
}
}
func (v Vehicle) SetDelayCharging(chargingId string, delayCharging *DelayCharging) (chargingLocation *ChargingLocation, err error) {
cl := ChargingLocation{
Status: "Accepted",
DelayCharging: delayCharging,
}
return v.client.Vehicles.UpdateChargingLocation(v.VehicleID, chargingId, &cl)
}
/*
Properties
*/
// IsHeaterSupported returns true if either Remote Heater or Preclimatization is supported
func (v Vehicle) IsHeaterSupported() bool {
return v.Attributes.RemoteHeaterSupported || v.Attributes.PreclimatizationSupported
}
func (v Vehicle) IsHeaterOn() bool {
switch v.Status.Heater.Status {
case "on":
return true
case "off":
return false
default:
log.Printf("IsHeaterOn :: unexpected response: %s\n", v.Status.Heater.Status)
return false
}
}
func (v Vehicle) IsLockSupported() bool {
return v.Attributes.LockSupported
}
func (v Vehicle) IsUnlockSupported() bool {
return v.Attributes.UnlockSupported
}
func (v Vehicle) IsLocked() bool {
return v.Status.CarLocked
}
func (v Vehicle) IsRemoteHeaterSupported() bool {
return v.Attributes.RemoteHeaterSupported
}
func (v Vehicle) IsPreclimatizationSupported() bool {
return v.Attributes.PreclimatizationSupported
}
func (v Vehicle) IsJournalLogSupported() bool {
return v.Attributes.JournalLogSupported
}
func (v Vehicle) IsJournalLogEnabled() bool {
return v.Attributes.JournalLogEnabled
}
func (v Vehicle) IsHonkAndBlinkSupported() bool {
return v.Attributes.HonkAndBlinkSupported
}
func (v Vehicle) IsEngineStartSupported() bool {
return v.Attributes.EngineStartSupported
}
type VehicleAttributes struct {
EngineCode string `json:"engineCode"`
ExteriorCode string `json:"exteriorCode"`
InteriorCode string `json:"interiorCode"`
TyreDimensionCode string `json:"tyreDimensionCode"`
TyreInflationPressureLightCode string `json:"tyreInflationPressureLightCode"`
TyreInflationPressureHeavyCode string `json:"tyreInflationPressureHeavyCode"`
GearboxCode string `json:"gearboxCode"`
FuelType string `json:"fuelType"`
FuelTankVolume int `json:"fuelTankVolume"`
GrossWeight int `json:"grossWeight"`
ModelYear int `json:"modelYear"`
VehicleType string `json:"vehicleType"`
VehicleTypeCode string `json:"vehicleTypeCode"`
NumberOfDoors int `json:"numberOfDoors"`
RegistrationNumber string `json:"registrationNumber"`
CarLocatorDistance int `json:"carLocatorDistance"`
HonkAndBlinkDistance int `json:"honkAndBlinkDistance"`
BCallAssistanceNumber string `json:"bCallAssistanceNumber"`
CarLocatorSupported bool `json:"carLocatorSupported"`
HonkAndBlinkSupported bool `json:"honkAndBlinkSupported"`
HonkAndBlinkVersionsSupported []string `json:"honkAndBlinkVersionsSupported"`
RemoteHeaterSupported bool `json:"remoteHeaterSupported"`
UnlockSupported bool `json:"unlockSupported"`
LockSupported bool `json:"lockSupported"`
JournalLogSupported bool `json:"journalLogSupported"`
AssistanceCallSupported bool `json:"assistanceCallSupported"`
UnlockTimeFrame int `json:"unlockTimeFrame"`
VerificationTimeFrame int `json:"verificationTimeFrame"`
TimeFullyAccessible int `json:"timeFullyAccessible"`
TimePartiallyAccessible int `json:"timePartiallyAccessible"`
SubscriptionType string `json:"subscriptionType"`
SubscriptionStartDate string `json:"subscriptionStartDate"`
SubscriptionEndDate string `json:"subscriptionEndDate"`
ServerVersion string `json:"serverVersion"`
Vin string `json:"VIN"`
JournalLogEnabled bool `json:"journalLogEnabled"`
HighVoltageBatterySupported bool `json:"highVoltageBatterySupported"`
MaxActiveDelayChargingLocations int `json:"maxActiveDelayChargingLocations"`
PreclimatizationSupported bool `json:"preclimatizationSupported"`
SendPOIToVehicleVersionsSupported []string `json:"sendPOIToVehicleVersionsSupported"`
ClimatizationCalendarVersionsSupported []string `json:"climatizationCalendarVersionsSupported"`
ClimatizationCalendarMaxTimers int `json:"climatizationCalendarMaxTimers"`
VehiclePlatform string `json:"vehiclePlatform"`
VinLower string `json:"vin"`
OverrideDelayChargingSupported bool `json:"overrideDelayChargingSupported"`
EngineStartSupported bool `json:"engineStartSupported"`
StatusParkedIndoorSupported bool `json:"status.parkedIndoor.supported"`
Country struct {
Iso2 string `json:"iso2"`
} `json:"country"`
client *Client // added for interface simplification
}
func (va VehicleAttributes) VIN() string {
switch {
case len(va.Vin) > 0:
return va.Vin
case len(va.VinLower) > 0:
return va.VinLower
default:
log.Panicln("vin could not be retrieved from VehicleAttributes")
return ""
}
}
type VehicleStatus struct {
AverageFuelConsumption float64 `json:"averageFuelConsumption"`
AverageFuelConsumptionTimestamp string `json:"averageFuelConsumptionTimestamp"`
AverageSpeed int `json:"averageSpeed"`
AverageSpeedTimestamp string `json:"averageSpeedTimestamp"`
BrakeFluid string `json:"brakeFluid"`
BrakeFluidTimestamp string `json:"brakeFluidTimestamp"`
BulbFailures []string `json:"bulbFailures"`
BulbFailuresTimestamp string `json:"bulbFailuresTimestamp"`
CarLocked bool `json:"carLocked"`
CarLockedTimestamp string `json:"carLockedTimestamp"`
ConnectionStatus string `json:"connectionStatus"`
ConnectionStatusTimestamp string `json:"connectionStatusTimestamp"`
DistanceToEmpty int `json:"distanceToEmpty"`
DistanceToEmptyTimestamp string `json:"distanceToEmptyTimestamp"`
Doors struct {
TailgateOpen bool `json:"tailgateOpen"`
RearRightDoorOpen bool `json:"rearRightDoorOpen"`
RearLeftDoorOpen bool `json:"rearLeftDoorOpen"`
FrontRightDoorOpen bool `json:"frontRightDoorOpen"`
FrontLeftDoorOpen bool `json:"frontLeftDoorOpen"`
HoodOpen bool `json:"hoodOpen"`
Timestamp string `json:"timestamp"`
} `json:"doors"`
EngineRunning bool `json:"engineRunning"`
EngineRunningTimestamp string `json:"engineRunningTimestamp"`
FuelAmount int `json:"fuelAmount"`
FuelAmountLevel int `json:"fuelAmountLevel"`
FuelAmountLevelTimestamp string `json:"fuelAmountLevelTimestamp"`
FuelAmountTimestamp string `json:"fuelAmountTimestamp"`
Heater struct {
SeatSelection struct {
FrontDriverSide bool `json:"frontDriverSide"`
FrontPassengerSide bool `json:"frontPassengerSide"`
RearDriverSide bool `json:"rearDriverSide"`
RearPassengerSide bool `json:"rearPassengerSide"`
RearMid bool `json:"rearMid"`
} `json:"seatSelection"`
Status string `json:"status"`
Timer1 struct {
Time string `json:"time"`
State bool `json:"state"`
} `json:"timer1"`
Timer2 struct {
Time string `json:"time"`
State bool `json:"state"`
} `json:"timer2"`
Timestamp string `json:"timestamp"`
} `json:"heater"`
HvBattery struct {
HvBatteryChargeStatusDerived string `json:"hvBatteryChargeStatusDerived"`
HvBatteryChargeStatusDerivedTimestamp string `json:"hvBatteryChargeStatusDerivedTimestamp"`
HvBatteryChargeModeStatus string `json:"hvBatteryChargeModeStatus"`
HvBatteryChargeModeStatusTimestamp string `json:"hvBatteryChargeModeStatusTimestamp"`
HvBatteryChargeStatus string `json:"hvBatteryChargeStatus"`
HvBatteryChargeStatusTimestamp string `json:"hvBatteryChargeStatusTimestamp"`
HvBatteryLevel int `json:"hvBatteryLevel"`
HvBatteryLevelTimestamp string `json:"hvBatteryLevelTimestamp"`
DistanceToHVBatteryEmpty int `json:"distanceToHVBatteryEmpty"`
DistanceToHVBatteryEmptyTimestamp string `json:"distanceToHVBatteryEmptyTimestamp"`
HvBatteryChargeWarning string `json:"hvBatteryChargeWarning"`
HvBatteryChargeWarningTimestamp string `json:"hvBatteryChargeWarningTimestamp"`
TimeToHVBatteryFullyCharged int `json:"timeToHVBatteryFullyCharged"`
TimeToHVBatteryFullyChargedTimestamp string `json:"timeToHVBatteryFullyChargedTimestamp"`
} `json:"hvBattery"`
Odometer int `json:"odometer"`
OdometerTimestamp string `json:"odometerTimestamp"`
ParkedIndoor bool `json:"parkedIndoor"`
ParkedIndoorTimestamp string `json:"parkedIndoorTimestamp"`
RemoteClimatizationStatus string `json:"remoteClimatizationStatus"`
RemoteClimatizationStatusTimestamp string `json:"remoteClimatizationStatusTimestamp"`
ServiceWarningStatus string `json:"serviceWarningStatus"`
ServiceWarningStatusTimestamp string `json:"serviceWarningStatusTimestamp"`
TheftAlarm struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
Timestamp string `json:"timestamp"`
} `json:"theftAlarm"`
TimeFullyAccessibleUntil string `json:"timeFullyAccessibleUntil"`
TimePartiallyAccessibleUntil string `json:"timePartiallyAccessibleUntil"`
TripMeter1 int `json:"tripMeter1"`
TripMeter1Timestamp string `json:"tripMeter1Timestamp"`
TripMeter2 int `json:"tripMeter2"`
TripMeter2Timestamp string `json:"tripMeter2Timestamp"`
WasherFluidLevel string `json:"washerFluidLevel"`
WasherFluidLevelTimestamp string `json:"washerFluidLevelTimestamp"`
Windows struct {
FrontLeftWindowOpen bool `json:"frontLeftWindowOpen"`
FrontRightWindowOpen bool `json:"frontRightWindowOpen"`
Timestamp string `json:"timestamp"`
RearLeftWindowOpen bool `json:"rearLeftWindowOpen"`
RearRightWindowOpen bool `json:"rearRightWindowOpen"`
} `json:"windows"`
client *Client // added for interface simplification
}
type VehiclePosition struct {
Position Position `json:"position"`
CalculatedPosition Position `json:"calculatedPosition"`
client *Client
}
type VehicleTrips struct {
Trips []struct {
ID int `json:"id"`
Name string `json:"name"`
Category string `json:"category"`
UserNotes string `json:"userNotes"`
Trip string `json:"trip"`
RouteDetails struct {
Route string `json:"route"`
TotalWaypoints int `json:"totalWaypoints"`
BoundingBox struct {
MinLongitude float64 `json:"minLongitude"`
MinLatitude float64 `json:"minLatitude"`
MaxLongitude float64 `json:"maxLongitude"`
MaxLatitude float64 `json:"maxLatitude"`
} `json:"boundingBox"`
} `json:"routeDetails,omitempty"`
TripDetails []struct {
FuelConsumption float64 `json:"fuelConsumption"`
ElectricalConsumption float64 `json:"electricalConsumption"`
ElectricalRegeneration float64 `json:"electricalRegeneration"`
Distance float64 `json:"distance"`
StartOdometer int `json:"startOdometer"`
StartTime string `json:"startTime"`
StartPosition struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
StreetAddress string `json:"streetAddress"`
PostalCode string `json:"postalCode"`
City string `json:"city"`
ISO2CountryCode string `json:"ISO2CountryCode"`
Region string `json:"Region"`
} `json:"startPosition"`
EndOdometer int `json:"endOdometer"`
EndTime string `json:"endTime"`
EndPosition struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
StreetAddress string `json:"streetAddress"`
PostalCode string `json:"postalCode"`
City string `json:"city"`
ISO2CountryCode string `json:"ISO2CountryCode"`
Region string `json:"Region"`
} `json:"endPosition"`
} `json:"tripDetails"`
} `json:"trips"`
client *Client // added for interface simplification
}
type VehicleServiceStatus struct {
Status string `json:"status"`
StatusTimestamp string `json:"statusTimestamp"`
StartTime string `json:"startTime"`
ServiceType string `json:"serviceType"`
FailureReason string `json:"failureReason"` // TODO: no idea about the actual type
Service string `json:"service"` // hyperlink
VehicleID string `json:"vehicleId"` // VIN
CustomerServiceID string `json:"customerServiceId"`
client *Client // added for interface simplification
}
/*
The call towards vss.client.Vehicles.GetServiceStatus failes with runtime error: invalid memory address or nil pointer dereference
which is pretty weird because i'm passing a regular string to a simple function.
*/
func (vss *VehicleServiceStatus) Refresh() error {
vssNew, err := vss.client.Vehicles.GetServiceStatus(vss.Service)
if err != nil {
return err
}
// manually refreshing the original struct with the new values
vss.Status = vssNew.Status
vss.StatusTimestamp = vssNew.StatusTimestamp
vss.StartTime = vssNew.StartTime
vss.ServiceType = vssNew.ServiceType
vss.FailureReason = vssNew.FailureReason
vss.Service = vssNew.Service
vss.VehicleID = vssNew.VehicleID
vss.CustomerServiceID = vssNew.CustomerServiceID
return nil
}