Skip to content

Commit

Permalink
handle fetch errors and update
Browse files Browse the repository at this point in the history
  • Loading branch information
trympet committed Jul 18, 2020
1 parent e0a6c0d commit 2db921d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
43 changes: 26 additions & 17 deletions src/util/vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Vehicle extends VehicleApi {
private lockTargetState: CharacteristicValue;
private bootUnlock = false;
private updateTask: NodeJS.Timeout;
private isUpdating = false;

constructor(
private config: Config,
Expand Down Expand Up @@ -302,7 +303,6 @@ export class Vehicle extends VehicleApi {

default:
return false;
break;
}

setTimeout(() => this.Update(), 5 * 1000); // Update 5 seconds after value change
Expand All @@ -312,26 +312,35 @@ export class Vehicle extends VehicleApi {
* Refreshes state from VOC API. Note that the state may be delayed by quite a bit.
*/
private async Update(): Promise<void> {
if (this.isUpdating) {
return;
}
this.isUpdating = true;
this.log.debug("Updating...");
await this.RequestUpdate();
await wait(5);
const newState = await this.GetUpdate();
if (this.bootUnlock) {
// User has unlocked boot
if (!newState.carLocked) {
// Car has been fully unlocked
this.bootUnlock = false;
try {
await this.RequestUpdate();
await wait(5);
const newState = await this.GetUpdate();
if (this.bootUnlock) {
// User has unlocked boot
if (!newState.carLocked) {
// Car has been fully unlocked
this.bootUnlock = false;
} else {
// Car is still partially locked, but indicate to the user that the vehicle is in fact unlocked.
// Manually set car unlocked, since boot is open. This isn't shown by the API,
// only by querying for services by /services?active=true. No point in implementing this, since
// this is stateful.
newState.carLocked = false;
}
} else {
// Car is still partially locked, but indicate to the user that the vehicle is in fact unlocked.
// Manually set car unlocked, since boot is open. This isn't shown by the API,
// only by querying for services by /services?active=true. No point in implementing this, since
// this is stateful.
newState.carLocked = false;
this.lockTargetState = newState.carLocked ? 1 : 0;
}
} else {
this.lockTargetState = newState.carLocked ? 1 : 0;
this.state = newState;
} catch (error) {
this.log.error("Failed to update vehicle status from Volvo On Call API.");
}
this.state = newState;
this.isUpdating = false;
}

private Lock() {
Expand Down
8 changes: 7 additions & 1 deletion src/util/vehicleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export class VehicleApi extends REST {
* Make a call to the VOC API.
*/
public async Call(method: string, data?: Record<string, unknown>) {
const initialCall: CallState = await this.Post(method, this.vehicleUrl, data);
let initialCall: CallState;
try {
initialCall = await this.Post(method, this.vehicleUrl, data);
} catch (error) {
this.log.error(`Failed to post ${method} to Volvo On Call API.`);
return false;
}
// Sometimes the VOC API doesn't return valid JSON. Therefore we have this edgecase.
if (!initialCall["service"] || !initialCall["status"] || !this.VALID_STATUS.includes(initialCall["status"] || "")) {
this.log.error(`Failed to execute ${method}: ${initialCall["errorDescription"]}`);
Expand Down

0 comments on commit 2db921d

Please sign in to comment.