11'use strict' ;
22
33// Import packages
4+ const debug = require ( 'debug' ) ( 'TuyAPI' ) ;
45const dgram = require ( 'dgram' ) ;
56const forge = require ( 'node-forge' ) ;
67const retryConnect = require ( 'net-retry-connect' ) ;
@@ -60,6 +61,9 @@ function TuyaDevice(options) {
6061 // Create cipher from key
6162 this . devices [ i ] . cipher = forge . cipher . createCipher ( 'AES-ECB' , this . devices [ i ] . key ) ;
6263 }
64+
65+ debug ( 'Device(s): ' ) ;
66+ debug ( this . devices ) ;
6367}
6468
6569/**
@@ -84,6 +88,8 @@ TuyaDevice.prototype.resolveIds = function () {
8488 // add IPs to devices in array and return true
8589 return new Promise ( resolve => {
8690 this . listener . on ( 'message' , message => {
91+ debug ( 'Received UDP message.' ) ;
92+
8793 const thisId = this . _extractJSON ( message ) . gwId ;
8894
8995 if ( needIP . length > 0 ) {
@@ -150,6 +156,9 @@ TuyaDevice.prototype.get = function (options) {
150156 requests [ currentDevice . type ] . status . command . devId = currentDevice . id ;
151157 }
152158
159+ debug ( 'Payload: ' ) ;
160+ debug ( requests [ currentDevice . type ] . status . command ) ;
161+
153162 // Create byte buffer from hex data
154163 const thisData = Buffer . from ( JSON . stringify ( requests [ currentDevice . type ] . status . command ) ) ;
155164 const buffer = this . _constructBuffer ( currentDevice . type , thisData , 'status' ) ;
@@ -224,6 +233,9 @@ TuyaDevice.prototype.set = function (options) {
224233 thisRequest . dps [ options . dps . toString ] = options . set ;
225234 }
226235
236+ debug ( 'Payload: ' ) ;
237+ debug ( thisRequest ) ;
238+
227239 // Encrypt data
228240 currentDevice . cipher . start ( { iv : '' } ) ;
229241 currentDevice . cipher . update ( forge . util . createBuffer ( JSON . stringify ( thisRequest ) , 'utf8' ) ) ;
@@ -259,15 +271,21 @@ TuyaDevice.prototype.set = function (options) {
259271* @returns {Promise<string> } - returned data
260272*/
261273TuyaDevice . prototype . _send = function ( ip , buffer ) {
274+ debug ( 'Sending this data: ' , buffer . toString ( 'hex' ) ) ;
275+
262276 return new Promise ( ( resolve , reject ) => {
263277 retryConnect . to ( { port : 6668 , host : ip , retryOptions : { retries : 5 } } , ( error , client ) => {
264278 if ( error ) {
265279 reject ( error ) ;
266280 }
281+
267282 client . write ( buffer ) ;
268283
269284 client . on ( 'data' , data => {
270285 client . destroy ( ) ;
286+
287+ debug ( 'Received data back.' ) ;
288+
271289 resolve ( data ) ;
272290 } ) ;
273291 client . on ( 'error' , error => {
@@ -302,6 +320,8 @@ TuyaDevice.prototype._constructBuffer = function (type, data, command) {
302320* @returns {Object } extracted object
303321*/
304322TuyaDevice . prototype . _extractJSON = function ( data ) {
323+ debug ( 'Parsing this data to JSON: ' , data . toString ( 'hex' ) ) ;
324+
305325 data = data . toString ( ) ;
306326
307327 // Find the # of occurrences of '{' and make that # match with the # of occurrences of '}'
0 commit comments