diff --git a/README.md b/README.md index 212f0899..c7285c03 100755 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Use the Get Account Vehicles request to see which requests your vehicle supports ## Sample -Use a random version 4 uuid (there are online generators) as a deviceId. +Use a random version 4 uuid as a deviceId. Generator available [here](https://www.uuidgenerator.net/version4). import OnStar from "onstarjs"; @@ -24,7 +24,11 @@ Use a random version 4 uuid (there are online generators) as a deviceId. username: "foo@bar.com", password: "p@ssw0rd", onStarPin: "1234", + + // Optional checkRequestStatus: true, // When false, requests are complete when 'In Progress' (Much faster). + requestPollingIntervalSeconds: 6, // When checkRequestStatus is true, this is how often status check requests will be made + requestPollingTimeoutSeconds: 60, // When checkRequestStatus is true, this is when requests while polling are considered timed out }; const onStar = OnStar.create(config); diff --git a/src/RequestService.ts b/src/RequestService.ts index 1f27529b..620d719c 100644 --- a/src/RequestService.ts +++ b/src/RequestService.ts @@ -40,8 +40,9 @@ enum OnStarApiCommand { class RequestService { private config: OnStarConfig; private authToken?: OAuthToken; - private checkRequestTimeout = 6000; private checkRequestStatus: boolean; + private requestPollingTimeoutSeconds: number; + private requestPollingIntervalSeconds: number; private tokenRefreshPromise?: Promise; private tokenUpgradePromise?: Promise; @@ -56,6 +57,10 @@ class RequestService { }; this.checkRequestStatus = this.config.checkRequestStatus ?? true; + this.requestPollingTimeoutSeconds = + config.requestPollingTimeoutSeconds ?? 60; + this.requestPollingIntervalSeconds = + config.requestPollingIntervalSeconds ?? 6; } setClient(client: HttpClient) { @@ -70,8 +75,14 @@ class RequestService { return this; } - setCheckRequestTimeout(timeoutMs: number) { - this.checkRequestTimeout = timeoutMs; + setRequestPollingTimeoutSeconds(seconds: number) { + this.requestPollingTimeoutSeconds = seconds; + + return this; + } + + setRequestPollingIntervalSeconds(seconds: number) { + this.requestPollingIntervalSeconds = seconds; return this; } @@ -354,9 +365,10 @@ class RequestService { const requestTimestamp = new Date(requestTime).getTime(); - const requestGiveup = this.checkRequestTimeout * 10; - - if (Date.now() >= requestTimestamp + requestGiveup) { + if ( + Date.now() >= + requestTimestamp + this.requestPollingTimeoutSeconds * 1000 + ) { throw new RequestError("Command Timeout") .setResponse(response) .setRequest(request); @@ -435,7 +447,7 @@ class RequestService { private checkRequestPause() { return new Promise((resolve) => - setTimeout(resolve, this.checkRequestTimeout), + setTimeout(resolve, this.requestPollingIntervalSeconds * 1000), ); } } diff --git a/src/types.ts b/src/types.ts index ed0d5bf8..14d30cb0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,6 +18,8 @@ export interface OnStarConfig { password: string; onStarPin: string; checkRequestStatus?: boolean; + requestPollingIntervalSeconds?: number; + requestPollingTimeoutSeconds?: number; } export interface OAuthToken { diff --git a/test/functional/OnStarJs.test.ts b/test/functional/OnStarJs.test.ts index 15eed377..67e45daa 100644 --- a/test/functional/OnStarJs.test.ts +++ b/test/functional/OnStarJs.test.ts @@ -37,4 +37,22 @@ describe("OnStarJs", () => { expect(result.status).toEqual("success"); expect(result.response?.data).toHaveProperty("commandResponse"); }); + + test.skip("Diagnostics Request Successful", async () => { + jest.setTimeout(60000); + + onStar.setCheckRequestStatus(true); + + const result = await onStar.diagnostics(); + + if (!result.response?.data || typeof result.response?.data === "string") { + throw new Error("Invalid response returned"); + } + + expect(result.status).toEqual("success"); + expect(result.response?.data.commandResponse?.status).toEqual("success"); + expect(result.response?.data.commandResponse?.body).toHaveProperty( + "diagnosticResponse", + ); + }); }); diff --git a/test/unit/RequestService.test.ts b/test/unit/RequestService.test.ts index 0c5d3cd3..eec4de47 100644 --- a/test/unit/RequestService.test.ts +++ b/test/unit/RequestService.test.ts @@ -54,10 +54,10 @@ describe("RequestService", () => { testConfig, instance(tokenHandler), httpClient, - ); - - requestService.setAuthToken(authToken); - requestService.setCheckRequestTimeout(1); + ) + .setAuthToken(authToken) + .setRequestPollingIntervalSeconds(0) + .setRequestPollingTimeoutSeconds(0); }); test("start", async () => {