Skip to content

Commit

Permalink
Fix set() resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jun 1, 2019
1 parent 0166802 commit a959dd2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class TuyaDevice extends EventEmitter {

this._currentSequenceN = 0;
this._resolvers = {};

this._waitingForSetToResolve = false;
}

/**
Expand Down Expand Up @@ -195,12 +197,13 @@ class TuyaDevice extends EventEmitter {
});

// Send request and wait for response
this._waitingForSetToResolve = true;
return new Promise((resolve, reject) => {
try {
// Send request
this._send(buffer).then(data => {
resolve(data);
});
this._send(buffer);

this._setResolver = resolve;
} catch (error) {
reject(error);
}
Expand Down Expand Up @@ -405,6 +408,17 @@ class TuyaDevice extends EventEmitter {
*/
this.emit('data', packet.payload, packet.commandByte, packet.sequenceN);

// Status response to SET command
if (packet.sequenceN === 0 &&
packet.commandByte === CommandType.STATUS &&
this._waitingForSetToResolve) {
this._setResolver(packet.payload);

// Remove resolver
this._setResolver = undefined;
return;
}

// Call data resolver for sequence number
if (packet.sequenceN in this._resolvers) {
this._resolvers[packet.sequenceN](packet.payload);
Expand Down
32 changes: 31 additions & 1 deletion 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
Expand Up @@ -42,10 +42,11 @@
"p-timeout": "3.1.0"
},
"devDependencies": {
"@tuyapi/stub": "0.1.2",
"@tuyapi/stub": "0.1.3",
"ava": "1.4.1",
"clone": "2.1.2",
"coveralls": "3.0.3",
"delay": "4.2.0",
"documentation": "9.3.1",
"nyc": "13.3.0",
"xo": "0.24.0"
Expand Down
22 changes: 21 additions & 1 deletion test/find.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import test from 'ava';
import TuyaStub from '@tuyapi/stub';
import clone from 'clone';
import delay from 'delay';

const TuyAPI = require('..');

const stub = new TuyaStub({id: '22325186db4a2217dc8e',
key: '4226aa407d5c1e2b',
state: {1: false, 2: true}});

// You may notice that at the end of each test
// there's a delay() before the function exits.
// This is to prevent race conditions that can
// occur in which a UDP broadcast lags after the
// server is torn down and is captured by the
// following test, skewing the results.

test.serial('find device on network using deprecated resolveId', async t => {
const stubDevice = new TuyAPI({id: '22325186db4a2217dc8e',
key: '4226aa407d5c1e2b'});
Expand All @@ -21,6 +29,8 @@ test.serial('find device on network using deprecated resolveId', async t => {
stubDevice.disconnect();
thisStub.shutdown();

await delay(100);

t.not(stubDevice.device.ip, undefined);
});

Expand All @@ -37,6 +47,8 @@ test.serial('find device on network by ID', async t => {
stubDevice.disconnect();
thisStub.shutdown();

await delay(100);

t.not(stubDevice.device.ip, undefined);
});

Expand All @@ -53,6 +65,8 @@ test.serial('find device on network by IP', async t => {
stubDevice.disconnect();
thisStub.shutdown();

await delay(100);

t.not(stubDevice.device.id, undefined);
});

Expand All @@ -70,6 +84,8 @@ test.serial('find returns if both ID and IP are already set', async t => {
stubDevice.disconnect();
thisStub.shutdown();

await delay(100);

t.is(true, result);
});

Expand All @@ -81,10 +97,12 @@ test.serial('find throws timeout error', async t => {
thisStub.startServer();

await t.throwsAsync(() => {
return stubDevice.find({timeout: 1}).catch(error => {
return stubDevice.find({timeout: 1}).catch(async error => {
stubDevice.disconnect();
thisStub.shutdown();

await delay(100);

throw error;
});
});
Expand All @@ -103,5 +121,7 @@ test.serial('find with option all', async t => {
stubDevice.disconnect();
thisStub.shutdown();

await delay(100);

t.truthy(foundDevices.length);
});

0 comments on commit a959dd2

Please sign in to comment.