Skip to content

Commit

Permalink
Merge pull request #107 from Hacksore/develop
Browse files Browse the repository at this point in the history
6.0.1
  • Loading branch information
Hacksore authored Mar 4, 2021
2 parents 805cd04 + 0addf5b commit e7947d8
Show file tree
Hide file tree
Showing 11 changed files with 2,118 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "bluelinky",
"version": "6.0.0",
"version": "6.0.1",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "rollup -c rollup.config.js",
"stamps": "node ./tools/generate.stamps.js",
"dev": "rollup -w -c rollup.config.js",
"lint": "eslint ./src --ext .js,.jsx,.ts,.tsx",
"prepublishOnly": "npm run build",
Expand Down
1 change: 1 addition & 0 deletions src/constants/europe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const EU_API_HOST = 'prd.eu-ccapi.hyundai.com:8080';
export const EU_BASE_URL = `https://${EU_API_HOST}`;

export const EU_CLIENT_ID = '6d477c38-3ca4-4cf3-9557-2a1929a94654';
export const EU_APP_ID = '99cfff84-f4e2-4be8-a5ed-e5b755eb6581';

export const EU_ENDPOINTS = {
session: `${EU_BASE_URL}/api/v1/user/oauth2/authorize?response_type=code&state=test&client_id=${EU_CLIENT_ID}&redirect_uri=${EU_BASE_URL}/api/v1/user/oauth2/redirect`,
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/european.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { URLSearchParams } from 'url';

import { CookieJar } from 'tough-cookie';
import { VehicleRegisterOptions } from '../interfaces/common.interfaces';
import { getStamp } from '../tools/european.tools';

export class EuropeanController extends SessionController {
constructor(userConfig: BlueLinkyConfig) {
Expand Down Expand Up @@ -148,6 +149,7 @@ export class EuropeanController extends SessionController {
'Connection': 'Keep-Alive',
'Accept-Encoding': 'gzip',
'User-Agent': 'okhttp/3.10.0',
'Stamp': await getStamp(),
},
body: {
pushRegId: credentials.gcm.token,
Expand Down Expand Up @@ -176,6 +178,7 @@ export class EuropeanController extends SessionController {
'Accept-Encoding': 'gzip',
'User-Agent': 'okhttp/3.10.0',
'grant_type': 'authorization_code',
'Stamp': await getStamp(),
},
body: formData.toString(),
cookieJar,
Expand Down Expand Up @@ -212,6 +215,7 @@ export class EuropeanController extends SessionController {
headers: {
'Authorization': this.session.accessToken,
'ccsp-device-id': this.session.deviceId,
'Stamp': await getStamp(),
},
json: true,
});
Expand All @@ -226,6 +230,7 @@ export class EuropeanController extends SessionController {
headers: {
'Authorization': this.session.accessToken,
'ccsp-device-id': this.session.deviceId,
'Stamp': await getStamp(),
},
json: true,
}
Expand Down
16 changes: 16 additions & 0 deletions src/interfaces/common.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export interface Session {
controlTokenExpiresAt?: number;
}

export enum EVPlugTypes {
UNPLUGED = 0,
FAST = 1,
PORTABLE = 2,
STATION = 3
}

// Status remapped
export interface VehicleStatus {
engine: {
Expand All @@ -32,6 +39,15 @@ export interface VehicleStatus {
charging?: boolean;
timeToFullCharge?: unknown;
range: number;
rangeGas?: number;
rangeEV?: number;
plugedTo? : EVPlugTypes;
estimatedCurrentChargeDuration?: number;
estimatedFastChargeDuration?: number;
estimatedPortableChargeDuration?: number;
estimatedStationChargeDuration?: number;
batteryCharge12v?: number;
batteryChargeHV?: number;
adaptiveCruiseControl: boolean;
};
climate: {
Expand Down
1,003 changes: 1,003 additions & 0 deletions src/tools/european.hyundai.token.collection.ts

Large diffs are not rendered by default.

1,003 changes: 1,003 additions & 0 deletions src/tools/european.kia.token.collection.ts

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/tools/european.tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import stamps from './european.hyundai.token.collection';

export const getStamp = (): string => {
return stamps[Math.floor(Math.random() * stamps.length)];
};
34 changes: 31 additions & 3 deletions src/vehicles/european.vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
VehicleRegisterOptions,
VehicleStatusOptions,
RawVehicleStatus,
EVPlugTypes,
} from '../interfaces/common.interfaces';
import got from 'got';

Expand All @@ -16,6 +17,7 @@ import { Vehicle } from './vehicle';
import { EuropeanController } from '../controllers/european.controller';
import { celciusToTempCode, tempCodeToCelsius } from '../util';
import { EU_BASE_URL } from '../constants/europe';
import { getStamp } from '../tools/european.tools';

export default class EuropeanVehicle extends Vehicle {
public region = REGIONS.EU;
Expand Down Expand Up @@ -57,6 +59,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
json: true,
}
Expand Down Expand Up @@ -87,6 +90,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
json: true,
}
Expand All @@ -107,6 +111,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
body: {
action: 'close',
Expand Down Expand Up @@ -134,6 +139,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
body: {
action: 'open',
Expand Down Expand Up @@ -169,6 +175,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
json: true,
}
Expand All @@ -185,6 +192,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
json: true,
}
Expand All @@ -199,6 +207,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
json: true,
}
Expand Down Expand Up @@ -230,6 +239,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
json: true,
}
Expand All @@ -240,7 +250,7 @@ export default class EuropeanVehicle extends Vehicle {
? response.body.resMsg
: response.body.resMsg.vehicleStatusInfo.vehicleStatus;

const parsedStatus = {
const parsedStatus: VehicleStatus = {
chassis: {
hoodOpen: vehicleStatus?.hoodOpen,
trunkOpen: vehicleStatus?.trunkOpen,
Expand Down Expand Up @@ -271,12 +281,26 @@ export default class EuropeanVehicle extends Vehicle {
engine: {
ignition: vehicleStatus.engine,
adaptiveCruiseControl: vehicleStatus?.acc,
range: vehicleStatus?.evStatus?.drvDistance[0].rangeByFuel?.totalAvailableRange?.value,
rangeGas: vehicleStatus?.evStatus?.drvDistance[0]?.rangeByFuel?.gasModeRange?.value ?? vehicleStatus?.dte?.value,
// EV
range: vehicleStatus?.evStatus?.drvDistance[0]?.rangeByFuel?.totalAvailableRange?.value,
rangeEV: vehicleStatus?.evStatus?.drvDistance[0]?.rangeByFuel?.evModeRange?.value,
plugedTo: vehicleStatus?.evStatus?.batteryPlugin ?? EVPlugTypes.UNPLUGED,
charging: vehicleStatus?.evStatus?.batteryCharge,
estimatedCurrentChargeDuration: vehicleStatus?.evStatus?.remainTime2?.atc?.value,
estimatedFastChargeDuration: vehicleStatus?.evStatus?.remainTime2?.etc1?.value,
estimatedPortableChargeDuration: vehicleStatus?.evStatus?.remainTime2?.etc2?.value,
estimatedStationChargeDuration: vehicleStatus?.evStatus?.remainTime2?.etc3?.value,
batteryCharge12v: vehicleStatus?.battery?.batSoc,
batteryChargeHV: vehicleStatus?.evStatus?.batteryStatus,
},
} as VehicleStatus;
};

if(!parsedStatus.engine.range) {
if (parsedStatus.engine.rangeEV || parsedStatus.engine.rangeGas) {
parsedStatus.engine.range = (parsedStatus.engine.rangeEV ?? 0) + (parsedStatus.engine.rangeGas ?? 0);
}
}

this._status = statusConfig.parsed ? parsedStatus : vehicleStatus;

Expand All @@ -293,6 +317,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
json: true,
}
Expand All @@ -312,6 +337,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
json: true,
}
Expand Down Expand Up @@ -342,6 +368,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
body: {
action: 'start',
Expand Down Expand Up @@ -369,6 +396,7 @@ export default class EuropeanVehicle extends Vehicle {
'Authorization': this.controller.session.controlToken,
'ccsp-device-id': this.controller.session.deviceId,
'Content-Type': 'application/json',
'Stamp': await getStamp(),
},
body: {
action: 'stop',
Expand Down
32 changes: 32 additions & 0 deletions tools/european.tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { execSync, spawn } = require('child_process');

const APP_IDS = {
hyundai: '99cfff84-f4e2-4be8-a5ed-e5b755eb6581',
kia: '693a33fa-c117-43f2-ae3b-61a02d24f417'
};

exports.getStamps = async (brand) => {
if (!APP_IDS[brand]) {
throw new Error(`${brand} is not managed.`);
}

return new Promise((resolve, reject) => {
execSync('docker pull hacksore/hks');
const process = spawn('docker', ['run', 'hacksore/hks', brand, 'list', APP_IDS[brand]]);
const list = [];
let errors = '';
process.stdout.on('data', (data) => {
const chunk = 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) => {
if(code === 0) {
return resolve(list);
}
reject(errors);
});
});
};
19 changes: 19 additions & 0 deletions tools/generate.stamps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { join, resolve } = require('path');
const { writeFileSync } = require('fs');
const { getStamps } = require('./european.tools.js');

const SIZE = 1000;

const generateStampsForBrand = async (brand) => {
const stamps = await getStamps(brand);
const array = stamps.slice(1, SIZE + 1);

writeFileSync(
join(resolve('.'), 'src', 'tools', `european.${brand}.token.collection.ts`),
`/* eslint-disable */
export default ${JSON.stringify(array, undefined, 4)};`
);
};

generateStampsForBrand('hyundai').catch((err) => console.error(err));
generateStampsForBrand('kia').catch((err) => console.error(err));

0 comments on commit e7947d8

Please sign in to comment.