-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
429 lines (363 loc) · 15.1 KB
/
app.py
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
import json
import logging
from botocore.vendored import requests
from datetime import date, datetime
from chalice import Chalice, CORSConfig
#####
# VARS
#####
cors_config = CORSConfig(
allow_origin='*',
allow_headers=['Accept'],
max_age=600
)
apiEndpoint = 'https://owner-api.teslamotors.com/api/1/'
res = {}
#####
# HELPERS
#####
def json_serial(obj):
logger.debug('in json_serial()...')
if isinstance(obj, (datetime, date)):
return obj.isoformat()
raise TypeError("Type %s not serializable" % type(obj))
def return_json(statusCode, body, headers={}):
logger.debug('in return_json()......')
headers['Content-Type'] = 'application/json'
headers['Access-Control-Allow-Headers'] = 'Content-Type,X-Amz-Date,Authorization,X-Api-Key'
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE'
headers['Access-Control-Allow-Origin'] = '*'
return {
'statusCode': statusCode,
'body': json.loads(json.dumps(body, default=json_serial)),
'headers': headers}
#####
# LOGGING
#####
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
#####
# CHALICE
#####
app = Chalice(app_name="root")
#####
# ROUTES
#####
@app.route('/command/{vehicleId}/sentry_mode_off', methods=['POST'], cors=cors_config)
def vehicles_command_sentry_mode_off(vehicleId):
global res
logger.info('in /command/{vehicleId}/sentry_mode_off POST')
try:
res = command(vehicleId, app.current_request.json_body, 'set_sentry_mode', {'on': False})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to turn off sentry mode.'
return return_json(500, res)
@app.route('/command/{vehicleId}/sentry_mode_on', methods=['POST'], cors=cors_config)
def vehicles_command_sentry_mode_on(vehicleId):
global res
logger.info('in /command/{vehicleId}/sentry_mode_on POST')
try:
res = command(vehicleId, app.current_request.json_body, 'set_sentry_mode', {'on': 'on'})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to turn on sentry mode.'
return return_json(500, res)
@app.route('/command/{vehicleId}/remote_steering_wheel_heater_off', methods=['POST'], cors=cors_config)
def vehicles_command_remote_steering_wheel_heater_off(vehicleId):
global res
logger.info('in /command/{vehicleId}/remote_steering_wheel_heater_off POST')
try:
res = command(vehicleId, app.current_request.json_body, 'remote_steering_wheel_heater_request', {'on': False})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to turn off steering wheel heater.'
return return_json(500, res)
@app.route('/command/{vehicleId}/remote_steering_wheel_heater_on', methods=['POST'], cors=cors_config)
def vehicles_command_remote_steering_wheel_heater_on(vehicleId):
global res
logger.info('in /command/{vehicleId}/remote_steering_wheel_heater_on POST')
try:
res = command(vehicleId, app.current_request.json_body, 'remote_steering_wheel_heater_request', {'on': True})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to turn on steering wheel heater.'
return return_json(500, res)
@app.route('/command/{vehicleId}/remote_seat_heater_request/{seats}/{level}', methods=['POST'], cors=cors_config)
def vehicles_seat_heater_request(vehicleId, seats, level):
global res
seatsDict = {
'driver': 0,
'passenger': 1,
'left': 2,
'center': 4,
'right': 5}
levelDict = {
'off': 0,
'low': 1,
'medium': 2,
'high': 3}
logger.info('in /command/{vehicleId}/remote_seat_heater_request/{seats}/{level} POST')
try:
res = command(vehicleId, app.current_request.json_body, 'remote_seat_heater_request', {'heater': seatsDict[seats], 'level': levelDict[level]})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to set seat heat temperature.'
return return_json(500, res)
@app.route('/command/{vehicleId}/set_temps/passenger/{temp}', methods=['POST'], cors=cors_config)
def vehicles_command_set_temp_passenger(vehicleId, temp):
global res
logger.info('in /command/{vehicleId}/set_temps/passenger/{temp} POST')
try:
res = command(vehicleId, app.current_request.json_body, 'set_temps', {'driver_temp': 22.2, 'passenger_temp': temp})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to set passenger temprature for HVAC.'
return return_json(500, res)
@app.route('/command/{vehicleId}/set_temps/driver/{temp}', methods=['POST'], cors=cors_config)
def vehicles_command_set_temp_driver(vehicleId, temp):
global res
logger.info('in /command/{vehicleId}/set_temps/driver/{temp} POST')
try:
res = command(vehicleId, app.current_request.json_body, 'set_temps', {'driver_temp': temp, 'passenger_temp': 22.2})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to set driver temprature for HVAC.'
return return_json(500, res)
@app.route('/command/{vehicleId}/set_temps/{temp}', methods=['POST'], cors=cors_config)
def vehicles_command_set_temp(vehicleId, temp):
global res
logger.info('in /command/{vehicleId}/set_temps/{temp} POST')
try:
res = command(vehicleId, app.current_request.json_body, 'set_temps', {'driver_temp': temp, 'passenger_temp': temp})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to set temprature for HVAC.'
return return_json(500, res)
@app.route('/command/{vehicleId}/auto_conditioning_start', methods=['POST'], cors=cors_config)
def vehicles_command_auto_conditioning_start(vehicleId):
global res
logger.info('in /command/{vehicleId}/auto_conditioning_start POST')
try:
res = command(vehicleId, app.current_request.json_body, 'auto_conditioning_start')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to turn on HVAC.'
return return_json(500, res)
@app.route('/command/{vehicleId}/auto_conditioning_stop', methods=['POST'], cors=cors_config)
def vehicles_command_auto_conditioning_stop(vehicleId):
global res
logger.info('in /command/{vehicleId}/auto_conditioning_stop POST')
try:
res = command(vehicleId, app.current_request.json_body, 'auto_conditioning_stop')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to turn off HVAC.'
return return_json(500, res)
@app.route('/command/{vehicleId}/set_charge_limit/{chargeLimit}', methods=['POST'], cors=cors_config)
def vehicles_command_set_charge_limit(vehicleId, chargeLimit):
global res
logger.info('in /command/{vehicleId}/set_charge_limit/{chargeLimit} POST')
try:
res = command(vehicleId, app.current_request.json_body, 'set_charge_limit', {'percent': chargeLimit})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = f'Failed to set charge limit to {chargeLimit}.'
return return_json(500, res)
@app.route('/command/{vehicleId}/charge_max_range', methods=['POST'], cors=cors_config)
def vehicles_command_charge_max_range(vehicleId):
global res
logger.info('in /command/{vehicleId}/charge_max_range POST')
try:
res = command(vehicleId, app.current_request.json_body, 'charge_max_range')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to set charge standard to max.'
return return_json(500, res)
@app.route('/command/{vehicleId}/charge_standard', methods=['POST'], cors=cors_config)
def vehicles_command_charge_standard(vehicleId):
global res
logger.info('in /command/{vehicleId}/charge_standard POST')
try:
res = command(vehicleId, app.current_request.json_body, 'charge_standard')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to set charge standard.'
return return_json(500, res)
@app.route('/command/{vehicleId}/charge_stop', methods=['POST'], cors=cors_config)
def vehicles_command_charge_stop(vehicleId):
global res
logger.info('in /command/{vehicleId}/charge_stop POST')
try:
res = command(vehicleId, app.current_request.json_body, 'charge_stop')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to stop charging.'
return return_json(500, res)
@app.route('/command/{vehicleId}/charge_start', methods=['POST'], cors=cors_config)
def vehicles_command_charge_start(vehicleId):
global res
logger.info('in /command/{vehicleId}/charge_start POST')
try:
res = command(vehicleId, app.current_request.json_body, 'charge_start')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to start charging.'
return return_json(500, res)
@app.route('/command/{vehicleId}/charge_port_door_open', methods=['POST'], cors=cors_config)
def vehicles_command_charge_port_door_open(vehicleId):
global res
logger.info('in /command/{vehicleId}/charge_port_door_open POST')
try:
res = command(vehicleId, app.current_request.json_body, 'charge_port_door_open')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to open charge port door.'
return return_json(500, res)
@app.route('/command/{vehicleId}/charge_port_door_close', methods=['POST'], cors=cors_config)
def vehicles_command_charge_port_door_close(vehicleId):
global res
logger.info('in /command/{vehicleId}/charge_port_door_close POST')
try:
res = command(vehicleId, app.current_request.json_body, 'charge_port_door_close')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to close charge port door.'
return return_json(500, res)
@app.route('/command/{vehicleId}/open_frunk', methods=['POST'], cors=cors_config)
def vehicles_command_open_frunk(vehicleId):
global res
logger.info('in /command/{vehicleId}/open_frunk POST')
try:
res = command(vehicleId, app.current_request.json_body, 'actuate_trunk', {"which_trunk": 'front'})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to open frunk.'
return return_json(500, res)
@app.route('/command/{vehicleId}/open_trunk', methods=['POST'], cors=cors_config)
def vehicles_command_open_trunk(vehicleId):
global res
logger.info('in /command/{vehicleId}/open_trunk POST')
try:
res = command(vehicleId, app.current_request.json_body, 'actuate_trunk', {"which_trunk": 'rear'})
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to open trunk.'
return return_json(500, res)
@app.route('/command/{vehicleId}/honk_horn', methods=['POST'], cors=cors_config)
def vehicles_command_honk_horn(vehicleId):
global res
logger.info('in /command/{vehicleId}/honk_horn POST')
try:
res = command(vehicleId, app.current_request.json_body, 'honk_horn')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to honk horn.'
return return_json(500, res)
@app.route('/command/{vehicleId}/flash_lights', methods=['POST'], cors=cors_config)
def vehicles_command_flash_lights(vehicleId):
global res
logger.info('in /command/{vehicleId}/flash_lights POST')
try:
res = command(vehicleId, app.current_request.json_body, 'flash_lights')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to flash lights.'
return return_json(500, res)
@app.route('/command/{vehicleId}/door_lock', methods=['POST'], cors=cors_config)
def vehicles_command_door_lock(vehicleId):
global res
logger.info('in /command/{vehicleId}/door_lock POST')
try:
res = command(vehicleId, app.current_request.json_body, 'door_lock')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to lock door.'
return return_json(500, res)
@app.route('/command/{vehicleId}/door_unlock', methods=['POST'], cors=cors_config)
def vehicles_command_door_unlock(vehicleId):
global res
logger.info('in /command/{vehicleId}/door_unlock POST')
try:
res = command(vehicleId, app.current_request.json_body, 'door_unlock')
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to unlock door.'
return return_json(500, res)
@app.route('/command/{vehicleId}/wake_up', methods=['POST'], cors=cors_config)
def vehicles_command_wake_up(vehicleId):
global res
logger.info('in /command/{vehicleId}/wake_up POST')
try:
res = wake(vehicleId, app.current_request.json_body)
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to wake up car.'
return return_json(500, res)
@app.route('/vehicles', methods=['POST'], cors=cors_config)
def vehicles_list():
global res
logger.info('in /vehicles POST')
try:
res = postVehicles(app.current_request.json_body)
return return_json(200, res)
except Exception as e:
logger.error(str(e))
res['error'] = 'Failed to list vehicles'
return return_json(500, res)
def wake(vehicleId, Params):
bearerToken = Params['bearer_token']
ret = requests.post(
f'{apiEndpoint}vehicles/{vehicleId}/wake_up',
headers={
'Authorization': f'bearer {bearerToken}'})
if ret.status_code == 200:
return f'wake_up on {vehicleId} successful.'
else:
raise Exception(f'vehicle_id:{vehicleId}, problem with command:wake_up')
def command(vehicleId, Params, command, data={}):
bearerToken = Params['bearer_token']
ret = requests.post(
f'{apiEndpoint}vehicles/{vehicleId}/command/{command}',
data=data,
headers={
'Authorization': f'bearer {bearerToken}'})
if ret.status_code == 200:
return f'{command} on {vehicleId} successful.'
else:
raise Exception(f'vehicle_id:{vehicleId} problem with command:{command}')
def postVehicles(Params):
bearerToken = Params['bearer_token']
response = requests.get(
f'{apiEndpoint}vehicles',
headers={
'Authorization': f'bearer {bearerToken}'})
vehicles_json = response.json()
vehicles = []
for vehicle in vehicles_json['response']:
vehicles.append(str(vehicle['id']))
return vehicles