Skip to content

Commit

Permalink
Clean up debug() statements
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Feb 13, 2019
1 parent 77fab92 commit 4efcbf0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
19 changes: 7 additions & 12 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ <h3 class='fl m0' id='tuyadevice'>



<div class='pre p1 fill-light mt0'>disconnect(): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>></div>
<div class='pre p1 fill-light mt0'>disconnect()</div>



Expand All @@ -715,14 +715,6 @@ <h3 class='fl m0' id='tuyadevice'>




<div class='py1 quiet mt1 prose-big'>Returns</div>
<code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>></code>:








Expand Down Expand Up @@ -750,8 +742,7 @@ <h3 class='fl m0' id='tuyadevice'>



<p>Returns current connection status to device.
(<code>true</code> if connected, <code>false</code> otherwise.)</p>
<p>Returns current connection status to device.</p>



Expand All @@ -775,7 +766,11 @@ <h3 class='fl m0' id='tuyadevice'>

<div class='py1 quiet mt1 prose-big'>Returns</div>
<code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>:

(
<code>true</code>
if connected,
<code>false</code>
otherwise.)



Expand Down
33 changes: 17 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class TuyaDevice extends EventEmitter {
devId: this.device.id
};

debug('Payload: ', payload);
debug('GET Payload:');
debug(payload);

// Create byte buffer
const buffer = Parser.encode({
Expand Down Expand Up @@ -210,7 +211,8 @@ class TuyaDevice extends EventEmitter {
dps
};

debug('Payload:', payload);
debug('SET Payload:');
debug(payload);

// Encrypt data
const data = this.device.cipher.encrypt({
Expand Down Expand Up @@ -287,7 +289,6 @@ class TuyaDevice extends EventEmitter {
/**
* Sends a heartbeat ping to the device
* @private
* @returns {Promise<string>} returned data
*/
_sendPing() {
debug(`Pinging ${this.device.ip}`);
Expand Down Expand Up @@ -316,7 +317,7 @@ class TuyaDevice extends EventEmitter {
this.client = new net.Socket();

// Attempt to connect
debug('Connect', this.device.ip);
debug(`Connecting to ${this.device.ip}...`);
this.client.connect(this.device.port, this.device.ip);

// Default connect timeout is ~1 minute,
Expand All @@ -338,7 +339,7 @@ class TuyaDevice extends EventEmitter {

// Parse response data
this.client.on('data', data => {
debug('Received data back:', this.client.remoteAddress);
debug('Received response');
debug(data.toString('hex'));

// Response was received, so stop waiting
Expand All @@ -356,10 +357,11 @@ class TuyaDevice extends EventEmitter {
data = dataRes.data;

if (typeof data === 'object') {
debug('Data:', this.client.remoteAddress, data, dataRes.commandByte);
debug('Parsed response data:');
debug(data);
} else if (typeof data === 'undefined') {
if (dataRes.commandByte === 0x09) { // PONG received
debug('Pong', this.device.ip, this.client ? this.client.destroyed : true);
debug('Pong', this.device.ip);
return;
}

Expand All @@ -368,11 +370,11 @@ class TuyaDevice extends EventEmitter {
return;
}

debug('undefined', this.client.remoteAddress, data, dataRes.commandByte);
debug(`Undefined data with command byte ${dataRes.commandByte}`);
} else { // Message is encrypted
// eslint-disable-next-line max-len
debug('decrypt', this.client.remoteAddress, this.device.cipher.decrypt(data), dataRes.commandByte);
data = this.device.cipher.decrypt(data);
debug('Decrypted response data:');
debug(data);
}

/**
Expand All @@ -397,7 +399,7 @@ class TuyaDevice extends EventEmitter {

// Handle socket closure
this.client.on('close', () => {
debug('Socket closed:', this.device.ip);
debug(`Socket closed: ${this.device.ip}`);

this._connected = false;

Expand Down Expand Up @@ -461,7 +463,6 @@ class TuyaDevice extends EventEmitter {
* Disconnects from the device, use to
* close the socket or exit gracefully
* when using a persistent connection.
* @returns {Promise<Boolean>}
*/
disconnect() {
debug('Disconnect');
Expand All @@ -483,19 +484,19 @@ class TuyaDevice extends EventEmitter {

/**
* Returns current connection status to device.
* (`true` if connected, `false` otherwise.)
* @returns {Boolean}
* (`true` if connected, `false` otherwise.)
*/
isConnected() {
return this._connected;
}

/**
* Checks a given input string.
* Returns `true` if is string and length != 0, returns `false` otherwise.
* @private
* @param {String} input
* @param {String} input input string
* @returns {Boolean}
* `true` if is string and length != 0, `false` otherwise.
*/
checkIfValidString(input) {
if (input === undefined || typeof input !== typeof 'string' || input.length === 0) {
Expand Down Expand Up @@ -539,7 +540,7 @@ class TuyaDevice extends EventEmitter {
if (this.checkIfValidString(this.device.id) &&
this.checkIfValidString(this.device.ip)) {
// Don't need to do anything
debug('IP and ID are both resolved.');
debug('IP and ID are already both resolved.');
return Promise.resolve(true);
}

Expand Down

0 comments on commit 4efcbf0

Please sign in to comment.