Skip to content

Commit

Permalink
Merge pull request #59 from Hacksore/develop
Browse files Browse the repository at this point in the history
Fix #54 and issue with parsed status property tirePressureWarningLampAll
  • Loading branch information
Hacksore authored Jul 28, 2020
2 parents 2c97e73 + cef5763 commit 1f51c0c
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 69 deletions.
32 changes: 21 additions & 11 deletions __tests__/controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@ const getController = region => {
};

describe('AmericanController', () => {
it('call getVehicles and check length', async () => {
const controller = getController('US');
const controller = getController('US');

it('call login and get valid response', async () => {
(got as any).mockReturnValueOnce({
body: {
access_token: 'jest',
refresh_token: 'test',
},
statusCode: 200,
});

expect(await controller.login()).toEqual('login good');
});

it('call getVehicles and check length', async () => {
(got as any).mockReturnValueOnce({
body: JSON.stringify({
enrolledVehicleDetails: [
Expand All @@ -58,7 +70,6 @@ describe('AmericanController', () => {
});

describe('EuropeanController', () => {

it('call getVehicles and check length', async () => {
const controller = getController('EU');
controller.session.accessToken = 'MockToken';
Expand All @@ -75,25 +86,25 @@ describe('EuropeanController', () => {
regId: '123123',
gen: '2',
name: 'Car',
id: '12345',
id: '12345',
},
],
},
},
statusCode: 200,
});

(got as any).mockReturnValueOnce({
body: {
resMsg: {
vinInfo: [
{
{
basic: {
modelYear: '2019',
vin: '5555',
id: '123456'
}
}
id: '123456',
},
},
],
},
},
Expand All @@ -106,8 +117,7 @@ describe('EuropeanController', () => {
});

describe('CanadianController', () => {

it('call getVehicles and check length', async () => {
it('call getVehicles and check length', async () => {
const controller = getController('CA');

(got as any).mockReturnValueOnce({
Expand Down
5 changes: 4 additions & 1 deletion lib/constants/america.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//TODO: Someone needs to figure out the Kia endpoints
// we can then make a fork easier to maintain

export const API_HOST = 'api.telematics.hyundaiusa.com';

export const BASE_URL = 'https://api.telematics.hyundaiusa.com';
export const BASE_URL = `https://${API_HOST}`;
export const CLIENT_ID = '815c046afaa4471aa578827ad546cc76';
export const CLIENT_SECRET = 'GXZveJJAVTehh/OtakM3EQ==';
51 changes: 27 additions & 24 deletions lib/constants/canada.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
// Kia seems to use myuvo.ca as mentioned by @wcomartin
// forks can modify some things to make this work
export const CA_API_HOST = 'mybluelink.ca';
export const CA_BASE_URL = `https://${CA_API_HOST}`;
export const CLIENT_ORIGIN = 'SPA';
export const CA_BASE_URL = 'https://mybluelink.ca';

export const CA_ENDPOINTS = {
login: `${CA_BASE_URL}/tods/api/lgn`,
logout: `${CA_BASE_URL}/tods/api/lgout`,
// Account
myAccount: `${CA_BASE_URL}/tods/api/acctinfo`,
nextService: `${CA_BASE_URL}/tods/api/nxtsvc`,
preferedDealer: `${CA_BASE_URL}/tods/api/gtprfrdlr`,
// Vehicle
vehicleList: `${CA_BASE_URL}/tods/api/vhcllst`,
vehicleInfo: `${CA_BASE_URL}/tods/api/sltvhcl`,
status: `${CA_BASE_URL}/tods/api/lstvhclsts`,
remoteStatus: `${CA_BASE_URL}/tods/api/rltmvhclsts`,
// Car commands with preauth (PIN)
lock: `${CA_BASE_URL}/tods/api/drlck`,
unlock: `${CA_BASE_URL}/tods/api/drulck`,
start: `${CA_BASE_URL}/tods/api/evc/rfon`,
stop: `${CA_BASE_URL}/tods/api/evc/rfoff`,
locate: `${CA_BASE_URL}/tods/api/fndmcr`,
hornlight: `${CA_BASE_URL}/tods/api/hornlight`,
// System
verifyAccountToken: `${CA_BASE_URL}/tods/api/vrfyacctkn`,
verifyPin: `${CA_BASE_URL}/tods/api/vrfypin`,
verifyToken: `${CA_BASE_URL}/tods/api/vrfytnc`,
};
login: `${CA_BASE_URL}/tods/api/lgn`,
logout: `${CA_BASE_URL}/tods/api/lgout`,
// Account
myAccount: `${CA_BASE_URL}/tods/api/acctinfo`,
nextService: `${CA_BASE_URL}/tods/api/nxtsvc`,
preferedDealer: `${CA_BASE_URL}/tods/api/gtprfrdlr`,
// Vehicle
vehicleList: `${CA_BASE_URL}/tods/api/vhcllst`,
vehicleInfo: `${CA_BASE_URL}/tods/api/sltvhcl`,
status: `${CA_BASE_URL}/tods/api/lstvhclsts`,
remoteStatus: `${CA_BASE_URL}/tods/api/rltmvhclsts`,
// Car commands with preauth (PIN)
lock: `${CA_BASE_URL}/tods/api/drlck`,
unlock: `${CA_BASE_URL}/tods/api/drulck`,
start: `${CA_BASE_URL}/tods/api/evc/rfon`,
stop: `${CA_BASE_URL}/tods/api/evc/rfoff`,
locate: `${CA_BASE_URL}/tods/api/fndmcr`,
hornlight: `${CA_BASE_URL}/tods/api/hornlight`,
// System
verifyAccountToken: `${CA_BASE_URL}/tods/api/vrfyacctkn`,
verifyPin: `${CA_BASE_URL}/tods/api/vrfypin`,
verifyToken: `${CA_BASE_URL}/tods/api/vrfytnc`,
};
5 changes: 4 additions & 1 deletion lib/constants/europe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const EU_BASE_URL = 'https://prd.eu-ccapi.hyundai.com:8080';
export const EU_API_HOST = 'prd.eu-ccapi.hyundai.com:8080';
export const EU_BASE_URL = `https://${EU_API_HOST}`;

export const EU_ENDPOINTS = {
session: `${EU_BASE_URL}/api/v1/user/oauth2/authorize?response_type=code&state=test&client_id=6d477c38-3ca4-4cf3-9557-2a1929a94654&redirect_uri=${EU_BASE_URL}/api/v1/user/oauth2/redirect`,
Expand All @@ -13,3 +14,5 @@ export const EU_CONSTANTS = {
'Basic NmQ0NzdjMzgtM2NhNC00Y2YzLTk1NTctMmExOTI5YTk0NjU0OktVeTQ5WHhQekxwTHVvSzB4aEJDNzdXNlZYaG10UVI5aVFobUlGampvWTRJcHhzVg==',
GCMSenderID: '199360397125',
};

export const EU_CLIENT_ID = '6d477c38-3ca4-4cf3-9557-2a1929a94654';
5 changes: 5 additions & 0 deletions lib/controllers/american.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class AmericanController extends SessionController {
return Promise.resolve('Token not expired, no need to refresh');
}

// TODO: come up with a better return value?
public async login(): Promise<string> {
try {
logger.debug('Logging in to API');
Expand All @@ -63,6 +64,10 @@ export class AmericanController extends SessionController {
json: true,
});

if (response.statusCode !== 200) {
return Promise.resolve('login bad');
}

this.session.accessToken = response.body.access_token;
this.session.refreshToken = response.body.refresh_token;
this.session.tokenExpiresAt = Math.floor(
Expand Down
14 changes: 1 addition & 13 deletions lib/controllers/canadian.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,6 @@ export class CanadianController extends SessionController {
brandIndicator: vehicle.brandIndicator,
regId: vehicle.regid,
generation: vehicle.genType,
// pin: this.config.pin,
// vehicleId: vehicle.vehicleId,
// vin: vehicle.vin,
// nickname: vehicle.nickName,
// defaultVehicle: vehicle.defaultVehicle,
// modelName: vehicle.modelName,
// modelYear: vehicle.modelYear,
// fuelKindCode: vehicle.fuelKindCode,
// genType: vehicle.genType,
// subscriptionEndDate: vehicle.subscriptionEndDate,
// mileageForNextService: vehicle.mileageForNextService,
// daysForNextService: vehicle.daysForNextService,
} as VehicleRegisterOptions;

this.vehicles.push(new CanadianVehicle(vehicleConfig, this));
Expand All @@ -105,7 +93,7 @@ export class CanadianController extends SessionController {
//////////////////////////////////////////////////////////////////////////////
// Account
//////////////////////////////////////////////////////////////////////////////

// TODO: deprecated account specific data
public async myAccount(): Promise<AccountInfo> {
logger.info('Begin myAccount request');
try {
Expand Down
26 changes: 12 additions & 14 deletions lib/controllers/european.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EU_CONSTANTS, EU_BASE_URL } from './../constants/europe';
import { EU_CONSTANTS, EU_BASE_URL, EU_API_HOST, EU_CLIENT_ID } from './../constants/europe';
import { BlueLinkyConfig, Session } from './../interfaces/common.interfaces';
import * as pr from 'push-receiver';
import got from 'got';
Expand Down Expand Up @@ -27,7 +27,7 @@ export class EuropeanController extends SessionController {
controlToken: undefined,
deviceId: this.uuidv4(),
tokenExpiresAt: 0,
controlTokenExpiresAt: 0
controlTokenExpiresAt: 0,
};

private vehicles: Array<EuropeanVehicle> = [];
Expand Down Expand Up @@ -80,15 +80,15 @@ export class EuropeanController extends SessionController {
method: 'POST',
json: true,
body: {
"email": this.userConfig.username,
"password": this.userConfig.password,
'email': this.userConfig.username,
'password': this.userConfig.password,
},
cookieJar,
});

if(authCodeResponse){
if (authCodeResponse) {
const regexMatch = /code=([^&]*)/g.exec(authCodeResponse.body.redirectUrl);
if(regexMatch !== null){
if (regexMatch !== null) {
this.session.refreshToken = regexMatch[1];
} else {
throw new Error('@EuropeControllerLogin: AuthCode was not found');
Expand All @@ -99,10 +99,9 @@ export class EuropeanController extends SessionController {
const notificationReponse = await got(`${EU_BASE_URL}/api/v1/spa/notifications/register`, {
method: 'POST',
headers: {
'ccsp-service-id': '6d477c38-3ca4-4cf3-9557-2a1929a94654',
'ccsp-service-id': EU_CLIENT_ID,
'Content-Type': 'application/json;charset=UTF-8',
'Content-Length': '231',
'Host': 'prd.eu-ccapi.hyundai.com:8080',
'Host': EU_API_HOST,
'Connection': 'Keep-Alive',
'Accept-Encoding': 'gzip',
'User-Agent': 'okhttp/3.10.0',
Expand All @@ -115,15 +114,15 @@ export class EuropeanController extends SessionController {
json: true,
});

if(notificationReponse) {
if (notificationReponse) {
this.session.deviceId = notificationReponse.body.resMsg.deviceId;
}

const formData = new URLSearchParams();
formData.append('grant_type', 'authorization_code');
formData.append('redirect_uri', ALL_ENDPOINTS.EU.redirectUri);

if(this.session.refreshToken){
if (this.session.refreshToken) {
formData.append('code', this.session.refreshToken);
}

Expand All @@ -132,8 +131,7 @@ export class EuropeanController extends SessionController {
headers: {
'Authorization': EU_CONSTANTS.basicToken,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': '154',
'Host': 'prd.eu-ccapi.hyundai.com:8080',
'Host': EU_API_HOST,
'Connection': 'Keep-Alive',
'Accept-Encoding': 'gzip',
'User-Agent': 'okhttp/3.10.0',
Expand All @@ -146,7 +144,7 @@ export class EuropeanController extends SessionController {
Promise.reject(`Get token failed: ${err}`);
});

if(response){
if (response) {
const responseBody = JSON.parse(response.body);
this.session.accessToken = 'Bearer ' + responseBody.access_token;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/vehicles/american.vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default class AmericanVehicle extends Vehicle {
frontLeft: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampFrontLeft,
frontRight: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampFrontRight,
rearRight: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampRearRight,
all: !!vehicleStatus.tirePressureLamp.trunkOpenStatus,
all: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampAll,
},
},
climate: {
Expand Down
2 changes: 1 addition & 1 deletion lib/vehicles/canadian.vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class CanadianVehicle extends Vehicle {
frontLeft: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampFrontLeft,
frontRight: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampFrontRight,
rearRight: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampRearRight,
all: !!vehicleStatus.tirePressureLamp.trunkOpenStatus,
all: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampAll,
},
},
climate: {
Expand Down
2 changes: 1 addition & 1 deletion lib/vehicles/european.vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class EuropeanVehicle extends Vehicle {
frontLeft: !!vehicleStatus.tirePressureLamp.tirePressureLampFL,
frontRight: !!vehicleStatus.tirePressureLamp.tirePressureLampFR,
rearRight: !!vehicleStatus.tirePressureLamp.tirePressureLampRR,
all: !!vehicleStatus.tirePressureLamp.tirePressureLampAll,
all: !!vehicleStatus.tirePressureLamp.tirePressureWarningLampAll,
},
},
climate: {
Expand Down
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bluelinky",
"version": "5.1.1",
"version": "5.1.2",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 1f51c0c

Please sign in to comment.