forked from codetheweb/tuyapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
698 lines (596 loc) · 19.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// Import packages
const dgram = require('dgram');
const net = require('net');
const {EventEmitter} = require('events');
const pTimeout = require('p-timeout');
const pRetry = require('p-retry');
const {default: PQueue} = require('p-queue');
const debug = require('debug')('TuyAPI');
// Helpers
const {isValidString} = require('./lib/utils');
const {MessageParser, CommandType} = require('./lib/message-parser');
const {UDP_KEY} = require('./lib/config');
/**
* Represents a Tuya device.
*
* You *must* pass either an IP or an ID. If
* you're experiencing problems when only passing
* one, try passing both if possible.
* @class
* @param {Object} options
* @param {String} [options.ip] IP of device
* @param {Number} [options.port=6668] port of device
* @param {String} [options.id] ID of device (also called `devId`)
* @param {String} [options.gwID=''] gateway ID (not needed for most devices),
* if omitted assumed to be the same as `options.id`
* @param {String} options.key encryption key of device (also called `localKey`)
* @param {String} [options.productKey] product key of device (currently unused)
* @param {Number} [options.version=3.1] protocol version
* @param {Boolean} [options.nullPayloadOnJSONError=false] if true, emits a data event
* containing a payload of null values for on-device JSON parsing errors
* @param {Boolean} [options.issueGetOnConnect=true] if true, sends GET request after
* connection is established. This should probably be `false` in synchronous usage.
* @example
* const tuya = new TuyaDevice({id: 'xxxxxxxxxxxxxxxxxxxx',
* key: 'xxxxxxxxxxxxxxxx'})
*/
class TuyaDevice extends EventEmitter {
constructor({
ip,
port = 6668,
id,
gwID = id,
key,
productKey,
version = 3.1,
nullPayloadOnJSONError = false,
issueGetOnConnect = true
} = {}) {
super();
// Set device to user-passed options
this.device = {ip, port, id, gwID, key, productKey, version};
this.globalOptions = {
issueGetOnConnect
};
this.nullPayloadOnJSONError = nullPayloadOnJSONError;
// Check arguments
if (!(isValidString(id) ||
isValidString(ip))) {
throw new TypeError('ID and IP are missing from device.');
}
// Check key
if (!isValidString(this.device.key) || this.device.key.length !== 16) {
throw new TypeError('Key is missing or incorrect.');
}
// Handles encoding/decoding, encrypting/decrypting messages
this.device.parser = new MessageParser({
key: this.device.key,
version: this.device.version});
// Contains array of found devices when calling .find()
this.foundDevices = [];
// Private instance variables
// Socket connected state
this._connected = false;
this._responseTimeout = 2; // Seconds
this._connectTimeout = 5; // Seconds
this._pingPongPeriod = 10; // Seconds
this._pingPongTimeout = null;
this._lastPingAt = new Date();
this._currentSequenceN = 0;
this._resolvers = {};
this._setQueue = new PQueue({
concurrency: 1
});
}
/**
* Gets a device's current status.
* Defaults to returning only the value of the first DPS index.
* @param {Object} [options]
* @param {Boolean} [options.schema]
* true to return entire list of properties from device
* @param {Number} [options.dps=1]
* DPS index to return
* @example
* // get first, default property from device
* tuya.get().then(status => console.log(status))
* @example
* // get second property from device
* tuya.get({dps: 2}).then(status => console.log(status))
* @example
* // get all available data from device
* tuya.get({schema: true}).then(data => console.log(data))
* @returns {Promise<Boolean|Object>}
* returns boolean if single property is requested, otherwise returns object of results
*/
get(options = {}) {
const payload = {
gwId: this.device.gwID,
devId: this.device.id,
t: Math.round(new Date().getTime() / 1000).toString(),
dps: {},
uid: this.device.id
};
debug('GET Payload:');
debug(payload);
// Create byte buffer
const buffer = this.device.parser.encode({
data: payload,
commandByte: CommandType.DP_QUERY,
sequenceN: ++this._currentSequenceN
});
// Send request and parse response
return new Promise((resolve, reject) => {
// Send request
this._send(buffer).then(async data => {
if (data === 'json obj data unvalid' && options.schema !== true) {
// Some devices don't respond to DP_QUERY so, for DPS get commands, fall
// back to using SEND with null value. This appears to always work as
// long as the DPS key exist on the device.
// For schema there's currently no fallback options
const setOptions = {
dps: options.dps ? options.dps : 1,
set: null
};
data = await this.set(setOptions);
}
if (typeof data !== 'object' || options.schema === true) {
// Return whole response
resolve(data);
} else if (options.dps) {
// Return specific property
resolve(data.dps[options.dps]);
} else {
// Return first property by default
resolve(data.dps['1']);
}
})
.catch(reject);
});
}
/**
* Sets a property on a device.
* @param {Object} options
* @param {Number} [options.dps=1] DPS index to set
* @param {*} [options.set] value to set
* @param {Boolean} [options.multiple=false]
* Whether or not multiple properties should be set with options.data
* @param {Object} [options.data={}] Multiple properties to set at once. See above.
* @example
* // set default property
* tuya.set({set: true}).then(() => console.log('device was turned on'))
* @example
* // set custom property
* tuya.set({dps: 2, set: false}).then(() => console.log('device was turned off'))
* @example
* // set multiple properties
* tuya.set({
* multiple: true,
* data: {
* '1': true,
* '2': 'white'
* }}).then(() => console.log('device was changed'))
* @example
* // set custom property for a specific (virtual) deviceId
* tuya.set({
* dps: 2,
* set: false,
* devId: '04314116cc50e346566e'
* }).then(() => console.log('device was turned off'))
* @returns {Promise<Object>} - returns response from device
*/
set(options) {
// Check arguments
if (options === undefined || Object.entries(options).length === 0) {
throw new TypeError('No arguments were passed.');
}
// Defaults
let dps = {};
if (options.multiple === true) {
dps = options.data;
} else if (options.dps === undefined) {
dps = {
1: options.set
};
} else {
dps = {
[options.dps.toString()]: options.set
};
}
// Get time
const timeStamp = parseInt(new Date() / 1000, 10);
// Construct payload
const payload = {
devId: options.devId || this.device.id,
gwId: this.device.gwID,
uid: '',
t: timeStamp,
dps
};
debug('SET Payload:');
debug(payload);
// Encode into packet
const buffer = this.device.parser.encode({
data: payload,
encrypted: true, // Set commands must be encrypted
commandByte: CommandType.CONTROL,
sequenceN: ++this._currentSequenceN
});
// Queue this request and limit concurrent set requests to one
return this._setQueue.add(() => pTimeout(new Promise((resolve, reject) => {
// Send request and wait for response
try {
// Send request
this._send(buffer);
this._setResolver = resolve;
} catch (error) {
reject(error);
}
}), this._responseTimeout * 1000, () => {
// Only gets here on timeout so clear resolver function and emit error
this._setResolver = undefined;
this.emit(
'error',
'Timeout waiting for status response from device id: ' + this.device.id
);
}));
}
/**
* Sends a query to a device. Helper function
* that connects to a device if necessary and
* wraps the entire operation in a retry.
* @private
* @param {Buffer} buffer buffer of data
* @returns {Promise<Any>} returned data for request
*/
_send(buffer) {
// Retry up to 5 times
return pRetry(() => {
return new Promise((resolve, reject) => {
// Send data
this.connect().then(() => {
try {
this.client.write(buffer);
// Add resolver function
this._resolvers[this._currentSequenceN] = data => resolve(data);
} catch (error) {
reject(error);
}
})
.catch(error => reject(error));
});
}, {retries: 5});
}
/**
* Sends a heartbeat ping to the device
* @private
*/
async _sendPing() {
debug(`Pinging ${this.device.ip}`);
// Create byte buffer
const buffer = this.device.parser.encode({
data: Buffer.allocUnsafe(0),
commandByte: CommandType.HEART_BEAT,
sequenceN: ++this._currentSequenceN
});
// Check for response
const now = new Date();
this._pingPongTimeout = setTimeout(() => {
if (this._lastPingAt < now) {
this.disconnect();
}
}, this._responseTimeout * 1000);
// Send ping
this.client.write(buffer);
}
/**
* Connects to the device. Can be called even
* if device is already connected.
* @returns {Promise<Boolean>} `true` if connect succeeds
* @emits TuyaDevice#connected
* @emits TuyaDevice#disconnected
* @emits TuyaDevice#data
* @emits TuyaDevice#error
*/
connect() {
if (!this.isConnected()) {
return new Promise((resolve, reject) => {
this.client = new net.Socket();
// Attempt to connect
debug(`Connecting to ${this.device.ip}...`);
this.client.connect(this.device.port, this.device.ip);
// Default connect timeout is ~1 minute,
// 5 seconds is a more reasonable default
// since `retry` is used.
this.client.setTimeout(this._connectTimeout * 1000, () => {
/**
* Emitted on socket error, usually a
* result of a connection timeout.
* Also emitted on parsing errors.
* @event TuyaDevice#error
* @property {Error} error error event
*/
// this.emit('error', new Error('connection timed out'));
this.client.destroy();
reject(new Error('connection timed out'));
});
// Add event listeners to socket
// Parse response data
this.client.on('data', data => {
debug(`Received data: ${data.toString('hex')}`);
let packets;
try {
packets = this.device.parser.parse(data);
if (this.nullPayloadOnJSONError) {
for (const packet of packets) {
if (packet.payload && packet.payload === 'json obj data unvalid') {
this.emit('error', packet.payload);
packet.payload = {
dps: {
1: null,
2: null,
3: null,
101: null,
102: null,
103: null
}
};
}
}
}
} catch (error) {
debug(error);
this.emit('error', error);
return;
}
packets.forEach(packet => {
debug('Parsed:');
debug(packet);
this._packetHandler.bind(this)(packet);
});
});
// Handle errors
this.client.on('error', err => {
debug('Error event from socket.', this.device.ip, err);
this.emit('error', new Error('Error from socket: ' + err.message));
if (!this._connected) {
reject(err);
}
this.client.destroy();
});
// Handle socket closure
this.client.on('close', () => {
debug(`Socket closed: ${this.device.ip}`);
this.disconnect();
});
this.client.on('connect', async () => {
debug('Socket connected.');
this._connected = true;
// Remove connect timeout
this.client.setTimeout(0);
/**
* Emitted when socket is connected
* to device. This event may be emitted
* multiple times within the same script,
* so don't use this as a trigger for your
* initialization code.
* @event TuyaDevice#connected
*/
this.emit('connected');
// Periodically send heartbeat ping
this._pingPongInterval = setInterval(async () => {
await this._sendPing();
}, this._pingPongPeriod * 1000);
// Automatically ask for current state so we
// can emit a `data` event as soon as possible
if (this.globalOptions.issueGetOnConnect) {
this.get();
}
// Return
resolve(true);
});
});
}
// Return if already connected
return Promise.resolve(true);
}
_packetHandler(packet) {
// Response was received, so stop waiting
clearTimeout(this._sendTimeout);
if (packet.commandByte === CommandType.HEART_BEAT) {
debug(`Pong from ${this.device.ip}`);
/**
* Emitted when a heartbeat ping is returned.
* @event TuyaDevice#heartbeat
*/
this.emit('heartbeat');
this._lastPingAt = new Date();
return;
}
if (packet.commandByte === CommandType.CONTROL && packet.payload === false) {
debug('Got SET ack.');
return;
}
/**
* Emitted when data is returned from device.
* @event TuyaDevice#data
* @property {Object} data received data
* @property {Number} commandByte
* commandByte of result
* (e.g. 7=requested response, 8=proactive update from device)
* @property {Number} sequenceN the packet sequence number
*/
this.emit('data', packet.payload, packet.commandByte, packet.sequenceN);
// Status response to SET command
if (packet.sequenceN === 0 &&
packet.commandByte === CommandType.STATUS &&
typeof this._setResolver === 'function') {
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);
// Remove resolver
delete this._resolvers[packet.sequenceN];
}
}
/**
* Disconnects from the device, use to
* close the socket and exit gracefully.
*/
disconnect() {
if (!this._connected) {
return;
}
debug('Disconnect');
this._connected = false;
// Clear timeouts
clearTimeout(this._sendTimeout);
clearTimeout(this._connectTimeout);
clearTimeout(this._responseTimeout);
clearInterval(this._pingPongInterval);
clearTimeout(this._pingPongTimeout);
if (this.client) {
this.client.destroy();
}
/**
* Emitted when a socket is disconnected
* from device. Not an exclusive event:
* `error` and `disconnected` may be emitted
* at the same time if, for example, the device
* goes off the network.
* @event TuyaDevice#disconnected
*/
this.emit('disconnected');
}
/**
* Returns current connection status to device.
* @returns {Boolean}
* (`true` if connected, `false` otherwise.)
*/
isConnected() {
return this._connected;
}
/**
* @deprecated since v3.0.0. Will be removed in v4.0.0. Use find() instead.
*/
resolveId(options) {
// eslint-disable-next-line max-len
console.warn('resolveId() is deprecated since v4.0.0. Will be removed in v5.0.0. Use find() instead.');
return this.find(options);
}
/**
* Finds an ID or IP, depending on what's missing.
* If you didn't pass an ID or IP to the constructor,
* you must call this before anything else.
* @param {Object} [options]
* @param {Boolean} [options.all]
* true to return array of all found devices
* @param {Number} [options.timeout=10]
* how long, in seconds, to wait for device
* to be resolved before timeout error is thrown
* @example
* tuya.find().then(() => console.log('ready!'))
* @returns {Promise<Boolean|Array>}
* true if ID/IP was found and device is ready to be used
*/
find({timeout = 10, all = false} = {}) {
if (isValidString(this.device.id) &&
isValidString(this.device.ip)) {
// Don't need to do anything
debug('IP and ID are already both resolved.');
return Promise.resolve(true);
}
// Create new listeners
const listener = dgram.createSocket({type: 'udp4', reuseAddr: true});
listener.bind(6666);
const listenerEncrypted = dgram.createSocket({type: 'udp4', reuseAddr: true});
listenerEncrypted.bind(6667);
const broadcastHandler = (resolve, reject) => message => {
debug('Received UDP message.');
const parser = new MessageParser({key: UDP_KEY, version: this.device.version});
let dataRes;
try {
dataRes = parser.parse(message)[0];
} catch (error) {
debug(error);
reject(error);
}
debug('UDP data:');
debug(dataRes);
const thisID = dataRes.payload.gwId;
const thisIP = dataRes.payload.ip;
// Add to array if it doesn't exist
if (!this.foundDevices.some(e => (e.id === thisID && e.ip === thisIP))) {
this.foundDevices.push({id: thisID, ip: thisIP});
}
if (!all &&
(this.device.id === thisID || this.device.ip === thisIP) &&
dataRes.payload) {
// Add IP
this.device.ip = dataRes.payload.ip;
// Add ID and gwID
this.device.id = dataRes.payload.gwId;
this.device.gwID = dataRes.payload.gwId;
// Change product key if neccessary
this.device.productKey = dataRes.payload.productKey;
// Change protocol version if necessary
if (this.device.version !== dataRes.payload.version) {
this.device.version = dataRes.payload.version;
// Update the parser
this.device.parser = new MessageParser({
key: this.device.key,
version: this.device.version});
}
// Cleanup
listener.close();
listener.removeAllListeners();
listenerEncrypted.close();
listenerEncrypted.removeAllListeners();
resolve(true);
}
};
debug(`Finding missing IP ${this.device.ip} or ID ${this.device.id}`);
// Find IP for device
return pTimeout(new Promise((resolve, reject) => { // Timeout
listener.on('message', broadcastHandler(resolve, reject));
listener.on('error', err => {
reject(err);
});
listenerEncrypted.on('message', broadcastHandler(resolve, reject));
listenerEncrypted.on('error', err => {
reject(err);
});
}), timeout * 1000, () => {
// Have to do this so we exit cleanly
listener.close();
listener.removeAllListeners();
listenerEncrypted.close();
listenerEncrypted.removeAllListeners();
// Return all devices
if (all) {
return this.foundDevices;
}
// Otherwise throw error
// eslint-disable-next-line max-len
throw new Error('find() timed out. Is the device powered on and the ID or IP correct?');
});
}
/**
* Toggles a boolean property.
* @param {Number} [property=1] property to toggle
* @returns {Promise<Boolean>} the resulting state
*/
async toggle(property = '1') {
property = property.toString();
// Get status
const status = await this.get({dps: property});
// Set to opposite
await this.set({set: !status, dps: property});
// Return new status
return this.get({dps: property});
}
}
module.exports = TuyaDevice;