-
Notifications
You must be signed in to change notification settings - Fork 106
/
teslams.js
executable file
·670 lines (633 loc) · 24.1 KB
/
teslams.js
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
'use strict';
var request = require('request');
var util = require('util');
var JSONbig = require('json-bigint');
var portal = 'https://owner-api.teslamotors.com/api/1';
exports.portal = portal;
var owner_api = 'https://owner-api.teslamotors.com';
exports.portal = owner_api;
var token = '';
exports.token = token;
// emulate the android mobile app
var version = '2.1.79';
var model = 'SM-G900V';
var codename = 'REL';
var release = '4.4.4';
var locale = 'en_US';
var user_agent = 'Model S ' + version + ' (' + model + '; Android ' + codename + ' ' + release + '; ' + locale + ')';
var x_tesla_user_agent = 'TeslaApp/3.4.4-350/fad4a582e/android/9.0.0';
//Common HTTP header variable for all requests. Includes authentication credentials (token) and user agent string
var http_header;
var report = function(error, response, body, cb) {
if (!!cb) cb(error || (new Error(response.statusCode + ': ' + body)), body);
};
var report2 = function(call, body, cb) {
if (typeof cb === 'function') cb(new Error('expecting JSON response to ' + call + ' request'), body);
};
// backwards-compatible with previous API
// all() gives the callback the raw response to the /vehicles call
// vehicles gives the callback the first vehicle in the array returned
// get_vid gives the callback the ID of the first vehicle in the array returned
var all = exports.all = function(options, cb) {
if (!cb) cb = function(error, response, body) {/* jshint unused: false */};
//add option to call without using email and password
if (options.token) {
exports.token = options.token;
// set common HTTP Header used for all requests
http_header = {
'Authorization': 'Bearer ' + options.token,
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': user_agent,
'X-Tesla-User-Agent': x_tesla_user_agent,
//'Accept-Encoding': 'gzip'
// 'Accept-Encoding': 'gzip,deflate'
};
request( {
method : 'GET',
url: portal + '/vehicles',
gzip: true,
headers: http_header
}, cb);
} else {
http_header = {
'Authorization': 'Bearer ' + options.token,
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': user_agent,
'X-Tesla-User-Agent': x_tesla_user_agent,
};
request( {
method: 'POST',
url: owner_api + '/oauth/token',
gzip: true,
headers: http_header,
form: {
"grant_type" : "password",
"client_id" : 'e4a9949fcfa04068f59abb5a658f2bac0a3428e4652315490b659d5ab3f35a9e',
"client_secret" : 'c75f14bbadc8bee3a7594412c31416f8300256d7668ea7e6e7f06727bfb9d220',
"email" : options.email,
"password" : options.password }
}, function (error, response, body) {
try{
var authdata = JSON.parse( body );
token = authdata.access_token;
exports.token = token;
// set common HTTP Header used for all requests
http_header = {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': user_agent,
//'Accept-Encoding': 'gzip'
// 'Accept-Encoding': 'gzip,deflate'
};
} catch (e) {
console.log( 'Error parsing response to oauth token request');
}
if ((!!error) || ((response.statusCode !== 200) && (response.statusCode !== 302))) return report(error, response, body, cb);
request( {
method : 'GET',
url: portal + '/vehicles',
gzip: true,
headers: http_header
}, cb);
});
}
};
// returns first vehicle in list
var vehicles = exports.vehicles = function(options, cb) {
if (!cb) cb = function(data) {/* jshint unused: false */};
all(options, function (error, response, body) {
var data;
try { data = JSONbig.parse(body); } catch(err) { return cb(new Error('login failed\nerr: ' + err + '\nbody: ' + body)); }
if (!util.isArray(data.response)) return cb(new Error('expecting an array from Tesla Motors cloud service'));
data = data.response[0];
data.id = JSONbig.stringify(data.id);
cb((!!data.id) ? data : (new Error('expecting vehicle ID from Tesla Motors cloud service')));
});
};
// returns ID of first vehicle in list as a string to avoid bigint issues
exports.get_vid = function(options, cb) {
vehicles(options, function(data) {
if (!!data.id) data = data.id; if (!!cb) cb(data);
});
};
function set_token( token ) {
exports.token = token;
// set common HTTP Header used for all requests
http_header = {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': user_agent,
//'Accept-Encoding': 'gzip'
// 'Accept-Encoding': 'gzip,deflate'
};
}
exports.set_token = set_token;
function mobile_enabled( vid, cb ) {
request( {
method: 'GET',
url: portal + '/vehicles/' + vid + '/mobile_enabled',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('mobile_enabled', body, cb);
}
});
}
exports.mobile_enabled = mobile_enabled;
function get_charge_state( vid, cb ) {
request( {
method: 'GET',
url: portal + '/vehicles/' + vid + '/data_request/charge_state',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('charge_state', body, cb);
}
});
}
exports.get_charge_state = get_charge_state;
function get_climate_state( vid, cb ) {
request( {
method: 'GET',
url: portal + '/vehicles/' + vid + '/data_request/climate_state',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('climate_state', body, cb);
}
});
}
exports.get_climate_state = get_climate_state;
function get_drive_state( vid, cb ) {
request( {
method: 'GET',
url: portal + '/vehicles/' + vid + '/data_request/drive_state',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('drive_state', body, cb);
}
});
}
exports.get_drive_state = get_drive_state;
function get_vehicle_state( vid, cb ) {
request( {
method: 'GET',
url: portal + '/vehicles/' + vid + '/data_request/vehicle_state',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('vehicle_state', body, cb);
}
});
}
exports.get_vehicle_state = get_vehicle_state;
function get_gui_settings( vid, cb ) {
request( {
method: 'GET',
url: portal + '/vehicles/' + vid + '/data_request/gui_settings',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('gui_settings', body, cb);
}
});
}
exports.get_gui_settings = get_gui_settings;
function wake_up( vid, cb ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/wake_up',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('wake_up', body, cb);
}
});
}
exports.wake_up = wake_up;
function open_charge_port( vid, cb ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/charge_port_door_open',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('charge_port_door_open', body, cb);
}
});
}
exports.open_charge_port = open_charge_port;
var CHARGE_OFF = 0; // changes charge state to ON without effecting range mode
var CHARGE_ON = 1; // changes charge state to OFF without effecting range mode
function charge_state( params, cb ) {
var vid = params.id;
var state = params.charge;
// Change the range mode if necessary
if (state == CHARGE_ON || state == "on" || state == "start" || state === true ) {
state = "start";
}
if (state == CHARGE_OFF || state == "off" || state == "stop" || state === false ) {
state = "stop";
}
if (state == "start" || state == "stop" ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/charge_' + state,
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('charge_' + state, body, cb);
}
});
} else {
if (typeof cb == 'function') return cb( new Error("Invalid charge state = " + state));
else return false;
}
}
exports.charge_state = charge_state;
exports.CHARGE_OFF = CHARGE_OFF;
exports.CHARGE_ON = CHARGE_ON;
var RANGE_STD = 0; // changes range mode to STANDARD without effecting charge state
var RANGE_MAX = 1; // changes range mode to MAX_RANGE without effecting charge state
function charge_range( params, cb ) {
var vid = params.id;
var range = params.range;
var percent = params.percent;
if (range == RANGE_STD || range == "std" || range == "standard" ) {
range = "standard";
}
if (range == RANGE_MAX || range == "max" || range == "max_range") {
range = "max_range";
}
if (range == "standard" || range == "max_range" ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/charge_' + range,
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('charge_' + range, body, cb);
}
});
} else if ( range == "set" && (percent >= 50) && (percent <= 100) ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/set_charge_limit',
gzip: true,
headers: http_header,
form: {
"percent" : percent.toString()
}
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('set_charge_limit', body, cb);
}
});
} else {
if (typeof cb == 'function') return cb( new Error("Invalid charge range = " + range));
else return false;
}
}
exports.charge_range = charge_range;
exports.RANGE_STD = RANGE_STD;
exports.RANGE_MAX = RANGE_MAX;
function flash( vid, cb ) {
request({
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/flash_lights',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('flash_lights', body, cb);
}
});
}
exports.flash = flash;
function honk( vid, cb ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/honk_horn',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('honk_horn', body, cb);
}
});
}
exports.honk = honk;
var LOCK_OFF = 0;
var LOCK_ON = 1;
function door_lock( params, cb ) {
var vid = params.id;
var state = params.lock;
if (state == "lock" || state === true || state == "on" || state == "close" ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/door_lock',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('door_lock', body, cb);
}
});
} else if (state == "unlock" || state === false || state == "off" || state == "open" ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/door_unlock',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('door_unlock', body, cb);
}
});
} else {
if (typeof cb == 'function') return cb( new Error("Invalid door lock state = " + state));
else return false;
}
}
exports.door_lock = door_lock;
exports.LOCK_OFF = LOCK_OFF;
exports.LOCK_ON = LOCK_ON;
var TEMP_HI = 32;
var TEMP_LO = 17;
function set_temperature( params, cb ) {
var dtemp = params.dtemp;
var ptemp = params.ptemp;
var vid = params.id;
var error = false;
//var temp_str = "";
if ( dtemp !== undefined && dtemp <= TEMP_HI && dtemp >= TEMP_LO) {
//temp_str = 'driver_temp=' + dtemp; // change from string to JSON form data
} else {
error = true;
}
// if no passenger temp is passed, the driver temp is also used as the passenger temp
if ( ptemp !== undefined && ptemp <= TEMP_HI && ptemp >= TEMP_LO) {
//temp_str = temp_str +'&passenger_temp=' + ptemp; // change from string to JSON form data
} else if ( ptemp === undefined ) {
ptemp = dtemp;
} else {
error = true;
}
if (!error) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/set_temps',
gzip: true,
headers: http_header,
form: {
"driver_temp" : dtemp.toString(),
"passenger_temp" : ptemp.toString(),
}
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('set_temps', body, cb);
}
});
} else {
if (typeof cb == 'function') return cb( new Error('Invalid temperature setting (' + dtemp + 'C), Passenger (' + ptemp + 'C)'));
else return false;
}
}
exports.set_temperature = set_temperature;
exports.TEMP_HI = TEMP_HI;
exports.TEMP_LO = TEMP_LO;
var CLIMATE_OFF = 0;
var CLIMATE_ON = 1;
function auto_conditioning( params, cb ) {
var vid = params.id;
var state = params.climate;
if (state == CLIMATE_ON) { state = true; }
if (state == CLIMATE_OFF) { state = false; }
if (state == "start" || state === true || state == "on" ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/auto_conditioning_start',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('auto_conditioning_start', body, cb);
}
});
} else if (state == "stop" || state === false || state == "off" ) {
request( {
method: 'POST',
url: portal + '/vehicles/' + vid + '/command/auto_conditioning_stop',
gzip: true,
headers: http_header
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('auto_conditioning_stop', body, cb);
}
});
} else {
if (typeof cb == 'function') return cb( new Error("Invalid auto conditioning state = " + state));
else return false;
}
}
exports.auto_conditioning = auto_conditioning;
exports.CLIMATE_OFF = CLIMATE_OFF;
exports.CLIMATE_ON = CLIMATE_ON;
var ROOF_CLOSE = 0;
var ROOF_VENT = 1;
var ROOF_COMFORT = 2;
var ROOF_OPEN = 3;
function sun_roof( params, cb ) {
var vid = params.id;
var state = params.roof;
var percent = params.percent;
// add a check that their is a sunroof on the car??
if (state == ROOF_CLOSE) { state = "close"; }
if (state == ROOF_VENT) { state = "vent"; }
if (state == ROOF_COMFORT) { state = "comfort"; }
if (state == ROOF_OPEN) { state = "open"; }
if (state == "open" || state == "close" || state == "comfort" || state == "vent") {
request( {
method: 'POST',
url: portal +'/vehicles/' + vid + '/command/sun_roof_control',
gzip: true,
headers: http_header,
form: {
'state': state
}
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('sun_roof_control ' + state, body, cb);
}
});
} else if ( (state == "move") && (percent >= 0) && (percent <= 100) ) {
request( {
method: 'POST',
url: portal +'/vehicles/' + vid + '/command/sun_roof_control',
gzip: true,
headers: http_header,
form: {
'state': 'move',
'percent': percent.toString()
}
}, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (typeof cb == 'function') return cb( data.response );
else return true;
} catch (err) {
return report2('sun_roof_control move', body, cb);
}
});
} else {
if (typeof cb == 'function') return cb( new Error("Invalid sun roof state " + util.inspect(params)));
else return false;
}
}
exports.sun_roof = sun_roof;
exports.ROOF_CLOSE = ROOF_CLOSE;
exports.ROOF_VENT = ROOF_VENT;
exports.ROOF_COMFORT = ROOF_COMFORT;
exports.ROOF_OPEN = ROOF_OPEN;
//left off here//
// Streaming API stuff is below. Everything above is the REST API
//
// Required options to teslams.stream() are {
// email: 'your teslamotors.com login',
// password: 'token returned from a prior call to teslams.vehicles()',
// vehicle_id: 'Long form vehicle_id returned from a prior call to teslams.vehicles()'
// a callback that expects ( error, response, body) for the HTTP response
// }
// See examples/examplestream.js for a simple one poll working example of how to use this function
// See examples/streaming.js for a more complicated but useful continuous polling example of streaming
exports.stream_columns = [ 'speed',
'odometer',
'soc',
'elevation',
'est_heading',
'est_lat',
'est_lng',
'power',
'shift_state',
'range',
'est_range',
'heading'
];
exports.stream = function(options, cb) {
if (!cb) cb = function(error, response, body) {/* jshint unused: false */};
request({ method : 'GET',
url : 'https://streaming.vn.teslamotors.com/stream/' + options.vehicle_id + '/?values=' + exports.stream_columns.join(','),
gzip: true,
auth :
{ user : options.email,
pass : options.password
}
}, cb);
};