forked from hjespers/teslams
-
Notifications
You must be signed in to change notification settings - Fork 2
/
teslams.js
executable file
·479 lines (447 loc) · 14.5 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
var request = require('request');
var util = require('util');
var portal = 'https://portal.vn.teslamotors.com';
exports.portal = portal;
var report = function(error, response, body, cb) {
if (!!cb) cb(error || (new Error(response.statusCode + ': ' + 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 */};
request({ method : 'POST'
, url : portal + '/login'
, form :
{ "user_session[email]" : options.email
, "user_session[password]" : options.password
}
}, function (error, response, body) {
if ((!!error) || ((response.statusCode !== 200) && (response.statusCode !== 302))) return report(error, response, body, cb);
request(portal + '/vehicles', 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 = JSON.parse(body); } catch(err) { return cb(new Error('login failed')); }
if (!util.isArray(data)) return cb(new Error('expecting an array from Tesla Motors cloud service'));
data = data[0];
cb((!!data.id) ? data : (new Error('expecting vehicle ID from Tesla Motors cloud service')));
});
};
// returns ID of first vehicle in list
exports.get_vid = function(options, cb) {
vehicles(options, function(data) {
if (!!data.id) data = data.id;
if (!!cb) cb(data);
});
};
function mobile_enabled( vid, cb ) {
request( portal + '/vehicles/' + vid + '/mobile_enabled', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
} catch (err) {
if (cb !== undefined) {
return cb( err );
} else {
return err;
}
}
});
}
exports.mobile_enabled = mobile_enabled;
function get_charge_state( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/charge_state', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
try {
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
} catch (err) {
if (cb !== undefined) {
return cb( err );
} else {
return err;
}
}
});
}
exports.get_charge_state = get_charge_state;
function get_climate_state( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/climate_state', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
}
exports.get_climate_state = get_climate_state;
function get_drive_state( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/drive_state', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
}
exports.get_drive_state = get_drive_state;
function get_vehicle_state( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/vehicle_state', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
}
exports.get_vehicle_state = get_vehicle_state;
function get_gui_settings( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/gui_settings', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
}
exports.get_gui_settings = get_gui_settings;
function wake_up( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/wake_up', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
// console.log("\nWake up Model S it's time to talk!");
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
}
exports.wake_up = wake_up;
function open_charge_port( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/charge_port_door_open', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
}
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;
// console.log ('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( portal + '/vehicles/' + vid + '/command/charge_' + state, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
} else {
if (cb !== undefined) {
var err = new Error("Invalid charge state = " + state + " or percent = " + percent);
return cb( err );
} else {
return;
}
}
}
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( portal + '/vehicles/' + vid + '/command/charge_' + range, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return true;
}
});
} else if ( range == "set" && (percent >= 50) && (percent <= 100) ) {
request( portal + '/vehicles/' + vid + '/command/set_charge_limit?state=set&percent=' + percent, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return true;
}
});
} else {
if (cb !== undefined) {
var err = new Error("Invalid charge range = " + range);
return cb( err );
} else {
return;
}
}
}
exports.charge_range = charge_range;
exports.RANGE_STD = RANGE_STD;
exports.RANGE_MAX = RANGE_MAX;
function flash( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/flash_lights', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return true;
}
});
}
exports.flash = flash;
function honk( vid, cb ) {
request( portal + '/vehicles/' + vid + '/command/honk_horn', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
}
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( portal + '/vehicles/' + vid + '/command/door_lock', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
} else if (state == "unlock" || state === false || state == "off" || state == "open" ) {
request( portal + '/vehicles/' + vid + '/command/door_unlock', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
} else {
if (cb !== undefined) {
var err = new Error("Invalid door lock state = " + state);
return cb( err );
} else {
return;
}
}
}
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;
} 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;
} else if ( ptemp === undefined ) {
ptemp = dtemp;
temp_str = temp_str +'&passenger_temp=' + dtemp;
} else {
error = true;
}
if (!error) {
request( portal + '/vehicles/' + vid + '/command/set_temps?' + temp_str, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
} else {
if (cb !== undefined) {
var err = new Error('Invalid temperature setting (' + dtemp + 'C), Passenger (' + ptemp + 'C)');
return cb( err );
} else {
return;
}
}
}
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( portal + '/vehicles/' + vid + '/command/auto_conditioning_start', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
} else if (state == "stop" || state === false || state == "off" ) {
request( portal + '/vehicles/' + vid + '/command/auto_conditioning_stop', function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
} else {
if (cb !== undefined) {
var err = new Error("Invalid auto conditioning state = " + state);
return cb( err );
} else {
return;
}
}
}
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( portal +'/vehicles/' + vid + '/command/sun_roof_control?state=' + state, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
} else if ( (state == "move") && (percent >= 0) && (percent <= 100) ) {
request( portal +'/vehicles/' + vid + '/command/sun_roof_control?state=move&percent=' + percent, function (error, response, body) {
if ((!!error) || (response.statusCode !== 200)) return report(error, response, body, cb);
var data = JSON.parse(body);
if (cb !== undefined) {
return cb( data );
} else {
return;
}
});
} else {
if (cb !== undefined) {
var err = new Error("Invalid sun roof state " + util.inspect(params));
return cb( err );
} else {
return;
}
}
}
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;
exports.stream_columns = [ 'speed'
, 'odometer'
, 'soc'
, 'elevation'
, 'est_heading'
, 'est_lat'
, 'est_lng'
, 'power'
, 'shift_state'
, 'range'
];
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(',')
, auth :
{ user : options.email
, pass : options.password
}
}, cb);
};