-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
teslajs.js
2158 lines (1874 loc) · 64.5 KB
/
teslajs.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file This is a Node.js module encapsulating the unofficial Tesla API set
*
* Github: https://github.com/mseminatore/TeslaJS
* NPM: https://www.npmjs.com/package/teslajs
*
* @copyright Copyright (c) 2016 Mark Seminatore
*
* @license MIT
*
* Refer to included LICENSE file for usage rights and restrictions
*/
"use strict";
var request = require('request').defaults({
headers: {
"x-tesla-user-agent": "TeslaApp/3.4.4-350/fad4a582e/android/8.1.0",
"user-agent": "Mozilla/5.0 (Linux; Android 8.1.0; Pixel XL Build/OPM4.171019.021.D1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91 Mobile Safari/537.36"
},
json: true,
gzip: true,
body: {}
});
var Promise = require('promise');
var websocket = require('ws');
//=======================
// Streaming API portal
//=======================
/**
* @global
* @default
*/
var streamingPortal = "wss://streaming.vn.teslamotors.com/streaming/";
exports.streamingPortal = streamingPortal;
var streamingBaseURI = process.env.TESLAJS_STREAMING || streamingPortal;
//===========================
// New OAuth-based API portal
//===========================
/**
* @global
* @default
*/
var portal = "https://owner-api.teslamotors.com";
exports.portal = portal;
var portalBaseURI = process.env.TESLAJS_SERVER || portal;
//=======================
// Log levels
//=======================
/**
* @global
* @default
*/
var API_LOG_ALWAYS = 0;
exports.API_LOG_ALWAYS = API_LOG_ALWAYS;
/**
* @global
* @default
*/
var API_ERR_LEVEL = 1;
exports.API_ERR_LEVEL = API_ERR_LEVEL;
/**
* @global
* @default
*/
var API_CALL_LEVEL = 2;
exports.API_CALL_LEVEL = API_CALL_LEVEL;
/**
* @global
* @default
*/
var API_RETURN_LEVEL = 3;
exports.API_RETURN_LEVEL = API_RETURN_LEVEL;
/**
* @global
* @default
*/
var API_BODY_LEVEL = 4;
exports.API_BODY_LEVEL = API_BODY_LEVEL;
/**
* @global
* @default
*/
var API_REQUEST_LEVEL = 5;
exports.API_REQUEST_LEVEL = API_REQUEST_LEVEL;
/**
* @global
* @default
*/
var API_RESPONSE_LEVEL = 6;
exports.API_RESPONSE_LEVEL = API_RESPONSE_LEVEL;
/**
* @global
* @default
*/
var API_LOG_ALL = 255; // this value must be the last
exports.API_LOG_ALL = API_LOG_ALL;
var logLevel = process.env.TESLAJS_LOG || 0;
/**
* Node-style callback function
* @callback nodeBack
* @param {function} error - function which receives the error result
* @param {function} data - function which receives the data result
*/
/**
* TeslaJS options parameter
* @typedef optionsType
* @type {object}
* @property {string} authToken - Tesla provided OAuth token
* @property {string} vehicleID - Tesla provided long vehicle id
* @property {?int} [carIndex] - index of vehicle within vehicles JSON
*/
/*
* Adjustable console logging
* @param {int} level - logging level
* @param {string} str - text to log
*/
function log(level, str) {
if (logLevel < level) {
return;
}
// console.log("[" + new Date().toISOString() + "] " + str);
console.log(str);
}
/*
* Ensure value is within [min..max]
*/
function clamp(value, min, max) {
if (value < min) {
value = min;
}
if (value > max) {
value = max;
}
return value;
}
/**
* Set the current logging level
* @param {int} level - logging level
*/
exports.setLogLevel = function setLogLevel(level) {
logLevel = level;
}
/**
* Get the current logging level
* @return {int} the current logging level
*/
exports.getLogLevel = function getLogLevel() {
return logLevel;
}
/**
* Set the portal base URI
* @param {string} uri - URI for Tesla servers
*/
exports.setPortalBaseURI = function setPortalBaseURI(uri) {
if (!uri) {
portalBaseURI = portal; // reset to the default Tesla servers
} else {
portalBaseURI = uri;
}
}
/**
* Get the portal base URI
* @return {string} URI for Tesla servers
*/
exports.getPortalBaseURI = function getPortalBaseURI() {
return portalBaseURI;
}
/**
* Set the streaming base URI
* @param {string} uri - URI for Tesla streaming servers
*/
exports.setStreamingBaseURI = function setStreamingBaseURI(uri) {
if (!uri) {
streamingBaseURI = streamingPortal; // reset to the default Tesla servers
} else {
streamingBaseURI = uri;
}
}
/**
* Get the streaming base URI
* @return {string} URI for Tesla streaming servers
*/
exports.getStreamingBaseURI = function getStreamingBaseURI() {
return streamingBaseURI;
}
/**
* Return the car model from vehicle JSON information
* @param {object} vehicle - vehicle JSON
* @return {string} vehicle model string
*/
exports.getModel = function getModel(vehicle) {
var result = exports.vinDecode(vehicle);
return result.carType;
}
/**
* Return an object containing properties decoded from the vehicle VIN
* @param {object} vehicle - vehicle JSON
* @return {object} vehicle properties
*/
exports.vinDecode = function vinDecode(vehicle) {
var result = {
carType: "Model S",
awd: false,
year: 2012
};
if (!vehicle || !vehicle.vin) {
return result;
}
var dateCode = vehicle.vin.charCodeAt(9);
result.year = 2010 + dateCode - 'A'.charCodeAt(0);
// handle the skipped 'I' code. We may also need to skip 'O'
if (dateCode > 73) {
result.year--;
}
var model = vehicle.vin.charAt(3);
switch (model) {
case "S":
result.carType = "Model S";
break;
case "3":
result.carType = "Model 3";
break;
case "X":
result.carType = "Model X";
break;
case "Y":
result.carType = "Model Y";
break;
case "R":
result.carType = "Roadster";
break;
}
// Check for AWD config 2, 4 or B
if (
vehicle.vin.charAt(7) == "2" || // Dual Motor (standard) (Designated for Model S & Model X)
vehicle.vin.charAt(7) == "4" || // Dual Motor (performance) (Designated for Model S & Model X)
vehicle.vin.charAt(7) == "B" || // Dual motor - standard Model 3
vehicle.vin.charAt(7) == "C" || // Dual motor - performance Model 3
vehicle.vin.charAt(7) == "E" // Dual motor - Model Y
) {
result.awd = true;
}
return result;
}
/**
* Return the paint color from vehicle JSON information
* @param {object} vehicle - vehicle JSON
* @return {string} the vehicle paint color
*/
exports.getPaintColor = function getPaintColor(vehicle) {
var colors = {
"PBCW": "white",
"PBSB": "black",
"PMAB": "metallic brown",
"PMBL": "metallic black",
"PMMB": "metallic blue",
"PMMR": "multi-coat red",
"PPMR": "multi-coat red",
"PMNG": "steel grey",
"PMSG": "metallic green",
"PMSS" : "metallic silver",
"PPSB": "ocean blue",
"PPSR" : "signature red", //premium signature red"
"PPSW": "pearl white",
"PPTI": "titanium",
"PMTG": "metallic grey" // dolphin grey
};
if (!vehicle || !vehicle.option_codes) {
return "unknown";
}
var paintColor = vehicle.option_codes.match(/PBCW|PBSB|PMAB|PMBL|PMMB|PMMR|PPMR|PMNG|PMSG|PMSS|PPSB|PPSR|PPSW|PPTI|PMTG/);
return colors[paintColor] || "black";
}
/**
* Return the vehicle VIN from vehicle JSON information
* @param {object} vehicle - vehicle JSON
* @return {string} the vehicle VIN
*/
exports.getVin = function getVin(vehicle) {
if (!vehicle || !vehicle.vin) {
throw new Error("invalid parameter");
}
return vehicle.vin;
}
/**
* Return the vehicle VIN from vehicle JSON information
* @param {object} vehicle - vehicle JSON
* @return {string} the short version of the vehicle VIN
*/
exports.getShortVin = function getShortVin(vehicle) {
if (!vehicle || !vehicle.vin) {
throw new Error("invalid parameter");
}
return vehicle.vin.substr(11);
}
/**
* Login to the server and receive OAuth tokens
* @function login
* @param {Object} credentials - object of Tesla credentials
* @param {string} credentials.username - email address used on Tesla.com
* @param {string} credentials.password - password used on Tesla.command
* @param {string} credentials.mfaPassCode - MFA password
* @param {string} credentials.mfaDeviceName - MFA device name
* @param {nodeBack} callback - Node-style callback
* @returns {object} {response, body, authToken, refreshToken}
*/
exports.login = function login(credentials, callback) {
log(API_CALL_LEVEL, "TeslaJS.login()");
// Compatibility with old username/password API
if (typeof arguments[0] == 'string' && typeof arguments[1] == 'string') {
credentials = {username: arguments[0], password: arguments[1]};
callback = arguments[2];
}
credentials = credentials || {};
callback = callback || function (err, result) { /* do nothing! */ }
if (!credentials.username || !credentials.password) {
callback("login() requires username and password", null);
return;
}
require('./src/auth').login({identity: credentials.username, credential: credentials.password, mfaPassCode: credentials.mfaPassCode, mfaDeviceName: credentials.mfaDeviceName}, function (error, response, body) {
log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response));
log(API_RESPONSE_LEVEL, "\nBody: " + JSON.stringify(body));
var loginResult = body || {};
callback(error, { error: error, response: response, body: body, authToken: loginResult.access_token, refreshToken: loginResult.refresh_token });
log(API_RETURN_LEVEL, "TeslaJS.login() completed.");
});
}
/**
* Login to the server and receive OAuth tokens
* @function loginAsync
* @param {Object} credentials - object of Tesla credentials
* @param {string} credentials.username - email address used on Tesla.com
* @param {string} credentials.password - password used on Tesla.command
* @param {string} credentials.mfaPassCode - MFA password
* @param {string} credentials.mfaDeviceName - MFA device name
* @returns {Promise} {response, body, authToken, refreshToken}
*/
exports.loginAsync = Promise.denodeify(exports.login);
/**
* Retrieve new OAuth and refresh tokens using a refresh_token
* @param {string} refresh_token - a valid OAuth refresh_token from a previous login
* @param {nodeBack} callback - Node-style callback
* @returns {object} {response, body, authToken, refreshToken}
*/
exports.refreshToken = function refreshToken(refresh_token, callback) {
log(API_CALL_LEVEL, "TeslaJS.refreshToken()");
callback = callback || function (err, result) { /* do nothing! */ }
if (!refresh_token) {
callback("refreshToken() requires a refresh_token", null);
return;
}
var req = {
method: 'POST',
url: portalBaseURI + '/oauth/token',
body: {
"grant_type": "refresh_token",
"refresh_token": refresh_token
}
};
log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req));
request(req, function (error, response, body) {
log(API_RESPONSE_LEVEL, "\nResponse: " + body);
callback(error, { error: error, response: response, body: JSON.stringify(body), authToken: body.access_token, refreshToken: body.refresh_token });
log(API_RETURN_LEVEL, "TeslaJS.refreshToken() completed.");
});
}
/**
* Async call to retrieve new OAuth and refresh tokens using a refresh_token
* @function refreshTokenAsync
* @param {string} refresh_token - a valid OAuth refresh_token from a previous login
* @returns {Promise} {response, body, authToken, refreshToken}
*/
exports.refreshTokenAsync = Promise.denodeify(exports.refreshToken);
/**
* Logout and invalidate the current auth token
* @param {string} authToken - Tesla provided OAuth token
* @param {nodeBack} callback - Node-style callback
*/
exports.logout = function logout(authToken, callback) {
log(API_CALL_LEVEL, "TeslaJS.logout()");
callback = callback || function (err, result) { /* do nothing! */ }
request({
method: 'POST',
url: portalBaseURI + '/oauth/revoke',
headers: { Authorization: "Bearer " + authToken, 'Content-Type': 'application/json; charset=utf-8' }
}, function (error, response, body) {
callback(error, { error: error, response: response, body: JSON.stringify(body) });
log(API_RETURN_LEVEL, "TeslaJS.logout() completed.");
});
}
/**
* Logout and invalidate the current auth token
* @function logoutAsync
* @param {string} authToken - Tesla provided OAuth token
* @returns {Promise} result
*/
exports.logoutAsync = Promise.denodeify(exports.logout);
/**
* Return vehicle information on the requested vehicle
* @function vehicle
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {Vehicle} vehicle JSON data
*/
exports.vehicle = function vehicle(options, callback) {
log(API_CALL_LEVEL, "TeslaJS.vehicle()");
callback = callback || function (err, vehicle) { /* do nothing! */ }
var req = {
method: 'GET',
url: portalBaseURI + '/api/1/vehicles',
headers: { Authorization: "Bearer " + options.authToken, 'Content-Type': 'application/json; charset=utf-8' }
};
log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req));
request(req, function (error, response, body) {
if (error) {
log(API_ERR_LEVEL, error);
return callback(error, null);
}
if (response.statusCode != 200) {
return callback(response.statusMessage, null);
}
log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body));
log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response));
try {
body = body.response[options.carIndex || 0];
body.id = body.id_s;
options.vehicleID = body.id;
callback(null, body);
} catch (e) {
log(API_ERR_LEVEL, 'Error parsing vehicles response');
callback(e, null);
}
log(API_RETURN_LEVEL, "\nGET request: " + "/vehicles" + " completed.");
});
}
/**
* Return vehicle information on the requested vehicle
* @function vehicleAsync
* @param {optionsType} options - options object
* @returns {Promise} vehicle JSON data
*/
exports.vehicleAsync = Promise.denodeify(exports.vehicle);
/**
* Return vehicle information on the requested vehicle. Uses options.vehicleID
* to determine which vehicle to fetch data for.
* @function vehicleById
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {Vehicle} vehicle JSON data
*/
exports.vehicleById = function vehicle(options, callback) {
log(API_CALL_LEVEL, "TeslaJS.vehicleById()");
callback = callback || function (err, vehicle) { /* do nothing! */ }
var req = {
method: 'GET',
url: portalBaseURI + '/api/1/vehicles/' + options.vehicleID,
headers: { Authorization: "Bearer " + options.authToken, 'Content-Type': 'application/json; charset=utf-8' }
};
log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req));
request(req, function (error, response, body) {
if (error) {
log(API_ERR_LEVEL, error);
return callback(error, null);
}
if (response.statusCode != 200) {
return callback(response.statusMessage, null);
}
log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body));
log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response));
try {
body = body.response;
callback(null, body);
} catch (e) {
log(API_ERR_LEVEL, 'Error parsing vehicle response');
callback(e, null);
}
log(API_RETURN_LEVEL, "\nGET request: " + "/vehicles/" + options.vehicleID + " completed.");
});
}
/**
* Return vehicle information on the requested vehicle. Uses options.vehicleID
* to determine which vehicle to fetch data for.
* @function vehicleByIdAsync
* @param {optionsType} options - options object
* @returns {Promise} vehicle JSON data
*/
exports.vehicleByIdAsync = Promise.denodeify(exports.vehicleById);
/**
* Return vehicle information on ALL vehicles
* @function vehicles
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {Vehicles[]} array of vehicle JSON data
*/
exports.vehicles = function vehicles(options, callback) {
log(API_CALL_LEVEL, "TeslaJS.vehicles()");
callback = callback || function (err, vehicle) { /* do nothing! */ }
var req = {
method: 'GET',
url: portalBaseURI + '/api/1/vehicles',
headers: { Authorization: "Bearer " + options.authToken, 'Content-Type': 'application/json; charset=utf-8' }
};
log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req));
request(req, function (error, response, body) {
if (error) {
log(API_ERR_LEVEL, error);
return callback(error, null);
}
if (response.statusCode != 200) {
return callback(response.statusMessage, null);
}
log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body));
log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response));
try {
body = body.response;
callback(null, body);
} catch (e) {
log(API_ERR_LEVEL, 'Error parsing vehicles response');
callback(e, null);
}
log(API_RETURN_LEVEL, "\nGET request: " + "/vehicles" + " completed.");
});
}
/**
* Return vehicle information on ALL vehicles
* @function vehiclesAsync
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {Promise} array of vehicle JSON data
*/
exports.vehiclesAsync = Promise.denodeify(exports.vehicles);
/**
* Generic REST call for GET commands
* @function get_command
* @param {optionsType} options - options object
* @param {string} command - REST command
* @param {nodeBack} callback - Node-style callback
*/
exports.get_command = get_command;
function get_command(options, command, callback) {
log(API_CALL_LEVEL, "GET call: " + command + " start.");
callback = callback || function (err, data) { /* do nothing! */ }
var req = {
method: "GET",
url: portalBaseURI + "/api/1/vehicles/" + options.vehicleID + "/" + command,
headers: { Authorization: "Bearer " + options.authToken, 'Content-Type': 'application/json; charset=utf-8'}
};
log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req));
request(req, function (error, response, body) {
if (error) {
log(API_ERR_LEVEL, error);
return callback(error, null);
}
if (response.statusCode != 200) {
var str = "Error response: " + response.statusCode;
log(API_ERR_LEVEL, str);
return callback(str, null);
}
log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body));
log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response));
try {
body = body.response;
callback(null, body);
} catch (e) {
log(API_ERR_LEVEL, 'Error parsing GET call response');
log(API_ERR_LEVEL, e);
callback(e, null);
}
log(API_RETURN_LEVEL, "\nGET request: " + command + " completed.");
});
}
/**
* Generic Async REST call for GET commands
* @function get_commandAsync
* @param {optionsType} options - options object
* @param {string} command - REST command
* @returns {Promise} result
*/
exports.get_commandAsync = Promise.denodeify(exports.get_command);
/**
* Generic REST call for POST commands
* @function
* @param {optionsType} options - options object
* @param {string} command - REST command
* @param {object} body - JSON payload
* @param {nodeBack} callback - Node-style callback
*/
exports.post_command = post_command;
function post_command(options, command, body, callback) {
log(API_CALL_LEVEL, "POST call: " + command + " start.");
callback = callback || function (err, data) { /* do nothing! */ }
var cmd = {
method: "POST",
url: portalBaseURI + "/api/1/vehicles/" + options.vehicleID + "/" + command,
headers: { Authorization: "Bearer " + options.authToken, 'content-type': 'application/json; charset=UTF-8' },
body: body || null
};
log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(cmd));
request(cmd, function (error, response, body) {
if (error) {
log(API_ERR_LEVEL, error);
return callback(error, null);
}
if (response.statusCode != 200) {
var str = "Error response: " + response.statusCode;
log(API_ERR_LEVEL, str);
return callback(str, null);
}
log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body));
log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response));
try {
body = body.response;
callback(null, body);
} catch (e) {
log(API_ERR_LEVEL, 'Error parsing POST call response');
callback(e, null);
}
log(API_RETURN_LEVEL, "\nPOST command: " + command + " completed.");
});
}
/**
* Generic Async REST call for POST commands
* @function post_commandAsync
* @param {optionsType} options - options object
* @param {string} command - REST command
* @param {object} body - JSON payload
* @returns {Promise} result
*/
exports.post_commandAsync = Promise.denodeify(exports.post_command);
/**
* GET all vehicle data in a single call
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} vehicle_data object
*/
exports.vehicleData = function vehicleData(options, callback){
get_command(options, "vehicle_data", callback);
}
/**
* Async version to GET all vehicle data in a single call
* @function vehicleDataAsync
* @param {optionsType} options - options object
* @returns {Promise} vehicle_data object
*/
exports.vehicleDataAsync = Promise.denodeify(exports.vehicleData);
/**
* GET the vehicle config
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} vehicle_config object
*/
exports.vehicleConfig = function vehicleConfig(options, callback) {
get_command(options, "data_request/vehicle_config", callback);
}
/**
* Async version to GET the vehicle config
* @function vehicleConfigAsync
* @param {optionsType} options - options object
* @returns {Promise} vehicle_config object
*/
exports.vehicleConfigAsync = Promise.denodeify(exports.vehicleConfig);
/**
* GET the vehicle state
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} vehicle_state object
*/
exports.vehicleState = function vehicleState(options, callback) {
get_command(options, "data_request/vehicle_state", callback);
}
/**
* Async version to GET the vehicle state
* @function vehicleStateAsync
* @param {optionsType} options - options object
* @returns {Promise} vehicle_state object
*/
exports.vehicleStateAsync = Promise.denodeify(exports.vehicleState);
/**
* GET the climate state
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} climate_state object
*/
exports.climateState = function climateState(options, callback) {
get_command(options, "data_request/climate_state", callback);
}
/**
* GET the climate state
* @function climateStateAsync
* @param {optionsType} options - options object
* @returns {Promise} climate_state object
*/
exports.climateStateAsync = Promise.denodeify(exports.climateState);
/**
* GET nearby charging sites
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} climate_state object
*/
exports.nearbyChargers = function nearbyChargers(options, callback) {
get_command(options, "nearby_charging_sites", callback);
}
/**
* @function nearbyChargersAsync
* @param {optionsType} options - options object
* @returns {Promise} climate_state object
*/
exports.nearbyChargersAsync = Promise.denodeify(exports.nearbyChargers);
/**
* GET the drive state
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} drive_state object
*/
exports.driveState = function driveState(options, callback) {
get_command(options, "data_request/drive_state", callback);
}
/**
* @function driveStateAsync
* @param {optionsType} options - options object
* @returns {Promise} drive_state object
*/
exports.driveStateAsync = Promise.denodeify(exports.driveState);
/**
* GET the charge state
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} charge_state object
*/
exports.chargeState = function chargeState(options, callback) {
get_command(options, "data_request/charge_state", callback);
}
/**
* @function chargeStateAsync
* @param {optionsType} options - options object
* @returns {Promise} charge_state object
*/
exports.chargeStateAsync = Promise.denodeify(exports.chargeState);
/**
* GET the GUI settings
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} gui_settings object
*/
exports.guiSettings = function guiSettings(options, callback) {
get_command(options, "data_request/gui_settings", callback);
}
/**
* @function guiSettingsAsync
* @param {optionsType} options - options object
* @returns {Promise} gui_settings object
*/
exports.guiSettingsAsync = Promise.denodeify(exports.guiSettings);
/**
* GET the mobile enabled status
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} mobile_enabled object
*/
exports.mobileEnabled = function mobileEnabled(options, callback) {
get_command(options, "mobile_enabled", callback);
}
/**
* @function mobileEnabledAsync
* @param {optionsType} options - options object
* @returns {Promise} mobile_enabled object
*/
exports.mobileEnabledAsync = Promise.denodeify(exports.mobileEnabled);
/**
* Honk the horn
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} result
*/
exports.honkHorn = function honk(options, callback) {
post_command(options, "command/honk_horn", null, callback);
}
/**
* @function honkHornAsync
* @param {optionsType} options - options object
* @returns {Promise} result
*/
exports.honkHornAsync = Promise.denodeify(exports.honkHorn);
/**
* Flash the lights
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} result
*/
exports.flashLights = function flashLights(options, callback) {
post_command(options, "command/flash_lights", null, callback);
}
/**
* @function flashLightsAsync
* @param {optionsType} options - options object
* @returns {Promise} result
*/
exports.flashLightsAsync = Promise.denodeify(exports.flashLights);
/**
* Start charging the car
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} result
*/
exports.startCharge = function startCharge(options, callback) {
post_command(options, "command/charge_start", null, callback);
}
/**
* Start charging the car
* @function startChargeAsync
* @param {optionsType} options - options object
* @returns {Promise} result
*/
exports.startChargeAsync = Promise.denodeify(exports.startCharge);
/**
* Stop charging the car
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} result
*/
exports.stopCharge = function stopCharge(options, callback) {
post_command(options, "command/charge_stop", null, callback);
}
/**
* Stop charging the car
* @function stopChargeAsync
* @param {optionsType} options - options object
* @returns {Promise} result
*/
exports.stopChargeAsync = Promise.denodeify(exports.stopCharge);
/**
* Open the charge port, or releases the latch if the charge port is open, a cable is plugged in, and charging is stopped
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {object} result
*/
exports.openChargePort = function openChargePort(options, callback) {
post_command(options, "command/charge_port_door_open", null, callback);
}
/**
* Open the charge port, or releases the latch if the charge port is open, a cable is plugged in, and charging is stopped
* @function openChargePortAsync
* @param {optionsType} options - options object
* @returns {Promise} result
*/
exports.openChargePortAsync = Promise.denodeify(exports.openChargePort);