Skip to content

Commit 6958bae

Browse files
committed
Make emitted null payload behavior opt-in
1 parent 6b50f74 commit 6958bae

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

docs/index.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset='utf-8'>
5-
<title>tuyapi 5.3.0 | Documentation</title>
5+
<title>tuyapi 5.3.2 | Documentation</title>
66
<meta name='description' content='An easy-to-use API for devices that use Tuya&#39;s cloud services'>
77
<meta name='viewport' content='width=device-width,initial-scale=1'>
88
<link href='assets/bass.css' rel='stylesheet'>
@@ -15,7 +15,7 @@
1515
<div id='split-left' class='overflow-auto fs0 height-viewport-100'>
1616
<div class='py1 px2'>
1717
<h3 class='mb0 no-anchor'>tuyapi</h3>
18-
<div class='mb1'><code>5.3.0</code></div>
18+
<div class='mb1'><code>5.3.2</code></div>
1919
<input
2020
placeholder='Filter'
2121
id='filter-input'
@@ -405,6 +405,18 @@ <h3 class='fl m0' id='tuyadevice'>
405405

406406

407407

408+
<tr>
409+
<td class='break-word'><span class='code bold'>options.nullPayloadOnJSONError</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
410+
411+
(default <code>false</code>)
412+
</td>
413+
<td class='break-word'><span>if true, emits a data event
414+
containing a payload of null values for on-device JSON parsing errors
415+
</span></td>
416+
</tr>
417+
418+
419+
408420
</tbody>
409421
</table>
410422

index.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,29 @@ const {UDP_KEY} = require('./lib/config');
2727
* @param {String} options.key encryption key of device (also called `localKey`)
2828
* @param {String} [options.productKey] product key of device (currently unused)
2929
* @param {Number} [options.version=3.1] protocol version
30+
* @param {Boolean} [options.nullPayloadOnJSONError=false] if true, emits a data event
31+
* containing a payload of null values for on-device JSON parsing errors
3032
* @example
3133
* const tuya = new TuyaDevice({id: 'xxxxxxxxxxxxxxxxxxxx',
3234
* key: 'xxxxxxxxxxxxxxxx'})
3335
*/
3436
class TuyaDevice extends EventEmitter {
35-
constructor({ip, port = 6668, id, gwID = id, key, productKey, version = 3.1} = {}) {
37+
constructor({
38+
ip,
39+
port = 6668,
40+
id,
41+
gwID = id,
42+
key,
43+
productKey,
44+
version = 3.1,
45+
nullPayloadOnJSONError = false
46+
} = {}) {
3647
super();
3748
// Set device to user-passed options
3849
this.device = {ip, port, id, gwID, key, productKey, version};
3950

51+
this.nullPayloadOnJSONError = nullPayloadOnJSONError;
52+
4053
// Check arguments
4154
if (!(isValidString(id) ||
4255
isValidString(ip))) {
@@ -314,20 +327,22 @@ class TuyaDevice extends EventEmitter {
314327
try {
315328
packets = this.device.parser.parse(data);
316329

317-
for (const packet of packets) {
318-
if (packet.payload && packet.payload === 'json obj data unvalid') {
319-
this.emit('error', packet.payload);
320-
321-
packet.payload = {
322-
dps: {
323-
1: null,
324-
2: null,
325-
3: null,
326-
101: null,
327-
102: null,
328-
103: null
329-
}
330-
};
330+
if (this.this.nullPayloadOnJSONError) {
331+
for (const packet of packets) {
332+
if (packet.payload && packet.payload === 'json obj data unvalid') {
333+
this.emit('error', packet.payload);
334+
335+
packet.payload = {
336+
dps: {
337+
1: null,
338+
2: null,
339+
3: null,
340+
101: null,
341+
102: null,
342+
103: null
343+
}
344+
};
345+
}
331346
}
332347
}
333348
} catch (error) {

0 commit comments

Comments
 (0)