Skip to content

Commit

Permalink
Debug Logging Support
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelahern committed Mar 9, 2024
1 parent fed4d45 commit 1e4b49c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Example platform config in the Homebridge config.json:
"name": "My WattBox 2",
...
}
]
],
"debug": false
}
]
```
5 changes: 5 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
}
}
}
},
"debug": {
"title": "Debug Logging",
"type": "boolean",
"required": false
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface WattBoxConfig {
devices: WattBoxDeviceConfig[],
debug?: boolean
}

export interface WattBoxDeviceConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class WattBoxPlatform implements DynamicPlatformPlugin {
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
const accessory = existingAccessory ?? new this.api.platformAccessory(deviceConfig.name, uuid);

const deviceApi = new WattBoxDeviceApi(deviceConfig.host, deviceConfig.username, deviceConfig.password);
const deviceApi = new WattBoxDeviceApi(deviceConfig.host, deviceConfig.username, deviceConfig.password, this.log, this.config.debug ?? false, `[${accessory.displayName}]`);

try {
const deviceInfo = await deviceApi.getDeviceInfo();
Expand Down
40 changes: 35 additions & 5 deletions src/wattbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Mutex, MutexInterface } from "async-mutex";
import { Logger } from "homebridge";
import { Socket } from "net";
import { PromiseSocket } from "promise-socket";

Expand All @@ -8,19 +9,29 @@ export class WattBoxDeviceApi {
private readonly host: string;
private readonly username: string;
private readonly password: string;
private readonly log: Logger;
private readonly logDebug: boolean;
private readonly logPrefix: string;
private readonly mutex: Mutex;

constructor(host: string, username: string, password: string) {
constructor(host: string, username: string, password: string, log: Logger, logDebug: boolean, logPrefix: string) {
this.host = host;
this.username = username;
this.password = password;
this.log = log;
this.logDebug = logDebug;
this.logPrefix = logPrefix;
this.mutex = new Mutex();
}

public async getDeviceInfo() {
const client = new PromiseSocket();
const mutexRelease = await this.mutex.acquire();

if (this.logDebug) {
this.log.debug(`${this.logPrefix} getDeviceInfo()`);
}

try {
await this.login(client);

Expand Down Expand Up @@ -71,6 +82,10 @@ export class WattBoxDeviceApi {
const client = new PromiseSocket();
const mutexRelease = await this.mutex.acquire();

if (this.logDebug) {
this.log.debug(`${this.logPrefix} getDeviceStatus()`);
}

try {
await this.login(client);

Expand Down Expand Up @@ -112,15 +127,18 @@ export class WattBoxDeviceApi {
}

try {
if (this.logDebug) {
this.log.debug(`${this.logPrefix} subscribeDeviceStatus() -> poll()`);
}
PubSub.publish(topic, await this.getDeviceStatus());
}
catch (error: unknown) {
if (error instanceof Error) {
// TODO: Log...
catch (err) {
if (err instanceof Error) {
this.log.error(`${this.logPrefix} subscribeDeviceStatus() -> ${err.message}`)
}
}

setTimeout(poll, 20000);
setTimeout(poll, 10000);
}

setTimeout(poll, 0);
Expand All @@ -137,6 +155,10 @@ export class WattBoxDeviceApi {
const client = new PromiseSocket();
const mutexRelease = await this.mutex.acquire();

if (this.logDebug) {
this.log.debug(`${this.logPrefix} setOutletAction()`);
}

try {
await this.login(client);

Expand All @@ -153,6 +175,10 @@ export class WattBoxDeviceApi {
}

private async login(client: PromiseSocket<Socket>) {
if (this.logDebug) {
this.log.debug(`${this.logPrefix} login()`);
}

client.setEncoding("utf8");
client.setTimeout(10000);

Expand All @@ -176,6 +202,10 @@ export class WattBoxDeviceApi {
}

private async logout(client: PromiseSocket<Socket>, mutexRelease: MutexInterface.Releaser) {
if (this.logDebug) {
this.log.debug(`${this.logPrefix} logout()`);
}

try {
// !Exit
await client.write("!Exit\n");
Expand Down

0 comments on commit 1e4b49c

Please sign in to comment.