Skip to content

Commit 1d32f13

Browse files
committed
Add debug and debugLevel options.
1 parent 91386aa commit 1d32f13

12 files changed

Lines changed: 92 additions & 85 deletions

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ Before you can configure, you must go to the [Tuya IoT Platform](https://iot.tuy
7676
- `options.endpoint` - **required** : The endpoint URL taken from the [API Reference > Endpoints](https://developer.tuya.com/en/docs/iot/api-request?id=Ka4a8uuo1j4t4#title-1-Endpoints) table.
7777
- `options.accessId` - **required** : The Access ID obtained from [Tuya IoT Platform > Cloud Develop](https://iot.tuya.com/cloud)
7878
- `options.accessKey` - **required** : The Access Secret obtained from [Tuya IoT Platform > Cloud Develop](https://iot.tuya.com/cloud)
79+
- `options.debug` - **optional**: Includes debugging output in the Homebridge log. (Default: `false`)
80+
- `options.debugLevel` - **optional**: An optional list of strings seperated with comma `,`. `api` represents for HTTP API log, `mqtt` represents for MQTT log, and device ID represents for device log. If blank, all logs are outputed.
7981

8082
#### For "Smart Home" Project
8183

@@ -88,7 +90,9 @@ Before you can configure, you must go to the [Tuya IoT Platform](https://iot.tuy
8890
- `options.password` - **required** : The app account's password. MD5 salted password is also available for increased security.
8991
- `options.appSchema` - **required** : The app schema: 'tuyaSmart' for the Tuya Smart App, or 'smartlife' for the Smart Life App.
9092
- `options.endpoint` - **optional** : The endpoint URL can be inferred from the [API Reference > Endpoints](https://developer.tuya.com/en/docs/iot/api-request?id=Ka4a8uuo1j4t4#title-1-Endpoints) table based on the country code provided. Only manually set this value if you encounter login issues and need to specify the endpoint for your account location.
91-
- `options.homeWhitelist` - **optional**: An array of integer values for the home IDs you want to whitelist. If provided, only devices with matching Home IDs will be included. You can find the Home ID in the homebridge log.
93+
- `options.homeWhitelist` - **optional**: An array of integer values for the home IDs you want to whitelist. If provided, only devices with matching Home IDs will be included. You can find the Home ID in the Homebridge log.
94+
- `options.debug` - **optional**: Includes debugging output in the Homebridge log. (Default: `false`)
95+
- `options.debugLevel` - **optional**: An optional list of strings seperated with comma `,`. `api` represents for API and MQTT log, device ID represents for specific device log. If blank, all logs are outputed.
9296

9397

9498
#### Advanced options
@@ -160,15 +164,9 @@ After Homebridge has been successfully launched, the device information list wil
160164
**⚠️Please make sure to remove sensitive information such as `ip`, `lon`, `lat`, `local_key`, and `uid` before submitting the file.**
161165

162166

163-
#### 2. Enable Homebridge Debug Mode
167+
#### 2. Enable Debug Mode
164168

165-
For Homebridge Web UI users:
166-
- Go to the `Homebridge Setting` page
167-
- Turn on the `Homebridge Debug Mode -D` switch
168-
- Restart Homebridge.
169-
170-
For Homebridge Command Line Users:
171-
- Start Homebridge with the `-D` flag: `homebridge -D`
169+
Add debug option in the plugin config, then restart Homebridge.
172170

173171
#### 3. Collect Logs
174172

config.schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@
268268
}
269269
}
270270
}
271+
},
272+
"debug": {
273+
"title": "Enable Debug Logging",
274+
"type": "boolean",
275+
"default": false
276+
},
277+
"debugLevel": {
278+
"title": "Debug Level",
279+
"description": "An optional list of strings seperated with comma `,`. `api` represents for API and MQTT log, device ID represents for specific device log. If blank, all logs are outputed.",
280+
"type": "string"
271281
}
272282
}
273283
}

src/accessory/BaseAccessory.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ class BaseAccessory {
3030

3131
public deviceManager = this.platform.deviceManager!;
3232
public device = this.deviceManager.getDevice(this.accessory.context.deviceID)!;
33-
public log = new PrefixLogger(this.platform.log, this.device.name.length > 0 ? this.device.name : this.device.id);
33+
public log = new PrefixLogger(
34+
this.platform.log,
35+
this.device.name.length > 0 ? this.device.name : this.device.id,
36+
this.platform.options.debug && ((this.platform.options.debugLevel ?? '').length > 0
37+
? this.platform.options.debugLevel?.includes(this.device.id)
38+
: true),
39+
);
40+
3441
public intialized = false;
3542

3643
public adaptiveLightingController?;

src/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface TuyaPlatformCustomConfigOptions {
2727
username: string;
2828
password: string;
2929
deviceOverrides?: Array<TuyaPlatformDeviceConfig>;
30+
debug?: boolean;
31+
debugLevel?: string;
3032
}
3133

3234
export interface TuyaPlatformHomeConfigOptions {
@@ -40,6 +42,8 @@ export interface TuyaPlatformHomeConfigOptions {
4042
appSchema: string;
4143
homeWhitelist?: Array<number>;
4244
deviceOverrides?: Array<TuyaPlatformDeviceConfig>;
45+
debug?: boolean;
46+
debugLevel?: string;
4347
}
4448

4549
export type TuyaPlatformConfigOptions = TuyaPlatformCustomConfigOptions | TuyaPlatformHomeConfigOptions;
@@ -54,6 +58,8 @@ export const customOptionsSchema = {
5458
accessId: { type: 'string', required: true },
5559
accessKey: { type: 'string', required: true },
5660
deviceOverrides: { 'type': 'array' },
61+
debug: { type: 'boolean' },
62+
debugLevel: { 'type': 'string' },
5763
},
5864
};
5965

@@ -68,5 +74,7 @@ export const homeOptionsSchema = {
6874
appSchema: { 'type': 'string', required: true },
6975
homeWhitelist: { 'type': 'array' },
7076
deviceOverrides: { 'type': 'array' },
77+
debug: { type: 'boolean' },
78+
debugLevel: { 'type': 'string' },
7179
},
7280
};

src/core/TuyaOpenAPI.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ export default class TuyaOpenAPI {
8585
public accessKey: string,
8686
public log: Logger = console,
8787
public lang = 'en',
88+
public debug = false,
8889
) {
89-
this.log = new PrefixLogger(log, TuyaOpenAPI.name);
90+
this.log = new PrefixLogger(log, TuyaOpenAPI.name, debug);
9091
}
9192

9293
static getDefaultEndpoint(countryCode: number) {

src/core/TuyaOpenMQ.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ export default class TuyaOpenMQ {
3737
constructor(
3838
public api: TuyaOpenAPI,
3939
public log: Logger = console,
40+
public debug = false,
4041
) {
41-
this.log = new PrefixLogger(log, TuyaOpenMQ.name);
42+
this.log = new PrefixLogger(log, TuyaOpenMQ.name, debug);
4243
}
4344

4445
start() {

src/device/TuyaCustomDeviceManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ export default class TuyaCustomDeviceManager extends TuyaDeviceManager {
66

77
constructor(
88
public api: TuyaOpenAPI,
9+
public debug = false,
910
) {
10-
super(api);
11+
super(api, debug);
1112
this.mq.version = '2.0';
1213
}
1314

src/device/TuyaDeviceManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ export default class TuyaDeviceManager extends EventEmitter {
3232

3333
constructor(
3434
public api: TuyaOpenAPI,
35+
public debug = false,
3536
) {
3637
super();
3738

3839
const log = (this.api.log as PrefixLogger).log;
39-
this.log = new PrefixLogger(log, TuyaDeviceManager.name);
40+
this.log = new PrefixLogger(log, TuyaDeviceManager.name, debug);
4041

4142
this.mq = new TuyaOpenMQ(api, log);
4243
this.mq.addMessageListener(this.onMQTTMessage.bind(this));

src/platform.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,10 @@ export class TuyaPlatform implements DynamicPlatformPlugin {
253253
const DEFAULT_PASS = 'homebridge';
254254

255255
let res;
256-
const { endpoint, accessId, accessKey } = this.options;
257-
const api = new TuyaOpenAPI(endpoint, accessId, accessKey, this.log);
258-
const deviceManager = new TuyaCustomDeviceManager(api);
256+
const { endpoint, accessId, accessKey, debug, debugLevel } = this.options;
257+
const debugMode = debug && ((debugLevel ?? '').length > 0 ? debugLevel?.includes('api') : true);
258+
const api = new TuyaOpenAPI(endpoint, accessId, accessKey, this.log, 'en', debugMode);
259+
const deviceManager = new TuyaCustomDeviceManager(api, debugMode);
259260

260261
this.log.info('Get token.');
261262
res = await api.getToken();
@@ -341,13 +342,16 @@ export class TuyaPlatform implements DynamicPlatformPlugin {
341342
}
342343

343344
let res;
344-
const { accessId, accessKey, countryCode, username, password, appSchema, endpoint } = this.options;
345+
const { accessId, accessKey, countryCode, username, password, appSchema, endpoint, debug, debugLevel } = this.options;
346+
const debugMode = debug && ((debugLevel ?? '').length > 0 ? debugLevel?.includes('api') : true);
345347
const api = new TuyaOpenAPI(
346348
(endpoint && endpoint.length > 0) ? endpoint : TuyaOpenAPI.getDefaultEndpoint(countryCode),
347349
accessId,
348350
accessKey,
349-
this.log);
350-
const deviceManager = new TuyaHomeDeviceManager(api);
351+
this.log,
352+
'en',
353+
debugMode);
354+
const deviceManager = new TuyaHomeDeviceManager(api, debugMode);
351355

352356
this.log.info('Log in to Tuya Cloud.');
353357
res = await api.homeLogin(countryCode, username, password, appSchema);

src/util/FfmpegStreamingProcess.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@ export class FfmpegStreamingProcess {
3636
readonly stdin: Writable;
3737

3838
constructor(
39-
cameraName: string,
4039
sessionId: string,
4140
videoProcessor: string,
4241
ffmpegArgs: string[],
4342
log: PrefixLogger,
44-
debug = false,
4543
delegate: StreamingDelegate,
4644
callback?: StreamRequestCallback,
4745
) {
4846

49-
log.debug('Stream command: ' + videoProcessor + ' ' + ffmpegArgs.join(' '), cameraName, debug);
47+
log.debug(`Stream command: ${videoProcessor} ${ffmpegArgs.map(value => JSON.stringify(value)).join(' ')}`);
5048

5149
let started = false;
5250
const startTime = Date.now();
@@ -63,11 +61,11 @@ export class FfmpegStreamingProcess {
6361
const runtime = (Date.now() - startTime) / 1000;
6462
const message = 'Getting the first frames took ' + runtime + ' seconds.';
6563
if (runtime < 5) {
66-
log.debug(message, cameraName, debug);
64+
log.debug(message);
6765
} else if (runtime < 22) {
68-
log.warn(message, cameraName);
66+
log.warn(message);
6967
} else {
70-
log.error(message, cameraName);
68+
log.error(message);
7169
}
7270
}
7371
}
@@ -81,14 +79,12 @@ export class FfmpegStreamingProcess {
8179
callback();
8280
callback = undefined;
8381
}
84-
if (debug && line.match(/\[(panic|fatal|error)\]/)) { // For now only write anything out when debug is set
85-
log.error(line, cameraName);
86-
} else if (debug) {
87-
log.debug(line, cameraName, true);
82+
if (line.match(/\[(panic|fatal|error)\]/)) {
83+
log.error(line);
8884
}
8985
});
9086
this.process.on('error', (error: Error) => {
91-
log.error('FFmpeg process creation failed: ' + error.message, cameraName);
87+
log.error('FFmpeg process creation failed: ' + error.message);
9288
if (callback) {
9389
callback(new Error('FFmpeg process creation failed'));
9490
}
@@ -102,15 +98,15 @@ export class FfmpegStreamingProcess {
10298
const message = 'FFmpeg exited with code: ' + code + ' and signal: ' + signal;
10399

104100
if (this.killTimeout && code === 0) {
105-
log.debug(message + ' (Expected)', cameraName, debug);
101+
log.debug(message + ' (Expected)');
106102
} else if (code === null || code === 255) {
107103
if (this.process.killed) {
108-
log.debug(message + ' (Forced)', cameraName, debug);
104+
log.debug(message + ' (Forced)');
109105
} else {
110-
log.error(message + ' (Unexpected)', cameraName);
106+
log.error(message + ' (Unexpected)');
111107
}
112108
} else {
113-
log.error(message + ' (Error)', cameraName);
109+
log.error(message + ' (Error)');
114110
delegate.stopStream(sessionId);
115111
if (!started && callback) {
116112
callback(new Error(message));

0 commit comments

Comments
 (0)