forked from bogosj/tesla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.go
408 lines (376 loc) · 16.1 KB
/
states.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
package tesla
import (
"fmt"
"log"
"strconv"
"strings"
"time"
)
// ChargeState contains the current charge states that exist within the vehicle.
type ChargeState struct {
ChargingState string `json:"charging_state"`
ChargeLimitSoc int `json:"charge_limit_soc"`
ChargeLimitSocStd int `json:"charge_limit_soc_std"`
ChargeLimitSocMin int `json:"charge_limit_soc_min"`
ChargeLimitSocMax int `json:"charge_limit_soc_max"`
ChargeToMaxRange bool `json:"charge_to_max_range"`
BatteryHeaterOn bool `json:"battery_heater_on"`
NotEnoughPowerToHeat bool `json:"not_enough_power_to_heat"`
MaxRangeChargeCounter int `json:"max_range_charge_counter"`
FastChargerPresent bool `json:"fast_charger_present"`
FastChargerType string `json:"fast_charger_type"`
BatteryRange float64 `json:"battery_range"`
EstBatteryRange float64 `json:"est_battery_range"`
IdealBatteryRange float64 `json:"ideal_battery_range"`
BatteryLevel int `json:"battery_level"`
UsableBatteryLevel int `json:"usable_battery_level"`
BatteryCurrent interface{} `json:"battery_current"`
ChargeEnergyAdded float64 `json:"charge_energy_added"`
ChargeMilesAddedRated float64 `json:"charge_miles_added_rated"`
ChargeMilesAddedIdeal float64 `json:"charge_miles_added_ideal"`
ChargerVoltage interface{} `json:"charger_voltage"`
ChargerPilotCurrent interface{} `json:"charger_pilot_current"`
ChargerActualCurrent interface{} `json:"charger_actual_current"`
ChargerPower interface{} `json:"charger_power"`
TimeToFullCharge float64 `json:"time_to_full_charge"`
TripCharging interface{} `json:"trip_charging"`
ChargeRate float64 `json:"charge_rate"`
ChargePortDoorOpen bool `json:"charge_port_door_open"`
MotorizedChargePort bool `json:"motorized_charge_port"`
ScheduledChargingStartTime interface{} `json:"scheduled_charging_start_time"`
ScheduledChargingPending bool `json:"scheduled_charging_pending"`
UserChargeEnableRequest interface{} `json:"user_charge_enable_request"`
ChargeEnableRequest bool `json:"charge_enable_request"`
EuVehicle bool `json:"eu_vehicle"`
ChargerPhases interface{} `json:"charger_phases"`
ChargePortLatch string `json:"charge_port_latch"`
ChargeCurrentRequest int `json:"charge_current_request"`
ChargeCurrentRequestMax int `json:"charge_current_request_max"`
ManagedChargingActive bool `json:"managed_charging_active"`
ManagedChargingUserCanceled bool `json:"managed_charging_user_canceled"`
ManagedChargingStartTime interface{} `json:"managed_charging_start_time"`
ChargePortcoldWeatherMode bool `json:"charge_port_cold_weather_mode"`
ConnChargeCable string `json:"conn_charge_cable"`
FastChargerBrand string `json:"fast_charger_brand"`
MinutesToFullCharge int `json:"minutes_to_full_charge"`
}
// ClimateState contains the current climate states availale from the vehicle.
type ClimateState struct {
InsideTemp float64 `json:"inside_temp"`
OutsideTemp float64 `json:"outside_temp"`
DriverTempSetting float64 `json:"driver_temp_setting"`
PassengerTempSetting float64 `json:"passenger_temp_setting"`
LeftTempDirection float64 `json:"left_temp_direction"`
RightTempDirection float64 `json:"right_temp_direction"`
IsAutoConditioningOn bool `json:"is_auto_conditioning_on"`
IsFrontDefrosterOn bool `json:"is_front_defroster_on"`
IsRearDefrosterOn bool `json:"is_rear_defroster_on"`
FanStatus interface{} `json:"fan_status"`
IsClimateOn bool `json:"is_climate_on"`
MinAvailTemp float64 `json:"min_avail_temp"`
MaxAvailTemp float64 `json:"max_avail_temp"`
SeatHeaterLeft int `json:"seat_heater_left"`
SeatHeaterRight int `json:"seat_heater_right"`
SeatHeaterRearLeft int `json:"seat_heater_rear_left"`
SeatHeaterRearRight int `json:"seat_heater_rear_right"`
SeatHeaterRearCenter int `json:"seat_heater_rear_center"`
SeatHeaterRearRightBack int `json:"seat_heater_rear_right_back"`
SeatHeaterRearLeftBack int `json:"seat_heater_rear_left_back"`
SmartPreconditioning bool `json:"smart_preconditioning"`
BatteryHeater bool `json:"battery_heater"`
BatteryHeaterNoPower interface{} `json:"battery_heater_no_power"`
ClimateKeeperMode string `json:"climate_keeper_mode"`
DefrostMode int `json:"defrost_mode"`
IsPreconditioning bool `json:"is_preconditioning"`
RemoteHeaterControlEnabled bool `json:"remote_heater_control_enabled"`
SideMirrorHeaters bool `json:"side_mirror_heaters"`
WiperBladeHeater bool `json:"wiper_blade_heater"`
}
// DriveState contains the current drive state of the vehicle.
type DriveState struct {
ShiftState interface{} `json:"shift_state"`
Speed float64 `json:"speed"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Heading int `json:"heading"`
GpsAsOf int64 `json:"gps_as_of"`
NativeLatitude float64 `json:"native_latitude"`
NativeLocationSupported int `json:"native_location_supported"`
NativeLongitude float64 `json:"native_longitude"`
NativeType string `json:"native_type"`
Power int `json:"power"`
}
// GuiSettings contains the current GUI settings of the vehicle.
type GuiSettings struct {
GuiDistanceUnits string `json:"gui_distance_units"`
GuiTemperatureUnits string `json:"gui_temperature_units"`
GuiChargeRateUnits string `json:"gui_charge_rate_units"`
Gui24HourTime bool `json:"gui_24_hour_time"`
GuiRangeDisplay string `json:"gui_range_display"`
ShowRangeUnits bool `json:"show_range_units"`
}
// VehicleState contains the current state of the vehicle.
type VehicleState struct {
APIVersion int `json:"api_version"`
AutoParkState string `json:"autopark_state"`
AutoParkStateV2 string `json:"autopark_state_v2"`
AutoParkStateV3 string `json:"autopark_state_v3"`
CalendarSupported bool `json:"calendar_supported"`
CarType string `json:"car_type"`
CarVersion string `json:"car_version"`
CenterDisplayState int `json:"center_display_state"`
DarkRims bool `json:"dark_rims"`
DriverFrontDoor int `json:"df"`
DriverRearDoor int `json:"dr"`
ExteriorColor string `json:"exterior_color"`
FrontTrunk int `json:"ft"`
HasSpoiler bool `json:"has_spoiler"`
Locked bool `json:"locked"`
NotificationsSupported bool `json:"notifications_supported"`
Odometer float64 `json:"odometer"`
ParsedCalendarSupported bool `json:"parsed_calendar_supported"`
PerfConfig string `json:"perf_config"`
PassengerFrontDoor int `json:"pf"`
PassengerRearDoor int `json:"pr"`
RearSeatHeaters int `json:"rear_seat_heaters"`
RemoteStart bool `json:"remote_start"`
RemoteStartSupported bool `json:"remote_start_supported"`
RightHandDrive bool `json:"rhd"`
RoofColor string `json:"roof_color"`
RearTrunk int `json:"rt"`
SentryMode bool `json:"sentry_mode"`
SentryModeAvailable bool `json:"sentry_mode_available"`
SeatType int `json:"seat_type"`
SpoilerType string `json:"spoiler_type"`
SunRoofInstalled int `json:"sun_roof_installed"`
SunRoofPercentOpen int `json:"sun_roof_percent_open"`
SunRoofState string `json:"sun_roof_state"`
ThirdRowSeats string `json:"third_row_seats"`
ValetMode bool `json:"valet_mode"`
VehicleName string `json:"vehicle_name"`
WheelType string `json:"wheel_type"`
FrontDriverWindow int `json:"fd_window"`
FrontPassengerWindow int `json:"fp_window"`
RearDriverWindow int `json:"rd_window"`
RearPassengerWindow int `json:"rp_window"`
IsUserPresent bool `json:"is_user_present"`
RemoteStartEnabled bool `json:"remote_start_enabled"`
ValetPinNeeded bool `json:"valet_pin_needed"`
MediaState struct {
RemoteControlEnabled bool `json:"remote_control_enabled"`
} `json:"media_state"`
SoftwareUpdate struct {
DownloadPerc int `json:"download_perc"`
ExpectedDurationSec int `json:"expected_duration_sec"`
InstallPerc int `json:"install_perc"`
Status string `json:"status"`
Version string `json:"version"`
} `json:"software_update" `
SpeedLimitMode struct {
Active bool `json:"active"`
CurrentLimitMph float64 `json:"current_limit_mph"`
MaxLimitMph float64 `json:"max_limit_mph"`
MinLimitMph float64 `json:"min_limit_mph"`
PinCodeSet bool `json:"pin_code_set"`
} `json:"speed_limit_mode"`
}
// ServiceData contains the service data of the vehicle.
type ServiceData struct {
ServiceETC time.Time `json:"service_etc"`
ServiceStatus string `json:"service_status"`
}
// StateRequest represents the request to get the states of the vehicle.
type StateRequest struct {
Response struct {
Timestamp timeMsec `json:"timestamp"`
*ChargeState
*ClimateState
*DriveState
*GuiSettings
*VehicleState
*ServiceData
} `json:"response"`
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
// MobileEnabledResponse is the response when a state is requested.
type MobileEnabledResponse struct {
Bool bool `json:"response"`
}
// MobileEnabled returns if the vehicle is mobile enabled for Tesla API control
func (v *Vehicle) MobileEnabled() (bool, error) {
r := &MobileEnabledResponse{}
if err := v.c.getJSON(v.c.baseURL+"/vehicles/"+strconv.FormatInt(v.ID, 10)+"/mobile_enabled", r); err != nil {
return false, err
}
return r.Bool, nil
}
type timeSecs struct {
time.Time
}
func (t *timeSecs) UnmarshalJSON(b []byte) error {
i, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
t.Time = (time.Unix(i, 0))
return nil
}
type timeMsec struct {
time.Time
}
func (t *timeMsec) UnmarshalJSON(b []byte) error {
i, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
t.Time = (time.Unix(i/1000, i%1000))
return nil
}
// NearbyChargingSitesResponse represents the charging sites returned from the API.
type NearbyChargingSitesResponse struct {
Response struct {
CongestionSyncTimeUtcSecs timeSecs `json:"congestion_sync_time_utc_secs"`
DestinationCharging []struct {
Location struct {
Lat float64 `json:"lat"`
Long float64 `json:"long"`
} `json:"location"`
Name string `json:"name"`
Type string `json:"type"`
DistanceMiles float64 `json:"distance_miles"`
} `json:"destination_charging"`
Superchargers []struct {
Location struct {
Lat float64 `json:"lat"`
Long float64 `json:"long"`
} `json:"location"`
Name string `json:"name"`
Type string `json:"type"`
DistanceMiles float64 `json:"distance_miles"`
AvailableStalls int `json:"available_stalls"`
TotalStalls int `json:"total_stalls"`
SiteClosed bool `json:"site_closed"`
} `json:"superchargers"`
Timestamp timeMsec `json:"timestamp"`
} `json:"response"`
}
// NearbyChargingSites returns the charging sites near the vehicle.
func (v *Vehicle) NearbyChargingSites() (*NearbyChargingSitesResponse, error) {
resp := &NearbyChargingSitesResponse{}
path := strings.Join([]string{v.c.baseURL, "vehicles", strconv.FormatInt(v.ID, 10), "nearby_charging_sites"}, "/")
if err := v.c.getJSON(path, resp); err != nil {
return nil, err
}
return resp, nil
}
// ChargeState returns the charge state of the vehicle.
func (v *Vehicle) ChargeState() (*ChargeState, error) {
stateRequest, err := v.c.fetchState("charge_state", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.ChargeState, nil
}
// ClimateState returns the climate state of the vehicle.
func (v Vehicle) ClimateState() (*ClimateState, error) {
stateRequest, err := v.c.fetchState("climate_state", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.ClimateState, nil
}
// DriveState returns the drive state of the vehicle.
func (v Vehicle) DriveState() (*DriveState, error) {
stateRequest, err := v.c.fetchState("drive_state", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.DriveState, nil
}
// GuiSettings returns the GUI settings of the vehicle.
func (v Vehicle) GuiSettings() (*GuiSettings, error) {
stateRequest, err := v.c.fetchState("gui_settings", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.GuiSettings, nil
}
// VehicleState returns the state of the vehicle.
func (v Vehicle) VehicleState() (*VehicleState, error) {
stateRequest, err := v.c.fetchState("vehicle_state", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.VehicleState, nil
}
// ServiceData returns the service data for the vehicle.
func (v Vehicle) ServiceData() (*ServiceData, error) {
stateRequest, err := v.c.fetchState("service_data", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.ServiceData, nil
}
func stateError(sr *StateRequest) error {
if sr.Error == "" {
return nil
}
if sr.ErrorDescription != "" {
return fmt.Errorf("%s: %s", sr.Error, sr.ErrorDescription)
}
return fmt.Errorf("%s", sr.Error)
}
// A utility function to fetch the appropriate state of the vehicle
func (c *Client) fetchState(resource string, id int64) (*StateRequest, error) {
stateRequest := &StateRequest{}
path := strings.Join([]string{c.baseURL, "vehicles", strconv.FormatInt(id, 10), "data_request", resource}, "/")
if err := c.getJSON(path, stateRequest); err != nil {
return nil, err
}
if err := stateError(stateRequest); err != nil {
return nil, err
}
return stateRequest, nil
}
// Data : Get data of the vehicle (calling this will not permit the car to sleep)
func (v Vehicle) Data(vid int64) (*StateRequest, error) {
log.Println("Retreiving vehicle data")
stateRequest := &StateRequest{}
// climate_state
stateRequestClimate, err := v.c.fetchState("climate_state", v.ID)
if err != nil {
log.Println("Error getting climate_state")
return nil, err
}
stateRequest.Response.ClimateState = stateRequestClimate.Response.ClimateState
// drive_state
stateRequestGui, err := v.c.fetchState("drive_state", v.ID)
if err != nil {
log.Println("Error getting drive_state")
return nil, err
}
stateRequest.Response.DriveState = stateRequestGui.Response.DriveState
// gui_settings
stateRequestSettings, err := v.c.fetchState("gui_settings", v.ID)
if err != nil {
log.Println("Error getting gui_settings")
return nil, err
}
stateRequest.Response.GuiSettings = stateRequestSettings.Response.GuiSettings
// vehicle_state
stateRequestVehicle, err := v.c.fetchState("vehicle_state", v.ID)
if err != nil {
log.Println("Error getting vehicle_state")
return nil, err
}
stateRequest.Response.VehicleState = stateRequestVehicle.Response.VehicleState
// charge_state
stateRequestCharge, err := v.c.fetchState("charge_state", v.ID)
if err != nil {
log.Println("Error getting charge_state")
return nil, err
}
stateRequest.Response.ChargeState = stateRequestCharge.Response.ChargeState
return stateRequest, nil
}