Skip to content

Commit

Permalink
Add debugging tools
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jan 19, 2018
1 parent 1714ca5 commit 9d2e055
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 8 deletions.
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

// Import packages
const debug = require('debug')('TuyAPI');
const dgram = require('dgram');
const forge = require('node-forge');
const retryConnect = require('net-retry-connect');
Expand Down Expand Up @@ -60,6 +61,9 @@ function TuyaDevice(options) {
// Create cipher from key
this.devices[i].cipher = forge.cipher.createCipher('AES-ECB', this.devices[i].key);
}

debug('Device(s): ');
debug(this.devices);
}

/**
Expand All @@ -84,6 +88,8 @@ TuyaDevice.prototype.resolveIds = function () {
// add IPs to devices in array and return true
return new Promise(resolve => {
this.listener.on('message', message => {
debug('Received UDP message.');

const thisId = this._extractJSON(message).gwId;

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

debug('Payload: ');
debug(requests[currentDevice.type].status.command);

// Create byte buffer from hex data
const thisData = Buffer.from(JSON.stringify(requests[currentDevice.type].status.command));
const buffer = this._constructBuffer(currentDevice.type, thisData, 'status');
Expand Down Expand Up @@ -224,6 +233,9 @@ TuyaDevice.prototype.set = function (options) {
thisRequest.dps[options.dps.toString] = options.set;
}

debug('Payload: ');
debug(thisRequest);

// Encrypt data
currentDevice.cipher.start({iv: ''});
currentDevice.cipher.update(forge.util.createBuffer(JSON.stringify(thisRequest), 'utf8'));
Expand Down Expand Up @@ -259,15 +271,21 @@ TuyaDevice.prototype.set = function (options) {
* @returns {Promise<string>} - returned data
*/
TuyaDevice.prototype._send = function (ip, buffer) {
debug('Sending this data: ', buffer.toString('hex'));

return new Promise((resolve, reject) => {
retryConnect.to({port: 6668, host: ip, retryOptions: {retries: 5}}, (error, client) => {
if (error) {
reject(error);
}

client.write(buffer);

client.on('data', data => {
client.destroy();

debug('Received data back.');

resolve(data);
});
client.on('error', error => {
Expand Down Expand Up @@ -302,6 +320,8 @@ TuyaDevice.prototype._constructBuffer = function (type, data, command) {
* @returns {Object} extracted object
*/
TuyaDevice.prototype._extractJSON = function (data) {
debug('Parsing this data to JSON: ', data.toString('hex'));

data = data.toString();

// Find the # of occurrences of '{' and make that # match with the # of occurrences of '}'
Expand Down
116 changes: 109 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuyapi",
"version": "2.0.0",
"version": "2.0.1",
"description": "An easy-to-use API for devices that use Tuya's cloud services (currently only supports smart plugs)",
"main": "index.js",
"scripts": {
Expand All @@ -25,6 +25,7 @@
},
"homepage": "https://github.com/codetheweb/tuyapi#readme",
"dependencies": {
"debug": "^3.1.0",
"net-retry-connect": "^0.1.1",
"node-forge": "^0.7.1",
"string-occurrence": "^1.2.0"
Expand Down

0 comments on commit 9d2e055

Please sign in to comment.