-
Notifications
You must be signed in to change notification settings - Fork 78
/
debug.ts
253 lines (243 loc) · 7.75 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
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
/* eslint-disable */
// TODO: add all calls from EU and CA
import config from './config.json';
import BlueLinky from './src';
import inquirer from 'inquirer';
import { Vehicle } from './src/vehicles/vehicle';
const apiCalls = [
{ name: 'exit', value: 'exit' },
{ name: 'start', value: 'start' },
{ name: 'vehicles', value: 'vehicles' },
{ 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' },
{ name: '[EU] monthly report', value: 'monthlyReport' },
{ name: '[EU] trip informations', value: 'tripInfo' },
{ name: '[EU] drive informations', value: 'drvInfo' },
{ name: '[EV] get charge targets', value: 'getChargeTargets' },
{ name: '[EV] set charge targets', value: 'setChargeTargets' },
{ name: '[EV] start charging', value: 'startCharge' },
{ name: '[EV] stop charging', value: 'stopCharge' },
];
let client: BlueLinky;
let vehicle;
const { username, password, pin, useInfo = false } = config;
const onReadyHandler = <T extends Vehicle>(vehicles: T[]) => {
vehicle = vehicles[0];
askForCommandInput();
};
const askForRegionInput = () => {
inquirer
.prompt([
{
type: 'list',
name: 'region',
message: 'What Region are you in?',
choices: ['CN', 'US', 'EU', 'CA', 'AU'],
},
{
type: 'list',
name: 'brand',
message: 'Which brand are you using?',
choices: ['hyundai', 'kia'],
},
])
.then(answers => {
if (answers.command == 'exit') {
return;
} else {
console.log(answers);
console.log('Logging in...');
createInstance(answers.region, answers.brand);
}
});
};
const createInstance = (region, brand) => {
// global abuse :)
client = new BlueLinky({
username,
password,
region,
brand,
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 'vehicles':
const vehicles = await client.getVehicles();
const response = vehicles.map(v => {
const { name, vin, nickname, regDate } = v.vehicleConfig;
return { name, vin, nickname, regDate };
});
console.log('vehicles', JSON.stringify(response, null, 2));
break;
case 'status':
const status = await vehicle.status({
refresh: false,
parsed: true,
useInfo,
});
console.log('status : ' + JSON.stringify(status, null, 2));
break;
case 'statusU':
const statusU = await vehicle.status({
refresh: false,
parsed: false,
useInfo,
});
console.log('status : ' + JSON.stringify(statusU, null, 2));
break;
case 'statusR':
const statusR = await vehicle.status({
refresh: true,
parsed: true,
useInfo,
});
console.log('status remote : ' + JSON.stringify(statusR, null, 2));
break;
case 'fullStatus':
const fullStatus = await vehicle.fullStatus({
refresh: false,
parsed: false,
useInfo,
});
console.log('full status cached : ' + JSON.stringify(fullStatus, null, 2));
break;
case 'fullStatusR':
const fullStatusR = await vehicle.fullStatus({
refresh: true,
parsed: false,
useInfo,
});
console.log('full status remote : ' + JSON.stringify(fullStatusR, null, 2));
break;
case 'start':
const startRes = await vehicle.start({
airCtrl: false,
igniOnDuration: 10,
airTempvalue: 24,
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;
case 'monthlyReport':
const report = await vehicle.monthlyReport();
console.log('monthyReport : ' + JSON.stringify(report, null, 2));
break;
case 'drvInfo':
const info = await vehicle.driveHistory();
console.log('drvInfo : ');
console.dir(info, { depth: null });
break;
case 'tripInfo':
const currentYear = new Date().getFullYear();
const { year, month, day } = await inquirer.prompt([
{
type: 'list',
name: 'year',
message: 'Which year?',
choices: new Array(currentYear - 2015).fill(0).map((_, i) => currentYear - i),
},
{
type: 'list',
name: 'month',
message: 'Which month?',
choices: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
},
{
type: 'list',
name: 'day',
message: 'Which day (0 to ignore the day)?',
choices: new Array(32).fill(0).map((_, i) => i),
},
]);
const trips = await vehicle.tripInfo({ year, month, day: day === 0 ? undefined : day });
console.log('trips : ' + JSON.stringify(trips, null, 2));
break;
case 'getChargeTargets':
const targets = await vehicle.getChargeTargets();
console.log('targets : ' + JSON.stringify(targets, null, 2));
break;
case 'startCharge':
await vehicle.startCharge();
break;
case 'stopCharge':
await vehicle.stopCharge();
break;
case 'setChargeTargets':
const { fast, slow } = await inquirer.prompt([
{
type: 'list',
name: 'fast',
message: 'What fast charge limit do you which to set?',
choices: [50, 60, 70, 80, 90, 100],
},
{
type: 'list',
name: 'slow',
message: 'What slow charge limit do you which to set?',
choices: [50, 60, 70, 80, 90, 100],
},
]);
await vehicle.setChargeTargets({ fast, slow });
console.log('targets : OK');
break;
}
askForCommandInput();
} catch (err) {
console.log(err);
}
}
askForRegionInput();