-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
74 lines (60 loc) · 1.97 KB
/
test.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
const tuyaCloud = require('./main');
const readline = require('readline');
const fs = require('fs');
if (!fs.existsSync('./credentials.json') || !fs.statSync('./credentials.json').isFile()) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.clear();
rl.question('What is your Tuya Cloud "Client ID" ? ', (clientID) => {
console.clear();
rl.question('What is your Tuya Cloud "Secret" ? ', (secret) => {
console.clear();
rl.question('What is your Tuya Cloud server region ? (eu / us / cn / in) ', (region) => {
console.clear();
rl.close();
if (!clientID || !secret) process.exit(200);
init({ clientID, secret, region }, true);
});
});
});
} else {
let credentials = null;
try {
credentials = JSON.parse(fs.readFileSync('./credentials.json', { encoding: 'utf8' }));
} catch (error) {
fs.unlinkSync('./credentials.json');
console.error('Wrong "Cloud ID" or "Secret", please retry...');
process.exit(200);
}
init(credentials);
}
function init(credentials, save = false) {
tuyaCloud.connect(credentials).then(async (tuya) => {
if (save) fs.writeFileSync('./credentials.json', JSON.stringify(credentials), { encoding: 'utf8' });
console.log('Logged !')
const tempSensor = tuya.device('xxxxxxxxxxxxxxxxxxxxxx');
console.log(await tempSensor.getInfos());
setInterval(async () => {
console.log(await tempSensor.getStatus());
}, 10000);
const light = tuya.device('xxxxxxxxxxxxxxxxxxxxxx');
console.log(await light.getFunctions());
console.log(await light.getStatus());
light.sendCommands([
{
code: 'colour_data',
value: {
h: 120, // Green
s: 1000,
v: 1000,
},
},
]);
}).catch((error) => {
if (fs.existsSync('./credentials.json')) fs.unlinkSync('./credentials.json');
console.error(error.message);
});
}
setInterval(() => {}, 2000);