forked from Hacksore/bluelinky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.ts
153 lines (143 loc) · 4.15 KB
/
debug.ts
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
/* eslint-disable */
// TODO: add all calls from EU and CA
import config from './config.json';
import BlueLinky from './lib';
import inquirer from 'inquirer';
const apiCalls = [
{ name: 'exit', value: 'exit' },
{ name: 'start', value: 'start' },
{ name: 'odometer', value: 'odometer' },
{ name: 'stop', value: 'stop' },
{ name: 'status (on server cache)', value: 'status' },
{ name: 'status (on server cache) unparsed', value: 'statusU' },
{ name: 'status refresh (fetch from vehicle)', value: 'statusR' },
{ name: 'full raw status (on server cache)', value: 'fullStatus' },
{ name: 'full raw status refresh (fetch from vehicle)', value: 'fullStatusR' },
{ name: 'lock', value: 'lock' },
{ name: 'unlock', value: 'unlock' },
{ name: 'locate', value: 'locate' },
];
let vehicle;
const { username, password, vin, pin } = config;
const onReadyHandler = vehicles => {
vehicle = vehicles[0];
askForCommandInput();
};
const askForRegionInput = () => {
inquirer
.prompt([
{
type: 'list',
name: 'region',
message: 'What Region are you in?',
choices: ['US', 'EU', 'CA'],
},
])
.then(answers => {
if (answers.command == 'exit') {
return;
} else {
console.log(answers)
console.log('Logging in...');
createInstance(answers.region);
}
});
};
const createInstance = region => {
const client = new BlueLinky({
username,
password,
region: region,
pin
});
client.on('ready', onReadyHandler);
};
function askForCommandInput() {
console.log('');
inquirer
.prompt([
{
type: 'list',
name: 'command',
message: 'What you wanna do?',
choices: apiCalls,
},
])
.then(answers => {
if (answers.command == 'exit') {
return;
} else {
performCommand(answers.command);
}
});
}
async function performCommand(command) {
try {
switch (command) {
case 'exit':
return;
case 'locate':
const locate = await vehicle.location();
console.log('locate : ' + JSON.stringify(locate, null, 2));
break;
case 'odometer':
const odometer = await vehicle.odometer();
console.log('odometer', JSON.stringify(odometer, null, 2));
break;
case 'status':
const status = await vehicle.status({
refresh: false,
parsed: true,
});
console.log('status : ' + JSON.stringify(status, null, 2));
break;
case 'statusR':
const statusR = await vehicle.status({
refresh: true,
parsed: true
});
console.log('status remote : ' + JSON.stringify(statusR, null, 2));
break;
case 'fullStatus':
const fullStatus = await vehicle.fullStatus({
refresh: false,
parsed: false
});
console.log('full status cached : ' + JSON.stringify(fullStatus, null, 2));
break;
case 'fullStatusR':
const fullStatusR = await vehicle.fullStatus({
refresh: true,
parsed: false
});
console.log('full status remote : ' + JSON.stringify(fullStatusR, null, 2));
break;
case 'start':
const startRes = await vehicle.start({
airCtrl: false,
igniOnDuration: 10,
airTempvalue: 70,
defrost: false,
heating1: false,
});
console.log('start : ' + JSON.stringify(startRes, null, 2));
break;
case 'stop':
const stopRes = await vehicle.stop();
console.log('stop : ' + JSON.stringify(stopRes, null, 2));
break;
case 'lock':
const lockRes = await vehicle.lock();
console.log('lock : ' + JSON.stringify(lockRes, null, 2));
break;
case 'unlock':
const unlockRes = await vehicle.unlock();
console.log('unlock : ' + JSON.stringify(unlockRes, null, 2));
break;
}
askForCommandInput();
} catch (err) {
console.log(err);
}
}
askForRegionInput();