/
TeslaOAuth2Client.js
993 lines (911 loc) · 27.8 KB
/
TeslaOAuth2Client.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
const { OAuth2Client, OAuth2Error, OAuth2Token } = require('homey-oauth2app');
// const fetch = require('node-fetch');
const dns = require('dns');
dns.setDefaultResultOrder('ipv4first');
const https = require('./https');
const { API_URL } = require('homey-oauth2app/lib/OAuth2Client');
const TeslaOAuth2Token = require('./TeslaOAuth2Token');
const HTTP_TIMEOUT = 15000; // xx sec Timeout
const CONSTANTS = require('./constants');
// const COMMAND_API_URL = 'https://teslaproxy.hpwinkler.de';
const COMMAND_API_URL = 'https://teslaproxy.rwdevelopment.de';
module.exports = class TesladOAuth2Client extends OAuth2Client {
// Required:
// Regional requirements
// North America, Asia-Pacific (excluding China): https://fleet-api.prd.na.vn.cloud.tesla.com
// Europe, Middle East, Africa: https://fleet-api.prd.eu.vn.cloud.tesla.com
static API_URL = 'https://fleet-api.prd.na.vn.cloud.tesla.com';
static TOKEN_URL = 'https://auth.tesla.com/oauth2/v3/token';
static AUTHORIZATION_URL = 'https://auth.tesla.com/oauth2/v3/authorize';
static SCOPES = [
'openid',
'offline_access',
'vehicle_device_data',
'vehicle_cmds',
'vehicle_charging_cmds'
];
// Optional:
static TOKEN = TeslaOAuth2Token; // Default: OAuth2Token
static REDIRECT_URL = 'https://callback.athom.com/oauth2/callback'; // Default: 'https://callback.athom.com/oauth2/callback'
// Overload what needs to be overloaded here
async onInit(){
await super.onInit();
this.region = null;
}
async onGetTokenByCode({ code }) {
// return super.onGetTokenByCode({code});
try{
let body = {
code: code,
grant_type: 'authorization_code',
client_id: this._clientId,
client_secret: this._clientSecret,
redirect_uri: this._redirectUrl,
audience: 'https://fleet-api.prd.na.vn.cloud.tesla.com'
};
body = JSON.stringify( body );
let response = await https.request(
'POST',
this._tokenUrl,
{
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
}
},
body
);
let region = await https.request(
'GET',
this._apiUrl + '/api/1/users/region',
{
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + response.access_token
}
},
''
);
const token = new this._tokenConstructor({
access_token: response.access_token,
refresh_token: response.refresh_token,
token_type: response.token_type,
expires_in: response.expires_in,
region: region.region,
fleet_api_base_url: region.fleet_api_base_url
});
return token;
// return new OAuth2Token(response);
}
catch(error){
this.log("onGetTokenByCode error: "+error.message);
throw error;
}
}
async onRefreshToken() {
const token = this.getToken();
if (!token) {
throw new OAuth2Error('Missing Token');
}
this.debug('Refreshing token...');
if (!token.isRefreshable()) {
throw new OAuth2Error('Token cannot be refreshed');
}
// const body = new URLSearchParams();
// body.append('grant_type', 'refresh_token');
// body.append('client_id', this._clientId);
// body.append('client_secret', this._clientSecret);
// body.append('refresh_token', token.refresh_token);
// const response = await fetch(this._tokenUrl, {
// body,
// method: 'POST',
// });
// if (!response.ok) {
// return this.onHandleRefreshTokenError({ response });
// }
let response = {};
try{
let body = {
grant_type: 'refresh_token',
client_id: this._clientId,
client_secret: this._clientSecret,
refresh_token: token.refresh_token
};
body = JSON.stringify( body );
response = await https.request(
'POST',
this._tokenUrl,
{
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
}
},
body
);
this._token = await this.onHandleRefreshTokenResponse(response);
this.debug('Refreshed token!', this._token);
this.save();
return this.getToken();
}
catch (error){
this.log("onRefreshToken error: "+error.message);
throw new OAuth2Error(error.message);
}
}
async onHandleRefreshTokenResponse(response) {
const json = response;
const token = new this._tokenConstructor({
...this._token, // merge with old token for properties such as refresh_token
...json,
});
return token;
}
// async onHandleNotOK({ body }) {
// throw new OAuth2Error(body.error);
// }
// Set a default timeout for reuqests
async onBuildRequest(req){
let result = await super.onBuildRequest(req);
result.opts.timeout = HTTP_TIMEOUT;
return result;
}
async onHandleResult({
result,
status,
statusText,
headers,
}){
this.log('[result]', {
status: status, statusText: statusText
});
if (result.response){
this.debug('[res JSON]', {
result: result.response
});
return result.response;
}
else{
this.debug('[res]', {
result
});
return result;
}
}
// Turn a fetch error into standard error containing only the error type as message
async onRequestError({
req,
url,
opts,
err,
}){
// this.debug('onRequestError', err);
this.log('onRequestError', err);
if (err.constructor.name == 'FetchError'){
throw new Error(err.type);
}
throw err;
}
async onHandleNotOK({
body,
status,
statusText,
headers,
}) {
if (status == 426){
const message = this.homey.__('devices.car.command_error_426');
const err = new Error(message);
err.status = status;
err.statusText = statusText;
return err;
}
else{
return super.onHandleNotOK({
body,
status,
statusText,
headers,
});
}
}
// API HANDLING =======================================================================================
async getCommandUrl({host, path, api='command'}){
let result = path;
if (api == 'rest'){
if (host != undefined){
result = host + path;
}
}
else if ( api == 'command'){
result = COMMAND_API_URL + path;
// check redirect URL to prevent FETCH error on POST request
try{
let redirectUrl = await https.getRedirectUrl(
result,
{}
);
result = redirectUrl + path;
}
catch(error){
// keep original URL
this.log("getCommandUrl error: "+error.message);
}
}
return result;
}
getCommandHeaders(){
let appVersion = this.homey.app.manifest.version.split('.');
let version = appVersion[0]*10000 + appVersion[1]*100 + appVersion[2]*1;
version = version.toString();
if (version.length < 6){
version = '0' + version;
}
return {
'X-Homey-App-Version': version
}
}
// RESULT HANDLING =======================================================================================
async _checkResult(result){
let content = {};
switch (typeof result){
case 'string':
try{
if (result.length > 0){
content = JSON.parse(result);
}
else{
content = result;
}
}
catch(error){
content = {};
}
break;
case 'object':
content = result;
}
if (content.response != undefined){
content = content.response;
}
if (content.result || content.totalResults || content == ''){
return true;
}
else{
if (content.reason && CONSTANTS.API_ERRORS_WHITELIST.indexOf(content.reason) > -1){
// ignore errors that aor in the whitelist (e.g. 'already_set')
return true;
}
let message = content.reason || content.string || content || '';
throw new Error(message);
}
}
// API ENDPOINTS =======================================================================================
async getApiStatus() {
return await this.get({
path: '/status'
});
}
// USER ENDPOINTS ==========================================================================================
async getUserRegion() {
return await this.get({
path: '/api/1/users/region'
});
}
// VEHICLE ENDPOINTS =======================================================================================
async getVehicles() {
return await this.get({
path: this.getToken().fleet_api_base_url + '/api/1/vehicles'
});
}
async getVehicle(id) {
return await this.get({
path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id
});
}
async getVehicleData(id, endpoints = []) {
let query = '';
for (let i=0; i<endpoints.length; i++){
if (query == ''){
}
else{
query = query + ';';
}
query = query + endpoints[i];
}
query = encodeURIComponent(query);
if (query != ''){
query = '?endpoints=' + query;
}
return await this.get({
path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/vehicle_data' + query
});
}
async getVehicleAppRegistry(id) {
return await this.post({
path: this.getToken().fleet_api_base_url+'/api/1/vehicles/fleet_status',
json: {
vins:[ id ]
}
});
}
async commandWakeUp(id) {
await this.post({
path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/wake_up'
});
}
// VEHICLE SIGNED COMMAND ENDPOINTS ================================================================================
async signedCommand(id, buffer){
let body = buffer.toString('base64');
let result = await this.post({
path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/signed_command',
json: { "routable_message": body }
});
// await this._checkResult(result);
return Buffer.from(result, 'base64');
}
// VEHICLE COMMAND ENDPOINTS =======================================================================================
async commandDoorLock(api, id, {locked}){
let endpoint;
if (locked){
endpoint = '/api/1/vehicles/'+id+'/command/door_lock';
}
else{
endpoint = '/api/1/vehicles/'+id+'/command/door_unlock';
}
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: endpoint,
api: api}),
headers: this.getCommandHeaders()
});
await this._checkResult(result);
}
async commandSentryMode(api, id, {state}){
let result = await this.post({
// path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/command/set_sentry_mode',
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_sentry_mode',
api: api}),
headers: this.getCommandHeaders(),
json: {"on": state}
});
await this._checkResult(result);
}
async commandFlashLights(api, id, {}) {
let result = await this.post({
// path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/command/flash_lights'
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/flash_lights',
api: api}),
headers: this.getCommandHeaders()
});
await this._checkResult(result);
}
async commandHonkHorn(api, id, {}) {
let result = await this.post({
// path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/command/honk_horn'
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/honk_horn',
api: api}),
headers: this.getCommandHeaders()
});
await this._checkResult(result);
}
async commandTrunk(api, id, {trunk}) {
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/actuate_trunk',
api: api}),
headers: this.getCommandHeaders(),
json:{"which_trunk": trunk}
});
await this._checkResult(result);
}
async commandWindowPosition(api, id, {position}) {
let result = await this.post({
// path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/command/window_control',
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/schedule_software_update',
api: api}),
headers: this.getCommandHeaders(),
json:{"command": position}
});
await this._checkResult(result);
}
async commandScheduleSoftwareUpdate(api, id, {minutes}) {
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/schedule_software_update',
api: api}),
headers: this.getCommandHeaders(),
json:{"offset_sec": minutes * 60}
});
await this._checkResult(result);
}
async commandSetTemperature(api, id, {driverTemperature, passengerTemperature}) {
let result = await this.post({
// path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/command/set_temps',
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_temps',
api: api}),
headers: this.getCommandHeaders(),
json:{
"driver_temp": driverTemperature,
"passenger_temp": passengerTemperature
}
});
await this._checkResult(result);
}
async commandPreconditioning(api, id, {on}){
let endpoint;
if(on){
endpoint = '/api/1/vehicles/'+id+'/command/auto_conditioning_start';
}
else{
endpoint = '/api/1/vehicles/'+id+'/command/auto_conditioning_stop';
}
let result = await this.post({
// path: endpoint
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: endpoint,
api: api}),
headers: this.getCommandHeaders()
});
await this._checkResult(result);
}
async commandOverheatprotectionMode(api, id, {mode}){
let result = await this.post({
// path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/command/set_cabin_overheat_protection',
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_cabin_overheat_protection',
api: api}),
headers: this.getCommandHeaders(),
json:{
"on": (mode != 'off'),
"fan_only": (mode == 'fan_only')
}
});
await this._checkResult(result);
}
async commandOverheatprotectionLevel(api, id, {level}){
let cop_temp;
if (level == 'low'){
cop_temp = 1;
}
else if (level == 'medium'){
cop_temp = 2;
}
else if (level == 'high'){
cop_temp = 3;
}
let result = await this.post({
// path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/command/set_cop_temp',
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_cop_temp',
api: api}),
headers: this.getCommandHeaders(),
json:{
"cop_temp": cop_temp
}
});
await this._checkResult(result);
}
async commandClimateKeeperMode(api, id, {mode}){
let mode_int = 0;
switch ( mode ) {
case 'off':
mode_int = 0;
break;
case 'on':
mode_int = 1;
break;
case 'dog':
mode_int = 2;
break;
case 'camp':
mode_int = 3;
break;
}
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_climate_keeper_mode',
api: api}),
headers: this.getCommandHeaders(),
json:{
"climate_keeper_mode": mode_int
}
});
await this._checkResult(result);
}
async commandSteeringWheelHeatLevel(api, id, {level}){
let command;
let json;
if (level == 'auto'){
command = 'remote_auto_steering_wheel_heat_climate_request';
json = {
"on": true
}
}
else{
command = 'remote_steering_wheel_heat_level_request';
json = {
"level": level
}
}
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/'+command,
api: api}),
headers: this.getCommandHeaders(),
json: json
});
await this._checkResult(result);
}
async commandSteeringWheelHeat(api, id, {level}){
let command = 'remote_steering_wheel_heater_request';
let json = {
"on": (level == 'on')
}
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/'+command,
api: api}),
headers: this.getCommandHeaders(),
json: json
});
await this._checkResult(result);
}
async commandSeatHeatLevel(api, id, {level, seat}){
let command;
let json;
if (level == 'auto'){
command = 'remote_auto_seat_climate_request';
json = {
"auto_seat_position": seat,
"auto_climate_on": true
}
}
else{
command = 'remote_seat_heater_request';
json = {
"level": level,
"heater": seat
}
}
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/'+command,
api: api}),
headers: this.getCommandHeaders(),
json: json
});
await this._checkResult(result);
}
async commandDefrost(api, id, {on}){
let result = await this.post({
// path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/command/set_preconditioning_max',
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_preconditioning_max',
api: api}),
headers: this.getCommandHeaders(),
json: {
"on": on
}
});
await this._checkResult(result);
}
async commandChargePort(api, id, {state}){
let endpoint;
if(state){
endpoint = '/api/1/vehicles/'+id+'/command/charge_port_door_open';
}
else{
endpoint = '/api/1/vehicles/'+id+'/command/charge_port_door_close';
}
let result = await this.post({
// path: endpoint
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: endpoint,
api: api}),
headers: this.getCommandHeaders()
});
await this._checkResult(result);
}
async commandChargeOn(api, id, {state}){
let endpoint;
if(state){
endpoint = '/api/1/vehicles/'+id+'/command/charge_start';
}
else{
endpoint = '/api/1/vehicles/'+id+'/command/charge_stop';
}
let result = await this.post({
// path: endpoint
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: endpoint,
api: api}),
headers: this.getCommandHeaders()
});
await this._checkResult(result);
}
async commandChargeLimit(api, id, {limit}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_charge_limit',
api: api}),
headers: this.getCommandHeaders(),
json: {
"percent": limit
}
});
await this._checkResult(result);
}
async commandChargeCurrent(api, id, {current}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_charging_amps',
api: api}),
headers: this.getCommandHeaders(),
json: {
"charging_amps": current
}
});
await this._checkResult(result);
}
async commandScheduleCharging(api, id, args){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_scheduled_charging',
api: api}),
headers: this.getCommandHeaders(),
json: {
"enable": true,
"time": (args.hh * 60 + args.mm)
}
});
await this._checkResult(result);
}
async commandDeactivateScheduledCharging(api, id, {}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_scheduled_charging',
api: api}),
headers: this.getCommandHeaders(),
json: {
"enable": false,
"time": 0
}
});
await this._checkResult(result);
}
async commandScheduleDeparture(api, id, args){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_scheduled_departure',
api: api}),
headers: this.getCommandHeaders(),
json: {
"enable": true,
"departure_time": (args.hh * 60 + args.mm),
"preconditioning_enabled": args.preconditioning_enabled,
"preconditioning_weekdays_only": args.preconditioning_weekdays_only,
"off_peak_charging_enabled": args.off_peak_charging_enabled,
"off_peak_charging_weekdays_only": args.off_peak_charging_weekdays_only,
"end_off_peak_time": (args.op_hh * 60 + args.op_mm)
}
});
await this._checkResult(result);
}
async commandDeactivateScheduledDeparture(api, id, {}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/set_scheduled_departure',
api: api}),
headers: this.getCommandHeaders(),
json: {
"enable": false,
"departure_time": 0,
"preconditioning_enabled": false,
"preconditioning_weekdays_only": false,
"off_peak_charging_enabled": false,
"off_peak_charging_weekdays_only": false,
"end_off_peak_time": 0
}
});
await this._checkResult(result);
}
async commandNavigateGpsRequest(api, id, {latitude, longitude, order=1, locale, time}){
// This endpoint only supports REST API
// let result = await this.post({
// path: await this.getCommandUrl({
// host: this.getToken().fleet_api_base_url,
// path: '/api/1/vehicles/'+id+'/command/navigation_gps_request',
// api: 'rest'}), // force REST API
// json: {
// "lat": latitude,
// "lon": longitude,
// "order": order
// }
// });
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/navigation_request',
api: 'rest'}), // force REST API
headers: this.getCommandHeaders(),
json: {
"type": "share_dest_content_coords",
"locale": locale,
"timestamp_ms": time,
// "value": request
"value": {
lat: latitude,
long: longitude
// "android.intent.extra.TEXT": request
}
}
});
await this._checkResult(result);
}
async commandNavigationRequest(api, id, {request, locale, time}){
// This endpoint only supports REST API
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/navigation_request',
api: 'rest'}), // force REST API
headers: this.getCommandHeaders(),
json: {
"type": "share_ext_content_raw",
"locale": locale,
"timestamp_ms": time,
// "value": request
"value": {
"android.intent.extra.TEXT": request
}
}
});
await this._checkResult(result);
}
async commandNavigateScRequest(api, id, {sucId, order=1}){
// This endpoint only supports REST API
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/navigation_sc_request',
api: 'rest'}), // force REST API
headers: this.getCommandHeaders(),
json: {
"id": sucId,
"order": order
}
});
await this._checkResult(result);
}
async commandMediaNextTrack(api, id, {}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/media_next_track',
api: api}),
headers: this.getCommandHeaders(),
json:{}
});
await this._checkResult(result);
}
async commandMediaPrevTrack(api, id, {}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/media_prev_track',
api: api}),
headers: this.getCommandHeaders(),
json:{}
});
await this._checkResult(result);
}
async commandMediaTogglePlayback(api, id, {}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/media_toggle_playback',
api: api}),
headers: this.getCommandHeaders(),
json:{}
});
await this._checkResult(result);
}
async commandMediaAdjustVolume(api, id, {volume}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/adjust_volume',
api: api}),
headers: this.getCommandHeaders(),
json:{
volume: volume
}
});
await this._checkResult(result);
}
async commandMediaNextFav(api, id, {}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/media_next_fav',
api: api}),
headers: this.getCommandHeaders(),
json:{}
});
await this._checkResult(result);
}
async commandMediaPrevFav(api, id, {}){
let result = await this.post({
path: await this.getCommandUrl({
host: this.getToken().fleet_api_base_url,
path: '/api/1/vehicles/'+id+'/command/media_prev_fav',
api: api}),
headers: this.getCommandHeaders(),
json:{}
});
await this._checkResult(result);
}
// CHARGING ENDPOINTS =======================================================================================
async getChargingHistory(id, {startTime}) {
let json = {
id: id,
sortBy: 'chargeStartDateTime',
sortOrder: 'DESC'
};
if (startTime != undefined){
json['startTime'] = startTime;
}
let result = await this.get({
path: '/api/1/dx/charging/history',
json: json
});
await this._checkResult(result);
return result.data;
}
async getNearbyChargingSites(id, {count, radius}) {
let json = {
detail: true
};
if (count != undefined){
json['count'] = count;
}
if (radius != undefined){
json['radius'] = radius;
}
return await this.get({
path: this.getToken().fleet_api_base_url+'/api/1/vehicles/'+id+'/nearby_charging_sites',
query: json
// json: json
});
}
}