Skip to content

Commit

Permalink
Adding more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
kopiro committed Aug 27, 2024
1 parent 2bb32c4 commit 0ba3a27
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
10 changes: 8 additions & 2 deletions src/cameraAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ export class CameraAccessory {

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

this.log.debug(
`Status "${tapoServiceStr}" not found in status`,
cameraStatus
);
return null;
} catch (err) {
this.log.error("Error getting status:", err);
Expand All @@ -126,7 +132,7 @@ export class CameraAccessory {
})
.onSet(async (newValue) => {
try {
this.log.info(
this.log.debug(
`Setting "${tapoServiceStr}" to ${newValue ? "on" : "off"}...`
);
this.camera.setStatus(tapoServiceStr, Boolean(newValue));
Expand Down
17 changes: 3 additions & 14 deletions src/cameraPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import {
API,
IndependentPlatformPlugin,
Logging,
PlatformAccessory,
PlatformConfig,
} from "homebridge";
import { CameraAccessory, CameraConfig } from "./cameraAccessory";
import { PLUGIN_ID, PLATFORM_NAME } from "./pkg";

export interface CameraPlatformConfig extends PlatformConfig {
cameras?: CameraConfig[];
Expand All @@ -26,20 +24,11 @@ export class CameraPlatform implements IndependentPlatformPlugin {
private discoverDevices() {
this.config.cameras?.forEach(async (cameraConfig) => {
try {
const accessory = new CameraAccessory(this, cameraConfig);
await accessory.setup();
const cameraAccessory = new CameraAccessory(this, cameraConfig);
await cameraAccessory.setup();
} catch (err) {
this.log.error(
`Error during setup of camera ${cameraConfig.name}`,
err
);
this.log.error("Error during setup of camera", cameraConfig, err);
}
});
}

private removeAccessory(platformAccessory: PlatformAccessory) {
this.api.unregisterPlatformAccessories(PLUGIN_ID, PLATFORM_NAME, [
platformAccessory,
]);
}
}
71 changes: 43 additions & 28 deletions src/tapoCamera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,32 @@ export class TAPOCamera extends OnvifCamera {
.update(this.cnonce + this.hashedSha256Password + nonce)
.digest("hex")
.toUpperCase();
const hashedNoncesWithMD5 = crypto
.createHash("md5")
.update(this.cnonce + this.hashedMD5Password + nonce)
.digest("hex")
.toUpperCase();

if (deviceConfirm === hashedNoncesWithSHA256 + nonce + this.cnonce) {
this.passwordEncryptionMethod = "sha256";
return true;
}

const hashedNoncesWithMD5 = crypto
.createHash("md5")
.update(this.cnonce + this.hashedMD5Password + nonce)
.digest("hex")
.toUpperCase();
if (deviceConfirm === hashedNoncesWithMD5 + nonce + this.cnonce) {
this.passwordEncryptionMethod = "md5";
return true;
}

this.log.debug(
'Invalid device confirm, expected "sha256" or "md5" to match, but none found',
{
hashedNoncesWithMD5,
hashedNoncesWithSHA256,
deviceConfirm,
nonce,
cnonce: this,
}
);

return false;
}

Expand All @@ -162,7 +172,7 @@ export class TAPOCamera extends OnvifCamera {

let fetchParams = {};
if (isSecureConnection) {
this.log.debug("StokRefresh: Using secure connection");
this.log.debug("refreshStok: Using secure connection");
fetchParams = {
method: "post",
body: JSON.stringify({
Expand All @@ -175,7 +185,7 @@ export class TAPOCamera extends OnvifCamera {
}),
};
} else {
this.log.debug("StokRefresh: Using unsecure connection");
this.log.debug("refreshStok: Using unsecure connection");
fetchParams = {
method: "post",
body: JSON.stringify({
Expand All @@ -196,15 +206,14 @@ export class TAPOCamera extends OnvifCamera {
responseData = (await response.json()) as TAPOCameraRefreshStokResponse;

this.log.debug(
"StokRefresh: Login response :>> ",
"refreshStok: Login response",
response.status,
JSON.stringify(responseData)
responseData
);

if (response.status === 401) {
if (responseData.result?.data?.code === 40411) {
throw new Error("Invalid credentials");
}
if (response.status === 401 && responseData.result?.data?.code === 40411) {
this.log.debug("StokRefresh: Invalid credentials, code 40411");
throw new Error("Invalid credentials");
}

const nonce = responseData.result?.data?.nonce;
Expand Down Expand Up @@ -243,13 +252,15 @@ export class TAPOCamera extends OnvifCamera {
responseData = (await response.json()) as TAPOCameraRefreshStokResponse;

this.log.debug(
"StokRefresh: Start_seq response :>>",
"refreshStok: Start_seq response",
response.status,
JSON.stringify(responseData)
responseData
);

if (responseData?.result?.start_seq) {
if (responseData?.result?.user_group !== "root") {
this.log.debug("Incorrect user_group detected");

// # encrypted control via 3rd party account does not seem to be supported
// # see https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/issues/456
throw new Error("Incorrect user_group detected");
Expand All @@ -265,6 +276,8 @@ export class TAPOCamera extends OnvifCamera {
responseData?.result?.data?.sec_left &&
responseData.result.data.sec_left > 0
) {
this.log.debug("StokRefresh: Temporary Suspension", responseData);

throw new Error(
`StokRefresh: Temporary Suspension: Try again in ${responseData.result.data.sec_left} seconds`
);
Expand All @@ -275,6 +288,8 @@ export class TAPOCamera extends OnvifCamera {
responseData?.data?.sec_left &&
responseData.data.sec_left > 0
) {
this.log.debug("StokRefresh: Temporary Suspension (40404)", responseData);

throw new Error(
`StokRefresh: Temporary Suspension: Try again in ${responseData.data.sec_left} seconds`
);
Expand All @@ -293,7 +308,7 @@ export class TAPOCamera extends OnvifCamera {
this.log.debug(
`Unexpected response, retrying: ${loginRetryCount}/${MAX_LOGIN_RETRIES}.`,
response.status,
JSON.stringify(responseData)
responseData
);
return this.refreshStok(loginRetryCount + 1);
}
Expand Down Expand Up @@ -473,11 +488,7 @@ export class TAPOCamera extends OnvifCamera {
responseData = responseDataTmp as TAPOCameraResponse;
}

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

// Apparently the Tapo C200 returns 500 on successful requests,
// but it's indicating an expiring token, therefore refresh the token next time
Expand All @@ -492,7 +503,11 @@ export class TAPOCamera extends OnvifCamera {
responseData.error_code === -40401 ||
responseData.error_code === -1
) {
this.log.debug("API request failed, trying reauth");
this.log.debug(
"API request failed, trying reauth",
response.status,
responseData
);
this.stok = undefined;
return this.apiRequest(req, loginRetryCount + 1);
}
Expand Down Expand Up @@ -671,11 +686,11 @@ export class TAPOCamera extends OnvifCamera {
);
const led = operations.find((r) => r.method === "getLedStatus");

if (!alert) this.log.warn("No alert config found");
if (!lensMask) this.log.warn("No lens mask config found");
if (!notifications) this.log.warn("No notifications config found");
if (!motionDetection) this.log.warn("No motion detection config found");
if (!led) this.log.warn("No led config found");
if (!alert) this.log.debug("No alert config found");
if (!lensMask) this.log.debug("No lens mask config found");
if (!notifications) this.log.debug("No notifications config found");
if (!motionDetection) this.log.debug("No motion detection config found");
if (!led) this.log.debug("No led config found");

return {
alarm: alert
Expand Down

0 comments on commit 0ba3a27

Please sign in to comment.