1- const OpenAPI = require ( '@tuyapi/openapi ' ) ;
1+ const { TuyaContext } = require ( '@tuya/tuya-connector-nodejs ' ) ;
22const inquirer = require ( 'inquirer' ) ;
33const colors = require ( 'colors' ) ;
44const any = require ( 'promise.any' ) ;
55const AggregateError = require ( 'es-aggregate-error/polyfill' ) ( ) ;
6+ const { regionToUrl} = require ( './helpers' ) ;
67
78const REGIONS = [ 'eu' , 'us' , 'cn' , 'in' ] ;
89
@@ -56,28 +57,31 @@ const list = async (conf, options) => {
5657 }
5758
5859 // Get seed device
59- let userId = null ;
60+ let userId ;
6061 let foundAPIRegion = savedAPIRegion ;
6162
6263 try {
63- if ( savedAPIRegion && useExistingKeys ) {
64- const api = new OpenAPI ( { key : answers . apiKey , secret : answers . apiSecret , region : savedAPIRegion } ) ;
65- await api . getToken ( ) ;
66- const device = await api . getDevice ( answers . deviceId ) ;
64+ const { device, region} = await any ( ( savedAPIRegion ? [ savedAPIRegion ] : REGIONS ) . map ( async region => {
65+ const api = new TuyaContext ( {
66+ baseUrl : regionToUrl ( region ) ,
67+ accessKey : answers . apiKey ,
68+ secretKey : answers . apiSecret
69+ } ) ;
70+
71+ const result = await api . request ( {
72+ method : 'GET' ,
73+ path : `/v1.0/devices/${ answers . deviceId } `
74+ } ) ;
75+
76+ if ( ! result . success ) {
77+ throw new Error ( `${ result . code } : ${ result . msg } ` ) ;
78+ }
6779
68- userId = device . uid ;
69- } else {
70- const { device, region} = await any ( REGIONS . map ( async region => {
71- const api = new OpenAPI ( { key : answers . apiKey , secret : answers . apiSecret , region} ) ;
72- await api . getToken ( ) ;
73- const device = await api . getDevice ( answers . deviceId ) ;
80+ return { device : result . result , region} ;
81+ } ) ) ;
7482
75- return { device, region} ;
76- } ) ) ;
77-
78- userId = device . uid ;
79- foundAPIRegion = region ;
80- }
83+ userId = device . uid ;
84+ foundAPIRegion = region ;
8185 } catch ( error ) {
8286 if ( process . env . DEBUG ) {
8387 if ( error . constructor === AggregateError ) {
@@ -94,13 +98,23 @@ const list = async (conf, options) => {
9498 }
9599
96100 // Get user devices
97- const api = new OpenAPI ( { key : answers . apiKey , secret : answers . apiSecret , region : foundAPIRegion } ) ;
98- await api . getToken ( ) ;
101+ const api = new TuyaContext ( {
102+ baseUrl : regionToUrl ( savedAPIRegion ) ,
103+ accessKey : answers . apiKey ,
104+ secretKey : answers . apiSecret
105+ } ) ;
106+
107+ const result = await api . request ( {
108+ method : 'GET' ,
109+ path : `/v1.0/users/${ userId } /devices`
110+ } ) ;
99111
100- const devices = await api . getDevicesByUser ( userId ) ;
112+ if ( ! result . success ) {
113+ throw new Error ( `${ result . code } : ${ result . msg } ` ) ;
114+ }
101115
102116 const groupedDevices = { } ;
103- for ( const device of devices ) {
117+ for ( const device of result . result ) {
104118 if ( device . node_id ) {
105119 if ( ! groupedDevices [ device . local_key ] || ! groupedDevices [ device . local_key ] . subDevices ) {
106120 groupedDevices [ device . local_key ] = { ...groupedDevices [ device . local_key ] , subDevices : [ ] } ;
0 commit comments