11'use strict' ;
22
33// Import packages
4- const debug = require ( 'debug' ) ( 'TuyAPI' ) ;
54const dgram = require ( 'dgram' ) ;
6- const forge = require ( 'node-forge' ) ;
7- const retryConnect = require ( 'net-retry-connect' ) ;
5+ const net = require ( 'net' ) ;
86const stringOccurrence = require ( 'string-occurrence' ) ;
7+ const timeout = require ( 'p-timeout' ) ;
8+ const forge = require ( 'node-forge' ) ;
9+ const retry = require ( 'retry' ) ;
10+ const debug = require ( 'debug' ) ( 'TuyAPI' ) ;
911
1012// Import requests for devices
1113const requests = require ( './requests.json' ) ;
@@ -84,9 +86,10 @@ TuyaDevice.prototype.resolveIds = function () {
8486 }
8587 }
8688
87- // Todo: add timeout for when IP cannot be found, then reject(with error)
88- // add IPs to devices in array and return true
89- return new Promise ( resolve => {
89+ debug ( 'Finding IP for devices ' + needIP ) ;
90+
91+ // Add IPs to devices in array
92+ return timeout ( new Promise ( resolve => { // Timeout
9093 this . listener . on ( 'message' , message => {
9194 debug ( 'Received UDP message.' ) ;
9295
@@ -111,6 +114,11 @@ TuyaDevice.prototype.resolveIds = function () {
111114 resolve ( true ) ;
112115 }
113116 } ) ;
117+ } ) , 10000 , ( ) => {
118+ // Have to do this so we exit cleanly
119+ this . listener . close ( ) ;
120+ this . listener . removeAllListeners ( ) ;
121+ throw new Error ( 'resolveIds() timed out. Is the device ID correct and is the device powered on?' ) ;
114122 } ) ;
115123} ;
116124
@@ -230,7 +238,7 @@ TuyaDevice.prototype.set = function (options) {
230238 if ( options . dps === undefined ) {
231239 thisRequest . dps = { 1 : options . set } ;
232240 } else {
233- thisRequest . dps [ options . dps . toString ] = options . set ;
241+ thisRequest . dps [ options . dps . toString ( ) ] = options . set ;
234242 }
235243
236244 debug ( 'Payload: ' ) ;
@@ -274,23 +282,34 @@ TuyaDevice.prototype._send = function (ip, buffer) {
274282 debug ( 'Sending this data: ' , buffer . toString ( 'hex' ) ) ;
275283
276284 return new Promise ( ( resolve , reject ) => {
277- retryConnect . to ( { port : 6668 , host : ip , retryOptions : { retries : 5 } } , ( error , client ) => {
278- if ( error ) {
285+ const client = new net . Socket ( ) ;
286+ const connectOperation = retry . operation ( ) ;
287+
288+ client . on ( 'error' , error => {
289+ if ( ! connectOperation . retry ( error ) ) {
279290 reject ( error ) ;
280291 }
292+ } ) ;
281293
282- client . write ( buffer ) ;
283-
284- client . on ( 'data' , data => {
285- client . destroy ( ) ;
286-
287- debug ( 'Received data back.' ) ;
294+ connectOperation . attempt ( ( ) => {
295+ client . connect ( 6668 , ip , ( ) => {
296+ const writeOperation = retry . operation ( ) ;
297+ writeOperation . attempt ( ( ) => {
298+ client . write ( buffer ) ;
288299
289- resolve ( data ) ;
290- } ) ;
291- client . on ( 'error' , error => {
292- error . message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.' ;
293- reject ( error ) ;
300+ client . on ( 'data' , data => {
301+ client . destroy ( ) ;
302+ debug ( 'Received data back.' ) ;
303+ resolve ( data ) ;
304+ } ) ;
305+ client . on ( 'error' , error => {
306+ error . message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.' ;
307+ console . log ( 'here' ) ;
308+ if ( ! writeOperation . retry ( error ) ) {
309+ reject ( error ) ;
310+ }
311+ } ) ;
312+ } ) ;
294313 } ) ;
295314 } ) ;
296315 } ) ;
0 commit comments