-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
849 lines (794 loc) · 34.9 KB
/
main.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
"use strict";
/*
* Created with @iobroker/create-adapter v1.34.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const axios = require("axios");
const qs = require("qs");
const crypto = require("crypto");
const Json2iob = require("./lib/json2iob");
const axiosCookieJarSupport = require("axios-cookiejar-support").default;
const tough = require("tough-cookie");
class Teslamotors extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "tesla-motors",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// Reset the connection indicator during startup
this.setState("info.connection", false, true);
if (this.config.interval < 0.1) {
this.log.info("Set interval to minimum 0.1");
this.config.interval = 0.1;
}
this.adapterConfig = "system.adapter." + this.name + "." + this.instance;
const obj = await this.getForeignObjectAsync(this.adapterConfig);
if (this.config.reset) {
if (obj) {
obj.native.session = {};
obj.native.cookies = "";
obj.native.captchaSvg = "";
obj.native.reset = false;
obj.native.captcha = "";
await this.setForeignObjectAsync(this.adapterConfig, obj);
this.log.info("Login Token resetted");
this.terminate();
}
}
if (this.config.captchaSvg && !this.config.captcha) {
this.log.info("Waiting for captcha");
return;
}
axiosCookieJarSupport(axios);
this.cookieJar = new tough.CookieJar();
if (obj && obj.native.cookies) {
this.cookieJar = tough.CookieJar.fromJSON(obj.native.cookies);
}
this.requestClient = axios.create();
this.session = {};
this.ownSession = {};
this.sleepTimes = {};
if (obj && obj.native.session && obj.native.session.refresh_token) {
this.session = obj.native.session;
await this.refreshToken();
}
this.updateInterval = null;
this.reLoginTimeout = null;
this.refreshTokenTimeout = null;
this.idArray = [];
this.json2iob = new Json2iob(this);
this.subscribeStates("*");
this.headers = {
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"x-tesla-user-agent": "TeslaApp/3.10.14-474/540f6f430/ios/12.5.1",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"accept-language": "de-de",
};
if (!this.session.access_token) {
await this.login();
}
if (this.session.access_token && this.ownSession.access_token) {
await this.getDeviceList();
this.updateDevices();
this.updateInterval = setInterval(async () => {
await this.updateDevices();
}, this.config.interval * 60 * 1000);
this.refreshTokenInterval = setInterval(() => {
this.refreshToken();
}, this.session.expires_in * 1000);
}
}
async login() {
const [code_verifier, codeChallenge] = this.getCodeChallenge();
this.state = this.randomString(43);
this.url =
"https://auth.tesla.com/oauth2/v3/authorize?audience=&client_id=ownerapi&code_challenge=" +
codeChallenge +
"&code_challenge_method=S256&locale=de&prompt=login&redirect_uri=https%3A%2F%2Fauth.tesla.com%2Fvoid%2Fcallback&response_type=code&scope=openid%20email%20offline_access&state=" +
this.state;
const htmlLoginForm = await this.requestClient({
method: "get",
url: this.url,
headers: this.headers,
jar: this.cookieJar,
withCredentials: true,
})
.then((res) => {
this.log.debug(JSON.stringify(res.data));
this.session = res.data;
this.setState("info.connection", true, true);
return res.data;
})
.catch((error) => {
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
if (!htmlLoginForm) {
return;
}
let form = {};
form = this.extractHidden(htmlLoginForm);
form["identity"] = this.config.username;
form["credential"] = this.config.password;
if (!this.config.captcha) {
await this.receiveCaptcha();
this.log.warn("Please enter captcha in instance setting");
return;
}
form["captcha"] = this.config.captcha;
const code = await this.requestClient({
method: "post",
url: this.url,
headers: this.headers,
data: qs.stringify(form),
jar: this.cookieJar,
withCredentials: true,
maxRedirects: 0,
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
if (res.data.split("messages = ")[1]) {
this.log.error(res.data.split("messages = ")[1].split(";")[0]);
const obj = await this.getForeignObjectAsync(this.adapterConfig);
this.log.info("reset captcha");
if (obj) {
obj.native.captchaSvg = "";
obj.native.captcha = "";
obj.native.mfa = "";
this.setForeignObject(this.adapterConfig, obj);
}
return;
}
if (this.config.mfa) {
const transactionid = res.data.split(' transaction_id: "')[1].split('",')[0];
await this.handleMfa(transactionid);
return await this.requestClient({
method: "post",
url: this.url,
headers: this.headers,
data: "transaction_id=" + transactionid,
jar: this.cookieJar,
withCredentials: true,
maxRedirects: 0,
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
})
.catch((error) => {
if (error.response && error.response.status === 302) {
return error.response.data.split("https://auth.tesla.com/void/callback?code=")[1].split("&")[0];
} else {
error.response && this.log.error(JSON.stringify(error.response.data));
this.log.error(error);
}
});
}
this.log.error("Missing mfa or check username passwor");
return;
})
.catch((error) => {
if (error.response && error.response.status === 302) {
return error.response.data.split("https://auth.tesla.com/void/callback?code=")[1].split("&")[0];
} else {
error.response && this.log.error(JSON.stringify(error.response.data));
this.log.error(error);
}
});
let data = {
grant_type: "authorization_code",
code: code,
client_id: "ownerapi",
redirect_uri: "https://auth.tesla.com/void/callback",
scope: "openid offline_access",
code_verifier: code_verifier,
};
this.log.debug(JSON.stringify(data));
await this.requestClient({
method: "post",
url: "https://auth.tesla.com/oauth2/v3/token",
headers: this.headers,
data: qs.stringify(data),
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
this.session = res.data;
await this.getOwnerToken();
this.log.info("Login successful");
this.setState("info.connection", true, true);
return res.data;
})
.catch((error) => {
this.setState("info.connection", false, true);
this.log.error(error);
if (error.response) {
this.log.error(JSON.stringify(error.response.data));
}
});
}
async receiveCaptcha() {
await this.requestClient({
method: "get",
url: "https://auth.tesla.com/captcha",
headers: {
accept: "image/png,image/svg+xml,image/*;q=0.8,video/*;q=0.8,*/*;q=0.5",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"accept-language": "de-de",
},
jar: this.cookieJar,
withCredentials: true,
})
.then(async (res) => {
this.log.silly(JSON.stringify(res.data));
const obj = await this.getForeignObjectAsync(this.adapterConfig);
if (obj) {
obj.native.captchaSvg = res.data;
obj.native.cookies = this.cookieJar.toJSON();
this.setForeignObject(this.adapterConfig, obj);
}
return res.data;
})
.catch((error) => {
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
}
async handleMfa(transaction_id) {
this.log.debug("start mfa");
const id = await this.requestClient({
method: "get",
url: "https://auth.tesla.com/oauth2/v3/authorize/mfa/factors?transaction_id=" + transaction_id,
headers: {
accept: "application/json",
"content-type": "application/json;charset=UTF-8",
"accept-language": "de-de",
"x-requested-with": "XMLHttpRequest",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
referer:
"https://auth.tesla.com/oauth2/v1/authorize?redirect_uri=https://www.tesla.com/teslaaccount/owner-xp/auth/callback&response_type=code&client_id=ownership&scope=offline_access%20openid%20ou_code%20email&audience=https%3A%2F%2Fownership.tesla.com%2",
},
jar: this.cookieJar,
withCredentials: true,
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
if (res.data.data && res.data.data[0] && res.data.data[0].id) {
return res.data.data[0].id;
}
this.log.error("MFA Init Failed");
})
.catch((error) => {
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
await this.requestClient({
method: "post",
url: "https://auth.tesla.com/oauth2/v3/authorize/mfa/verify",
headers: {
"content-type": "application/json;charset=UTF-8",
accept: "application/json",
"x-requested-with": "XMLHttpRequest",
"accept-language": "de-de",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
},
jar: this.cookieJar,
withCredentials: true,
data: { transaction_id: transaction_id, factor_id: id, passcode: this.config.mfa },
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
if (res.data.data && res.data.data.valid) {
return;
}
this.log.error("MFA Submit Failed");
})
.catch((error) => {
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
}
async getDeviceList() {
const headers = {
"Content-Type": "application/json",
Accept: "*/*",
"User-Agent": "ioBroker 1.0.0",
Authorization: "Bearer " + this.session.access_token,
};
await this.requestClient({
method: "get",
url: "https://owner-api.teslamotors.com/api/1/products",
headers: headers,
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
this.idArray = [];
for (let device of res.data.response) {
const id = device.id_s || device.id;
this.log.debug(id);
if (device.vehicle_id) {
this.idArray.push(id);
} else {
this.nonVehicles = true;
}
await this.setObjectNotExistsAsync(id, {
type: "device",
common: {
name: device.display_name || device.site_name || device.resource_type,
},
native: {},
});
this.json2iob.parse(id, device);
if (!device.vehicle_id) {
continue;
}
await this.setObjectNotExistsAsync(id + ".remote", {
type: "channel",
common: {
name: "Remote Controls",
},
native: {},
});
const remoteArray = [
{ command: "wake_up" },
{ command: "honk_horn" },
{ command: "flash_lights" },
{ command: "remote_start_drive" },
{ command: "trigger_homelink" },
{ command: "set_sentry_mode" },
{ command: "door_unlock" },
{ command: "door_lock" },
{ command: "actuate_trunk-rear" },
{ command: "actuate_trunk-front" },
{ command: "window_control-vent" },
{ command: "window_control-close" },
{ command: "sun_roof_control-vent" },
{ command: "sun_roof_control-close" },
{ command: "charge_port_door_open" },
{ command: "charge_port_door_close" },
{ command: "charge_start" },
{ command: "charge_stop" },
{ command: "charge_standard" },
{ command: "charge_max_range" },
{ command: "set_charge_limit", type: "number", role: "level" },
{ command: "set_temps-driver_temp", type: "number", role: "level" },
{ command: "set_temps-passenger_temp", type: "number", role: "level" },
{ command: "remote_seat_heater_request-0", type: "number", role: "level" },
{ command: "remote_seat_heater_request-1", type: "number", role: "level" },
{ command: "remote_seat_heater_request-2", type: "number", role: "level" },
{ command: "remote_seat_heater_request-4", type: "number", role: "level" },
{ command: "remote_seat_heater_request-5", type: "number", role: "level" },
{ command: "auto_conditioning_start" },
{ command: "auto_conditioning_stop" },
{ command: "media_toggle_playback" },
{ command: "media_next_track" },
{ command: "media_prev_track" },
{ command: "media_volume_up" },
{ command: "media_volume_down" },
{ command: "set_preconditioning_max" },
{ command: "remote_steering_wheel_heater_request" },
];
remoteArray.forEach((remote) => {
this.setObjectNotExists(id + ".remote." + remote.command, {
type: "state",
common: {
name: remote.name || "",
type: remote.type || "boolean",
role: remote.role || "button",
write: true,
read: true,
},
native: {},
});
});
}
})
.catch((error) => {
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
}
async updateDevices() {
const statusArray = [{ path: "", url: "https://owner-api.teslamotors.com/api/1/vehicles/{id}/vehicle_data" }];
const headers = {
"Content-Type": "application/json; charset=utf-8",
Accept: "*/*",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"x-tesla-user-agent": "TeslaApp/3.10.14-474/540f6f430/ios/12.5.1",
Authorization: "Bearer " + this.ownSession.access_token,
};
this.idArray.forEach(async (id) => {
//check state
let state = await this.checkState(id);
if (state === "asleep" && !this.config.wakeup) {
this.log.debug(id + " asleep skip update");
return;
}
const waitForSleep = await this.checkWaitForSleepState(id);
if (waitForSleep && !this.config.wakeup) {
if (!this.sleepTimes[id]) {
this.sleepTimes[id] = Date.now();
}
//wait 15min
if (Date.now() - this.sleepTimes[id] >= 900000) {
this.log.debug(id + " wait for sleep was not successful");
this.sleepTimes[id] = null;
} else {
this.log.debug(id + " skip update waiting for sleep");
return;
}
}
if (this.config.wakeup && state !== "online") {
while (state !== "online") {
const vehicleState = await this.sendCommand(id, "wake_up");
if (vehicleState === "error") {
break;
}
state = vehicleState.state;
}
}
statusArray.forEach(async (element) => {
let url = element.url.replace("{id}", id);
this.log.debug(url);
await this.requestClient({
method: "get",
url: url,
headers: headers,
})
.then((res) => {
this.log.debug(JSON.stringify(res.data));
if (!res.data) {
return;
}
let data = res.data.response;
this.json2iob.parse(id + element.path, data);
})
.catch((error) => {
if (error.response && error.response.status === 401) {
error.response && this.log.debug(JSON.stringify(error.response.data));
this.log.info(element.path + " receive 401 error. Refresh Token in 30 seconds");
clearTimeout(this.refreshTokenTimeout);
this.refreshTokenTimeout = setTimeout(() => {
this.refreshToken();
}, 1000 * 30);
return;
}
if (error.response && error.response.status === 404) {
if (element.path === "statusv1") {
this.statusBlock[id] = true;
}
}
this.log.error(url);
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
});
});
if (this.nonVehicles) {
this.getDeviceList();
}
}
async checkState(id) {
const headers = {
"Content-Type": "application/json; charset=utf-8",
Accept: "*/*",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"x-tesla-user-agent": "TeslaApp/3.10.14-474/540f6f430/ios/12.5.1",
Authorization: "Bearer " + this.ownSession.access_token,
};
return await this.requestClient({
method: "get",
url: "https://owner-api.teslamotors.com/api/1//vehicles/" + id,
headers: headers,
})
.then((res) => {
this.log.debug(JSON.stringify(res.data));
return res.data.response.state;
})
.catch((error) => {
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
return;
});
}
async refreshToken() {
await this.requestClient({
method: "post",
url: "https://auth.tesla.com/oauth2/v3/token",
headers: this.headers,
data: "grant_type=refresh_token&client_id=ownerapi&scope=openid email offline_access&refresh_token=" + this.session.refresh_token,
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
this.session.access_token = res.data.access_token;
this.session.expires_in = res.data.expires_in;
await this.getOwnerToken();
this.setState("info.connection", true, true);
return res.data;
})
.catch(async (error) => {
this.setState("info.connection", false, true);
this.log.error("refresh token failed");
this.log.error(error);
if (error.code === "ENOTFOUND") {
this.log.error("No connection to Tesla server please check your connection");
return;
}
this.session = {};
error.response && this.log.error(JSON.stringify(error.response.data));
this.log.error("Start relogin in 1min");
this.reLoginTimeout = setTimeout(() => {
this.login();
}, 1000 * 60 * 1);
});
}
async getOwnerToken() {
if (this.ownSession && this.ownSession.expires_in && this.ownSession.created_at) {
const endTimeStamp = this.ownSession.expires_in * 0.75 + this.ownSession.created_at;
if (Date.now() / 1000 <= endTimeStamp) {
this.log.debug("Skip OwnerToken request");
return;
}
}
await this.requestClient({
method: "post",
url: "https://owner-api.teslamotors.com/oauth/token",
headers: {
"content-type": "application/json; charset=utf-8",
accept: "*/*",
authorization: "bearer " + this.session.access_token,
"accept-language": "de-de",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"x-tesla-user-agent": "TeslaApp/3.10.14-474/540f6f430/ios/12.5.1",
},
data: { grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer", client_id: "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384" },
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
this.ownSession = res.data;
return res.data;
})
.catch(async (error) => {
this.setState("info.connection", false, true);
this.log.error("own token failed");
this.log.error(error);
this.session = {};
error.response && this.log.error(JSON.stringify(error.response.data));
this.log.error("Start relogin in 1min");
this.reLoginTimeout = setTimeout(() => {
this.login();
}, 1000 * 60 * 1);
});
}
async checkWaitForSleepState(id) {
let shift_state = await this.getStateAsync('driveState.shift_state');
let climate = await this.getStateAsync('command.Climate');
let chargeState = await this.getStateAsync('chargeState.charging_state');
if ((shift_state && shift_state.val !== null && shift_state.val !== "P") ||
(climate && climate.val) ||
(chargeState && !['Disconnected', 'Complete', 'NoPower', 'Stopped'].includes(chargeState.val))) {
return false;
}
const checkStates = [".drive_state.shift_state", ".drive_state.speed", ".climate_state.is_climate_on", ".vehicle_state.locked"];
for (let stateId of checkStates) {
const curState = await this.getStateAsync(id + stateId);
//laste update not older than 30min and last change not older then 30min
if (curState && (curState.ts <= Date.now() - 1800000 || curState.ts - curState.lc <= 1800000)) {
return false;
}
}
this.log.debug("30 min not change. Start waiting for sleep");
return true;
}
async sendCommand(id, command, action, value) {
const headers = {
"Content-Type": "application/json; charset=utf-8",
Accept: "*/*",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"x-tesla-user-agent": "TeslaApp/3.10.14-474/540f6f430/ios/12.5.1",
Authorization: "Bearer " + this.ownSession.access_token,
};
let url = "https://owner-api.teslamotors.com/api/1/vehicles/" + id + "/command/" + command;
if (command === "wake_up") {
url = "https://owner-api.teslamotors.com/api/1/vehicles/" + id + "/wake_up";
}
const passwordArray = ["remote_start_drive"];
const latlonArray = ["trigger_homelink", "window_control"];
const onArray = ["remote_steering_wheel_heater_request", "set_preconditioning_max", "set_sentry_mode"];
const valueArray = ["set_temps"];
const stateArray = ["sun_roof_control"];
const commandArray = ["window_control"];
const percentArray = ["set_charge_limit"];
let data = {};
if (command in passwordArray) {
data["password"] = this.config.password;
}
if (command in latlonArray) {
const latState = await this.getStateAsync(id + ".drive_state.latitude");
const lonState = await this.getStateAsync(id + ".drive_state.longitude");
data["lat"] = latState ? latState.val : 0;
data["lon"] = lonState ? lonState.val : 0;
}
if (onArray.includes(command)) {
data["on"] = value;
}
if (valueArray.includes(command)) {
if (command === "set_temps") {
const driverState = await this.getStateAsync(id + ".climate_state.driver_temp_setting");
const passengerState = await this.getStateAsync(id + ".climate_state.passenger_temp_setting");
data["driver_temp"] = driverState ? driverState.val : 23;
data["passenger_temp"] = passengerState ? passengerState.val : driverState.val;
}
data[action] = value;
}
if (stateArray.includes(command)) {
data["state"] = action;
}
if (commandArray.includes(command)) {
data["command"] = action;
}
if (percentArray.includes(command)) {
data["percent"] = value;
}
this.log.debug(url);
this.log.debug(JSON.stringify(data));
return await this.requestClient({
method: "post",
url: url,
headers: headers,
data: data,
})
.then((res) => {
this.log.info(JSON.stringify(res.data));
return res.data.response;
})
.catch((error) => {
if (error.response && error.response.status === 401) {
error.response && this.log.debug(JSON.stringify(error.response.data));
this.log.info(command + " receive 401 error. Refresh Token in 30 seconds");
clearTimeout(this.refreshTokenTimeout);
this.refreshTokenTimeout = setTimeout(() => {
this.refreshToken();
}, 1000 * 30);
return;
}
this.log.error(url);
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
return "error";
});
}
getCodeChallenge() {
let hash = "";
let result = "";
const chars = "0123456789abcdef";
result = "";
for (let i = 64; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
hash = crypto.createHash("sha256").update(result).digest("base64");
hash = hash.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
return [result, hash];
}
randomString(length) {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
extractHidden(body) {
const returnObject = {};
let matches;
if (body.matchAll) {
matches = body.matchAll(/<input (?=[^>]* name=["']([^'"]*)|)(?=[^>]* value=["']([^'"]*)|)/g);
} else {
this.log.warn("The adapter needs in the future NodeJS v12. https://forum.iobroker.net/topic/22867/how-to-node-js-f%C3%BCr-iobroker-richtig-updaten");
matches = this.matchAll(/<input (?=[^>]* name=["']([^'"]*)|)(?=[^>]* value=["']([^'"]*)|)/g, body);
}
for (const match of matches) {
returnObject[match[1]] = match[2];
}
return returnObject;
}
matchAll(re, str) {
let match;
const matches = [];
while ((match = re.exec(str))) {
// add all matched groups
matches.push(match);
}
return matches;
}
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
async onUnload(callback) {
try {
callback();
const obj = await this.getForeignObjectAsync(this.adapterConfig);
if (obj) {
obj.native.session = this.session;
// obj.native.captchaSvg = "";
// obj.native.captcha = "";
// obj.native.mfa = "";
this.log.debug("Session saved");
this.setForeignObject(this.adapterConfig, obj);
}
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (state) {
if (!state.ack) {
const vehicleid = id.split(".")[2];
let command = id.split(".")[4];
const action = command.split("-")[1];
command = command.split("-")[0];
let vehicleState = await this.checkState(vehicleid);
if (vehicleState !== "online") {
this.log.info("Wake up " + id);
while (vehicleState !== "online") {
const vehicleStateData = await this.sendCommand(vehicleid, "wake_up");
if (vehicleState === "error") {
break;
}
vehicleState = vehicleStateData.state;
}
}
await this.sendCommand(vehicleid, command, action, state.val);
this.refreshTimeout = setTimeout(async () => {
await this.updateDevices();
}, 10 * 1000);
} else {
const resultDict = { driver_temp_setting: "set_temps-driver_temp", passenger_temp_setting: "set_temps-passenger_temp" };
const idArray = id.split(".");
const stateName = idArray[idArray.length - 1];
const vin = id.split(".")[2];
let value = true;
if (resultDict[stateName] && isNaN(state.val)) {
if (!state.val || state.val === "INVALID" || state.val === "NOT_CHARGING" || state.val === "ERROR" || state.val === "UNLOCKED") {
value = false;
}
} else {
value = state.val;
}
if (resultDict[stateName]) {
await this.setStateAsync(vin + ".remote." + resultDict[stateName], value, true);
}
}
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Teslamotors(options);
} else {
// otherwise start the instance directly
new Teslamotors();
}