-
Notifications
You must be signed in to change notification settings - Fork 73
/
states.go
283 lines (259 loc) · 11.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
package tesla
import (
"encoding/json"
"strconv"
"log"
)
// 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"`
}
// 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"`
}
// 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"`
}
// 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"`
}
// 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"`
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"`
Df int `json:"df"`
Dr int `json:"dr"`
ExteriorColor string `json:"exterior_color"`
Ft 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"`
Pf int `json:"pf"`
Pr int `json:"pr"`
RearSeatHeaters int `json:"rear_seat_heaters"`
RemoteStart bool `json:"remote_start"`
RemoteStartSupported bool `json:"remote_start_supported"`
Rhd bool `json:"rhd"`
RoofColor string `json:"roof_color"`
Rt 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"`
}
// Represents the request to get the states of the vehicle
type StateRequest struct {
Response struct {
*ChargeState
*ClimateState
*DriveState
*GuiSettings
*VehicleState
} `json:"response"`
}
// The response when a state is requested
type Response struct {
Bool bool `json:"response"`
}
// MobileEnabled returns if the vehicle is mobile enabled for Tesla API control
func (v *Vehicle) MobileEnabled() (bool, error) {
body, err := ActiveClient.get(BaseURL + "/vehicles/" + strconv.FormatInt(v.ID, 10) + "/mobile_enabled")
if err != nil {
return false, err
}
response := &Response{}
err = json.Unmarshal(body, response)
if err != nil {
return false, err
}
return response.Bool, nil
}
// ChargeState returns the charge state of the vehicle
func (v *Vehicle) ChargeState() (*ChargeState, error) {
stateRequest, err := 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 := fetchState("/climate_state", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.ClimateState, nil
}
func (v Vehicle) DriveState() (*DriveState, error) {
stateRequest, err := 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 := fetchState("/gui_settings", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.GuiSettings, nil
}
func (v Vehicle) VehicleState() (*VehicleState, error) {
stateRequest, err := fetchState("/vehicle_state", v.ID)
if err != nil {
return nil, err
}
return stateRequest.Response.VehicleState, nil
}
// A utility function to fetch the appropriate state of the vehicle
func fetchState(resource string, id int64) (*StateRequest, error) {
stateRequest := &StateRequest{}
body, err := ActiveClient.get(BaseURL + "/vehicles/" + strconv.FormatInt(id, 10) + "/data_request" + resource)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, stateRequest)
if 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{}
/*log.Println(BaseURL + "/vehicles/" + strconv.FormatInt(vid, 10) + "/vehicle_data")
body, err := ActiveClient.get(BaseURL + "/vehicles/" + strconv.FormatInt(vid, 10) + "/vehicle_data")
if err != nil {
return nil, err
}
err = json.Unmarshal(body, stateRequest)
if err != nil {
return nil, err
}*/
// climate_state
stateRequestClimate, err := 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 := 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 := 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 := 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 := 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
}