forked from unparagoned/cloudtuya
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
95 lines (78 loc) · 2.41 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Example script using cloudtuya to connect, get states an change them
*/
const debug = require('debug')('cloudtuya');
const fs = require('fs');
const CloudTuya = require('./cloudtuya');
const Light = require('./devices/light');
const name = 'cloudtuya';
debug('booting %s', name);
// Load local files
let apiKeys = {};
let deviceData = {};
try{
apiKeys = require('./keys.json');
} catch(err) {
console.error('keys.json is missing.');
}
try{
deviceData = require('./devices.json');
} catch(err) {
console.warn('devices.json is missing. creating temporary');
deviceData = [{}];
}
/**
* Save Data Such a Devices to file
* @param {Object} data to save
* @param {String} [file="./devices.json"] to save to
*/
function saveDataToFile(data, file = './devices.json') {
debug(`Data ${JSON.stringify(data)}`);
fs.writeFile(file, JSON.stringify(data), (err) => {
if(err) {
return debug(err);
}
debug(`The file ${file} was saved!`);
return(file);
});
}
async function main() {
// Load from keys.json
const api = new CloudTuya({
userName: apiKeys.userName,
password: apiKeys.password,
bizType: apiKeys.bizType,
countryCode: apiKeys.countryCode,
region: apiKeys.region,
});
// Test device read from devics.json saved at the end.
var testId = deviceData[0].id || '10000000000';
debug(`device data ${deviceData} and ${deviceData[0].id} id or all ${deviceData[0].name}`);
// Connect to cloud api and get access token.
const tokens = await api.login();
debug(`Token ${JSON.stringify(tokens)}`);
// Get all devices registered on the Tuya app
let devices = await api.find();
debug(`devices ${JSON.stringify(devices)}`);
// Save device to device.json
saveDataToFile(devices);
// Setting new Device ID
testId = devices[0].id;
// Get state of a single device
const deviceStates = await api.state({
devId: testId,
});
const state = deviceStates.testId;
debug(`testId ${testId} has value ${state}`);
debug(`devices ${JSON.stringify(deviceStates)}`);
debug(`devices ${JSON.stringify(devices)}`);
// Example how to turn on a lamp and set brightness
var myLight = new Light({ api: api, deviceId: testId});
myLight.turnOn();
myLight.setBrightness(80);
var brightness = await myLight.getBrightness();
var isOn =(JSON.stringify(await myLight.isOn()));
console.log(`lamp on: ${isOn}`);
console.log(`brightness is set to ${brightness}`);
}
main();