Skip to content

Commit

Permalink
Merge pull request #8 from Hacksore/support-older-generations
Browse files Browse the repository at this point in the history
Support older generations
  • Loading branch information
Sean Boult authored Dec 7, 2019
2 parents 7d74945 + 9e07075 commit d5a70c5
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 129 deletions.
3 changes: 3 additions & 0 deletions lib/endpoints.ts → lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export const endpoints = {
enrollmentStatus: 'https://owners.hyundaiusa.com/bin/common/enrollmentStatus',
subscriptions: 'https://owners.hyundaiusa.com/bin/common/managesubscription'
};

export const GEN2 = 2;
export const GEN1 = 1;
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import got from 'got';
import { endpoints } from './endpoints';
import { endpoints } from './constants';
import Vehicle from './vehicle';
import { buildFormData } from './util';

Expand Down Expand Up @@ -78,7 +78,7 @@ class BlueLinky {
this.vehicles.push(vehicle);

return new Promise((resolve, reject) => {
vehicle.getEventEmitter().on('ready', () => resolve(vehicle));
vehicle.on('ready', () => resolve(vehicle));
});
}

Expand Down
66 changes: 38 additions & 28 deletions lib/vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BlueLinky from './index';
import EventEmitter from 'events';
import { endpoints } from './endpoints';
import { endpoints, GEN1, GEN2} from './constants';
import got from 'got';
import { buildFormData } from './util';

Expand All @@ -13,17 +13,18 @@ import {

import logger from './logger';

export default class Vehicle {
export default class Vehicle extends EventEmitter {
private vin: string|null;
private pin: string|null;
private eventEmitter: EventEmitter;
private bluelinky: BlueLinky;
private currentFeatures: object;
private gen: number = 2;
private regId: string|null = null;

constructor(config: VehicleConfig) {
super();
this.vin = config.vin;
this.pin = config.pin;
this.eventEmitter = new EventEmitter();
this.bluelinky = config.bluelinky;
this.currentFeatures = {};

Expand All @@ -44,18 +45,23 @@ export default class Vehicle {
});

}

const ownerInfo = await this.ownerInfo();
if (ownerInfo !== null) {
const vehicle = ownerInfo.result.OwnersVehiclesInfo.find(item => this.vin === item.VinNumber);
this.gen = vehicle.IsGen2;
this.regId = vehicle.RegistrationID;
logger.debug(`registering a gen ${this.gen} vehicle (${this.regId})`);
}

// we tell the vehicle it's loaded :D
this.eventEmitter.emit('ready');
this.emit('ready');
}

getVinNumber(): string|null {
return this.vin;
}

getEventEmitter(): EventEmitter {
return this.eventEmitter;
}

hasFeature(featureName: string): boolean {
return this.currentFeatures[featureName];
}
Expand All @@ -71,8 +77,6 @@ export default class Vehicle {
}

const formData = {
gen: 2,
regId: this.vin,
service: 'remoteunlock'
};

Expand All @@ -92,8 +96,6 @@ export default class Vehicle {
}

const response = await this._request(endpoints.remoteAction, {
gen: 2,
regId: this.vin,
service: 'remotelock'
});

Expand All @@ -107,8 +109,6 @@ export default class Vehicle {
async start(config: StartConfig): Promise<HyundaiResponse|null> {

const response = await this._request(endpoints.remoteAction, {
gen: 2,
regId: this.vin,
service: 'ignitionstart',
...config
});
Expand All @@ -123,8 +123,6 @@ export default class Vehicle {
async stop(): Promise<HyundaiResponse|null> {

const response = await this._request(endpoints.remoteAction, {
gen: 2,
regId: this.vin,
service: 'ignitionstop'
});

Expand All @@ -139,8 +137,6 @@ export default class Vehicle {
async flashLights(): Promise<HyundaiResponse|null> {

const response = await this._request(endpoints.remoteAction, {
gen: 2,
regId: this.vin,
service: 'light'
});

Expand All @@ -154,8 +150,6 @@ export default class Vehicle {
async panic(): Promise<HyundaiResponse|null> {

const response = await this._request(endpoints.remoteAction, {
gen: 2,
regId: this.vin,
service: 'horn'
});

Expand Down Expand Up @@ -221,6 +215,19 @@ export default class Vehicle {
};
}

async ownerInfo(): Promise<HyundaiResponse|null> {

const response = await this._request(endpoints.myAccount, {
service: 'getOwnerInfoService'
});

return {
result: response.RESPONSE_STRING,
status: response.E_IFRESULT,
errorMessage: response.E_IFFAILMSG
};
}

async features(): Promise<HyundaiResponse|null> {

const response = await this._request(endpoints.enrollmentStatus, {
Expand Down Expand Up @@ -276,10 +283,12 @@ export default class Vehicle {

async status(refresh: boolean = false): Promise<VehicleStatus|null> {

if (this.gen === GEN1) {
throw new Error('Status feature is not supported on gen 1 vehicles :(');
}

const response = await this._request(endpoints.status, {
services: 'getVehicleStatus', // THIS IS WHAT HAPPENS WHEN YOU MAKE A PRO TYPO.... services (plural)
gen: 2,
regId: this.vin,
refresh: refresh // I think this forces the their API to connect to the vehicle and pull the status
});

Expand All @@ -293,15 +302,16 @@ export default class Vehicle {
// handle token refresh if we need to
await this.bluelinky.handleTokenRefresh();

const merged = Object.assign({
const formData = buildFormData({
vin: this.vin,
username: this.bluelinky.username,
pin: this.pin,
url: 'https://owners.hyundaiusa.com/us/en/page/dashboard.html',
token: this.bluelinky.getAccessToken()
}, data);

const formData = buildFormData(merged);
token: this.bluelinky.getAccessToken(),
gen: this.gen,
regId: this.regId,
...data
});

const response = await got(endpoint, {
method: 'POST',
Expand Down
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": "0.0.7",
"version": "0.0.8",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Loading

0 comments on commit d5a70c5

Please sign in to comment.