Skip to content

Commit

Permalink
Use offical API client
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jul 5, 2021
1 parent a21dc59 commit a21e5d4
Show file tree
Hide file tree
Showing 5 changed files with 3,444 additions and 1,034 deletions.
25 changes: 21 additions & 4 deletions lib/cloud.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
const OpenAPI = require('@tuyapi/openapi');
const {TuyaContext} = require('@tuya/tuya-connector-nodejs');
const {regionToUrl} = require('./helpers');

const list = async options => {
const api = new OpenAPI({key: options.apiKey, secret: options.apiSecret, schema: options.schema, region: options.region});
const api = new TuyaContext({
baseUrl: regionToUrl(options.region),
accessKey: options.apiKey,
secretKey: options.apiSecret
});

await api.getToken();
const result = await api.request({
method: 'GET',
path: '/v1.0/devices',
query: {
page_no: 0,
page_size: 1000,
schema: options.schema
}
});

const {devices} = await api.getDevices();
if (!result.success) {
throw new Error(`${result.code}: ${result.msg}`);
}

const {devices} = result.result;

if (options.stringify) {
console.log(JSON.stringify(devices));
Expand Down
3 changes: 3 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
regionToUrl: region => `https://openapi.tuya${region}.com`
};
58 changes: 36 additions & 22 deletions lib/wizard.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const OpenAPI = require('@tuyapi/openapi');
const {TuyaContext} = require('@tuya/tuya-connector-nodejs');
const inquirer = require('inquirer');
const colors = require('colors');
const any = require('promise.any');
const AggregateError = require('es-aggregate-error/polyfill')();
const {regionToUrl} = require('./helpers');

const REGIONS = ['eu', 'us', 'cn', 'in'];

Expand Down Expand Up @@ -56,28 +57,31 @@ const list = async (conf, options) => {
}

// Get seed device
let userId = null;
let userId;
let foundAPIRegion = savedAPIRegion;

try {
if (savedAPIRegion && useExistingKeys) {
const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region: savedAPIRegion});
await api.getToken();
const device = await api.getDevice(answers.deviceId);
const {device, region} = await any((savedAPIRegion ? [savedAPIRegion] : REGIONS).map(async region => {
const api = new TuyaContext({
baseUrl: regionToUrl(region),
accessKey: answers.apiKey,
secretKey: answers.apiSecret
});

const result = await api.request({
method: 'GET',
path: `/v1.0/devices/${answers.deviceId}`
});

if (!result.success) {
throw new Error(`${result.code}: ${result.msg}`);
}

userId = device.uid;
} else {
const {device, region} = await any(REGIONS.map(async region => {
const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region});
await api.getToken();
const device = await api.getDevice(answers.deviceId);
return {device: result.result, region};
}));

return {device, region};
}));

userId = device.uid;
foundAPIRegion = region;
}
userId = device.uid;
foundAPIRegion = region;
} catch (error) {
if (process.env.DEBUG) {
if (error.constructor === AggregateError) {
Expand All @@ -94,13 +98,23 @@ const list = async (conf, options) => {
}

// Get user devices
const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region: foundAPIRegion});
await api.getToken();
const api = new TuyaContext({
baseUrl: regionToUrl(savedAPIRegion),
accessKey: answers.apiKey,
secretKey: answers.apiSecret
});

const result = await api.request({
method: 'GET',
path: `/v1.0/users/${userId}/devices`
});

const devices = await api.getDevicesByUser(userId);
if (!result.success) {
throw new Error(`${result.code}: ${result.msg}`);
}

const groupedDevices = {};
for (const device of devices) {
for (const device of result.result) {
if (device.node_id) {
if (!groupedDevices[device.local_key] || !groupedDevices[device.local_key].subDevices) {
groupedDevices[device.local_key] = {...groupedDevices[device.local_key], subDevices: []};
Expand Down
Loading

0 comments on commit a21e5d4

Please sign in to comment.