Skip to content

Commit 9d2e055

Browse files
committed
Add debugging tools
1 parent 1714ca5 commit 9d2e055

File tree

3 files changed

+131
-8
lines changed

3 files changed

+131
-8
lines changed

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
// Import packages
4+
const debug = require('debug')('TuyAPI');
45
const dgram = require('dgram');
56
const forge = require('node-forge');
67
const retryConnect = require('net-retry-connect');
@@ -60,6 +61,9 @@ function TuyaDevice(options) {
6061
// Create cipher from key
6162
this.devices[i].cipher = forge.cipher.createCipher('AES-ECB', this.devices[i].key);
6263
}
64+
65+
debug('Device(s): ');
66+
debug(this.devices);
6367
}
6468

6569
/**
@@ -84,6 +88,8 @@ TuyaDevice.prototype.resolveIds = function () {
8488
// add IPs to devices in array and return true
8589
return new Promise(resolve => {
8690
this.listener.on('message', message => {
91+
debug('Received UDP message.');
92+
8793
const thisId = this._extractJSON(message).gwId;
8894

8995
if (needIP.length > 0) {
@@ -150,6 +156,9 @@ TuyaDevice.prototype.get = function (options) {
150156
requests[currentDevice.type].status.command.devId = currentDevice.id;
151157
}
152158

159+
debug('Payload: ');
160+
debug(requests[currentDevice.type].status.command);
161+
153162
// Create byte buffer from hex data
154163
const thisData = Buffer.from(JSON.stringify(requests[currentDevice.type].status.command));
155164
const buffer = this._constructBuffer(currentDevice.type, thisData, 'status');
@@ -224,6 +233,9 @@ TuyaDevice.prototype.set = function (options) {
224233
thisRequest.dps[options.dps.toString] = options.set;
225234
}
226235

236+
debug('Payload: ');
237+
debug(thisRequest);
238+
227239
// Encrypt data
228240
currentDevice.cipher.start({iv: ''});
229241
currentDevice.cipher.update(forge.util.createBuffer(JSON.stringify(thisRequest), 'utf8'));
@@ -259,15 +271,21 @@ TuyaDevice.prototype.set = function (options) {
259271
* @returns {Promise<string>} - returned data
260272
*/
261273
TuyaDevice.prototype._send = function (ip, buffer) {
274+
debug('Sending this data: ', buffer.toString('hex'));
275+
262276
return new Promise((resolve, reject) => {
263277
retryConnect.to({port: 6668, host: ip, retryOptions: {retries: 5}}, (error, client) => {
264278
if (error) {
265279
reject(error);
266280
}
281+
267282
client.write(buffer);
268283

269284
client.on('data', data => {
270285
client.destroy();
286+
287+
debug('Received data back.');
288+
271289
resolve(data);
272290
});
273291
client.on('error', error => {
@@ -302,6 +320,8 @@ TuyaDevice.prototype._constructBuffer = function (type, data, command) {
302320
* @returns {Object} extracted object
303321
*/
304322
TuyaDevice.prototype._extractJSON = function (data) {
323+
debug('Parsing this data to JSON: ', data.toString('hex'));
324+
305325
data = data.toString();
306326

307327
// Find the # of occurrences of '{' and make that # match with the # of occurrences of '}'

package-lock.json

Lines changed: 109 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tuyapi",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "An easy-to-use API for devices that use Tuya's cloud services (currently only supports smart plugs)",
55
"main": "index.js",
66
"scripts": {
@@ -25,6 +25,7 @@
2525
},
2626
"homepage": "https://github.com/codetheweb/tuyapi#readme",
2727
"dependencies": {
28+
"debug": "^3.1.0",
2829
"net-retry-connect": "^0.1.1",
2930
"node-forge": "^0.7.1",
3031
"string-occurrence": "^1.2.0"

0 commit comments

Comments
 (0)