|
| 1 | +const OpenAPI = require('@tuyapi/openapi'); |
| 2 | +const inquirer = require('inquirer'); |
| 3 | +const colors = require('colors'); |
| 4 | + |
| 5 | +const list = async conf => { |
| 6 | + let questions = [ |
| 7 | + { |
| 8 | + name: 'deviceId', |
| 9 | + message: 'Provide the ID of a device currently registered in the app:' |
| 10 | + } |
| 11 | + ]; |
| 12 | + |
| 13 | + const apiCredentialQuestions = [ |
| 14 | + { |
| 15 | + name: 'apiKey', |
| 16 | + message: 'The API key from tuya.com:' |
| 17 | + }, |
| 18 | + { |
| 19 | + name: 'apiSecret', |
| 20 | + message: 'The API secret from tuya.com' |
| 21 | + }, |
| 22 | + { |
| 23 | + name: 'region', |
| 24 | + message: 'Select the region closest to you:', |
| 25 | + type: 'list', |
| 26 | + choices: [ |
| 27 | + {name: 'Americas', value: 'us'}, |
| 28 | + {name: 'Europe', value: 'eu'}, |
| 29 | + {name: 'Asia', value: 'cn'} |
| 30 | + ] |
| 31 | + } |
| 32 | + ]; |
| 33 | + |
| 34 | + const savedAPIKey = conf.get('apiKey'); |
| 35 | + const savedAPISecret = conf.get('apiSecret'); |
| 36 | + const savedAPIRegion = conf.get('apiRegion'); |
| 37 | + |
| 38 | + let useExistingKeys = false; |
| 39 | + if (savedAPIKey && savedAPISecret && savedAPIRegion) { |
| 40 | + const answers = await inquirer.prompt([ |
| 41 | + { |
| 42 | + type: 'confirm', |
| 43 | + name: 'useExistingKeys', |
| 44 | + message: `Do you want to use these saved API credentials? ${savedAPIKey} ${savedAPISecret} ${savedAPIRegion}` |
| 45 | + } |
| 46 | + ]); |
| 47 | + |
| 48 | + ({useExistingKeys} = answers); |
| 49 | + |
| 50 | + if (!useExistingKeys) { |
| 51 | + questions = [...apiCredentialQuestions, ...questions]; |
| 52 | + } |
| 53 | + } else { |
| 54 | + questions = [...apiCredentialQuestions, ...questions]; |
| 55 | + } |
| 56 | + |
| 57 | + const answers = await inquirer.prompt(questions); |
| 58 | + |
| 59 | + if (useExistingKeys) { |
| 60 | + answers.apiKey = savedAPIKey; |
| 61 | + answers.apiSecret = savedAPISecret; |
| 62 | + answers.region = savedAPIRegion; |
| 63 | + } |
| 64 | + |
| 65 | + // Create API instance |
| 66 | + const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region: answers.region}); |
| 67 | + |
| 68 | + await api.getToken(); |
| 69 | + |
| 70 | + // Get seed device |
| 71 | + let userId = null; |
| 72 | + |
| 73 | + try { |
| 74 | + const device = await api.getDevice(answers.deviceId); |
| 75 | + |
| 76 | + userId = device.uid; |
| 77 | + } catch { |
| 78 | + console.error(colors.red('There was an issue fetching that device. Make sure your account is linked and the ID is correct.')); |
| 79 | + |
| 80 | + // eslint-disable-next-line unicorn/no-process-exit |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + |
| 84 | + // Get user devices |
| 85 | + const devices = await api.getDevicesByUser(userId); |
| 86 | + |
| 87 | + // Output devices |
| 88 | + console.log(devices.map(device => ({name: device.name, id: device.id, key: device.local_key}))); |
| 89 | + |
| 90 | + // Save API key and secret |
| 91 | + conf.set('apiKey', answers.apiKey); |
| 92 | + conf.set('apiSecret', answers.apiSecret); |
| 93 | + conf.set('apiRegion', answers.region); |
| 94 | +}; |
| 95 | + |
| 96 | +module.exports = list; |
0 commit comments