Comparing version 7.5.1 to 7.5.2
91
index.js
@@ -180,3 +180,4 @@ // Import packages | ||
dps: options.dps ? options.dps : 1, | ||
set: null | ||
set: null, | ||
isSetCallToGetData: true | ||
}; | ||
@@ -237,5 +238,6 @@ data = await this.set(setOptions); | ||
debug('GET Payload:'); | ||
debug('GET Payload (refresh):'); | ||
debug(payload); | ||
const sequenceN = ++this._currentSequenceN; | ||
// Create byte buffer | ||
@@ -245,3 +247,3 @@ const buffer = this.device.parser.encode({ | ||
commandByte: CommandType.DP_REFRESH, | ||
sequenceN: ++this._currentSequenceN | ||
sequenceN | ||
}); | ||
@@ -251,2 +253,3 @@ | ||
return new Promise((resolve, reject) => { | ||
this._expectRefreshResponseForSequenceN = sequenceN; | ||
// Send request | ||
@@ -261,3 +264,4 @@ this._send(buffer).then(async data => { | ||
dps: options.requestedDPS ? options.requestedDPS : this._dpRefreshIds, | ||
set: null | ||
set: null, | ||
isSetCallToGetData: true | ||
}; | ||
@@ -291,2 +295,4 @@ data = await this.set(setOptions); | ||
* Whether or not multiple properties should be set with options.data | ||
* @param {Boolean} [options.isSetCallToGetData=false] | ||
* Wether or not the set command is used to get data | ||
* @param {Object} [options.data={}] Multiple properties to set at once. See above. | ||
@@ -342,2 +348,10 @@ * @param {Boolean} [options.shouldWaitForResponse=true] see | ||
// When set has only null values then it is used to get data | ||
if (!options.isSetCallToGetData) { | ||
options.isSetCallToGetData = true; | ||
Object.keys(dps).forEach(key => { | ||
options.isSetCallToGetData = options.isSetCallToGetData && dps[key] === null; | ||
}); | ||
} | ||
// Get time | ||
@@ -388,2 +402,6 @@ const timeStamp = parseInt(Date.now() / 1000, 10); | ||
if (options.shouldWaitForResponse && this._setResolver) { | ||
throw new Error('A set command is already in progress. Can not issue a second one that also should return a response.'); | ||
} | ||
debug('SET Payload:'); | ||
@@ -393,2 +411,3 @@ debug(payload); | ||
const commandByte = this.device.version === '3.4' ? CommandType.CONTROL_NEW : CommandType.CONTROL; | ||
const sequenceN = ++this._currentSequenceN; | ||
// Encode into packet | ||
@@ -399,3 +418,3 @@ const buffer = this.device.parser.encode({ | ||
commandByte, | ||
sequenceN: ++this._currentSequenceN | ||
sequenceN | ||
}); | ||
@@ -411,2 +430,3 @@ | ||
this._setResolver = resolve; | ||
this._setResolveAllowGet = options.isSetCallToGetData; | ||
} else { | ||
@@ -421,2 +441,5 @@ resolve(); | ||
this._setResolver = undefined; | ||
this._setResolveAllowGet = undefined; | ||
delete this._resolvers[sequenceN]; | ||
this._expectRefreshResponseForSequenceN = undefined; | ||
@@ -439,2 +462,3 @@ this.emit( | ||
_send(buffer) { | ||
const sequenceNo = this._currentSequenceN; | ||
// Retry up to 5 times | ||
@@ -449,3 +473,3 @@ return pRetry(() => { | ||
// Add resolver function | ||
this._resolvers[this._currentSequenceN] = data => resolve(data); | ||
this._resolvers[sequenceNo] = data => resolve(data); | ||
} catch (error) { | ||
@@ -773,3 +797,32 @@ reject(error); | ||
if (packet.commandByte === CommandType.DP_REFRESH) { | ||
debug('Received DP_REFRESH empty response packet.'); | ||
// If we did not get any STATUS packet, we need to resolve the promise. | ||
if (typeof this._setResolver === 'function') { | ||
debug('Received DP_REFRESH empty response packet without STATUS packet from set command - resolve'); | ||
this._setResolver(packet.payload); | ||
// Remove resolver | ||
this._setResolver = undefined; | ||
this._setResolveAllowGet = undefined; | ||
delete this._resolvers[packet.sequenceN]; | ||
this._expectRefreshResponseForSequenceN = undefined; | ||
} else { | ||
// Call data resolver for sequence number | ||
if (packet.sequenceN in this._resolvers) { | ||
debug('Received DP_REFRESH response packet - resolve'); | ||
this._resolvers[packet.sequenceN](packet.payload); | ||
// Remove resolver | ||
delete this._resolvers[packet.sequenceN]; | ||
this._expectRefreshResponseForSequenceN = undefined; | ||
} else if (this._expectRefreshResponseForSequenceN && this._expectRefreshResponseForSequenceN in this._resolvers) { | ||
debug('Received DP_REFRESH response packet without data - resolve'); | ||
this._resolvers[this._expectRefreshResponseForSequenceN](packet.payload); | ||
// Remove resolver | ||
delete this._resolvers[this._expectRefreshResponseForSequenceN]; | ||
this._expectRefreshResponseForSequenceN = undefined; | ||
} else { | ||
debug('Received DP_REFRESH response packet - no resolver found for sequence number' + packet.sequenceN); | ||
} | ||
} | ||
return; | ||
@@ -806,5 +859,3 @@ } | ||
// Status response to SET command | ||
// 3.4 response sequenceN is not '0' just next TODO verify | ||
if (/* Former code: packet.sequenceN === 0 && */ | ||
if ( | ||
packet.commandByte === CommandType.STATUS && | ||
@@ -817,5 +868,24 @@ typeof this._setResolver === 'function' | ||
this._setResolver = undefined; | ||
this._setResolveAllowGet = undefined; | ||
delete this._resolvers[packet.sequenceN]; | ||
this._expectRefreshResponseForSequenceN = undefined; | ||
return; | ||
} | ||
// Status response to SET command which was used to GET data and returns DP_QUERY response | ||
if ( | ||
packet.commandByte === CommandType.DP_QUERY && | ||
typeof this._setResolver === 'function' && | ||
this._setResolveAllowGet === true | ||
) { | ||
this._setResolver(packet.payload); | ||
// Remove resolver | ||
this._setResolver = undefined; | ||
this._setResolveAllowGet = undefined; | ||
delete this._resolvers[packet.sequenceN]; | ||
this._expectRefreshResponseForSequenceN = undefined; | ||
return; | ||
} | ||
// Call data resolver for sequence number | ||
@@ -827,2 +897,3 @@ if (packet.sequenceN in this._resolvers) { | ||
delete this._resolvers[packet.sequenceN]; | ||
this._expectRefreshResponseForSequenceN = undefined; | ||
} | ||
@@ -829,0 +900,0 @@ } |
@@ -147,3 +147,2 @@ const Cipher = require('./cipher'); | ||
const packageFromDiscovery = ( | ||
@@ -150,0 +149,0 @@ commandByte === CommandType.UDP || |
{ | ||
"name": "tuyapi", | ||
"version": "7.5.1", | ||
"version": "7.5.2", | ||
"description": "An easy-to-use API for devices that use Tuya's cloud services", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
72049
1659