-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteslaService.js
81 lines (66 loc) · 2.44 KB
/
teslaService.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
// Copyright (c) 2021 Tapani Saarinen
'use strict';
const fs = require('fs');
const tesla = require('teslajs');
const log = require('./logger');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function openChargePort() {
try {
const json = fs.readFileSync('tokens.json', {
encoding: 'utf8'
});
const options = JSON.parse(json);
if (!options) {
log.error('Invalid tokens file.');
return;
}
tesla.wakeUpAsync(options).catch(e => {
log.error('Failed to wake-up vehicle.', e);
});
log.info('Get charge state...');
const chargeState = await tesla.chargeStateAsync(options);
log.info('Charge port latch: ' + JSON.stringify(chargeState.charge_port_latch));
log.info('Charging state: ' + JSON.stringify(chargeState.charging_state));
if (chargeState.charge_port_latch === 'Engaged' && chargeState.charging_state !== 'Disconnected') {
if (chargeState.charging_state === 'Charging' || chargeState.charging_state === 'Starting') {
console.log('Stop charging...');
await tesla.stopChargeAsync(options);
await tesla.flashLightsAsync(options);
await tesla.flashLightsAsync(options);
for (let i = 0; i < 10; i++) {
await sleep(1000);
const cs = await tesla.chargeStateAsync(options);
if (cs.charging_state === 'Stopped') {
log.info('Charging stopped.');
break;
}
}
}
log.info('Release charge port latch...');
await tesla.openChargePortAsync(options);
await tesla.flashLightsAsync(options);
} else {
if (chargeState.charging_state === 'Disconnected') {
if (chargeState.charge_port_door_open) {
log.info('Close charge port...');
await tesla.closeChargePortAsync(options);
} else {
log.info('Open charge port...');
await tesla.openChargePortAsync(options);
}
} else {
log.info('Start charging...');
await tesla.startChargeAsync(options);
}
await tesla.flashLightsAsync(options);
}
const chargeStateAfter = await tesla.chargeStateAsync(options);
log.info('Charge port latch: ' + JSON.stringify(chargeStateAfter.charge_port_latch));
log.info('Charging state: ' + JSON.stringify(chargeStateAfter.charging_state));
} catch (e) {
log.error('Tesla API failed: ', e);
}
}
module.exports = {
openChargePort
};