Skip to content

Commit

Permalink
feat: add australia region support (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitnimble authored Mar 22, 2024
1 parent a5223a9 commit f29c85a
Show file tree
Hide file tree
Showing 17 changed files with 1,390 additions and 142 deletions.
80 changes: 39 additions & 41 deletions debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ const askForRegionInput = () => {
type: 'list',
name: 'region',
message: 'What Region are you in?',
choices: ['CN','US', 'EU', 'CA'],
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(answers);
console.log('Logging in...');
createInstance(answers.region, answers.brand);
}
Expand All @@ -72,7 +72,7 @@ const createInstance = (region, brand) => {
password,
region,
brand,
pin
pin,
});
client.on('ready', onReadyHandler);
};
Expand Down Expand Up @@ -187,31 +187,30 @@ async function performCommand(command) {
case 'drvInfo':
const info = await vehicle.driveHistory();
console.log('drvInfo : ');
console.dir(info,{ depth: null });
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 { 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;
Expand All @@ -226,21 +225,20 @@ async function performCommand(command) {
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],
}
]);
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;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"format": "prettier --write './src/**/*.{js,jsx,ts,tsx}'",
"prepublishOnly": "npm run build",
"test": "jest --verbose",
"debug": "cross-env LOG_LEVEL=debug ts-node debug.ts",
"debug": "ts-node debug.ts",
"eu:export:cfb": "cross-env LOG_LEVEL=debug ts-node ./scripts/export_eu_cfb.ts",
"au:export:cfb": "cross-env LOG_LEVEL=debug ts-node ./scripts/export_au_cfb.ts",
"test-ca": "npm run build && node test-ca.js",
"watch": "tsc -w"
},
Expand Down
6 changes: 6 additions & 0 deletions scripts/export_au_cfb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getBrandEnvironment } from '../src/constants/australia';
import { exportCfb } from './export_cfb';

exportCfb('hacksore/hks:native-au', 'australia.cfb.ts', getBrandEnvironment).catch(e =>
console.error(e)
);
76 changes: 76 additions & 0 deletions scripts/export_cfb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable no-console */
import { execSync, spawn } from 'child_process';
import { writeFileSync } from 'fs';
import { join } from 'path';
import { AustraliaBrandEnvironment } from '../src/constants/australia';
import { EuropeanBrandEnvironment } from '../src/constants/europe';
import { Brand } from '../src/interfaces/common.interfaces';

const brands: Brand[] = ['kia', 'hyundai'];

export const exportCfb = async (
dockerImage: string,
outputFilename: string,
getBrandEnvironment: (config: {
brand: Brand;
}) => EuropeanBrandEnvironment | AustraliaBrandEnvironment
) => {
console.debug(`Pulling image ${dockerImage}`);
execSync(`docker pull ${dockerImage}`);
const brandCFB = {
kia: '',
hyundai: '',
};
for (const brand of brands) {
try {
const { appId } = getBrandEnvironment({ brand });

const [first] = await new Promise<string[]>((resolve, reject) => {
console.debug(`Starting image ${dockerImage} - ${brand}`);
const process = spawn('docker', [
'run',
'--rm',
dockerImage,
brand,
'dumpCFB',
`${appId}:${Date.now()}`,
]);
const list: Array<string> = [];
let errors = '';

process.stdout.on('data', data => {
const chunk: Array<string> = data
.toString()
.split('\n')
.map(s => s.trim())
.filter(s => s != '');
list.push(...chunk);
});

process.stderr.on('data', data => {
errors += data + '\n';
});

process.on('close', code => {
console.debug(`Done with ${dockerImage} - ${brand}`);
if (code === 0) {
return resolve(list);
}
reject(errors);
});
});
brandCFB[brand] = first;
} catch (e) {
// Skip any unsupported brands in regions
continue;
}
}

const cfbFile = `// Auto generated file on ${new Date().toISOString()}
// run \`npm run eu:export:cfb\` or \`npm run au:export:cfb\` respectively to update it
export const kiaCFB = Buffer.from('${brandCFB.kia}', 'base64');
export const hyundaiCFB = Buffer.from('${brandCFB.hyundai}', 'base64');`;

writeFileSync(join(__dirname, '..', 'src', 'constants', outputFilename), cfbFile);
};
66 changes: 2 additions & 64 deletions scripts/export_eu_cfb.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,4 @@
/* eslint-disable no-console */
import { execSync, spawn } from 'child_process';
import { writeFileSync } from 'fs';
import { join } from 'path';
import { getBrandEnvironment } from '../src/constants/europe';
import { Brand } from '../src/interfaces/common.interfaces';
import { exportCfb } from './export_cfb';

const brands: Brand[] = ['kia', 'hyundai'];

const main = async () => {
console.debug(`Pulling image hacksore/hks:native`);
execSync('docker pull hacksore/hks:native');
const brandCFB = {
kia: '',
hyundai: '',
};
for (const brand of brands) {
const { appId } = getBrandEnvironment({ brand });

const [first] = await new Promise<string[]>((resolve, reject) => {
console.debug(`Starting image hacksore/hks:native - ${brand}`);
const process = spawn('docker', [
'run',
'--rm',
'hacksore/hks:native',
brand,
'dumpCFB',
`${appId}:${Date.now()}`,
]);
const list: Array<string> = [];
let errors = '';

process.stdout.on('data', data => {
const chunk: Array<string> = data
.toString()
.split('\n')
.map(s => s.trim())
.filter(s => s != '');
list.push(...chunk);
});

process.stderr.on('data', data => {
errors += data + '\n';
});

process.on('close', code => {
console.debug(`Done with hacksore/hks:native - ${brand}`);
if (code === 0) {
return resolve(list);
}
reject(errors);
});
});
brandCFB[brand] = first;
}

const cfbFile = `// Auto generated file on ${new Date().toISOString()}
// run \`npm run eu:export:cfb\` to update it
export const kiaCFB = Buffer.from('${brandCFB.kia}', 'base64');
export const hyundaiCFB = Buffer.from('${brandCFB.hyundai}', 'base64');`;

writeFileSync(join(__dirname, '..', 'src', 'constants', 'europe.cfb.ts'), cfbFile);
};

main().catch(e => console.error(e));
exportCfb('hacksore/hks:native', 'europe.cfb.ts', getBrandEnvironment).catch(e => console.error(e));
9 changes: 8 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
getBrandEnvironment as getCNBrandEnvironment,
ChineseBrandEnvironment,
} from './constants/china';
import {
getBrandEnvironment as getAUBrandEnvironment,
AustraliaBrandEnvironment,
} from './constants/australia';

import { Brand, VehicleStatusOptions } from './interfaces/common.interfaces';

Expand All @@ -21,16 +25,19 @@ export const ALL_ENDPOINTS = {
getEUBrandEnvironment({ brand }).endpoints,
CN: (brand: Brand): ChineseBrandEnvironment['endpoints'] =>
getCNBrandEnvironment({ brand }).endpoints,
AU: (brand: Brand): AustraliaBrandEnvironment['endpoints'] =>
getAUBrandEnvironment({ brand }).endpoints,
};

export const GEN2 = 2;
export const GEN1 = 1;
export type REGION = 'US' | 'CA' | 'EU' | 'CN';
export type REGION = 'US' | 'CA' | 'EU' | 'CN' | 'AU';
export enum REGIONS {
US = 'US',
CA = 'CA',
EU = 'EU',
CN = 'CN',
AU = 'AU',
}

// ev stuffz
Expand Down
5 changes: 5 additions & 0 deletions src/constants/australia.cfb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Auto generated file on 2024-03-20T22:41:31.183Z
// run `npm run eu:export:cfb` or `npm run au:export:cfb` respectively to update it

export const kiaCFB = Buffer.from('IDbMgWBXgic4MAyMgf5PFFRAdGX5O3IyC3uvN3scCs0gDpTFDuyvBorlAH9JMM2/hys=', 'base64');
export const hyundaiCFB = Buffer.from('V60WkEmyRQaAfrBF1623/7QL62MjLVbCHdItGzQ1g5T/hkmKmMVTaMHv4cKGzgD3kfc=', 'base64');
Loading

0 comments on commit f29c85a

Please sign in to comment.