forked from cwwilson08/tuyapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
376 lines (324 loc) · 11.6 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
'use strict';
// Import packages
const dgram = require('dgram');
const net = require('net');
const occurrence = require('substr-occurrence');
const timeout = require('p-timeout');
const forge = require('node-forge');
const retry = require('retry');
const debug = require('debug')('TuyAPI');
// Import requests for devices
const requests = require('./requests.json');
/**
* Represents a Tuya device.
* @class
* @param {Object} options - options for constructing a TuyaDevice
* @param {String} [options.type='outlet'] - type of device
* @param {String} [options.ip] - IP of device
* @param {Number} [options.port=6668] - port of device
* @param {String} options.id - ID of device
* @param {String} [options.uid=''] - UID of device
* @param {String} options.key - encryption key of device
* @param {Number} [options.version=3.1] - protocol version
* @example
* const tuya = new TuyaDevice({id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'})
* @example
* const tuya = new TuyaDevice([
* {id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'},
* {id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'}])
*/
function TuyaDevice(options) {
this.devices = [];
if (options.constructor === Array) { // If argument is [{id: '', key: ''}]
this.devices = options;
} else if (options.constructor === Object) { // If argument is {id: '', key: ''}
this.devices = [options];
}
// Standardize devices array
for (let i = 0; i < this.devices.length; i++) {
if (this.devices[i].id === undefined) {
throw new Error('ID is missing from device.');
}
if (this.devices[i].key === undefined) {
throw new Error('Encryption key is missing from device with ID ' + this.devices[i].id + '.');
}
if (this.devices[i].type === undefined) {
this.devices[i].type = 'outlet';
}
if (this.devices[i].uid === undefined) {
this.devices[i].uid = '';
}
if (this.devices[i].port === undefined) {
this.devices[i].port = 6668;
}
if (this.devices[i].version === undefined) {
this.devices[i].version = 3.1;
}
// Create cipher from key
this.devices[i].cipher = forge.cipher.createCipher('AES-ECB', this.devices[i].key);
}
this._connectTotalTimeout = undefined;
this._connectRetryAttempts = undefined;
debug('Device(s): ');
debug(this.devices);
}
/**
* Resolves IDs stored in class to IPs. If you didn't pass IPs to the constructor,
* you must call this before doing anything else.
* @returns {Promise<Boolean>} - true if IPs were found and devices are ready to be used
*/
TuyaDevice.prototype.resolveIds = function () {
// Find devices that need an IP
const needIP = [];
for (let i = 0; i < this.devices.length; i++) {
if (this.devices[i].ip === undefined) {
needIP.push(this.devices[i].id);
}
}
if (needIP.length === 0) {
debug('No IPs to search for');
return Promise.resolve(true);
}
// Create new listener
this.listener = dgram.createSocket('udp4');
this.listener.bind(6666);
debug('Finding IP for devices ' + needIP);
// Add IPs to devices in array
return timeout(new Promise(resolve => { // Timeout
this.listener.on('message', message => {
debug('Received UDP message.');
const thisId = this._extractJSON(message).gwId;
if (needIP.length > 0) {
if (needIP.includes(thisId)) {
const deviceIndex = this.devices.findIndex(device => {
if (device.id === thisId) {
return true;
}
return false;
});
this.devices[deviceIndex].ip = this._extractJSON(message).ip;
needIP.splice(needIP.indexOf(thisId), 1);
}
} else { // All devices have been resolved
this.listener.close();
this.listener.removeAllListeners();
resolve(true);
}
});
}), 10000, () => {
// Have to do this so we exit cleanly
this.listener.close();
this.listener.removeAllListeners();
throw new Error('resolveIds() timed out. Is the device ID correct and is the device powered on?');
});
};
/**
* Gets a device's current status. Defaults to returning only the value of the first result,
* but by setting {schema: true} you can get everything.
* @param {Object} [options] - optional options for getting data
* @param {String} [options.id] - ID of device
* @param {Boolean} [options.schema] - true to return entire schema, not just the first result
* @example
* // get status for device with one property
* tuya.get().then(status => console.log(status))
* @example
* // get status for specific device with one property
* tuya.get({id: 'xxxxxxxxxxxxxxxxxxxx'}).then(status => console.log(status))
* @example
* // get all available data from device
* tuya.get({schema: true}).then(data => console.log(data))
* @returns {Promise<Object>} - returns boolean if no options are provided, otherwise returns object of results
*/
TuyaDevice.prototype.get = function (options) {
let currentDevice;
// If no ID is provided
if (options === undefined || options.id === undefined) {
currentDevice = this.devices[0]; // Use first device in array
} else { // Otherwise
// find the device by id in this.devices
const index = this.devices.findIndex(device => {
if (device.id === options.id) {
return true;
}
return false;
});
currentDevice = this.devices[index];
}
// Add data to command
if ('gwId' in requests[currentDevice.type].status.command) {
requests[currentDevice.type].status.command.gwId = currentDevice.id;
}
if ('devId' in requests[currentDevice.type].status.command) {
requests[currentDevice.type].status.command.devId = currentDevice.id;
}
debug('Payload: ');
debug(requests[currentDevice.type].status.command);
// Create byte buffer from hex data
const thisData = Buffer.from(JSON.stringify(requests[currentDevice.type].status.command));
const buffer = this._constructBuffer(currentDevice.type, thisData, 'status');
return new Promise((resolve, reject) => {
this._send(currentDevice.ip, buffer).then(data => {
// Extract returned JSON
data = this._extractJSON(data);
if (options !== undefined && options.schema === true) {
resolve(data);
} else {
resolve(data.dps[options.dps.toString()]);
}
}).catch(err => {
reject(err);
});
});
};
/**
* Sets a property on a device.
* @param {Object} options - options for setting properties
* @param {String} [options.id] - ID of device
* @param {Boolean} options.set - `true` for on, `false` for off
* @param {Number} [options.dps] - dps index to change
* @example
* // set default property on default device
* tuya.set({set: true}).then(() => console.log('device was changed'))
* @example
* // set custom property on non-default device
* tuya.set({id: 'xxxxxxxxxxxxxxxxxxxx', 'dps': 2, set: true}).then(() => console.log('device was changed'))
* @returns {Promise<Boolean>} - returns `true` if the command succeeded
*/
TuyaDevice.prototype.set = function (options) {
let currentDevice;
// If no ID is provided
if (options === undefined || options.id === undefined) {
currentDevice = this.devices[0]; // Use first device in array
} else { // Otherwise
// find the device by id in this.devices
const index = this.devices.findIndex(device => {
if (device.id === options.id) {
return true;
}
return false;
});
currentDevice = this.devices[index];
}
const thisRequest = requests[currentDevice.type].set.command;
// Add data to command
const now = new Date();
if ('gwId' in thisRequest) {
thisRequest.gwId = currentDevice.id;
}
if ('devId' in thisRequest) {
thisRequest.devId = currentDevice.id;
}
if ('uid' in thisRequest) {
thisRequest.uid = currentDevice.uid;
}
if ('t' in thisRequest) {
thisRequest.t = (parseInt(now.getTime() / 1000, 10)).toString();
}
if (options.dps === undefined) {
thisRequest.dps = {1: options.set};
} else {
thisRequest.dps = {[options.dps.toString()]: options.set};
}
debug('Payload: ');
debug(thisRequest);
// Encrypt data
currentDevice.cipher.start({iv: ''});
currentDevice.cipher.update(forge.util.createBuffer(JSON.stringify(thisRequest), 'utf8'));
currentDevice.cipher.finish();
// Encode binary data to Base64
const data = forge.util.encode64(currentDevice.cipher.output.data);
// Create MD5 signature
const preMd5String = 'data=' + data + '||lpv=' + currentDevice.version + '||' + currentDevice.key;
const md5hash = forge.md.md5.create().update(preMd5String).digest().toHex();
const md5 = md5hash.toString().toLowerCase().substr(8, 16);
// Create byte buffer from hex data
const thisData = Buffer.from(currentDevice.version + md5 + data);
const buffer = this._constructBuffer(currentDevice.type, thisData, 'set');
// Send request to change status
return new Promise((resolve, reject) => {
this._send(currentDevice.ip, buffer).then(() => {
resolve(true);
}).catch(err => {
reject(err);
});
});
};
/**
* Sends a query to a device.
* @private
* @param {String} ip - IP of device
* @param {Buffer} buffer - buffer of data
* @returns {Promise<string>} - returned data
*/
TuyaDevice.prototype._send = function (ip, buffer) {
debug('Sending this data: ', buffer.toString('hex'));
return new Promise((resolve, reject) => {
const client = new net.Socket();
const connectOperation = retry.operation({
retries: this._connectRetryAttempts,
maxRetryTime: this._connectTotalTimeout,
});
client.on('error', error => {
if (!connectOperation.retry(error)) {
reject(error);
}
});
connectOperation.attempt((connectAttempts) => {
debug('connect attempt', connectAttempts);
client.connect(6668, ip, () => {
debug('write attempt', buffer);
client.write(buffer);
client.on('data', data => {
client.destroy();
debug('Received data back.');
resolve(data);
});
client.on('error', error => {
debug('failed to communicate', error);
error.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
reject(error);
});
});
});
});
};
/**
* Constructs a protocol-complient buffer given device type, data, and command.
* @private
* @param {String} type - type of device
* @param {String} data - data to put in buffer
* @param {String} command - command (status || set)
* @returns {Buffer} buffer - buffer of data
*/
TuyaDevice.prototype._constructBuffer = function (type, data, command) {
// Construct prefix of packet according to protocol
const prefixLength = (data.toString('hex').length + requests[type].suffix.length) / 2;
const prefix = requests[type].prefix + requests[type][command].hexByte + '000000' + prefixLength.toString(16);
// Create final buffer: prefix + data + suffix
return Buffer.from(prefix + data.toString('hex') + requests[type].suffix, 'hex');
};
/**
* Extracts JSON from a raw buffer and returns it as an object.
* @private
* @param {Buffer} data - buffer of data
* @returns {Object} extracted object
*/
TuyaDevice.prototype._extractJSON = function (data) {
debug('Parsing this data to JSON: ', data.toString('hex'));
data = data.toString();
// Find the # of occurrences of '{' and make that # match with the # of occurrences of '}'
const leftBrackets = occurrence('{', data);
let occurrences = 0;
let currentIndex = 0;
while (occurrences < leftBrackets) {
const index = data.indexOf('}', currentIndex + 1);
if (index !== -1) {
currentIndex = index;
occurrences++;
}
}
data = data.slice(data.indexOf('{'), currentIndex + 1);
data = JSON.parse(data);
return data;
};
module.exports = TuyaDevice;