Skip to content

Commit

Permalink
Refactor logs
Browse files Browse the repository at this point in the history
  • Loading branch information
kopiro committed Aug 7, 2024
1 parent 14d7878 commit 929594a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
36 changes: 18 additions & 18 deletions src/cameraAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,24 @@ export class CameraAccessory {
}

private async getStatusAndNotify() {
try {
const cameraStatus = await this.camera.getStatus();
this.platform.log.debug("Notifying new values", cameraStatus);

for (const [key, value] of Object.entries(cameraStatus)) {
const toggleService = this.toggleAccessories[key as keyof Status];
if (toggleService && value !== undefined) {
toggleService
.getCharacteristic(this.api.hap.Characteristic.On)
.updateValue(value);
}
const cameraStatus = await this.camera.getStatus();
this.platform.log.debug(
"Notifying new values",
JSON.stringify(cameraStatus)
);

for (const [key, value] of Object.entries(cameraStatus)) {
const toggleService = this.toggleAccessories[key as keyof Status];
if (toggleService && value !== undefined) {
toggleService
.getCharacteristic(this.api.hap.Characteristic.On)
.updateValue(value);
}
} catch (err) {
// When the camera stops responding or a token error occurs.
this.log.error("Error when retrieving data", err);
}
}

async setup() {
const cameraInfo = await this.camera.getDeviceInfo();
this.accessory.on(PlatformAccessoryEvent.IDENTIFY, () => {
this.log.info("Identify requested", cameraInfo);
});

this.setupInfoAccessory(cameraInfo);

Expand Down Expand Up @@ -272,8 +267,13 @@ export class CameraAccessory {

// Publish as external accessory
this.api.publishExternalAccessories(PLUGIN_ID, [this.accessory]);
this.accessory.on(PlatformAccessoryEvent.IDENTIFY, () => {
this.log.info("Identify requested", cameraInfo);
});

this.getStatusAndNotify();
setImmediate(() => {
this.getStatusAndNotify();
});

// Setup the polling by giving a 3s random delay
// to avoid all the cameras starting at the same time
Expand Down
24 changes: 12 additions & 12 deletions src/tapoCamera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export class TAPOCamera extends OnvifCamera {
return this.isSecureConnectionValue;
}

getStok(loginRetryCount = 0): Promise<string> {
async getStok(loginRetryCount = 0): Promise<string> {
if (this.stok) {
return new Promise((resolve) => resolve(this.stok!));
}
Expand All @@ -336,13 +336,12 @@ export class TAPOCamera extends OnvifCamera {
this.stokPromise = () => this.refreshStok(loginRetryCount);
}

return this.stokPromise()
.then(() => {
return this.stok!;
})
.finally(() => {
this.stokPromise = undefined;
});
try {
await this.stokPromise();
return this.stok!;
} finally {
this.stokPromise = undefined;
}
}

private async getAuthenticatedAPIURL(loginRetryCount = 0) {
Expand Down Expand Up @@ -414,12 +413,13 @@ export class TAPOCamera extends OnvifCamera {
const reqJson = JSON.stringify(req);

if (this.pendingAPIRequests.has(reqJson)) {
this.log.debug("API request already pending", reqJson);
return this.pendingAPIRequests.get(
reqJson
) as Promise<TAPOCameraResponse>;
}

this.log.debug("API new request", reqJson);
this.log.debug("API request", reqJson);

this.pendingAPIRequests.set(
reqJson,
Expand Down Expand Up @@ -472,9 +472,9 @@ export class TAPOCamera extends OnvifCamera {
}

this.log.debug(
`API response`,
response.status,
JSON.stringify(responseData)
"API response",
JSON.stringify(responseData),
`status = ${response.status}`
);

// Apparently the Tapo C200 returns 500 on successful requests,
Expand Down

0 comments on commit 929594a

Please sign in to comment.