forked from bogosj/tesla
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vehicles.go
141 lines (128 loc) · 5.9 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
package tesla
import (
"strings"
)
// VehiclePartialResponse represents the vehicle response root data as returned from the Tesla API.
type VehiclePartialResponse struct {
Color interface{} `json:"color"`
DisplayName string `json:"display_name"`
ID int64 `json:"id"`
OptionCodes string `json:"option_codes"`
VehicleID uint64 `json:"vehicle_id"`
Vin string `json:"vin"`
Tokens []string `json:"tokens"`
State string `json:"state"`
IDS string `json:"id_s"`
RemoteStartEnabled bool `json:"remote_start_enabled"`
CalendarEnabled bool `json:"calendar_enabled"`
NotificationsEnabled bool `json:"notifications_enabled"`
BackseatToken interface{} `json:"backseat_token"`
BackseatTokenUpdatedAt interface{} `json:"backseat_token_updated_at"`
AccessType string `json:"access_type"`
InService bool `json:"in_service"`
APIVersion int `json:"api_version"`
CommandSigning string `json:"command_signing"`
}
// Vehicle represents the vehicle as returned from the Tesla API.
type Vehicle struct {
Color interface{} `json:"color"`
DisplayName string `json:"display_name"`
ID int64 `json:"id"`
OptionCodes string `json:"option_codes"`
VehicleID uint64 `json:"vehicle_id"`
Vin string `json:"vin"`
Tokens []string `json:"tokens"`
State string `json:"state"`
IDS string `json:"id_s"`
RemoteStartEnabled bool `json:"remote_start_enabled"`
CalendarEnabled bool `json:"calendar_enabled"`
NotificationsEnabled bool `json:"notifications_enabled"`
BackseatToken interface{} `json:"backseat_token"`
BackseatTokenUpdatedAt interface{} `json:"backseat_token_updated_at"`
AccessType string `json:"access_type"`
InService bool `json:"in_service"`
APIVersion int `json:"api_version"`
CommandSigning string `json:"command_signing"`
c *Client
}
// VehicleConfig represents the configuration of a vehicle.
type VehicleConfig struct {
CanAcceptNavigationRequests bool `json:"can_accept_navigation_requests"`
CanActuateTrunks bool `json:"can_actuate_trunks"`
CarSpecialType string `json:"car_special_type"`
CarType string `json:"car_type"`
ChargePortType string `json:"charge_port_type"`
DefaultChargeToMax bool `json:"default_charge_to_max"`
DriverAssist string `json:"driver_assist"`
EceRestrictions bool `json:"ece_restrictions"`
EfficiencyPackage string `json:"efficiency_package"`
EUVehicle bool `json:"eu_vehicle"`
ExteriorColor string `json:"exterior_color"`
ExteriorTrim string `json:"exterior_trim"`
HasAirSuspension bool `json:"has_air_suspension"`
HasLudicrousMode bool `json:"has_ludicrous_mode"`
InteriorTrimType string `json:"interior_trim_type"`
KeyVersion int `json:"key_version"`
MotorizedChargePort bool `json:"motorized_charge_port"`
PerformancePackage string `json:"performance_package"`
Plg bool `json:"plg"`
RearDriveUnit string `json:"rear_drive_unit"`
RearSeatHeaters int `json:"rear_seat_heaters"`
RearSeatType int `json:"rear_seat_type"`
Rhd bool `json:"rhd"`
RoofColor string `json:"roof_color"`
SeatType int `json:"seat_type"`
SpoilerType string `json:"spoiler_type"`
SunRoofInstalled int `json:"sun_roof_installed"`
ThirdRowSeats string `json:"third_row_seats"`
Timestamp timeMsec `json:"timestamp"`
TrimBadging string `json:"trim_badging"`
UseRangeBadging bool `json:"use_range_badging"`
WheelType string `json:"wheel_type"`
}
// VehicleResponse contains the vehicle details from the Tesla API.
type VehicleResponse struct {
Response *Vehicle `json:"response"`
Count int `json:"count"`
}
// VehiclesResponse contains a slice of Vehicles from the Tesla API.
type VehiclesResponse struct {
Response []*Vehicle `json:"response"`
Count int `json:"count"`
}
// Vehicles fetches the vehicles associated to a Tesla account via the API.
func (c *Client) Vehicles() ([]*Vehicle, error) {
vehiclesResponse := &VehiclesResponse{}
if err := c.getJSON(c.baseURL+"/vehicles", vehiclesResponse); err != nil {
return nil, err
}
for _, v := range vehiclesResponse.Response {
v.c = c
}
return vehiclesResponse.Response, nil
}
// Vehicle fetches the vehicle by ID associated to a Tesla account via the API.
func (c *Client) Vehicle(vin string) (*Vehicle, error) {
resp := &VehicleResponse{}
if err := c.getJSON(c.baseURL+"/vehicles/"+vin, resp); err != nil {
return nil, err
}
resp.Response.c = c
return resp.Response, nil
}
// WithClient returns a copy of the vehicle with new client.
// Use e.g. when creating a secondary client for executing signed commands using Tesla Proxy.
func (v *Vehicle) WithClient(c *Client) *Vehicle {
vehicle := *v
vehicle.c = c
return &vehicle
}
func (v *Vehicle) basePath() string {
return strings.Join([]string{v.c.baseURL, "vehicles", v.Vin}, "/")
}
func (v *Vehicle) commandPath(command string) string {
return strings.Join([]string{v.basePath(), "command", command}, "/")
}
func (v *Vehicle) wakePath() string {
return strings.Join([]string{v.basePath(), "wake_up"}, "/")
}