Skip to content

Commit 87ce42f

Browse files
committed
More US code + fix on refresh token expiry date
1 parent dd72a97 commit 87ce42f

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/lib/bluelink-regions/canada.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class BluelinkCanada extends Bluelink {
123123
if (this.requestResponseValid(resp.resp, resp.json).valid) {
124124
return {
125125
accessToken: resp.json.result.token.accessToken,
126-
expiry: Math.floor(Date.now() / 1000) + resp.json.result.token.expireIn, // we only get a expireIn not a actual date
126+
expiry: Math.floor(Date.now() / 1000) + Number(resp.json.result.token.expireIn), // we only get a expireIn not a actual date
127127
authCookie: cookieValue,
128128
}
129129
}

src/lib/bluelink-regions/usa.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class BluelinkUSA extends Bluelink {
109109
return {
110110
accessToken: resp.json.access_token,
111111
refreshToken: resp.json.refresh_token,
112-
expiry: Math.floor(Date.now() / 1000) + resp.json.expires_in, // we only get a expireIn not a actual date
112+
expiry: Math.floor(Date.now() / 1000) + Number(resp.json.expires_in), // we only get a expireIn not a actual date
113113
authCookie: undefined,
114114
}
115115
}
@@ -227,4 +227,81 @@ export class BluelinkUSA extends Bluelink {
227227
if (this.config.debugLogging) await this.logger.log(error)
228228
throw Error(error)
229229
}
230+
231+
// US implementation does not seem to have a mechanism to check for succesful commands or not
232+
// for now do nothing but return success until we get some logs and can work out what to do
233+
protected async pollForCommandCompletion(resp: {
234+
resp: Record<string, any>
235+
json: any
236+
}): Promise<{ isSuccess: boolean; data: any }> {
237+
return {
238+
isSuccess: true,
239+
data: resp.json,
240+
}
241+
}
242+
243+
protected async lock(id: string): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
244+
return await this.lockUnlock(id, true)
245+
}
246+
247+
protected async unlock(id: string): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
248+
return await this.lockUnlock(id, false)
249+
}
250+
251+
protected async lockUnlock(id: string, shouldLock: boolean): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
252+
const api = shouldLock ? '/ac/v2/rcs/rdo/off' : '/ac/v2/rcs/rdo/on'
253+
const params = new URLSearchParams()
254+
params.append('userName', this.config.auth.username)
255+
params.append('vin', this.cache.car.vin)
256+
const resp = await this.request({
257+
url: this.apiDomain + api,
258+
method: 'POST',
259+
data: params.toString(),
260+
headers: {
261+
...this.carHeaders(),
262+
bluelinkservicepin: this.config.auth.pin,
263+
},
264+
validResponseFunction: this.requestResponseValid,
265+
})
266+
if (this.requestResponseValid(resp.resp, resp.json).valid) {
267+
return await this.pollForCommandCompletion(resp)
268+
}
269+
const error = `Failed to send lockUnlock command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
270+
if (this.config.debugLogging) await this.logger.log(error)
271+
throw Error(error)
272+
}
273+
274+
protected async startCharge(id: string): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
275+
return await this.chargeStopCharge(id, true)
276+
}
277+
278+
protected async stopCharge(id: string): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
279+
return await this.chargeStopCharge(id, false)
280+
}
281+
282+
protected async chargeStopCharge(
283+
id: string,
284+
shouldCharge: boolean,
285+
): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
286+
const api = shouldCharge ? '/ac/v2/evc/charge/start' : '/ac/v2/evc/charge/stop'
287+
const params = new URLSearchParams()
288+
params.append('userName', this.config.auth.username)
289+
params.append('vin', this.cache.car.vin)
290+
const resp = await this.request({
291+
url: this.apiDomain + api,
292+
method: 'POST',
293+
data: params.toString(),
294+
headers: {
295+
...this.carHeaders(),
296+
bluelinkservicepin: this.config.auth.pin,
297+
},
298+
validResponseFunction: this.requestResponseValid,
299+
})
300+
if (this.requestResponseValid(resp.resp, resp.json).valid) {
301+
return await this.pollForCommandCompletion(resp)
302+
}
303+
const error = `Failed to send charge command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
304+
if (this.config.debugLogging) await this.logger.log(error)
305+
throw Error(error)
306+
}
230307
}

0 commit comments

Comments
 (0)