Skip to content

Commit

Permalink
Making the plugin more resilient to reboots
Browse files Browse the repository at this point in the history
  • Loading branch information
kopiro committed Aug 9, 2024
1 parent bb14aad commit 83a710e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
58 changes: 34 additions & 24 deletions src/cameraAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,31 @@ export class CameraAccessory {
toggleService
.getCharacteristic(this.api.hap.Characteristic.On)
.onGet(async () => {
this.log.debug(`Getting "${tapoServiceStr}" status...`);
try {
this.log.debug(`Getting "${tapoServiceStr}" status...`);

const cameraStatus = await this.camera.getStatus();
const value = cameraStatus[tapoServiceStr];
if (value !== undefined) {
return value;
const cameraStatus = await this.camera.getStatus();
const value = cameraStatus[tapoServiceStr];
if (value !== undefined) return value;

return null;
} catch (err) {
this.log.error("Error getting status:", err);
return null;
}
throw new this.api.hap.HapStatusError(
this.api.hap.HAPStatus.RESOURCE_DOES_NOT_EXIST
);
})
.onSet(async (newValue) => {
this.log.info(
`Setting "${tapoServiceStr}" to ${newValue ? "on" : "off"}...`
);
this.camera.setStatus(tapoServiceStr, Boolean(newValue));
try {
this.log.info(
`Setting "${tapoServiceStr}" to ${newValue ? "on" : "off"}...`
);
this.camera.setStatus(tapoServiceStr, Boolean(newValue));
} catch (err) {
this.log.error("Error setting status:", err);
throw new this.api.hap.HapStatusError(
this.api.hap.HAPStatus.RESOURCE_DOES_NOT_EXIST
);
}
});

this.accessory.addService(toggleService);
Expand Down Expand Up @@ -201,19 +210,20 @@ export class CameraAccessory {
}

private async getStatusAndNotify() {
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);
try {
const cameraStatus = await this.camera.getStatus();
this.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) {
this.log.error("Error getting status:", err);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cameraPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class CameraPlatform implements IndependentPlatformPlugin {
} catch (err) {
this.log.error(
`Error during setup of camera ${cameraConfig.name}`,
(err as Error)?.message
err
);
}
});
Expand Down
8 changes: 5 additions & 3 deletions src/tapoCamera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type Status = {

export class TAPOCamera extends OnvifCamera {
private readonly kStreamPort = 554;
private readonly httpsAgent: Agent;
private readonly fetchAgent: Agent;

private readonly hashedMD5Password: string;
private readonly hashedSha256Password: string;
Expand All @@ -49,8 +49,10 @@ export class TAPOCamera extends OnvifCamera {
) {
super(log, config);

this.httpsAgent = new Agent({
this.fetchAgent = new Agent({
connectTimeout: 5_000,
connect: {
// TAPO devices have self-signed certificates
rejectUnauthorized: false,
},
});
Expand Down Expand Up @@ -100,7 +102,7 @@ export class TAPOCamera extends OnvifCamera {
return fetch(url, {
headers: this.getHeaders(),
// @ts-expect-error Dispatcher type not there
dispatcher: this.httpsAgent,
dispatcher: this.fetchAgent,
...data,
});
}
Expand Down

0 comments on commit 83a710e

Please sign in to comment.